octokit / rest.js

GitHub REST API client for JavaScript
https://octokit.github.io/rest.js
MIT License
548 stars 63 forks source link

[BUG]: `createOrUpdateFileContents` is encodeURI parameter `path` alway #409

Closed kritsananac closed 7 months ago

kritsananac commented 7 months ago

What happened?

I want update file content with this func createOrUpdateFileContents and put parameter path into option, it alway request error

The value of path is src/values.yaml not sure why is alway encodedURI (%2F is /)

Error Response

"request": {
  "method": "PUT",
  "url": "https://api.github.com/repos/[REDACTED ONWER]/[REDACTED REPO]/contents/%2Fsrc%2Fvalues.yaml",
  "headers": {
    "accept": "application/vnd.github.v3+json",
    "user-agent": "octokit-rest.js/20.0.2 octokit-core.js/5.1.0 Node.js/20.10.0 (darwin; arm64)",
    "authorization": "token [REDACTED]",
    "content-type": "application/json; charset=utf-8"
  },
  "body": "[REDACTED]",
  "request": {}
}

Versions

"@octokit/rest": "20.0.2",
"node": "20.10.0"

Relevant log output

private octokit: Octokit;
     private filePath = '/src/values.yaml';

     this.octokit = new Octokit({
       auth: [REDACTED]
     });

     const fileContent = await this.octokit.repos.getContent({
        owner,
        repo,
        path: filePath,
      });
      this.logger.debug(`Retrieve file ${repo} content done`);

      // Get content from response
      // yaml load and dump 

      await this.octokit.repos.createOrUpdateFileContents({
        owner,
        repo,
        path: filePath,
        message: `feat: Update file ${Date.now()}`,
        content: Buffer.from(updatedContent).toString('base64'),
        sha,
        branch: 'main',
      });

Code of Conduct

github-actions[bot] commented 7 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! 🚀

gr2m commented 7 months ago

Try removing the leading slash

- private filePath = '/src/values.yaml';
+ private filePath = 'src/values.yaml';

The path URL parameter being encoded is not a bug, it works the way it should

kritsananac commented 7 months ago

Hi, @gr2m

I trying with path src/inner.yaml and outer.yaml, It's still not working

PS' that path is exist file in repo

kritsananac commented 7 months ago

Response for path is src/inner.yaml

err: {
  "type": "RequestError",
  "message": "Not Found",
  "stack":
    HttpError: Not Found
      at /node_modules/@octokit/request/dist-node/index.js:124:21
      at processTicksAndRejections (node:internal/process/task_queues:95:5)
  "name": "HttpError",
  "status": 404,
  "response": {
    "url": "https://api.github.com/repos/[OWNER]/[REPO]/contents/src%2Finner.yaml",
gr2m commented 7 months ago

I'm not sure what's not working as the repo is private, but here is an example URL that works with the URL encoded path https://api.github.com/repos/octokit/rest.js/contents/src%2Findex.ts

kritsananac commented 7 months ago

I got it. Repo is already public Try to access 2 url in below, it's accessible and return JSON

https://api.github.com/repos/[OWNER]/[REPO]/contents/outer.yaml
https://api.github.com/repos/[OWNER]/[REPO]/contents/src%2fvalues.yaml

Maybe i have missing some configuration or something (It's not a bug, sorry...)

Let me ask, using personal access token (classic) need to allow any scope ?

kritsananac commented 7 months ago

My bad, I have using a wrong PAT