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?
CodePudding user response:
./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.