go-github icon indicating copy to clipboard operation
go-github copied to clipboard

Does this package support configuring a proxy?

Open songleo opened this issue 3 years ago • 3 comments

I want to use this package to au-create the issue in my repo. but my env is private, and we provide a proxy to access the internet. so I am not sure if I can configure a proxy with this package to call my github repo. thanks.

songleo avatar May 11 '22 08:05 songleo

I believe this is possible, but don't have the details. Hopefully others can comment and provide you with what you need. In the meantime, if you figure it out, feel free to report back to this issue with the solution to benefit others. Thanks.

gmlewis avatar May 11 '22 10:05 gmlewis

@gmlewis thanks, for now, I have no idea to do it. if I figure it out, I will post it here. thanks.

songleo avatar May 11 '22 10:05 songleo

You need to configure the proxy in a new http.client and pass it as a parameter to your "github client"


package main

import (
	"context"
    "fmt"
    "log"
    "github.com/google/go-github/v45/github"
	"net/http"
	"net/url"
)

func main() {
	//creating the proxyURL
	proxyStr := "<your-proxy-url>"
	proxyURL, err := url.Parse(proxyStr)
	if err != nil {
		log.Println(err)
	}

	transport := &http.Transport{
		Proxy: http.ProxyURL(proxyURL),
	}

	//adding the Transport object to the http Client
	httpclient := &http.Client{
		Transport: transport,
	}

	client := github.NewClient(httpclient)

	// list public repositories for org "github"
	opt := &github.RepositoryListByOrgOptions{Type: "public"}
	repos, _, err := client.Repositories.ListByOrg(context.Background(), "github", opt)
	if err != nil {
        log.Fatal(err)
    }

    // If no error was returned, print the returned message
    // to the console.
    fmt.Println(repos)
}

extravio avatar May 26 '22 04:05 extravio

Thank you, @extravio !

Closing due to solution provided above: https://github.com/google/go-github/issues/2363#issuecomment-1138153077

gmlewis avatar Mar 08 '23 18:03 gmlewis