1

I want to use https://github.com/google/go-github for creating a comment on an issue, but this test code fails:

package main

import (
    "golang.org/x/oauth2"
    "github.com/google/go-github/v49/github"
)

func main() {
    ctx := context.Background()
    ts := oauth2.StaticTokenSource(
        &oauth2.Token{AccessToken: "token_here"},
    )
    tc := oauth2.NewClient(ctx, ts)

    client := github.NewClient(tc)

    // list all repositories for the authenticated user
    repos, _, err := client.Repositories.List(ctx, "", nil)
}

but I'm just getting

# command-line-arguments
./main.go:9:9: undefined: context
./main.go:18:2: repos declared but not used
./main.go:18:12: err declared but not used

back... So - what I have to do to get this working and how can I send a comment (via my token) to an issue on github?

robbieperry22
  • 1,753
  • 1
  • 18
  • 49
githubgo
  • 23
  • 2
  • 1
    Please take the [Tour of Go](https://tour.golang.org/) to get a good overview of basic Go syntax, variable usage, and value types. – Adrian Jan 13 '23 at 22:41

1 Answers1

3
./main.go:9:9: undefined: context

You need to import the "context" package to be able to call context.Background()

./main.go:18:2: repos declared but not used
./main.go:18:12: err declared but not used

After calling client.Repositories.List(ctx, "", nil) you created 2 new variables: repos and err, but never used them anywhere. In Go, an unused variable is a compiler error, so either remove those variables, or preferably use them as you would.

So - what i have to do to get this working and how can i send a comment (via my token) to an issue on github?

To use the Github API, you will need to get yourself an access token, and replace "token_here" with that. Then you can do something as follows:

comment := &github.IssueComment{
    Body: github.String("Hello, world!"),
}
comment, _, err := client.Issues.CreateComment(
    context.Background(), 
    "OWNER", 
    "REPO", 
    ISSUE_NUMBER, 
    comment,
)
if err != nil {
    // handle any errors
}

... where OWNER is the owner of the repository, REPO is the name of the repository, and ISSUE_NUMBER is the number of the issue where you want to write the comment.

calogero
  • 53
  • 5
robbieperry22
  • 1,753
  • 1
  • 18
  • 49
  • This is really helpful but i can't get the issue number working, caused by `./main.go:21:19: cannot use "28" (untyped string constant) as int64 value in argument to github.Int64` but thx for your help! – githubgo Jan 13 '23 at 22:25
  • That's because "28" is a string type, and it is expecting an int64 type. I would recommend learning Go, as well as some basic programming skills before attempting what you are trying to do. I think even some basic knowledge would help you out. – robbieperry22 Jan 14 '23 at 02:12
  • thx again @robbieperry22 ! i just used `ISSUE_NUMBER := int(issueNumber)` for conversion and it works :-) – githubgo Jan 14 '23 at 10:55