Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
3 tasks done
apricot13 opened this issue Mar 29, 2023 · 1 comment
Open
3 tasks done

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

apricot13 opened this issue Mar 29, 2023 · 1 comment
Labels
hacktoberfest Issues for participation in Hacktoberfest Status: Up for grabs Issues that are ready to be worked on by anyone Type: Bug Something isn't working as documented Type: Support Any questions, information, or general needs around the SDK or GitHub APIs
Projects

Comments

@apricot13
Copy link

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.

  • I'm including the sha of the existing file
  • I've manually set my permissions so I am sure I have correct access to Contents with the GITHUB_TOKEN contents: write
  • I've set all sorts of combinations of Accept: "application/vnd.github+json",

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)"}'
  }
}
@ghost ghost added this to Inbox in JS Mar 29, 2023
@kfcampbell kfcampbell added Status: Up for grabs Issues that are ready to be worked on by anyone Type: Bug Something isn't working as documented Type: Support Any questions, information, or general needs around the SDK or GitHub APIs Priority: Normal labels Apr 10, 2023
@ghost ghost moved this from Inbox to Bugs in JS Apr 10, 2023
@nickfloyd nickfloyd added the hacktoberfest Issues for participation in Hacktoberfest label Sep 20, 2023
@SyedMuhammadFaheem
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hacktoberfest Issues for participation in Hacktoberfest Status: Up for grabs Issues that are ready to be worked on by anyone Type: Bug Something isn't working as documented Type: Support Any questions, information, or general needs around the SDK or GitHub APIs
Projects
Status: 🔥 Backlog
JS
  
Bugs
Development

No branches or pull requests

4 participants