首頁  >  文章  >  後端開發  >  如何建立特定版本的 Github APIv4 Golang 查詢

如何建立特定版本的 Github APIv4 Golang 查詢

WBOY
WBOY轉載
2024-02-13 23:12:08903瀏覽

如何构建特定版本的 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刪除