octokit / octokit.js

The all-batteries-included GitHub SDK for Browsers, Node.js, and Deno.
MIT License
6.84k stars 1k forks source link

[BUG]: Cannot create new commit with Octokit #2628

Closed Baroshem closed 4 months ago

Baroshem commented 4 months ago

What happened?

Hey there!

Thanks for the amazing package. I really appreciate how easy it is to use it and the type autocompletion is just great!

I recently wanted to create a new functionality that in short words would just create a new empty (or dummy text) commit after certain operation (create a new repository, push code there and additionaly add an empty commit after previous steps).

I looked at the documentation (https://docs.github.com/en/rest/git/commits?apiVersion=2022-11-28#create-a-commit) and created following code in my project:

      const commits = await octokit.request('GET /repos/{owner}/{repo}/commits', {
        owner: '<OWNER>',
        repo: '<REPO_NAME',
      })

      const res = await octokit.request('POST /repos/{owner}/{repo}/git/commits', {
        owner: '<OWNER>',
        repo: '<REPO_NAME',
        message: 'test',
        tree: commits.data[0].commit.tree.sha
      })

When I logged the response from the second request I got the log linked below in the Relevant Log Output section.

So it looks like the commit was successfully created.

But when I inspect GitHub I see the following:

image

Am I doing something wrong in the implementation? From the response it would mean that the operation was successful.

I will appreciate your help!

Thanks πŸ’š

Versions

"@octokit/auth-app": "^6.0.3",
"@octokit/core": "^5.1.0",

Node v18.17.1 npm 9.6.7

Relevant log output

{
   status: 201,
   data: {
     sha: '1986201a5b4c35933531e1df38f9d29dd13e9ea8',
     author: {},
     committer: {},
     tree: {
       sha: '<TREE_SHA>',
     },
     message: 'test',
     parents: [],
     verification: {
       verified: true,
       reason: 'valid',
       signature: '-----BEGIN PGP SIGNATURE-----\n' +
            <SIGNATURE>
         '-----END PGP SIGNATURE-----\n',
       payload: 'tree <TREE_SHA>' +
         'author <AUTHOR> +0000\n' +
         'committer <COMMITTER> +0000\n' +
         '\n' +
         'test'
     }
   }
 }

Code of Conduct

github-actions[bot] commented 4 months ago

πŸ‘‹ Hi! Thank you for this contribution! Just to let you know, our GitHub SDK team does a round of issue and PR reviews twice a week, every Monday and Friday! We have a process in place for prioritizing and responding to your input. Because you are a part of this community please feel free to comment, add to, or pick up any issues/PRs that are labled with Status: Up for grabs. You & others like you are the reason all of this works! So thank you & happy coding! πŸš€

kfcampbell commented 4 months ago

Hi @Baroshem, it does indeed look like the commit was successful. Is the red X present in your screenshot giving you hesitations? Usually that's present due to Actions failing on the commit. Are there workflow files present in the repository you're using?

Baroshem commented 4 months ago

Hey there, thanks for feedback.

I didnt meant the X cross but rather the fact that there was Initial commit and I have send additional request to create a new commit with a message test.

I figured out that I need to create a reference as well because without it the commit wouldnt be assigned to any commit tree.

I have managed to make it work by sending an additional request to create a ref.

I think the issue can be closed :)