从 github 个问题中检索最新评论

Retrieve the latest comment from a github issue

我想知道使用 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
}


提前致谢:)

您可以使用 Rest API v3 或 GraphQL v4。如果你打算遍历很多问题,graphQL 绝对值得

使用 Rest API v3

按照您的建议使用 go-github,您可以使用 :

ListComments(ctx context.Context, owner string, repo string, number int, opts *IssueListCommentsOptions)

例如来自this test

例如获取最近 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")
        }
    }
}

它将执行:

  • 一个获取最近 20 个打开的问题的请求
  • 每个问题一个请求以获得最后的评论

所以一共21个请求

使用 GraphQL v4

您可以使用 githubv4 library to use 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 存储库

the example 的修改

上面的代码将恰好执行 1 个请求