簡體   English   中英

如何使用 go-github 對 Github issue 發表評論?

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

我想使用https://github.com/google/go-github創建一個問題的評論,但是這個測試代碼失敗了:

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

但我剛開始

# 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 上的問題發送評論?

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

您需要導入"context" package 才能調用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)后,您創建了 2 個新變量: 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 {
    // handle any errors
}

...其中OWNER是存儲庫的所有者, REPO是存儲庫的名稱, ISSUE_NUMBER是您要在其中寫評論的問題的編號。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM