From 783cbbaa9ac30e62aa6af4e141527f4afe6cd91e Mon Sep 17 00:00:00 2001 From: Zachary Hu <6426329+outsinre@users.noreply.github.com> Date: Wed, 29 Nov 2023 15:54:02 +0800 Subject: [PATCH] chore(ci): fix workflow webhook notification and use "Kong/github-slack-mapping" file based mapping instead of variables for easier update (#12021) FTI-5564 --- .github/workflows/backport-fail-bot.yml | 51 ++++++++++++++++ .github/workflows/master-fail-bot | 39 ------------ .../workflows/release-and-tests-fail-bot.yml | 59 +++++++++++++++++++ .github/workflows/release-fail-bot.yml | 38 ------------ 4 files changed, 110 insertions(+), 77 deletions(-) create mode 100644 .github/workflows/backport-fail-bot.yml delete mode 100644 .github/workflows/master-fail-bot create mode 100644 .github/workflows/release-and-tests-fail-bot.yml delete mode 100644 .github/workflows/release-fail-bot.yml diff --git a/.github/workflows/backport-fail-bot.yml b/.github/workflows/backport-fail-bot.yml new file mode 100644 index 000000000000..90004154abae --- /dev/null +++ b/.github/workflows/backport-fail-bot.yml @@ -0,0 +1,51 @@ +name: Forward failed backport alert to Slack + +on: + issue_comment: + types: [created] + +jobs: + check_comment: + runs-on: ubuntu-latest + if: github.event.issue.pull_request != null && contains(github.event.comment.body, 'To backport manually, run these commands in your terminal') + + steps: + - name: Fetch mapping file + id: fetch_mapping + uses: actions/github-script@v6 + env: + ACCESS_TOKEN: ${{ secrets.PAT }} + with: + script: | + const url = 'https://raw.githubusercontent.com/Kong/github-slack-mapping/main/mapping.json'; + const headers = {Authorization: `token ${process.env.ACCESS_TOKEN}`}; + const response = await fetch(url, {headers}); + const mapping = await response.json(); + return mapping; + + - name: Generate Slack Payload + id: generate-payload + uses: actions/github-script@v6 + env: + SLACK_CHANNEL: gateway-notifications + SLACK_MAPPING: "${{ steps.fetch_mapping.outputs.result }}" + with: + script: | + const pr_url = ${{ github.event.issue.pull_request.html_url }}; + const slack_mapping = JSON.parse(process.env.SLACK_MAPPING); + const pr_author_github_id = ${{ github.event.issue.user.login }}; + const pr_author_slack_id = slack_mapping[pr_author_github_id]; + const author = pr_author_slack_id ? `<@${pr_author_slack_id}>` : pr_author_github_id; + const payload = { + text: `${pr_url} from ${author} failed to backport.`, + channel: process.env.SLACK_CHANNEL, + }; + return JSON.stringify(payload); + result-encoding: string + + - name: Send Slack Message + uses: slackapi/slack-github-action@e28cf165c92ffef168d23c5c9000cffc8a25e117 # v1.24.0 + with: + payload: ${{ steps.generate-payload.outputs.result }} + env: + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_GATEWAY_NOTIFICATIONS_WEBHOOK }} diff --git a/.github/workflows/master-fail-bot b/.github/workflows/master-fail-bot deleted file mode 100644 index 502b468f19b7..000000000000 --- a/.github/workflows/master-fail-bot +++ /dev/null @@ -1,39 +0,0 @@ -name: Notify Slack on Master CI failure - -on: - workflow_run: - workflows: ['*'] - types: [completed] - push: - branches: - - master - - release/* - - next/* -jobs: - notify: - runs-on: ubuntu-latest - if: ${{ github.event.workflow_run.conclusion == 'failure' }} - steps: - - name: Generate Slack Payload - id: generate-payload - uses: actions/github-script@v4 - with: - script: | - const repo_name = "${{ github.event.workflow_run.repository.full_name }}"; - const run_id = ${{ github.event.workflow_run.id }}; - const run_url = `https://github.com/${repo_name}/actions/runs/${run_id}`; - const workflow_name = "${{ github.event.workflow_run.name }}" - const branch_name = "${{ github.event.workflow_run.head_branch }}" - const payload = { - text: `Master Branch CI Failure! Workflow “${workflow_name}” failed in repo: “${repo_name}”, branch: "${branch_name}". Run URL: ${run_url}. Please check it.`, - icon_emoji: ":onfire:", - }; - return JSON.stringify(payload); - result-encoding: string - - - name: Send Slack Message - uses: slackapi/slack-github-action@v1.23.0 - with: - payload: ${{ steps.generate-payload.outputs.result }} - env: - SLACK_WEBHOOK_URL: ${{ secrets.SLACK_GATEWAY_NOTIFICATIONS_WEBHOOK }} diff --git a/.github/workflows/release-and-tests-fail-bot.yml b/.github/workflows/release-and-tests-fail-bot.yml new file mode 100644 index 000000000000..e420088c5c58 --- /dev/null +++ b/.github/workflows/release-and-tests-fail-bot.yml @@ -0,0 +1,59 @@ +name: Notify Slack user on workflow failure + +on: + workflow_run: + workflows: ["Package & Release", "Build & Test"] + types: + - completed + branches: + - master + - release/* + - next/* + +jobs: + notify_failure: + runs-on: ubuntu-latest + if: ${{ github.event.workflow_run.conclusion == 'failure' && github.event.workflow_run.event != 'schedule' }} + steps: + - name: Fetch mapping file + id: fetch_mapping + uses: actions/github-script@v6 + env: + ACCESS_TOKEN: ${{ secrets.PAT }} + with: + script: | + const url = 'https://raw.githubusercontent.com/Kong/github-slack-mapping/main/mapping.json'; + const headers = {Authorization: `token ${process.env.ACCESS_TOKEN}`}; + const response = await fetch(url, {headers}); + const mapping = await response.json(); + return mapping; + + - name: Generate Slack Payload + id: generate-payload + env: + SLACK_CHANNEL: gateway-notifications + SLACK_MAPPING: "${{ steps.fetch_mapping.outputs.result }}" + uses: actions/github-script@v6 + with: + script: | + const workflow_name = "${{ github.event.workflow_run.name }}"; + const repo_name = "${{ github.event.workflow_run.repository.full_name }}"; + const branch_name = "${{ github.event.workflow_run.head_branch }}"; + const run_url = "${{ github.event.workflow_run.html_url }}"; + const slack_mapping = JSON.parse(process.env.SLACK_MAPPING); + const actor_github_id = "${{ github.event.workflow_run.actor.login }}"; + const actor_slack_id = slack_mapping[actor_github_id]; + const actor = actor_slack_id ? `<@${actor_slack_id}>` : actor_github_id; + const payload = { + text: `Hello ${actor} , workflow “${workflow_name}” failed in repo: "${repo_name}", branch: "${branch_name}". Please check it: ${run_url}.`, + channel: process.env.SLACK_CHANNEL, + }; + return JSON.stringify(payload); + result-encoding: string + + - name: Send Slack Message + uses: slackapi/slack-github-action@e28cf165c92ffef168d23c5c9000cffc8a25e117 # v1.24.0 + with: + payload: ${{ steps.generate-payload.outputs.result }} + env: + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_GATEWAY_NOTIFICATIONS_WEBHOOK }} diff --git a/.github/workflows/release-fail-bot.yml b/.github/workflows/release-fail-bot.yml deleted file mode 100644 index 68d1c88637a2..000000000000 --- a/.github/workflows/release-fail-bot.yml +++ /dev/null @@ -1,38 +0,0 @@ -name: Notify Slack user on “Package & Release” failure - -on: - workflow_run: - workflows: ["Package & Release"] - types: - - completed - branches: - - master - - next/* - -jobs: - notify_failure: - runs-on: ubuntu-latest - if: ${{ github.event.workflow_run.conclusion == 'failure' }} - steps: - - name: Generate Slack Payload - id: generate_payload - uses: actions/github-script@v4 - with: - script: | - const repo_name = "${{ github.event.workflow_run.repository.full_name }}"; - const run_id = ${{ github.event.workflow_run.id }}; - const run_url = `https://github.com/${repo_name}/actions/runs/${run_id}`; - const workflow_name = "${{ github.event.workflow_run.name }}" - const branch_name = "${{ github.event.workflow_run.head_branch }}" - const payload = { - text: `Workflow “${workflow_name}” failed in repo: “${repo_name}”, branch: "${branch_name}". Run URL: ${run_url}. Please check it.`, - icon_emoji: ":onfire:", - }; - console.log(`::set-output name=payload::${JSON.stringify(payload)}`); - - - name: "Send Slack Message" - uses: slackapi/slack-github-action@v1.23.0 - with: - payload: ${{ steps.generate_payload.outputs.payload }} - env: - SLACK_WEBHOOK_URL: ${{ secrets.SLACK_GATEWAY_NOTIFICATIONS_WEBHOOK }}