octokit / octokit.js

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

404 on PUT content with content: write permissions set #2415

Open apricot13 opened 1 year ago

apricot13 commented 1 year ago

Hi there, I'm at a loss with updating the contents of a file using the rest api. I'm getting a 404 not found error when updating a file using github actions.

I can get the contents but not update it. I've seen others have the same issue with permissions and I'm fairly confident I've tried all the permissions things I know of but maybe theres something I missed. Its worth mentioning that the repo is an organisation one and not a personal one if that makes any difference.

My github_token permissions from the actions:

GITHUB_TOKEN Permissions
  Contents: write
  Issues: read
  Metadata: read

I can see from here that contents:write is needed for my calls and my GET issues call works fine along with my GET content one its just PUT content that fails.


async function updateReadMe(generatedData) {
  try {
    console.log("Getting current ROADMAP.md");
    const res = await request(
      `GET /repos/wearefuturegov/Outpost-Platform/contents/create-roadmap/ROADMAP.md`,
      {
        headers: {
          Accept: "application/vnd.github+json",
          "X-GitHub-Api-Version": "2022-11-28",
          authorization: process.env.GITHUB_TOKEN,
        },
      }
    );
    const { path, sha, content, encoding, size, html_url } = res.data;

    console.log(`Current version SHA: ${sha}. ${html_url}`);
    if (sha) {
      commitNewReadme(path, sha, encoding, generatedData);
    }
  } catch (error) {
    console.log(error);
  }
}

async function commitNewReadme(path, sha, encoding, updatedContent) {
  try {
    console.log("Updating current ROADMAP.md with new version");
    const updatedRoadmap = await request(
      `PUT /repos/wearefuturegov/Outpost-Platform/contents/create-roadmap/ROADMAP.md`,
      {
        owner: "wearefuturegov",
        repo: "Outpost-Platform",
        message: "Update ROADMAP",
        content: Buffer.from("Test", "utf-8").toString(encoding),
        path: "create-roadmap/ROADMAP.md",
        committer: [{ name: "fg-dev bot", email: "fg-dev@tpximpact.com" }],
        sha,
        type: "file",
        encoding: "base64",
        headers: {
          Accept: "application/vnd.github+json",
          "X-GitHub-Api-Version": "2022-11-28",
          authorization: process.env.GITHUB_TOKEN,
        },
      }
    );

    console.log(`Roadmap update committed: ${updatedRoadmap.commit.html_url}`);
  } catch (err) {
    console.log(err);
  }
}

And my github action

name: Update roadmap readme
on:
  issues:
    types: [labeled, unlabeled]
jobs:
  buildRoadmap:
    runs-on: ubuntu-latest
    permissions:
      contents: write
      issues: read
    steps:
      - uses: actions/checkout@v3

      - uses: actions/setup-node@v3
        with:
          node-version: 16

      - name: Install dependencies
        run: npm ci

      - name: Create roadmap
        env:
          GITHUB_TOKEN: ${{ github.token }}
          COMMIT_CHANGE: true
        run: npm run build-roadmap

This is the result I'm getting

Getting current README.md
Current version SHA: 772bc03b371e6dc38f1c637cc3c37270d11d9ec3. https://github.com/wearefuturegov/Outpost-Platform/blob/main/README.md
Updating current README.md with new version
RequestError [HttpError]: Not Found
    at /home/runner/work/Outpost-Platform/Outpost-Platform/node_modules/@octokit/request/dist-node/index.js:78:21
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async commitNewReadme (/home/runner/work/Outpost-Platform/Outpost-Platform/create-roadmap/index.js:366:28) {
  status: 404,
  response: {
    url: 'https://api.github.com/repos/wearefuturegov/Outpost-Platform/contents/README.md',
    status: 404,
    headers: {
      'access-control-allow-origin': '*',
      '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',
      connection: 'close',
      'content-encoding': 'gzip',
      'content-security-policy': "default-src 'none'",
      'content-type': 'application/json; charset=utf-8',
      date: 'Wed, 29 Mar 2023 [14](https://github.com/wearefuturegov/Outpost-Platform/actions/runs/4555089237/jobs/8033769995#step:5:15):22:05 GMT',
      'referrer-policy': 'origin-when-cross-origin, strict-origin-when-cross-origin',
      server: 'GitHub.com',
      'strict-transport-security': 'max-age=3[15](https://github.com/wearefuturegov/Outpost-Platform/actions/runs/4555089237/jobs/8033769995#step:5:16)36000; includeSubdomains; preload',
      'transfer-encoding': 'chunked',
      vary: 'Accept-Encoding, Accept, X-Requested-With',
      'x-content-type-options': 'nosniff',
      'x-frame-options': 'deny',
      'x-github-api-version-selected': '2022-11-28',
      'x-github-media-type': 'github.v3; format=json',
      'x-github-request-id': 'D808:589F:E17B:1D51C:6424498D',
      'x-ratelimit-limit': '60',
      'x-ratelimit-remaining': '52',
      'x-ratelimit-reset': '[16](https://github.com/wearefuturegov/Outpost-Platform/actions/runs/4555089237/jobs/8033769995#step:5:17)80102038',
      'x-ratelimit-resource': 'core',
      'x-ratelimit-used': '8',
      'x-xss-protection': '0'
    },
    data: {
      message: 'Not Found',
      documentation_url: 'https://docs.github.com/rest/reference/repos#create-or-update-file-contents'
    }
  },
  request: {
    method: 'PUT',
    url: 'https://api.github.com/repos/wearefuturegov/Outpost-Platform/contents/README.md',
    headers: {
      accept: 'application/vnd.github+json',
      'user-agent': 'octokit-request.js/6.2.3 Node.js/16.[19](https://github.com/wearefuturegov/Outpost-Platform/actions/runs/4555089237/jobs/8033769995#step:5:20).1 (linux; x64)',
      'x-github-api-version': '[20](https://github.com/wearefuturegov/Outpost-Platform/actions/runs/4555089237/jobs/8033769995#step:5:21)[22](https://github.com/wearefuturegov/Outpost-Platform/actions/runs/4555089237/jobs/8033769995#step:5:23)-11-28',
      authorization: '***',
      'content-type': 'application/json; charset=utf-8'
    },
    body: '{"owner":"wearefuturegov","repo":"Outpost-Platform","message":"Update ROADMAP","content":"VGVzdA==","committer":[{"name":"fg-dev bot","email":"fg-dev@tpximpact.com"}],"branch":"main","sha":"772bc03b371e6dc38f1c637cc3c37[27](https://github.com/wearefuturegov/Outpost-Platform/actions/runs/4555089237/jobs/8033769995#step:5:28)0d11d9ec3","type":"file","encoding":"base[64](https://github.com/wearefuturegov/Outpost-Platform/actions/runs/4555089237/jobs/8033769995#step:5:65)"}'
  }
}
SyedMuhammadFaheem commented 7 months ago

Hey @apricot13 Did you find the solution for this problem. I am stuck on the same thing.