我想知道使用Go从github问题中检索最新评论的最有效方法是什么。
我其实已经知道该怎么做了,但是我对表演不满意,所以我很想得到一些建议。
package main
import (
"context"
"fmt"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
"net/url"
"os"
)
func main() {
owner, repo := "owner", "repo"
token := oauth2.Token{AccessToken: os.Getenv("GITHUB_TOKEN")}
ts := oauth2.StaticTokenSource(&token)
ctx := context.Background()
tc := oauth2.NewClient(ctx, ts)
gc := github.NewClient(tc)
gc.BaseURL, _ = url.Parse("https://api.github.com/")
opts := github.IssueListByRepoOptions{}
issues, _, _ := gc.Issues.ListByRepo(ctx, owner, repo, &opts)
// Implement Here: get latest comment for issues[0]
return
}(预先谢谢:)
发布于 2020-05-21 04:04:27
您可以使用Rest v3或GraphQL v4。如果您计划循环处理许多问题,graphQL绝对值得
使用Rest v3
按照您的建议使用github,您可以使用:
ListComments(ctx context.Context, owner string, repo string, number int, opts *IssueListCommentsOptions)例如,来自这个测试
例如,要获得最近20个已打开问题的最后评论(来自您的代码)。
package main
import (
"context"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
"net/url"
"os"
"log"
)
func main() {
owner, repo := "google", "gson"
token := oauth2.Token{AccessToken: os.Getenv("GITHUB_TOKEN")}
ts := oauth2.StaticTokenSource(&token)
ctx := context.Background()
tc := oauth2.NewClient(ctx, ts)
gc := github.NewClient(tc)
gc.BaseURL, _ = url.Parse("https://api.github.com/")
opts := github.IssueListByRepoOptions{}
issues, _, _ := gc.Issues.ListByRepo(ctx, owner, repo, &opts)
for i := 0; i < len(issues); i++ {
opt := &github.IssueListCommentsOptions{}
comments, _, err := gc.Issues.ListComments(ctx, owner, repo, *issues[i].Number, opt)
if err != nil {
log.Println(err)
} else if len(comments) > 0 {
log.Println(*comments[0].Body)
} else {
log.Println("no comment for this issue")
}
}
}它将执行:
所以总共有21次请求
使用GraphQL v4
您可以使用githubv4库来使用Github GraphQL v4。
与GraphQL中的前面示例相同的是:
package main
import (
"context"
"github.com/shurcooL/githubv4"
"golang.org/x/oauth2"
"os"
"encoding/json"
"log"
)
func main() {
owner, repo := "google", "gson"
token := oauth2.Token{AccessToken: os.Getenv("GITHUB_TOKEN")}
ts := oauth2.StaticTokenSource(&token)
httpClient := oauth2.NewClient(context.Background(), ts)
client := githubv4.NewClient(httpClient)
{
var q struct {
Repository struct {
Issues struct {
Nodes []struct {
Number int
Comments struct {
Nodes []struct {
Body githubv4.String
}
} `graphql:"comments(last:$commentsLast)"`
}
PageInfo struct {
EndCursor githubv4.String
HasNextPage githubv4.Boolean
}
} `graphql:"issues(last:$issuesLast,states:OPEN)"`
} `graphql:"repository(owner:$repositoryOwner,name:$repositoryName)"`
}
variables := map[string]interface{}{
"repositoryOwner": githubv4.String(owner),
"repositoryName": githubv4.String(repo),
"issuesLast": githubv4.NewInt(20),
"commentsLast": githubv4.NewInt(1),
}
err := client.Query(context.Background(), &q, variables)
if err != nil {
log.Println(err)
return
}
printJSON(q)
}
}
func printJSON(v interface{}) {
w := json.NewEncoder(os.Stdout)
w.SetIndent("", "\t")
err := w.Encode(v)
if err != nil {
panic(err)
}
}这是github回购公司对例句的修改。
上面的代码将精确地执行1次请求。
https://stackoverflow.com/questions/61913259
复制相似问题