5

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

1 Answer 1

9

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
  }
}
Sign up to request clarification or add additional context in comments.

5 Comments

So impressed, you are a legend! I was able to accomplish step 1. with the following: {repository(name: "my-new-repository", owner: "AnnaBurd") { { object(expression: "main") { oid } }. Is there any meaningful delta between the solutions? Is there any good resource to learn this API?
@Oded Great, did you manage to make it work?
@Oded Sorry, I did not see at first our edited comment.
@Oded Beside the official GitHub GraphQL explorer, and its official documentation, a GraphQL playground can be useful to play around with this: github.com/graphql/graphql-playground (you have a all collection of them). github.com/EasyGraphQL/easygraphql-tester could help too.
Yes I did, with either your Step 1. or the one I added in comment above. The links you shared are great, but am not see how they lead to knowing how to reach the great solution you shared: iq.opengenus.org/api-requests-in-java/…. Guess it is time for my first Medium post

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.