octokit / octokit.js

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

[BUG]: Path gets URL-encoded when using fine-grained tokens #2422

Open tamoreton opened 1 year ago

tamoreton commented 1 year ago

What happened?

When using octokit.rest.repos.getContent like

    ({ data: markdown } = await octokit.rest.repos.getContent({
      owner: "infinityworks",
      repo: "handbook",
      path: "docs/introduction.md",
      mediaType: {
        format: "raw",
      },
    }));

using a classic PAT that looks like ghp_[REDACTED], it works as expected. However, I want to use a fine-grained token with read access to just the repo I need content from for security reasons. When I do exactly the same call as above but substituting a fine-grained PAT that looks like github_pat_[REDACTED], I get a RequestError [HttpError]: Not Found. When I log the response I note that the request field looks like:

 {
    "method": "GET",
    "url": "https://api.github.com/repos/infinityworks/handbook/contents/docs%2Fintroduction.md",
    "headers": {
      "accept": "application/vnd.github.v3.raw",
      "user-agent": "octokit.js/2.0.14 octokit-core.js/4.2.0 Node.js/18.13.0 (darwin; arm64)",
      "authorization": "token [REDACTED]"
    },
    "request": { "hook": /* ... */ }
  }

It seems that for some reason, using a fine-grained PAT causes getContent to go down a branch which URL-encodes path.

Versions

Octokit.js v2.0.14, Node v18.13.0

Relevant log output

No response

Code of Conduct

RupertBarrow commented 1 year ago

I'm getting the same issue when using a Github OAuthApp :

  // Authentication

  this.github.app = new OAuthApp({
    clientType: "oauth-app",
    clientId: this.config.client_id,
    clientSecret: this.config.client_secret,
  })
  // Request

  request: {
    method: 'GET',
    url: 'https://api.github.com/repos/<owner>/<repo>/contents/.github%2Fworkflows%2Fmywf.yml%3Fref%3Dfeature%2F1-add-checkout-and-deploy-to-github-actions',
    headers: {
      accept: 'application/vnd.github.v3+json',
      'user-agent': 'octokit.js/2.0.14 octokit-core.js/4.2.0 Node.js/18.16.0 (darwin; x64)',
      authorization: 'token [REDACTED]'
    }

Response.status is 404

RupertBarrow commented 1 year ago

Do you think that this is related to https://github.com/octokit/endpoint.js/issues/207 (which has been fixed) ?