|
| 1 | +name: pr-slack-notification |
| 2 | + |
| 3 | +# Based on: https://github.com/slackapi/slack-github-action/issues/269 |
| 4 | + |
| 5 | +on: |
| 6 | + workflow_dispatch: |
| 7 | + pull_request: |
| 8 | + types: |
| 9 | + - opened |
| 10 | + - ready_for_review |
| 11 | + - review_requested |
| 12 | + - closed |
| 13 | + issue_comment: |
| 14 | + types: |
| 15 | + - created |
| 16 | + pull_request_review: |
| 17 | + types: |
| 18 | + - submitted |
| 19 | + |
| 20 | +permissions: read-all |
| 21 | + |
| 22 | +defaults: |
| 23 | + run: |
| 24 | + shell: bash -euxo pipefail {0} |
| 25 | + |
| 26 | +jobs: |
| 27 | + github-context: |
| 28 | + runs-on: ubuntu-latest |
| 29 | + steps: |
| 30 | + - name: Debug |
| 31 | + env: |
| 32 | + GITHUB_CONTEXT: ${{ toJson(github) }} |
| 33 | + run: | |
| 34 | + echo "${GITHUB_CONTEXT}" |
| 35 | +
|
| 36 | + pr-slack-notification: |
| 37 | + runs-on: ubuntu-latest |
| 38 | + name: Sends a message to Slack when a PR is opened |
| 39 | + if: (github.event.action == 'opened' && github.event.pull_request.draft == false) || github.event.action == 'ready_for_review' |
| 40 | + steps: |
| 41 | + - name: Post PR summary message to slack |
| 42 | + id: message |
| 43 | + uses: slackapi/slack-github-action@485a9d42d3a73031f12ec201c457e2162c45d02d # v2.0.0 |
| 44 | + with: |
| 45 | + method: chat.postMessage |
| 46 | + token: ${{ secrets.MY_SLACK_BOT_TOKEN }} |
| 47 | + payload: | |
| 48 | + channel: ${{ secrets.MY_SLACK_CHANNEL_ID }} |
| 49 | + text: "💡 *${{ github.event.pull_request.user.login }}*: <${{ github.event.repository.html_url }}|${{ github.repository }}> - <${{ github.event.pull_request.html_url }}|#${{ github.event.pull_request.number }} - ${{ github.event.pull_request.title }}> (+${{ github.event.pull_request.additions }}, -${{ github.event.pull_request.deletions }})" |
| 50 | +
|
| 51 | + - name: Create file with slack message timestamp |
| 52 | + env: |
| 53 | + TS: ${{ steps.message.outputs.ts }} |
| 54 | + run: | |
| 55 | + echo "${TS}" > slack-message-timestamp.txt |
| 56 | +
|
| 57 | + - name: Cache slack message timestamp |
| 58 | + uses: actions/cache/save@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3 |
| 59 | + with: |
| 60 | + path: slack-message-timestamp.txt |
| 61 | + key: slack-message-timestamp-${{ github.event.pull_request.html_url }}-${{ steps.message.outputs.ts }} |
| 62 | + |
| 63 | + slack-emoji-react: |
| 64 | + runs-on: ubuntu-latest |
| 65 | + name: Adds emoji reaction to slack message when a PR is closed or reviewed |
| 66 | + if: ${{ startsWith(github.event.pull_request.html_url, 'https') || startsWith(github.event.issue.pull_request.html_url, 'https') }} |
| 67 | + steps: |
| 68 | + # gh commands needs to be executed in the repository |
| 69 | + - name: Checkout Code |
| 70 | + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 |
| 71 | + |
| 72 | + # https://stackoverflow.com/questions/74640750/github-actions-not-finding-cache |
| 73 | + # I can not use the cache action in this job because the cache is not shared between runs |
| 74 | + - name: Save slack timestamp as an environment variable |
| 75 | + id: slack-timestamp |
| 76 | + env: |
| 77 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 78 | + GITHUB_PULL_REQUEST_HTML_URL: ${{ github.event.pull_request.html_url || github.event.issue.pull_request.html_url }} |
| 79 | + run: | |
| 80 | + SLACK_TIMESTAMP=$(gh cache list --json key --jq "[.[].key|capture(\"${GITHUB_PULL_REQUEST_HTML_URL}-(?<x>.+)\").x][0]") |
| 81 | + echo "SLACK_TIMESTAMP=${SLACK_TIMESTAMP}" | tee -a "${GITHUB_ENV}" |
| 82 | + if [[ "${SLACK_TIMESTAMP}" != '' ]]; then |
| 83 | + echo "github_event_pull_request_html_url=true" >> "${GITHUB_OUTPUT}" |
| 84 | + fi |
| 85 | +
|
| 86 | + - name: Decide which emoji to add |
| 87 | + if: ${{ steps.slack-timestamp.outputs.github_event_pull_request_html_url == 'true' }} |
| 88 | + env: |
| 89 | + GITHUB_EVENT_ACTION: ${{ github.event.action }} |
| 90 | + GITHUB_EVENT_NAME: ${{ github.event_name }} |
| 91 | + GITHUB_EVENT_REVIEW_STATE: ${{ github.event.review.state }} |
| 92 | + run: | |
| 93 | + case "${GITHUB_EVENT_ACTION}" in |
| 94 | + created) |
| 95 | + if [[ "${GITHUB_EVENT_NAME}" == 'issue_comment' ]]; then |
| 96 | + echo "EMOJI=speech_balloon" >> "${GITHUB_ENV}" # 💬 |
| 97 | + fi |
| 98 | + ;; |
| 99 | + submitted) |
| 100 | + case "${GITHUB_EVENT_REVIEW_STATE}" in |
| 101 | + changes_requested) |
| 102 | + echo "EMOJI=repeat" >> "${GITHUB_ENV}" # 🔁 |
| 103 | + ;; |
| 104 | + approved) |
| 105 | + echo "EMOJI=ok" >> "${GITHUB_ENV}" # 🆗 |
| 106 | + ;; |
| 107 | + commented) |
| 108 | + echo "EMOJI=speech_balloon" >> "${GITHUB_ENV}" # 💬 |
| 109 | + ;; |
| 110 | + esac |
| 111 | + ;; |
| 112 | + review_requested) |
| 113 | + echo "EMOJI=eyes" >> "${GITHUB_ENV}" # 👀 |
| 114 | + ;; |
| 115 | + *) |
| 116 | + echo "EMOJI=false" >> "${GITHUB_ENV}" |
| 117 | + ;; |
| 118 | + esac |
| 119 | +
|
| 120 | + - name: React to PR summary message in slack with emoji |
| 121 | + if: ${{ steps.slack-timestamp.outputs.github_event_pull_request_html_url == 'true' && env.EMOJI != 'false' }} |
| 122 | + uses: slackapi/slack-github-action@485a9d42d3a73031f12ec201c457e2162c45d02d # v2.0.0 |
| 123 | + with: |
| 124 | + method: reactions.add |
| 125 | + token: ${{ secrets.MY_SLACK_BOT_TOKEN }} |
| 126 | + payload: | |
| 127 | + channel: ${{ secrets.MY_SLACK_CHANNEL_ID }} |
| 128 | + timestamp: "${{ env.SLACK_TIMESTAMP }}" |
| 129 | + name: ${{ env.EMOJI }} |
| 130 | +
|
| 131 | + - name: Update the original message with status Merged ✅ |
| 132 | + if: ${{ github.event.pull_request.merged && steps.slack-timestamp.outputs.github_event_pull_request_html_url == 'true' }} |
| 133 | + uses: slackapi/slack-github-action@485a9d42d3a73031f12ec201c457e2162c45d02d # v2.0.0 |
| 134 | + with: |
| 135 | + method: chat.update |
| 136 | + token: ${{ secrets.MY_SLACK_BOT_TOKEN }} |
| 137 | + payload: | |
| 138 | + channel: ${{ secrets.MY_SLACK_CHANNEL_ID }} |
| 139 | + ts: "${{ env.SLACK_TIMESTAMP }}" |
| 140 | + text: "✅ *${{ github.event.pull_request.user.login }}*: <${{ github.event.repository.html_url }}|${{ github.repository }}> - <${{ github.event.pull_request.html_url }}|#${{ github.event.pull_request.number }} - ${{ github.event.pull_request.title }}> (+${{ github.event.pull_request.additions }}, -${{ github.event.pull_request.deletions }})" |
| 141 | + attachments: |
| 142 | + - color: "28a745" |
| 143 | + fields: |
| 144 | + - title: "Status" |
| 145 | + short: true |
| 146 | + value: "Merged ✅" |
| 147 | +
|
| 148 | + - name: Update the original message with status Closed ❎ |
| 149 | + if: ${{ github.event.action == 'closed' && github.event.pull_request.merged == false && steps.slack-timestamp.outputs.github_event_pull_request_html_url == 'true' }} |
| 150 | + uses: slackapi/slack-github-action@485a9d42d3a73031f12ec201c457e2162c45d02d # v2.0.0 |
| 151 | + with: |
| 152 | + method: chat.update |
| 153 | + token: ${{ secrets.MY_SLACK_BOT_TOKEN }} |
| 154 | + payload: | |
| 155 | + channel: ${{ secrets.MY_SLACK_CHANNEL_ID }} |
| 156 | + ts: "${{ env.SLACK_TIMESTAMP }}" |
| 157 | + text: "❎ *${{ github.event.pull_request.user.login }}*: <${{ github.event.repository.html_url }}|${{ github.repository }}> - <${{ github.event.pull_request.html_url }}|#${{ github.event.pull_request.number }} - ${{ github.event.pull_request.title }}> (+${{ github.event.pull_request.additions }}, -${{ github.event.pull_request.deletions }})" |
| 158 | + attachments: |
| 159 | + - color: "fa7015" |
| 160 | + fields: |
| 161 | + - title: "Status" |
| 162 | + short: true |
| 163 | + value: "Closed ❎" |
0 commit comments