如何使用GitHub GraphQl API创建新提交?

发布于 2025-02-12 09:50:26 字数 656 浏览 0 评论 0 原文

我正在尝试使用github graphQl api创建新提交,并使用 createCommitonbranch 突变。 预期黑头应该使用什么值:“ ?????”? 如何从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: "?????"
    }
  ) 
}
}

I am trying to create a new commit using the github graphql api, using the createCommitOnBranch mutation.
What value should one use for expectedHeadOid: "?????"?
How can one get such value from the Graphql API?

This is my attempt so far:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狼亦尘 2025-02-19 09:50:26

它应该是您要创建的新提交的父母提交。
因此,“ Expectheadheadoid”:“ git rev-parse head” head> head head> head head head )上您想使用一个突变 createCommitonBranch 其端点)。

预期黑头(gitobjectId!)
提交提交之前的git提交期望在分支机构的头部。

git提交在提交之前的分支机构的头部预期。

carl brasic 在他的要点中显示 createCommitonbranch错误示例“如果您传递了date date expect> greenge> greenge> greenge> greation

我们是有意告诉API仅当提示是我们知道的值不是一个值时,只有在分支上附加提交。
假设此克隆与遥控器是最新的,这将始终以描述性错误失败。

  gucessheadoid =`git rev-parse head〜`
 

错误:

"message": "Expected branch to point to \"f786b7e2e0ec290972a2ada6858217ba16305933\" 
            but it did not.  Pull and try again."

此示例 /code>)获取第二个参数值( createCommitonBranchInput ),当您没有本地克隆的存储库时很有用:

第1步。查询最后一个对分支的犯罪的OID,因为需要进行提交。
示例GraphQl查询如下所示:

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

步骤2。使用称为“ createCommitonBranchInput ”的GraphQl突变,以创建使用新文件内容的提交:

----------------------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
  }
}

It should be the parent commit of the new commit you want to create.
Hence the "expectedHeadOid": "git rev-parse HEAD" which prints the SHA1 hash of HEAD (HEAD of the remote repository on top of which you want to append a new commit using a mutation createcommitonbranch, and its endpoint).

expectedHeadOid (GitObjectID!)
The git commit oid expected at the head of the branch prior to the commit.

In octokit, it is described as:

The git commit oid expected at the head of the branch prior to the commit.

Carl Brasic shows in his gist "createCommitOnBranch error example" what happens if you pass an out of date expectedHeadOid value

We are intentionally telling the API to append a commit to the branch only if the tip is a value that we know it is not.
Assuming this clone is up to date with the remote this will always fail with a descriptive error.

expectedHeadOid=`git rev-parse HEAD~`

Error:

"message": "Expected branch to point to \"f786b7e2e0ec290972a2ada6858217ba16305933\" 
            but it did not.  Pull and try again."

This example uses a first query (defaultBranchRef ) to get the parameter value for the second one (CreateCommitOnBranchInput), useful when you don't have a locally cloned repository:

Step 1. Query the OID of the last commit to the branch, as it is required to make a commit.
Example graphQL query is shown below:

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

Step 2. Use graphQL mutation called "CreateCommitOnBranchInput" to create a commit with new file content:

----------------------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
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文