Search code examples
githubgithub-api

Using new github tokens to list and create comments on pull requests


After I switched to the new Github Token format, I am not able anymore to perform a call for this github API:

https://docs.github.com/en/rest/issues/comments#list-issue-comments

My script does the following:

[[ -z "$GITHUB_REPO_SLUG" ]] && export GITHUB_REPO_SLUG="$TRAVIS_REPO_SLUG"
GITHUB_API_BASE_URL="https://api.github.com/repos/${GITHUB_REPO_SLUG}"
GITHUB_AUTH_HEADER="Authorization: token $GITHUB_TOKEN"

comment_ids=$(curl -s -H "$GITHUB_AUTH_HEADER" "$GITHUB_API_BASE_URL/issues/$pull_request_id/comments" \
        | jq --arg USER "$GITHUB_USER" -r '. | map(select(.user.login==$USER)) | map(.id) | .[]')

for comment_id in $(echo $comment_ids); do
        jq -n -r --arg message "$message" '{ body: $message }' \
            | curl -s -H "$GITHUB_AUTH_HEADER" "$GITHUB_API_BASE_URL/issues/comments/$comment_id" -X PATCH --data @- > /dev/null
        echo "Edited comment with id $comment_id of PR $pull_request_id"
done

The api call that list comments ids responds:

{
  "message": "Not Found",
  "documentation_url": "https://docs.github.com/rest/reference/issues#list-issue-comments"
}

This still works using tokens in the old format, but doesn't with tokens in the new format.

Is there any way to use the GHP to call that api?


Solution

  • The github user associated with the github token must be provided writing access to the repository.

    Since this was missing API responded 404 (default behaviour set for Github APIs).