'How to create an inline review comment in github in the diff view using github actions

I have been trying to use github actions to place a comment inline in the pull request diff. I have tried to follow this documentation: review-comment-in-diff however nothing seemed to work as expected. The documentation seems to state that you can just use the property position, but other docs say you need commit_id and path for it to work create-a-comment. If I do not add commit_id or path, I get an error message that they are missing, if I add it I get a validation error. I will show both below. Note that the first error message also mentions missing in_reply_to and line which according to the documentation is not needed for creating a new comment using position.

- name: publish line comment curl
        if: contains(env.SCRIPT_OUTPUT, 'FAIL')
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          curl \
            -X POST \
            -H "Accept: application/vnd.github.v3+json" \
            https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/comments \
            -H "Authorization: token $GITHUB_TOKEN" \
            --data '{  "body": "blah blah" ,
                    "path": "SomeDir/SomeFile.json",
                    "position": 1
                    }'

{ "message": "Invalid request.\n\nNo subschema in "oneOf" matched.\n"commit_id" wasn't supplied.\n"in_reply_to" wasn't supplied.\n"position" is not a permitted key.\n"commit_id", "line" weren't supplied.",

--data '{  "body": "blah blah" ,
                    "path": "SomeDir/SomeFile.json",
                    "position": 1,
                    "commit_id": "${{ github.sha }}"
                    }'

"message": "Validation Failed", "errors": [ { "resource": "PullRequestReviewComment", "code": "custom", "field": "pull_request_review_thread.end_commit_oid", "message": "pull_request_review_thread.end_commit_oid is not part of the pull request"



Solution 1:[1]

You need to use github.event.pull_request.head.sha instead of github.sha in order to get the latest commit. The error messages and documentation were quite cryptic so maybe this will help someone else. Also, it is required that you provide path and commit_id in addition to position. If you do all that you end up with a comment in the diff view (Files Changed Tab) and also a notification with the comment is posted in the main comment section of the pull request with a link to view it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Zak Keirn