登录
首页 >  Golang >  Go问答

获取最新评论从 Github 的问题列表

来源:stackoverflow

时间:2024-02-28 10:27:22 388浏览 收藏

一分耕耘,一分收获!既然都打开这篇《获取最新评论从 Github 的问题列表》,就坚持看下去,学下去吧!本文主要会给大家讲到等等知识点,如果大家对本文有好的建议或者看到有不足之处,非常欢迎大家积极提出!在后续文章我会继续更新Golang相关的内容,希望对大家都有所帮助!

问题内容

我想知道使用 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 来使用 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 个请求

今天关于《获取最新评论从 Github 的问题列表》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>