我正在尝试列出一个用户的repos,并使用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我知道这些repos是用户所属组织的一部分。
发布于 2021-07-15 22:10:00
您可以直接调用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}]
}https://stackoverflow.com/questions/68395166
复制相似问题