使用go-github打印出存储库的发布版本。

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

Print out repository releases with go-github

问题

我对Go语言非常陌生,使用go-github API打印给定仓库的所有发布版本时遇到了问题。

我正在根据项目中的示例来调整我的代码。

这是我目前的代码:

package main

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

func main() {

// 认证
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: "XXX"})
tc := oauth2.NewClient(oauth2.NoContext, ts)

client := github.NewClient(tc)

// 列出单个仓库的所有发布版本
fmt.Println("Releases for repo")
opt := &github.ListOptions{Page: 2, PerPage: 10}
releases, _, err := client.Repositories.ListReleases("hashicorp", "terraform", opt)

if err != nil {
    fmt.Printf("error: %v\n", err)
} else {
    for _, release := range releases {
        fmt.Printf("%v\n", release)
    }
}

这段代码可以正常运行(至少没有错误),但当我运行它时,代码没有返回任何内容。我觉得我可能漏掉了一些简单的东西,但我却束手无策。

英文:

I am very new to Go and am having issues printing out all releases for a given repo using the go-github api.

I am adapting my code from the example in the project here.

This is the code I have so far.

package main

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

func main() {

// authentication
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: "XXX"})
tc := oauth2.NewClient(oauth2.NoContext, ts)

client := github.NewClient(tc)

// list all releases for single repo
fmt.Println("Releases for repo")
opt := &github.ListOptions{Page: 2, PerPage: 10}
releases, _, err := client.Repositories.ListReleases("hashicorp", "terraform", opt)

if err != nil {
    fmt.Printf("error: %v\n", err)
} else {
    for _, release := range releases {
        fmt.Printf("%v\n", release)
    }
}

This works and runs okay (no errors at least), but when I run it, the code doesn't return anything. I have a feeling I am missing something simple but am stuck scratching my head.

答案1

得分: 2

GitHub关于列表发布API的文档中可以得知:

这将返回一个发布列表,其中不包括未与发布关联的常规Git标签。要获取Git标签列表,请使用存储库标签API

由于您指定的存储库只包含标签而没有发布,所以没有返回任何内容。

ListReleases更改为ListTags将产生类似以下的输出:

存储库的发布
{0xc2081e1a10 github.Commit{SHA:"d8292227960eab405863ffbc5d13cc85839aa2db", URL:"https://api.github.com/repos/hashicorp/terraform/commits/d8292227960eab405863ffbc5d13cc85839aa2db"} 0xc2081e1a50 0xc2081e1a60}
{0xc2081e1c40 github.Commit{SHA:"4ed2c8d956d2eae83a2a3750290711ce7fa1ec76", URL:"https://api.github.com/repos/hashicorp/terraform/commits/4ed2c8d956d2eae83a2a3750290711ce7fa1ec76"} 0xc2081e1c60 0xc2081e1c70}
{0xc2081e1ca0 github.Commit{SHA:"33e11f0eba1b634849784151b29f69314bfae827", URL:"https://api.github.com/repos/hashicorp/terraform/commits/33e11f0eba1b634849784151b29f69314bfae827"} 0xc2081e1cb0 0xc2081e1cc0}
{0xc2081e1cf0 github.Commit{SHA:"8175d7f82134d378234a407e529853da681af07e", URL:"https://api.github.com/repos/hashicorp/terraform/commits/8175d7f82134d378234a407e529853da681af07e"} 0xc2081e1d10 0xc2081e1d20}
{0xc2081e1d50 github.Commit{SHA:"166efa1ba0c7aa2fb5a7230f787c8499c765786e", URL:"https://api.github.com/repos/hashicorp/terraform/commits/166efa1ba0c7aa2fb5a7230f787c8499c765786e"} 0xc2081e1d60 0xc2081e1d70}
{0xc2081e1da0 github.Commit{SHA:"1894e985557f010a4bca873c5b10bedb1cd7de5d", URL:"https://api.github.com/repos/hashicorp/terraform/commits/1894e985557f010a4bca873c5b10bedb1cd7de5d"} 0xc2081e1dc0 0xc2081e1dd0}
{0xc2081e1e00 github.Commit{SHA:"0bc0c03fece07c4f21ee5195743116a4d418f234", URL:"https://api.github.com/repos/hashicorp/terraform/commits/0bc0c03fece07c4f21ee5195743116a4d418f234"} 0xc2081e1e10 0xc2081e1e20}
{0xc2081e1e50 github.Commit{SHA:"4e11ed4327fa638a4c2587489a8660c69beb4047", URL:"https://api.github.com/repos/hashicorp/terraform/commits/4e11ed4327fa638a4c2587489a8660c69beb4047"} 0xc2081e1e70 0xc2081e1e80}
{0xc2081e1eb0 github.Commit{SHA:"a4cf3fec1c8341041f41897ea84781e6be9b1dad", URL:"https://api.github.com/repos/hashicorp/terraform/commits/a4cf3fec1c8341041f41897ea84781e6be9b1dad"} 0xc2081e1ec0 0xc2081e1ed0}
{0xc2081e1f00 github.Commit{SHA:"146d081b65aba3e0f183d6e4b26f3e794fabaebf", URL:"https://api.github.com/repos/hashicorp/terraform/commits/146d081b65aba3e0f183d6e4b26f3e794fabaebf"} 0xc2081e1f20 0xc2081e1f30}
英文:

From the GitHub documentation on the list releases API:

> This returns a list of releases, which does not include regular Git tags that have not been associated with a release. To get a list of Git tags, use the Repository Tags API.

Since the repository you specified contains only tags and not releases, nothing is returned.

Changing ListReleases to ListTags yields output similar to the following:

Releases for repo
{0xc2081e1a10 github.Commit{SHA:"d8292227960eab405863ffbc5d13cc85839aa2db", URL:"https://api.github.com/repos/hashicorp/terraform/commits/d8292227960eab405863ffbc5d13cc85839aa2db"} 0xc2081e1a50 0xc2081e1a60}
{0xc2081e1c40 github.Commit{SHA:"4ed2c8d956d2eae83a2a3750290711ce7fa1ec76", URL:"https://api.github.com/repos/hashicorp/terraform/commits/4ed2c8d956d2eae83a2a3750290711ce7fa1ec76"} 0xc2081e1c60 0xc2081e1c70}
{0xc2081e1ca0 github.Commit{SHA:"33e11f0eba1b634849784151b29f69314bfae827", URL:"https://api.github.com/repos/hashicorp/terraform/commits/33e11f0eba1b634849784151b29f69314bfae827"} 0xc2081e1cb0 0xc2081e1cc0}
{0xc2081e1cf0 github.Commit{SHA:"8175d7f82134d378234a407e529853da681af07e", URL:"https://api.github.com/repos/hashicorp/terraform/commits/8175d7f82134d378234a407e529853da681af07e"} 0xc2081e1d10 0xc2081e1d20}
{0xc2081e1d50 github.Commit{SHA:"166efa1ba0c7aa2fb5a7230f787c8499c765786e", URL:"https://api.github.com/repos/hashicorp/terraform/commits/166efa1ba0c7aa2fb5a7230f787c8499c765786e"} 0xc2081e1d60 0xc2081e1d70}
{0xc2081e1da0 github.Commit{SHA:"1894e985557f010a4bca873c5b10bedb1cd7de5d", URL:"https://api.github.com/repos/hashicorp/terraform/commits/1894e985557f010a4bca873c5b10bedb1cd7de5d"} 0xc2081e1dc0 0xc2081e1dd0}
{0xc2081e1e00 github.Commit{SHA:"0bc0c03fece07c4f21ee5195743116a4d418f234", URL:"https://api.github.com/repos/hashicorp/terraform/commits/0bc0c03fece07c4f21ee5195743116a4d418f234"} 0xc2081e1e10 0xc2081e1e20}
{0xc2081e1e50 github.Commit{SHA:"4e11ed4327fa638a4c2587489a8660c69beb4047", URL:"https://api.github.com/repos/hashicorp/terraform/commits/4e11ed4327fa638a4c2587489a8660c69beb4047"} 0xc2081e1e70 0xc2081e1e80}
{0xc2081e1eb0 github.Commit{SHA:"a4cf3fec1c8341041f41897ea84781e6be9b1dad", URL:"https://api.github.com/repos/hashicorp/terraform/commits/a4cf3fec1c8341041f41897ea84781e6be9b1dad"} 0xc2081e1ec0 0xc2081e1ed0}
{0xc2081e1f00 github.Commit{SHA:"146d081b65aba3e0f183d6e4b26f3e794fabaebf", URL:"https://api.github.com/repos/hashicorp/terraform/commits/146d081b65aba3e0f183d6e4b26f3e794fabaebf"} 0xc2081e1f20 0xc2081e1f30}

huangapple
  • 本文由 发表于 2015年6月11日 04:24:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/30766606.html
匿名

发表评论

匿名网友

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

确定