Marcus
Marcus

Reputation: 1

GithubAPI Commit without fast-forward

since some days I try to interact with the GitHub API to create a commit. My Problem is there the result in GIT, after the reference update to the new Commit. Github returns every time with:

Status Code 422: {
    "message": "Update is not a fast forward",
    "documentation_url": "https://docs.github.com/enterprise/2.22/rest/reference/git#update-a-reference"
}

But I don't want to create a fast forward commit just a new commit on the master branch. Which Param do I forgot or did I set something wrong?

To reproduce the error I used postman:

  1. Create Blob - https://{{server}}/api/v3/repos/:owner/:repo/git/blobs
{
    "content":"This is my content at {{currentDate}}", 
    "encoding":"utf-8"

}

-> save response.sha in "shaBlob".
2. getRef - https://{{server}}/api/v3/repos/:owner/:repo/git/ref/heads/master
-> save response.object.sha in "shaMaster".
3. getLastCommit - https://{{server}}/api/v3/repos/:owner/:repo/git/commits/:commit_sha
-> save response.tree.sha in "shaMasterTree"
4. create Tree - https://{{server}}/api/v3/repos/:owner/:repo/git/trees
Body:

{
    "base_tree": "{{shaCommitLast}}",
    "tree": [{
        "path": "readme.md",
        "mode": "100644",
        "type": "blob",
        "sha": "{{shaBlob}}"
    }]
}

-> save response.sha in shaTreeNew
5. create Commit - https://{{server}}/api/v3/repos/:owner/:repo/git/commits Body:

{
    "message": "This is a bot Commit",
    "tree": "{{shaTreeNew}}",
    "author": {
        "name": "ASLob",
        "email": "[email protected]",
        "date": "{{currentDate}}"
    },
    "committer": {
        "name": "ASLob",
        "email": "[email protected]",
        "date": "{{currentDate}}"
    }
}

-> save response.sha in shaCommitNew 6. update Reference - https://{{server}}/api/v3/repos/:owner/:repo/git/refs/:ref Body:

{
    "sha": "{{shaCommitNew}}",
    "force": false
}

Upvotes: 0

Views: 819

Answers (1)

I also faced the problem and found a solution.

When submitting a commit:

parents: The SHAs of the commits that were the parents of this commit. If omitted or empty, the commit will be written as a root commit. For a single parent, an array of one SHA should be provided; for a merge commit, an array of more than one should be provided.

In our case, for the parent, since we want to add another commit on a particular branch, we need to get the SHA of it.

You can get more information here: https://dev.to/bro3886/create-a-folder-and-push-multiple-files-under-a-single-commit-through-github-api-23kc

Upvotes: 1

Related Questions