Home > Backend Development > Golang > How to build a Github APIv4 Golang query for a specific version

How to build a Github APIv4 Golang query for a specific version

WBOY
Release: 2024-02-13 23:12:08
forward
965 people have browsed it

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

php editor Youzi will introduce to you how to build a specific version of Github APIv4 Golang query. Github APIv4 is a powerful query tool that can help developers obtain and process various data on Github. However, sometimes we may need to obtain a specific version of data, and the official API does not directly provide such a function. In this article, we will explore how to write custom queries using Golang to get a specific version of data.

Question content

Using https://github.com/shurcooL/githubv4, I'm really having trouble pulling back a specific version for the gh repository.

When there is a v3 version, the following code block always returns nothing:

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)
Copy after login

I have successfully obtained the following two code blocks for the repository description and issue reaction:

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

This success returns the repository description ^

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)"`
}
Copy after login

This successfully got the response^

Workaround

Found that the author field is not a string, but of type "User". Change the requested field to "Description" which is a string and it is pulling back the posting information. If you really need an author, you need to define the user:

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

The above is the detailed content of How to build a Github APIv4 Golang query for a specific version. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template