octokit / rest.js

GitHub REST API client for JavaScript

Home Page:https://octokit.github.io/rest.js

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[BUG]: path .github/workflows/workflows.yml not commited by octokit

muyoungko opened this issue · comments

What happened?

When I try to commit programmatically by octokit, some path(likes .github/workflows/workflows.yml) cause 404 error.

Following code is

//upload blobs for commit
octokit.request(POST /repos/muyoungko/my_repository/git/blobs, {
content: somebyte.toString('base64'),
encoding: 'base64',
})

//get the blob_sha from it
let blob_sha ...

//make commit tree list
let tree_list = []
tree_list.push({
"path": '.github/workflows/workflows.yml', //If I remove dot from path my code work well
"mode":'100644',
"type": 'blob',
"sha": blob_sha
})

const tree = await octokit.request(POST /repos/muyoungko/my_repository/git/trees, {
"base_tree": last_commit_sha,
"tree": tree_list
})

const tree_sha = tree.data.sha
//create commit ERROR occured
let new_commit = await octokit.request(POST /repos/muyoungko/my_repository/git/commits, {
"message": aaa,
"author": {
"name": "muyoungko",
"email": "muyoungko@gmail.com"
},
"parents": [
last_commit_sha
],
"tree": tree_sha
})

It is just usual commit code, but if path has dot(.github directory) error is occured.

Versions

v20.0.2

Relevant log output

RequestError [HttpError]: Not Found
[0]     at /Users/muyoungko/Documents/devil/devil_admin/node_modules/@octokit/request/dist-node/index.js:112:21
[0]     at processTicksAndRejections (node:internal/process/task_queues:96:5)
[0]     at async /Users/muyoungko/Documents/devil/devil_admin/src/service/GithubService.js:112:26 {
[0]   status: 404,
[0]   response: {
[0]     url: 'https://api.github.com/repos/mondayless/bridge_front/git/trees',
[0]     status: 404,
[0]     headers: {
[0]       'access-control-allow-origin': '*',
[0]       'access-control-expose-headers': 'ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset',
[0]       connection: 'close',
[0]       'content-encoding': 'gzip',
[0]       'content-security-policy': "default-src 'none'",
[0]       'content-type': 'application/json; charset=utf-8',
[0]       date: 'Fri, 06 Oct 2023 07:51:46 GMT',
[0]       'referrer-policy': 'origin-when-cross-origin, strict-origin-when-cross-origin',
[0]       server: 'GitHub.com',
[0]       'strict-transport-security': 'max-age=31536000; includeSubdomains; preload',
[0]       'transfer-encoding': 'chunked',
[0]       vary: 'Accept-Encoding, Accept, X-Requested-With',
[0]       'x-accepted-oauth-scopes': '',
[0]       'x-content-type-options': 'nosniff',
[0]       'x-frame-options': 'deny',
[0]       'x-github-api-version-selected': '2022-11-28',
[0]       'x-github-media-type': 'github.v3; format=json',
[0]       'x-github-request-id': 'FEFD:959EB:DD98B:F50D4:651FBC92',
[0]       'x-oauth-scopes': 'gist, repo',
[0]       'x-ratelimit-limit': '5000',
[0]       'x-ratelimit-remaining': '4571',
[0]       'x-ratelimit-reset': '1696578813',
[0]       'x-ratelimit-resource': 'core',
[0]       'x-ratelimit-used': '429',
[0]       'x-xss-protection': '0'
[0]     },
[0]     data: {
[0]       message: 'Not Found',
[0]       documentation_url: 'https://docs.github.com/rest/git/trees#create-a-tree'
[0]     }
[0]   },
[0]   request: {
[0]     method: 'POST',
[0]     url: 'https://api.github.com/repos/mondayless/bridge_front/git/trees',
[0]     headers: {
[0]       accept: 'application/vnd.github.v3+json',
[0]       'user-agent': 'octokit-rest.js/20.0.2 octokit-core.js/5.0.1 Node.js/16.16.0 (darwin; x64)',
[0]       authorization: 'token [REDACTED]',
[0]       'content-type': 'application/json; charset=utf-8'
[0]     },
[0]     body: '{"base_tree":"334735f70cd8dfa826de612f7fd3edbf1444309d","tree":[{"path":".github/workflows/workflows.yml","mode":"100644","type":"blob","sha":"211c085683d90684d21d4fd5e1a87cd1308f667f"}]}',
[0]     request: {
[0]       fetch: <ref *1> [Function: fetch] {
[0]         isRedirect: [Function (anonymous)],
[0]         Promise: [Function: Promise],
[0]         default: [Circular *1],
[0]         Headers: [class Headers],
[0]         Request: [class Request],
[0]         Response: [class Response],
[0]         FetchError: [Function: FetchError]
[0]       },
[0]       hook: [Function: bound bound register]
[0]     }
[0]   }
[0] }

Code of Conduct

  • I agree to follow this project's Code of Conduct

👋 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! 🚀

Hey @muyoungko Thanks for reaching out and apologies for the trouble.

Would you mind trying to use the createCommit SDK API to create the commit as a sanity check. Note, you'll need to get the latest commit sha first...

  const {
    data: [
      {
        sha: latestCommitSha,
        commit: {
          tree: { sha: latestCommitTreeSha }
        }
      }
    ]
  } = await octokit.repos.listCommits({
    'muyoungko',
    'my_repository',
    sha: 'branch',
    per_page: 1
  });

[CREATE THE BLOB LIKE ABOVE]
[CREATE THE TREE LIKE ABOVE] - get newTreeSha

  const { data: commit } = await octokit.git.createCommit({
    'muyoungko',
    'my_repository',
    message: "test",
    tree: newTreeSha,
    parents: [latestCommitSha]
  });

console.log(commit);

Let me know what you find when you get the chance. The built n SDK methods tend to handle things like escaping and encoding much better than the generic request. Not that this is the answer, but I just want to make sure it's not something else.

I think accessing .github/workflows/*.yml files requires a special permission or scope

I think accessing .github/workflows/*.yml files requires a special permission or scope

Yes, other files are OK, only github/workflows/*.yml this one cause 404 error.

I don't know how to treat for it likes special permission or scope.

const { data: commit } = await octokit.git.createCommit({

I tried to do with

  • const { data: commit } = await octokit.git.createCommit({

but still same result http response code 404 on path ".github/workflows/workflows.yml"

The scope is called workflow, you need to enable it for your personal access tokens (classic)

For GitHub Apps and the new tokens, the permission is called Workflows:

It works. Thanks for your support.
I didn't know the settings on each files's authority.
Have a nice day~