Home > OS >  How to create new commit with the github graphql API?
How to create new commit with the github graphql API?

Time:07-03

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: "?????" } ) } }

CodePudding user response:

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