I J
I J

Reputation: 57

Retrieve the latest comment from a github issue

I am wondering what would be the most efficient way to retrieve the latest comment from a github issue using Go.

I actually know how to do this already but I am not satisfied with the performance so I would love to get some suggestions


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
}


Thanks in advance :)

Upvotes: 1

Views: 762

Answers (1)

Bertrand Martel
Bertrand Martel

Reputation: 45432

You can use Rest API v3 or GraphQL v4. If you plan to loop through a lot of issues, graphQL definitly worth it

Using Rest API v3

Using go-github as you suggested, you can use :

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

For example from this test

For example to get the last comment for the last 20 opened issues (from your code).

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")
        }
    }
}

It will perform :

  • one request to get the last 20 opened issues
  • one request for each issue to get the last comments

So a total of 21 requests

Using GraphQL v4

You can use githubv4 library to use Github GraphQL v4.

The same as previous example in GraphQL would be :

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)
    }
}

This is modification of the example from the github repo

The code above will perform exactly 1 request

Upvotes: 1

Related Questions