diff --git a/.github/workflows/example.yml b/.github/workflows/example.yml index 61dec71..e7d642e 100644 --- a/.github/workflows/example.yml +++ b/.github/workflows/example.yml @@ -19,8 +19,13 @@ jobs: script: | const prBody = context.payload.pull_request.body || ""; let issue = ""; - if (prBody.includes("run the test workflow")) { - issue = "It looks like your PR description has the phrase \"run the test workflow\" in it"; + const match = prBody.match(/run the test workflow(?: (\w+))?/i); + if (match) { + if (match[1]) { + issue = `It looks like your PR description has the phrase "run the test workflow ${match[1]}" in it`; + } else { + issue = 'It looks like your PR description has the phrase "run the test workflow" in it'; + } } core.setOutput("issue", issue); - name: Add guidance comment diff --git a/README.md b/README.md index 02c52b8..4e6a7db 100644 --- a/README.md +++ b/README.md @@ -1,36 +1,48 @@ -# `guidance-comment` Github Action +# `guidance-comments` GitHub Action -This is a github action specifically for use with pull requests, where developer +This is a GitHub action specifically for use with pull requests, where developer guidance is provided in PR comments. +The main use case is putting an approachable face on automated code checks, and +providing a space for developer education, alignment, and context directly in +the PR without taking up reviewer time to do so. + +The action handles showing the guidance when needed, and resolving or removing +it once the input conditions are no longer met. + The three states are: - Initial: - - When `inputs.showGuidance` is false, and no previous comments are + - When `inputs.show-guidance` is `"false"`, and no previous comments are present for this guidance. - Guidance: - - When `inputs.showGuidance` is true. - - Adds or updates comment with `inputs.guidanceComment` + - When `inputs.show-guidance` is `"true"`. + - Adds or updates comment with `inputs.guidance-comment` - Resolved: - - When `inputs.showGuidance` is false, and a previous guidance comment - exists (showing that guidance was previously needed) - - Adds or updates comment with `inputs.resolvedComment` + - When `inputs.show-guidance` is `"false"`, and a previous guidance + comment exists (showing that guidance was previously needed) + - Adds or updates comment with `inputs.resolved-comment` + +Under the hood, comment generation and updates are handled by Peter Evans' +fantastic +[create-or-update-comment](https://github.com/marketplace/actions/create-or-update-comment) +and [find-comment](https://github.com/marketplace/actions/find-comment) actions. ### Action Configuration -| Input | Type | Required | Default | Description | -| ------------------ | --------------------- | -------- | -------------- | -------------------------------------------------------------------------------------------------------------- | -| `name` | String | ✓ | N/A | The unique identifier for the message.
Example `-${{ github.event.pull_request.number }}` | -| `pr-number` | String | ✓ | N/A | The PR number.
Generally `${{ github.event.pull_request.number }}` | -| `show-guidance` | `"true"` or `"false"` | ✓ | N/A | If the guidance should be shown. | -| `guidance-comment` | String | | `''` | Comment to show in the Guidance state. _If empty or unset will delete existing comment._ | -| `resolved-comment` | String | | `''` | Comment to show in the Resolved state. _If empty or unset will delete existing comment._ | -| `token` | String | | `GITHUB_TOKEN` | GitHub token for API access. | +| Input | Type | Required | Default | Description | +| ------------------ | --------------------- | -------- | -------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `name` | String | ✓ | N/A | The unique identifier for the message.
Example `-${{ github.event.pull_request.number }}`
🚨 **Do not use unvalidated user input as this ends up in the resulting comments.** | +| `pr-number` | String | ✓ | N/A | The PR number.
Generally `${{ github.event.pull_request.number }}` | +| `show-guidance` | `"true"` or `"false"` | ✓ | N/A | If the guidance should be shown. | +| `guidance-comment` | String | | `''` | Comment to show in the Guidance state.
🚨 **Do not use raw user input as this ends up in the resulting comments and could pose a security risk through malicious links etc.** | +| `resolved-comment` | String | | `''` | Comment to show in the Resolved state.
If empty or unset will delete existing comment.
🚨 **Do not use raw user input as this ends up in the resulting comments and could pose a security risk through malicious links etc.** | +| `token` | String | | `GITHUB_TOKEN` | GitHub token for API access. | ### Permissions The composite action requires the following permissions be set on the -GITHUB_TOKEN by the workflow using it: +`GITHUB_TOKEN` by the workflow using it: ```yml permissions: @@ -56,10 +68,10 @@ jobs: # Simulate checking for issues in the codebase echo "issue=There was a problem" >> $GITHUB_OUTPUT - name: Add guidance comment - uses: betatorbust/guidance-comment@v1 + uses: betatorbust/guidance-comments@v1 with: name: 'example-guidance' - pr-number: ${{github.event.pull_request.number}} + pr-number: ${{ github.event.pull_request.number }} show-guidance: ${{ steps.check-for-issues.outputs.issue != '' }} guidance-comment: | diff --git a/action.yml b/action.yml index 36d80c4..74efcfd 100644 --- a/action.yml +++ b/action.yml @@ -46,10 +46,20 @@ runs: token: ${{ inputs.token }} issue-number: ${{ inputs.pr-number }} body-includes: ${{ steps.guidance-tag.outputs.tag }} + # Echo out the comment ID for debugging + - name: Debug Comment ID + shell: bash + run: | + # print out all the inputs and comment id if found + echo "show-guidance='${{ inputs.show-guidance }}'" + echo "guidance-comment='${{ inputs.guidance-comment }}'" + echo "resolved-comment='${{ inputs.resolved-comment }}'" + echo "comment-id='${{ steps.find-comment.outputs.comment-id }}'" # Handle when guidance should be shown - name: Send Initial Guidance - if: | - ${{ inputs.show-guidance == 'true' && inputs.guidance-comment != '' && steps.find-comment.outputs.comment-id == 0 }} + if: + ${{ inputs.show-guidance == 'true' && inputs.guidance-comment != + '' && steps.find-comment.outputs.comment-id == '' }} uses: peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043 with: token: ${{ inputs.token }} @@ -58,38 +68,43 @@ runs: ${{ steps.guidance-tag.outputs.tag }} ${{ inputs.guidance-comment }} - name: Update Guidance - if: | - ${{ inputs.show-guidance == 'true' && inputs.guidance-comment != '' && steps.find-comment.outputs.comment-id != 0 }} + if: + ${{ steps.find-comment.outputs.comment-id != '' && + inputs.show-guidance == 'true' && inputs.guidance-comment }} uses: peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043 with: token: ${{ inputs.token }} issue-number: ${{ inputs.pr-number }} comment-id: ${{ steps.find-comment.outputs.comment-id }} + edit-mode: replace body: | ${{ steps.guidance-tag.outputs.tag }} ${{ inputs.guidance-comment }} # Handle when guidance is resolved - name: Remove Guidance # Remove the comment if guidance is not needed and no resolved comment is set but a comment exists - if: | - ${{ steps.find-comment.outputs.comment-id != 0 && inputs.show-guidance == 'false' && inputs.resolved-comment == '' }} + if: + ${{ steps.find-comment.outputs.comment-id != '' && + inputs.show-guidance == 'false' && !inputs.resolved-comment }} uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea with: github-token: ${{ inputs.token }} script: | - github.issues.deleteComment({ + github.rest.issues.deleteComment({ owner: context.repo.owner, repo: context.repo.repo, - comment_id: ${{ steps.find-comment.outputs.comment-id }}, + comment_id: ${{ steps.find-comment.outputs.comment-id }} }) - name: Update Resolved Comment - if: | - ${{ inputs.show-guidance == 'false' && inputs.resolved-comment != '' && steps.find-comment.outputs.comment-id != 0 }} + if: + ${{ inputs.show-guidance == 'false' && inputs.resolved-comment && + steps.find-comment.outputs.comment-id != '' }} uses: peter-evans/create-or-update-comment@71345be0265236311c031f5c7866368bd1eff043 with: token: ${{ inputs.token }} issue-number: ${{ inputs.pr-number }} comment-id: ${{ steps.find-comment.outputs.comment-id }} + edit-mode: replace body: | ${{ steps.guidance-tag.outputs.tag }} ${{ inputs.resolved-comment }}