Is it possible to use GHA to automatically post a comment on any PR including changes to a certain filepath? #69944
-
Select Topic AreaQuestion BodyI've been searching to try and find an example relevant to my needs. I've found lots of similar things but I'm not sure how I can limit the scope of an auto comment to a specific subset? In my case, I'm looking to post a comment containing a PR checklist to any PR that hits our automated testing folder and any files under it. I found hashFiles(path) which seems like it's for a similar purpose. Also, as mentioned, I'm posting a checklist, I'm fairly worried the formatting will be completely ruined/be very difficult using GHA. Is there any way to test such things since it's an auto comment post or do you just need to merge the change and tweak from there? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
You can use GitHub Actions to automatically post a comment on any pull request (PR) that includes changes to a specific file path or directory. To achieve this, you can use the GitHub API to create a comment on the PR. Here's a high-level approach to implementing this:
Define Conditions: In the workflow file, define conditions that check if the PR includes changes to the specific file path or directory (e.g., the automated testing folder). You can use the GitHub.event.pull_request.changed_files context to access the list of changed files in the PR.
Here's an example of a GitHub Actions workflow YAML file that posts a comment to a PR if changes are made to files in a specific directory: name: Auto Comment on PR
on:
pull_request:
paths:
- 'path/to/automated_testing_folder/*'
jobs:
auto-comment:
runs-on: ubuntu-latest
steps:
- name: Check PR Changes
id: pr-changes
run: echo "::set-output name=changes::${{ toJson(github.event.pull_request.changed_files) }}"
- name: Create Comment
if: steps.pr-changes.outputs.changes != '[]'
run: |
# Use GitHub API to create a comment on the PR
PR_NUMBER=${{ github.event.pull_request.number }}
COMMENT="Your checklist comment here"
GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}
COMMENT_URL="https://api.github.com/repos/${{ github.repository }}/issues/${PR_NUMBER}/comments"
curl -s -H "Authorization: token ${GITHUB_TOKEN}" -X POST $COMMENT_URL -d "{\"body\":\"$COMMENT\"}" In this example, the workflow checks if any files in the Ensure you replace Remember that you can customize the comment's formatting and content within the workflow file as needed. To test the formatting, you can push a PR with changes to the specified directory, and the workflow will automatically create the comment on the PR. You can then review the comment and make any necessary adjustments to the formatting as required. |
Beta Was this translation helpful? Give feedback.
-
The answer to your question is yes. It is possible to use GitHub Actions to automatically post a comment on any PR including changes to a certain filepath. To do this, you can use the following steps: Create a new GitHub Actions workflow file in your repository. This workflow will run when a pull request is opened or modified. YAML on: jobs:
` In this example, the workflow checks if any files in the path/to/automated_testing_folder/ directory have changed in the PR. If changes are detected, it uses the GitHub API to post a comment on the PR with the specified checklist comment. Ensure you replace path/to/automated_testing_folder/* and Your checklist comment here with your specific file path and comment content. Remember that you can customize the comment's formatting and content within the workflow file as needed. To test the formatting, you can push a PR with changes to the specified directory, and the workflow will automatically create the comment on the PR. You can then review the comment and make any necessary adjustments to the formatting as required. In the case of the example you provided, it seems that the workflow is not triggering because the changed_files step is not returning any results. This could be because the features/* path is not being changed in the PR. To troubleshoot this, you can try the following steps: Add a debug step to the workflow file before the Create Comment step. This will print the contents of the changed_files output to the console. |
Beta Was this translation helpful? Give feedback.
-
Here's the solution I went with, sorry cannot remember if I posted it elsewhere at the time
|
Beta Was this translation helpful? Give feedback.
The answer to your question is yes. It is possible to use GitHub Actions to automatically post a comment on any PR including changes to a certain filepath.
To do this, you can use the following steps:
Create a new GitHub Actions workflow file in your repository. This workflow will run when a pull request is opened or modified.
In the workflow file, define the events that trigger the workflow. In your case, you want the workflow to trigger pull_request events.
Define conditions that check if the PR includes changes to the specific file path or directory. You can use the GitHub.event.pull_request.changed_files context to access the list of changed files in the PR.
Use the GitHub API to crea…