我想使用go GitHub client来获得像git branch -r origin/<branch> --contains <sha>一样的结果
发布于 2021-10-27 22:35:58
有没有办法通过go-github客户端验证git sha是否属于git分支?
我似乎找不到能够明确回答SHA是否属于分支的API端点,因此您必须在分支内迭代提交。类似于:
func main() {
ctx := context.Background()
sts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: "<token>"},
)
tc := oauth2.NewClient(ctx, sts)
client := github.NewClient(tc)
repoOwner := "<owner>"
repoName := "<repo>"
branchToSearch := "<branch>"
shaToFind := "<sha>"
resultsPerPage := 25
listOptions := github.ListOptions{PerPage: resultsPerPage}
for {
rc, resp, err := client.Repositories.ListCommits(ctx,
repoOwner,
repoName,
&github.CommitsListOptions{
SHA: branchToSearch,
ListOptions: listOptions,
},
)
if err != nil {
log.Panic(err)
}
for _, c := range rc {
if *c.SHA == shaToFind {
log.Printf("FOUND commit \"%s\" in the branch \"%s\"\n", shaToFind, branchToSearch)
return
}
}
if resp.NextPage == 0 {
break
}
listOptions.Page = resp.NextPage
}
log.Printf("NOT FOUND commit \"%s\" in the branch \"%s\"\n", shaToFind, branchToSearch)
}https://stackoverflow.com/questions/69743190
复制相似问题