我正在尝试使用 go-github 列出用户的存储库并获取他们所属的组织。
这些存储库属于一个组织,但组织值似乎是 nil
。
代码:
import "github.com/google/go-github/v37/github"
func GetClient(token) *GitHub {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
return client
}
func main() {
client := GetClient("TOKEN")
repos, _, err := client.Repositories.List(context.TODO(), "", nil)
if err != nil {
log.Fatal(err)
}
for _, repo := range repos {
println(*repo.Name)
org := repo.GetOrganization()
println(org)
println(repo.Organization)
}
}
输出:
api
0x0
0x0
api-inserter
0x0
0x0
bbprograms
0x0
0x0
console-api
0x0
0x0
console-spa
我知道这些存储库是用户所属组织的一部分。
答案 0 :(得分:0)
您可以直接调用 API:
package main
import (
"encoding/json"
"fmt"
"net/http"
)
func main() {
r, err := http.Get("https://api.github.com/users/bradfitz/orgs")
if err != nil {
panic(err)
}
defer r.Body.Close()
var orgs []struct{Login string}
json.NewDecoder(r.Body).Decode(&orgs)
fmt.Printf("%+v\n", orgs) // [{Login:djabberd} {Login:camlistore} {Login:tailscale}]
}