octokit / core.js

Extendable client for GitHub's REST & GraphQL APIs
MIT License
1.19k stars 310 forks source link

[BUG]: Rest call to delete comment returns 404 #634

Closed pantelis-karamolegkos closed 12 months ago

pantelis-karamolegkos commented 12 months ago

What happened?

I am trying to conditionally delete comments through the following workflow

on:
  pull_request:
    types: [ labeled ]

env:
  GH_TOKEN: ${{ github.token }}

jobs:
  clean-atlantis-comments:
    if: contains( github.event.pull_request.labels.*.name, 'atlantis-clean')
    name: clean atlantis and user comments invoking plan
    runs-on: ubuntu-latest

    steps:

      - name: checkout the project
        uses: actions/checkout@v4

      - name: list pull request comments
        shell: bash
        run: |
          gh pr view ${{ github.event.number }} --json comments > comments.json

      - name: delete comments
        uses: actions/github-script@v6
        with:
          result-encoding: string
          script: |
            const fs = require('fs');
            const jsonData = JSON.parse(fs.readFileSync('comments.json', 'utf-8'));
            const comments = jsonData.comments;
            for (const cmnt of comments) {
              comment_id = cmnt.id
              if (cmnt.author.login == 'pantelis-karamolegkos' || cmnt.body == 'foo') {
                github.rest.issues.deleteComment({
                  owner: context.repo.owner,
                  repo: context.repo.repo,
                  comment_id: comment_id,
                })
              }
            }

However this fails with

Run actions/github-script@v6
RequestError [HttpError]: Not Found
    at /home/runner/work/_actions/actions/github-script/v6/dist/index.js:6842:21
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  status: 404,
  response: {
    url: 'https://api.github.com/repos/MyOrg/MyRepo/issues/comments/IC_kwDODse5xs5r3EqV',
    status: 404,

Versions

Using actions/github-script@v6 which in turn uses @octokit/core version: 5.0.1

Relevant log output

No response

Code of Conduct

github-actions[bot] commented 12 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 12 months ago

By default, the github instance is authenticated with the GITHUB_TOKEN provided by GitHub Actions. This token has only access to the current repository. I assume that you are trying to delete a comment in another repository?

I would recommend to register a GitHub App with the needed permissions, install the app on the repositories you need it to have access to, and create an installation access token using https://github.com/actions/create-github-app-token, then pass that token to actions/github-script

For further questions, can you file an issue in https://github.com/actions/github-script?

pantelis-karamolegkos commented 12 months ago

Just for the record any action is taken on the repository the workflow on, is running as the corresponding variables are taken from the current context

owner: context.repo.owner,
repo: context.repo.repo,