首页  >  文章  >  后端开发  >  如何构建特定版本的 Github APIv4 Golang 查询

如何构建特定版本的 Github APIv4 Golang 查询

WBOY
WBOY转载
2024-02-13 23:12:08904浏览

如何构建特定版本的 Github APIv4 Golang 查询

php小编柚子将为大家介绍如何构建特定版本的 Github APIv4 Golang 查询的方法。Github APIv4是一个强大的查询工具,可以帮助开发者获取并处理Github上的各种数据。然而,有时候我们可能需要获取特定版本的数据,而官方API并没有直接提供这样的功能。在本文中,我们将探讨如何使用Golang编写自定义的查询,以获取特定版本的数据。

问题内容

使用 https://github.com/shurcooL/githubv4,我真的很难为 gh 存储库拉回特定版本。

当有 v3 版本时,以下代码块始终不返回任何内容:

var releaseQ struct {
  Repository struct {
    Release struct {
        Author githubv4.String
    } `graphql:"release(tagName:$tagName)"`
  } `graphql:"repository(owner:$repositoryOwner,name:$repositoryName)"`
}
variables = map[string]interface{}{
    "repositoryOwner": githubv4.String("jacobtomlinson"),
    "repositoryName":  githubv4.String("gha-find-replace"),
    "tagName":         githubv4.String("v3"),
}
err = client.Query(context.Background(), &releaseQ, variables)
if err != nil {
    fmt.Println("Query returned nothing")
}
fmt.Println("author:", releaseQ.Repository.Release.Author)

我已成功获得以下两个代码块,用于存储库描述和问题反应:

var repoDescriptionQ struct {
  Repository struct {
    Description string
  } `graphql:"repository(owner: \"jacobtomlinson\", name: \"gha-find-replace\")"`
}

此成功返回存储库描述 ^

variables := map[string]interface{}{
  "repositoryOwner": githubv4.String("jacobtomlinson"),
  "repositoryName":  githubv4.String("gha-find-replace"),
  "issueNumber":     githubv4.Int(55),
  "reactionContent": githubv4.ReactionContentThumbsDown,
}
var reactionQ struct {
Repository struct {
    Issue struct {
        ID        githubv4.ID
        Reactions struct {
            ViewerHasReacted githubv4.Boolean
        } `graphql:"reactions(content:$reactionContent)"`
    } `graphql:"issue(number:$issueNumber)"`
} `graphql:"repository(owner:$repositoryOwner,name:$repositoryName)"`
}

这成功地得到了反应^

解决方法

发现,作者字段不是字符串,而是“User”类型。将请求的字段更改为“描述”,这是一个字符串,它正在拉回发布信息。如果您确实需要作者,则需要定义用户:

var releaseQ struct {
    Repository struct {
        Release struct {
            Description githubv4.String
        } `graphql:"release(tagName:$tagName)"`
    } `graphql:"repository(owner:$repositoryOwner,name:$repositoryName)"`
}

以上是如何构建特定版本的 Github APIv4 Golang 查询的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:stackoverflow.com。如有侵权,请联系admin@php.cn删除