From de07abb3ebcc677db02853b22bc47bd27680ffb7 Mon Sep 17 00:00:00 2001 From: yanis-martin-sonarsource1 Date: Tue, 1 Oct 2024 11:42:23 +0200 Subject: [PATCH 1/2] SC-16085 Fix empty actor --- .github/workflows/main.yaml | 158 ++++++++++++++++++++++-------------- 1 file changed, 95 insertions(+), 63 deletions(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 93be533..b86e38c 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -25,68 +25,100 @@ jobs: && github.event.check_run.conclusion != 'skipped' && github.event.check_run.conclusion != 'neutral' steps: - - name: Extract and format username - id: extract - uses: actions/github-script@v4 - with: - script: | - const actor = '${{ github.actor }}'; - const regex = /(\w+)-(\w+)-(\w+)/; - const matches = actor.match(regex); - if (matches) { - const formattedUsername = `@${matches[1]} ${matches[2]}`; - return { formattedUsername }; - } else { - return { formattedUsername: '' }; - } - - name: Vault Secrets - id: secrets - uses: SonarSource/vault-action-wrapper@3996073b47b49ac5c58c750d27ab4edf469401c8 # 3.0.1 - with: - secrets: | - development/kv/data/slack webhook | slack_webhook; - - name: Send Slack notification - uses: slackapi/slack-github-action@37ebaef184d7626c5f204ab8d3baff4262dd30f0 # v1.27.0 - env: - SLACK_WEBHOOK_URL: ${{ fromJSON(steps.secrets.outputs.vault).slack_webhook }} - SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK - with: - payload: | - { - "channel":"${{ inputs.slackChannel }}", - "attachments":[ + - name: Vault Secrets + id: secrets + uses: SonarSource/vault-action-wrapper@3996073b47b49ac5c58c750d27ab4edf469401c8 # 3.0.1 + with: + secrets: | + development/kv/data/slack webhook | slack_webhook; + development/kv/data/slack token | slack_token; + + - name: Get Slack User ID + id: get-user-id + uses: actions/github-script@v7 + with: + script: | + const actor = '${{ github.actor }}'; + const regex = /(\w+)-(\w+)-(\w+)/; + const matches = actor.match(regex); + if (matches) + { + const email = `${matches[1]}.${matches[2]}@sonarsource.com`; + const token = '${{ fromJSON(steps.secrets.outputs.vault).slack_token }}'; + + const response = await fetch('https://slack.com/api/users.list', { + method: 'GET', + headers: { + 'Authorization': `Bearer ${token}`, + 'Content-Type': 'application/json' + } + }); + + const data = await response.json(); + if (data.ok) + { + const user = data.members.find(member => member.profile.email === email); + if (user) + { + core.exportVariable('user_id', user.id); + } + else + { + core.exportVariable('user_id', ''); + } + } + else + { + core.exportVariable('user_id', ''); + } + } + else { - "color":"#ee0000", - "blocks":[ - { - "type":"section", - "text":{ - "type":"mrkdwn", - "text":"*${{ github.event.check_run.app.name }}* - <${{ github.event.check_run.details_url }}|${{ github.event.check_run.name }}> ${{ github.event.check_run.conclusion }} in *${{ github.repository }}*" - } - }, - { - "type":"section", - "text":{ - "type":"mrkdwn", - "text":"Branch: *${{ github.event.check_run.check_suite.head_branch }}*\nCommit: *${{ github.event.check_run.head_sha }}*\nActor: <@${{ steps.extract.outputs.formattedUsername }}>" - } - }, - { - "type": "context", - "elements": [ - { - "type": "image", - "image_url": "${{ github.event.check_run.app.owner.avatar_url }}", - "alt_text": "Icon" - }, - { - "type": "mrkdwn", - "text": "<${{ github.event.check_run.details_url }}|View more details>" - } - ] - } - ] + core.exportVariable('user_id', ''); } - ] - } + + - name: Send Slack notification + uses: slackapi/slack-github-action@37ebaef184d7626c5f204ab8d3baff4262dd30f0 # v1.27.0 + env: + SLACK_WEBHOOK_URL: ${{ fromJSON(steps.secrets.outputs.vault).slack_webhook }} + SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK + with: + payload: | + { + "channel":"${{ inputs.slackChannel }}", + "attachments":[ + { + "color":"#ee0000", + "blocks":[ + { + "type":"section", + "text":{ + "type":"mrkdwn", + "text":"*${{ github.event.check_run.app.name }}* - <${{ github.event.check_run.details_url }}|${{ github.event.check_run.name }}> ${{ github.event.check_run.conclusion }} in *${{ github.repository }}*" + } + }, + { + "type":"section", + "text":{ + "type":"mrkdwn", + "text":"Branch: *${{ github.event.check_run.check_suite.head_branch }}*\nCommit: *${{ github.event.check_run.head_sha }}*\nActor: <@${{ env.user_id }}>" + } + }, + { + "type": "context", + "elements": [ + { + "type": "image", + "image_url": "${{ github.event.check_run.app.owner.avatar_url }}", + "alt_text": "Icon" + }, + { + "type": "mrkdwn", + "text": "<${{ github.event.check_run.details_url }}|View more details>" + } + ] + } + ] + } + ] + } \ No newline at end of file From 00696024af98ba1687478fbbac8dd475a3ddb4cd Mon Sep 17 00:00:00 2001 From: yanis-martin-sonarsource1 Date: Thu, 3 Oct 2024 15:30:09 +0200 Subject: [PATCH 2/2] fix trailing spaces --- .github/workflows/main.yaml | 65 +++++++++++++++---------------------- 1 file changed, 27 insertions(+), 38 deletions(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index eae2d30..89c7dd4 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -37,45 +37,34 @@ jobs: id: get-user-id uses: actions/github-script@v7 with: - script: | - const actor = '${{ github.actor }}'; - const regex = /(\w+)-(\w+)-(\w+)/; - const matches = actor.match(regex); - if (matches) - { - const email = `${matches[1]}.${matches[2]}@sonarsource.com`; - const token = '${{ fromJSON(steps.secrets.outputs.vault).slack_token }}'; - - const response = await fetch('https://slack.com/api/users.list', { - method: 'GET', - headers: { - 'Authorization': `Bearer ${token}`, - 'Content-Type': 'application/json' - } - }); - - const data = await response.json(); - if (data.ok) - { - const user = data.members.find(member => member.profile.email === email); - if (user) - { - core.exportVariable('user_id', user.id); - } - else - { - core.exportVariable('user_id', ''); - } - } - else - { - core.exportVariable('user_id', ''); - } - } - else - { + script: | + const actor = '${{ github.actor }}'; + const regex = /(\w+)-(\w+)-(\w+)/; + const matches = actor.match(regex); + if (matches) { + const email = `${matches[1]}.${matches[2]}@sonarsource.com`; + const token = '${{ fromJSON(steps.secrets.outputs.vault).slack_token }}'; + const response = await fetch('https://slack.com/api/users.list', { + method: 'GET', + headers: { + 'Authorization': `Bearer ${token}`, + 'Content-Type': 'application/json' + } + }); + const data = await response.json(); + if (data.ok) { + const user = data.members.find(member => member.profile.email === email); + if (user) { + core.exportVariable('user_id', user.id); + } else { core.exportVariable('user_id', ''); } + } else { + core.exportVariable('user_id', ''); + } + } else { + core.exportVariable('user_id', ''); + } - name: Send Slack notification uses: slackapi/slack-github-action@37ebaef184d7626c5f204ab8d3baff4262dd30f0 # v1.27.0 @@ -121,4 +110,4 @@ jobs: ] } ] - } \ No newline at end of file + }