如何使用 github graphql API 创建新的提交?

Ode*_*ded 4 github graphql

我正在尝试使用 github graphql api、使用createCommitOnBranch突变创建一个新的提交。ExpectedHeadOid 应该使用什么值:“??????”?如何从 Graphql API 获得这样的价值?

到目前为止,这是我的尝试:

{
mutation m1 {
  createCommitOnBranch(
    input: {
      branch: 
      {repositoryNameWithOwner: "some_repo/some_owner",
        branchName: "main"
      },
      message: {headline: "headline!"},
      fileChanges: {
        additions: {path: "README.md", contents: "SGVsbG8gV29ybGQ="}
      }
      expectedHeadOid: "?????"
    }
  ) 
}
}
Run Code Online (Sandbox Code Playgroud)

Von*_*onC 6

它应该是您要创建的新提交的父提交。
因此,它会打印(您想要使用突变在其上附加新提交的远程存储库及其端点"expectedHeadOid": "git rev-parse HEAD")的SHA1 哈希值。HEADHEADcreatecommitonbranch

expectedHeadOid(GitObjectID!)
提交之前预期位于分支头部的 git commit oid。

在 octokit 中,它被描述为:

git commit oid 预计在提交之前位于分支的头部。

Carl Brasic在他的要点“ createCommitOnBranch 错误示例”中展示了如果传递过期expectedHeadOid值会发生什么

我们故意告诉 API 仅当提示是我们知道它不是的值时才将提交附加到分支。
假设此克隆与遥控器是最新的,这将始终失败并出现描述性错误。

expectedHeadOid=`git rev-parse HEAD~`
Run Code Online (Sandbox Code Playgroud)

错误:

"message": "Expected branch to point to \"f786b7e2e0ec290972a2ada6858217ba16305933\" 
            but it did not.  Pull and try again."
Run Code Online (Sandbox Code Playgroud)

此示例使用第一个查询 ( defaultBranchRef) 获取第二个查询 ( CreateCommitOnBranchInput) 的参数值,当您没有本地克隆存储库时非常有用:

步骤1.查询最后一次提交到分支的OID,因为进行提交需要它。
graphQL 查询示例如下所示:

{
  repository(name: "my-new-repository", owner: "AnnaBurd") {
    defaultBranchRef {
      target {
        ... on Commit {
          history(first: 1) {
            nodes {
              oid
            }
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

步骤 2. 使用名为“”的 graphQL 突变CreateCommitOnBranchInput来创建包含新文件内容的提交:

----------------------mutation ------------------
mutation ($input: CreateCommitOnBranchInput!) {
  createCommitOnBranch(input: $input) {
    commit {
      url
    }
  }
}

-----------variables for mutation---------------
{
  "input": {
    "branch": {
      "repositoryNameWithOwner": "AnnaBurd/my-new-repository",
      "branchName": "main"
    },
    "message": {
      "headline": "Hello from GraphQL!"
    },
    "fileChanges": {
      "additions": [
        {
          "path": "myfile.txt",
          "contents": "SGVsbG8gZnJvbSBKQVZBIGFuZCBHcmFwaFFM"      <------- encoded base 64
        }
      ]
    },
    "expectedHeadOid": "db7a5d870738bf11ce1fc115267d13406f5d0e76"  <----- oid from step 1
  }
}
Run Code Online (Sandbox Code Playgroud)