|
| 1 | +name: pr-comment |
| 2 | +# description: | |
| 3 | +# This workflow is a trusted workflow that creates or updates comments in PRs. |
| 4 | +# |
| 5 | +# It may be called by other workflows executing on pull_requests against the go-openapi repos. |
| 6 | +# |
| 7 | +# The principle is to communicate the content of a comment between workflows using an uploaded artifact. |
| 8 | + |
| 9 | +on: |
| 10 | + workflow_call: |
| 11 | + inputs: |
| 12 | + run_id: |
| 13 | + description: | |
| 14 | + The run ID of the calling workflow that has emitted the message artifact, e.g. ${{ github.event.workflow_run.id }} |
| 15 | + required: true |
| 16 | + target_repo: |
| 17 | + description: | |
| 18 | + The target repository of the PR, e.g. ${{ github.repository }} |
| 19 | + required: true |
| 20 | + pr_number: |
| 21 | + description: | |
| 22 | + The pull request number, e.g. ${{ github.event.pull_request.number }} |
| 23 | + required: true |
| 24 | + pr_sha: |
| 25 | + description: | |
| 26 | + The commit sha for the originating pull request, e.g. ${{github.event.pull_request.head.sha}} |
| 27 | + required: true |
| 28 | + artifact_name: |
| 29 | + description: | |
| 30 | + The reference to the artifact containing the text of the comment. |
| 31 | +
|
| 32 | + At this moment, only supports "markdown_comment.txt" and "spelling_comment.txt" |
| 33 | + required: true |
| 34 | + comment_title: |
| 35 | + description: | |
| 36 | + Title is a text string used to uniquely identify a comment that will be upaded on subsequent commits. |
| 37 | + required: true |
| 38 | + reactions: |
| 39 | + description: | |
| 40 | + Optional emoji reaction added to the comment. |
| 41 | + required: false |
| 42 | + |
| 43 | +permissions: |
| 44 | + pull-requests: read # <- job will exchange token as-needed when it comes to writing. |
| 45 | + contents: read |
| 46 | + |
| 47 | +env: |
| 48 | + GITHUB_API: "https://api.github.com" |
| 49 | + MAX_MESSAGE_SIZE: 4096 |
| 50 | + |
| 51 | +jobs: |
| 52 | + pr-comment: |
| 53 | + runs-on: ubuntu-latest |
| 54 | + env: |
| 55 | + MESSAGE_FILE: "artifacts/${{ inputs.artifact_name}}" |
| 56 | + TARGET: "${{ inputs.target_repo }}" |
| 57 | + steps: |
| 58 | + - name: Validate inputs |
| 59 | + run: | |
| 60 | + if [[ "${{ env.TARGET }}" !~ /github.com\/go-openapi/ ]] ; then |
| 61 | + echo "This workflow only applies to target repos in github.com/go-openapi." |
| 62 | + exit 1 |
| 63 | + fi |
| 64 | + if [[ "${{ inputs.artifact_name }}" != "markdown_comment.txt" && "${{ inputs.artifact_name }}" != "spelling_comment.txt" && ]] ; then |
| 65 | + echo "This workflow only applies to artifacts named markdown_comment.txt or spelling_comment.txt" |
| 66 | + exit 1 |
| 67 | + fi |
| 68 | +
|
| 69 | + - name: Check originating PR |
| 70 | + # description: | |
| 71 | + # This check verifies that the originating PR has not been already modified |
| 72 | + # by the time this workflow executes. If this is the case, the job is skipped |
| 73 | + # and no comment is issued. |
| 74 | + id: check_pr |
| 75 | + env: |
| 76 | + PRN: "${{ inputs.pr_number }}" |
| 77 | + run: | |
| 78 | + LAST_COMMIT=$(\ |
| 79 | + curl -s \ |
| 80 | + -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ |
| 81 | + "${GITHUB_API}/repos/${TARGET}/pulls/${PRN}/commits" | \ |
| 82 | + jq -r '.[-1].sha'\ |
| 83 | + ) |
| 84 | +
|
| 85 | + if [[ "${LAST_COMMIT}" != "${{ inputs.pr_sha }}" ]] ; then |
| 86 | + echo "The PR has changed while we were about to commit it. Skip." |
| 87 | + echo "proceed=false" >> "${{GITHUB_OUTPUT}}" |
| 88 | + |
| 89 | + echo "::warning:: pull request comment skipped because ${{ github.event.pull_request.number }} has changed" |
| 90 | + exit 0 |
| 91 | + fi |
| 92 | + echo "proceed=true" >> "${{GITHUB_ENV}}" |
| 93 | +
|
| 94 | + - name: Download message artifact |
| 95 | + if: ${{ steps.check_pr.outputs.proceed == "true"}} |
| 96 | + uses: actions/download-artifact@v5 |
| 97 | + with: |
| 98 | + run_id: "${{ inputs.run_id }}" |
| 99 | + repository: "${{ env.TARGET }}" |
| 100 | + name: "${{ inputs.artifact_name }}" |
| 101 | + path: artifacts/ |
| 102 | + github_token: ${{secrets.GITHUB_TOKEN}} |
| 103 | + |
| 104 | + - name: Check message artifact size |
| 105 | + if: ${{ steps.check_pr.outputs.proceed == "true"}} |
| 106 | + id: load_artifact |
| 107 | + run: | |
| 108 | + SIZE=$(wc -c "${MESSAGE_FILE}" |
| 109 | + if [[ "${SIZE}" -gt "${{ env.MAX_MESSAGE_SIZE }}" ]] ; then |
| 110 | + # truncate the message up to MAX_MESSAGE_SIZE |
| 111 | + head -c ${{ env.MAX_MESSAGE_SIZE }} "${MESSAGE_FILE}" > /tmp/truncated |
| 112 | + mv /tmp/truncated "${MESSAGE_FILE}" |
| 113 | + echo "::warning:: comment message with size ${SIZE} has been truncated to ${{ env.MAX_MESSAGE_SIZE }} bytes." |
| 114 | + fi |
| 115 | + echo "message=$(cat ${MESSAGE_FILE})" >> "${GITHUB_OUTPUT}" |
| 116 | +
|
| 117 | + - name: Find previous PR comment |
| 118 | + if: ${{ steps.check_pr.outputs.proceed == "true"}} |
| 119 | + uses: peter-evans/find-comment@v3 |
| 120 | + id: find_comment |
| 121 | + with: |
| 122 | + repository: ${{ inputs.target_repo }} |
| 123 | + issue-number: ${{ inputs.pr_number }} |
| 124 | + body-includes: ${{ inputs.comment_title }} |
| 125 | + direction: last |
| 126 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 127 | + |
| 128 | + - name: Acquire write access to PR |
| 129 | + if: ${{ steps.check_pr.outputs.proceed == "true"}} |
| 130 | + id: acquire_write_token |
| 131 | + uses: actions/create-github-app-token@v2 |
| 132 | + with: |
| 133 | + app-id: ${{ secrets.CI_WORKFLOWS_PR_APP_ID }} |
| 134 | + private-key: ${{ secrets.CI_WORKFLOWS_PR_APP_PRIVATE_KEY }} |
| 135 | + |
| 136 | + - name: Create or update PR comment |
| 137 | + if: ${{ steps.check_pr.outputs.proceed == "true"}} |
| 138 | + uses: peter-evans/create-or-update-comment@v4 |
| 139 | + with: |
| 140 | + issue-number: ${{ inputs.pr_number }} |
| 141 | + comment-id: ${{ steps.find_comment.outputs.comment-id }} |
| 142 | + reactions: ${{ inputs.reactions }} |
| 143 | + reactions-edit-mode: replace |
| 144 | + body-path: ${{ env.MESSAGE_FILE }} |
| 145 | + edit-mode: replace |
| 146 | + token: ${{ steps.acquire_write_token.outputs.token }} |
| 147 | + |
| 148 | + - name: Notify |
| 149 | + run: | |
| 150 | + echo "::notice::Commented pull request ${{ inputs.pr_number }}" |
| 151 | + echo "::debug::${{ steps.load_artifacts.outputs.message }}" |
0 commit comments