Eyal Gerber
Eyal Gerber

Reputation: 1657

How to update the first comment in a pull request on GitHub via the command line?

I am trying to read and then update the first comment on my pull request page on GitHub via the command line but it seems that the API that GitHub is providing doesn't read the first comment but only subsequent comments. I am trying to implement a functionality similar to this GitHub Action (I just don't want to do it via GitHub actions).

screenshot of pull request page on github showing first comment that I would like to change

I tried using the API mentioned here to get the list of comments: https://docs.github.com/en/rest/issues/comments#list-issue-comments

But it lists all the comments except for the first one. I wanted to list all the comments so that I could grab the ID of the first comment and then I would update it. Any idea on how to do this?

Upvotes: 2

Views: 1493

Answers (2)

Eyal Gerber
Eyal Gerber

Reputation: 1657

So as it turns out, I tried to update the first comment using the GitHub API for pull requests comments or issues comments. But that was wrong because the first comment on the GitHub pull request page is actually not a comment! I realized it is the 'body' of the pull request and can super easily be updated using this update API and to read the 'body' you can use this get API:

For curl (GET):
This will get you all the data of the Pull Request. If you want just the body text then you would need to write additional commands to extract it. Check out the script in the next section under GitHub CLI (GET) which gives you the complete turn-key solution for getting the body text.

curl \
  -H "Accept: application/vnd.github+json" \ 
  -H "Authorization: token <TOKEN>" \
  https://api.github.com/repos/OWNER/REPO/pulls/PULL_NUMBER

For curl (UPDATE):

curl \
-X PATCH \
-H "Accept: application/vnd.github+json" \ 
-H "Authorization: token <TOKEN>" \
https://api.github.com/repos/OWNER/REPO/pulls/PULL_NUMBER \
-d '{"body":"updated body"}'

for GitHub CLI (GET):
This will get you all the information from the pull request and you will need to extract the body text (see the next code snippet for the script that gets you the body text).

# GitHub CLI api
# https://cli.github.com/manual/gh_api

gh api \
  -H "Accept: application/vnd.github+json" \
  /repos/OWNER/REPO/pulls/PULL_NUMBER

here is the code for extracting exactly the body text:

pr_data=$(gh api   -H "Accept: application/vnd.github+json"   /repos/OWNER/REPO/pulls/PULL_REQUEST_NUMBER)
body_row=$(grep "body" <<< "$pr_data")
body_string=`echo "$body_row" | awk -F 'body":' '{printf $NF}'`
body_string2=`echo "$body_string" | cut -d'"' -f 2`
echo $body_string2

for GitHub CLI (UPDATE):

# GitHub CLI api
# https://cli.github.com/manual/gh_api

gh api \
  --method PATCH \
  -H "Accept: application/vnd.github+json" \
  /repos/OWNER/REPO/pulls/PULL_NUMBER \
 -f body='updated body'

Upvotes: 2

VonC
VonC

Reputation: 1324703

The GitHub action you are refering to uses a GraphQL call to update comments. Same for reading comments.

That means you can call (curl) those graphQL, as in here for update, from command-line.

curl -H "Authorization: bearer <REPLACE WITH YOUR GITHUB PERSONAL ACCESS TOKEN>" -X POST -d \
"{ \
    \"query\": \"mutation { \
        updateIssueComment(input: { \
            id: \\\"REPLACE WITH COMMENT NODE ID\\\", \
            body: \\\"This is fantastic\\\" \
        }) { \
            issueComment { \
                lastEditedAt \
            } \
        } \
    }\"
} \
" https://api.github.com/graphql

To read comments though, you will need to take into account pagination.

Upvotes: 2

Related Questions