github - 使用 GraphQL 突变删除 github 中的一个分支

标签 github graphql github-api

我想使用 GraphQL 突变删除一个 github 分支,但我还没有找到关于 deleteRef 的足够信息命令。使用 GraphQL explorer我想出了这个废话:

mutation {
  deleteRef(input: {refId: "my-branch"}) {
    __typename
  }
}

我还不知道如何添加存储库信息以使突变具有任何意义,我包含 __typename 的唯一原因是因为 deleteRef block 不能'不要留空。我该如何修复这种突变?

最佳答案

您不能在该突变中返回任何内容,因为它不是 Void 类型。您需要像下面这样定义一个突变:

mutation DeleteBranch($branchRef: ID!) {
  deleteRef(input: {refId: $branchRef}) {
    __typename
    clientMutationId
  }
}

explorer您可以定义查询和突变,并选择在您按下按钮时执行哪一个。资源管理器中的完整示例是:

query GetBranchID {
  repository(name: "test-repo", owner: "bertrandmartel") {
    refs(refPrefix: "refs/heads/", first: 100) {
      nodes {
        id
        name
      }
    }
  }
}

mutation DeleteBranch($branchRef: ID!) {
  deleteRef(input: {refId: $branchRef}) {
    __typename
    clientMutationId
  }
}

带有变量:

{
  "branchRef": "MDM6UmVmMjQ5MDU0NzQ0Om1hc3Rlcg=="
}

您可以在资源管理器中执行以下操作:

  • 执行GetBranchID查询
  • 将引用复制粘贴到 branchRef 变量中
  • 执行DeleteBranch

如果没有资源管理器,这是使用 时的样子:

import requests

repo_name = "YOUR_REPO"
repo_owner = "YOUR_USERNAME"
token = "YOUR_TOKEN"

query = """
query {
  repository(name: \"""" + repo_name + """\", owner: \"""" + repo_owner + """\") {
    refs(refPrefix: "refs/heads/", first: 100) {
      nodes {
        id
        name
      }
    }
  }
}
"""

resp = requests.post('https://api.github.com/graphql', 
    json={ "query": query},
    headers= {"Authorization": f"Token {token}"}
)
body = resp.json()

for i in body["data"]["repository"]["refs"]["nodes"]:
    print(i["name"], i["id"])

chosenBranchId = input("Enter a branch ID: ")

query = """
mutation {
  deleteRef(input: {refId: \"""" + chosenBranchId + """\"}) {
    __typename
    clientMutationId
  }
}
"""

resp = requests.post('https://api.github.com/graphql', 
    json={ "query": query},
    headers= {"Authorization": f"Token {token}"}
)
print(resp.json())

关于github - 使用 GraphQL 突变删除 github 中的一个分支,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60655699/

相关文章:

oauth - 如何制作一个类似于Coderwall上的 “signup with Github”按钮?

typescript - Apollo GraphQL 服务器 + TypeScript

javascript - 在 javascript 中使用 OAuth 连接 Github

javascript - 如何从客户端 javascript 查询 GitHub api v4?

github - 如何使用 GitHub API 一次检索多个用户?

github - 如何将 GitHub 问题和 PRs 包含在 API 触发的 repo 导入到 GitLab 中?

从子文件夹中 Git 多个子项目?

Github api 用于获取对给定存储库分支完成的所有提交

javascript - graphQL 解析器不等待 Web API 完成

reactjs - 调用Apollo客户端的GraphQL查询