How to use go-github to post a comment on a Github issue?

huangapple go评论151阅读模式
英文:

How to use go-github to post a comment on a Github issue?

问题

我想使用https://github.com/google/go-github在问题上创建评论,但是这个测试代码失败了:

package main

import (
	"context"
	"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)

	// 列出授权用户的所有存储库
	repos, _, err := client.Repositories.List(ctx, "", nil)
}

但是我只得到了以下错误信息:

# 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

那么,我该怎么做才能使其正常工作,并且如何使用我的令牌发送评论到GitHub上的问题?

英文:

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?

答案1

得分: 3

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

你需要导入 context 包才能调用 context.Background()

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

在调用 client.Repositories.List(ctx, "", nil) 后,你创建了两个新变量:reposerr,但是没有在任何地方使用它们。在 Go 语言中,未使用的变量是一个编译器错误,所以要么删除这些变量,要么最好像使用它们一样使用它们。

> 那么,我该怎么做才能让它工作起来,并且如何使用我的令牌在 GitHub 上对问题发送评论呢?

要使用 GitHub API,你需要获取一个访问令牌,并将 token_here 替换为该令牌。然后你可以按照以下方式进行操作:

comment := &github.IssueComment{
    Body: github.String("Hello, world!"),
}
comment, _, err := client.Issues.CreateComment(
    context.Background(), 
    "OWNER", 
    "REPO", 
    ISSUE_NUMBER, 
    comment,
)
if err != nil {
    // 处理任何错误
}

... 其中 OWNER 是仓库的所有者,REPO 是仓库的名称,ISSUE_NUMBER 是你想要在哪个问题上写评论的问题编号。

英文:
./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.

huangapple
  • 本文由 发表于 2023年1月14日 03:04:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75113498.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定