diff --git a/.github/workflows/branch-deploy.yaml b/.github/workflows/branch-deploy.yaml index a42be4d600..25d0b51845 100644 --- a/.github/workflows/branch-deploy.yaml +++ b/.github/workflows/branch-deploy.yaml @@ -580,76 +580,86 @@ jobs: PR_NUMBER: ${{ github.event.number == '' && inputs.pr_number || github.event.number }} steps: - - name: Map users - id: map-actor-to-slack - uses: icalia-actions/map-github-actor@e568d1dd6023e406a1db36db4e1e0b92d9dd7824 # v0.0.2 - with: - actor-map: ${{ vars.SLACK_GITHUB_USERS_MAP }} - default-mapping: C067BD0377F - - - name: Post to a Slack channel - id: slack - uses: slackapi/slack-github-action@af78098f536edbc4de71162a307590698245be95 # v3.0.1 - with: - method: 'chat.postMessage' - token: ${{ secrets.SLACK_GHBOT_TOKEN }} - payload: | - { - "channel": "C067BD0377F", - "blocks": [ - { - "type": "header", - "text": { - "type": "plain_text", - "text": "Pull Request ${{ env.PR_NUMBER }} pre-staging deployment", - "emoji": true - } - }, - { - "type": "section", - "fields": [ - { - "type": "mrkdwn", - "text": "*Status:*\n${{ needs.deploy.result == 'success' && ':white_check_mark: Success' || ':x: Failure '}}" - }, - { - "type": "mrkdwn", - "text": ${{ toJson(env.PR_TEXT) }} - }, - { - "type": "mrkdwn", - "text": "*Workflow run:*\n<${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|View>" - } - ] - }, - { - "type": "section", - "fields": [ - { - "type": "mrkdwn", - "text": "*Author:*\n<@${{ steps.map-actor-to-slack.outputs.actor-mapping }}>" - }, - { - "type": "mrkdwn", - "text": "*Commit SHA:*\n<${{ github.server_url }}/${{ github.repository }}/commit/${{ github.event.pull_request.head.sha }}|${{ github.event.pull_request.head.sha }}>" - }, - { - "type": "mrkdwn", - "text": "*Deployed to:*\n" - }, - { - "type": "mrkdwn", - "text": "*Logs:*\n" - } - ] + - name: Find Slack user by GitHub username + id: resolve + uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 + env: + SLACK_BOT_TOKEN: ${{ secrets.SLACK_GHBOT_TOKEN }} + SLACK_GITHUB_FIELD_ID: "Xf0A2BPU8U77" + GITHUB_ACTOR: ${{ github.actor }} + with: + script: | + const token = process.env.SLACK_BOT_TOKEN; + const fieldId = process.env.SLACK_GITHUB_FIELD_ID; + const targetGithubUsername = process.env.GITHUB_ACTOR.toLowerCase(); + + // Collect all non-bot user IDs + const userIds = []; + let cursor; + do { + const params = new URLSearchParams({ limit: "200" }); + if (cursor) params.set("cursor", cursor); + const res = await fetch(`https://slack.com/api/users.list?${params}`, { + headers: { Authorization: `Bearer ${token}` } + }); + const data = await res.json(); + if (!data.ok) return core.setFailed(`Slack users.list error: ${data.error}`); + for (const member of data.members) { + if (!member.deleted && !member.is_bot) userIds.push(member.id); + } + cursor = data.response_metadata?.next_cursor; + } while (cursor); + + // Find user by fetching each profile until we match the GitHub username field + for (const userId of userIds) { + const res = await fetch(`https://slack.com/api/users.profile.get?user=${userId}`, { + headers: { Authorization: `Bearer ${token}` } + }); + const data = await res.json(); + if (!data.ok) continue; + const ghField = data.profile?.fields?.[fieldId]?.value; + if (ghField && ghField.toLowerCase() === targetGithubUsername) { + core.info(`Matched Slack user ${userId} for GitHub user ${targetGithubUsername}`); + core.setOutput("slack_user_id", userId); + return; } - ] - } - env: - PR_TEXT: | - *Pull Request:* - - GRAFANA_URL: ${{ secrets.STAGING_GRAFANA_URL }} + } + + core.warning(`No Slack user found with GitHub username: ${targetGithubUsername}`); + + - name: Send Slack notification + id: slack + if: steps.resolve.outputs.slack_user_id != '' + uses: slackapi/slack-github-action@af78098f536edbc4de71162a307590698245be95 # v3.0.1 + with: + method: 'chat.postMessage' + token: ${{ secrets.SLACK_GHBOT_TOKEN }} + payload: | + channel: ${{ steps.resolve.outputs.slack_user_id }} + blocks: + - type: header + text: + type: plain_text + text: "Pull Request ${{ env.PR_NUMBER }} pre-staging deployment" + emoji: true + - type: section + fields: + - type: mrkdwn + text: "*Status:*\n${{ needs.deploy.result == 'success' && ':white_check_mark: Success' || ':x: Failure '}}" + - type: mrkdwn + text: "*Pull Request:*\n" + - type: mrkdwn + text: "*Workflow run:*\n<${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|View>" + - type: section + fields: + - type: mrkdwn + text: "*Author:*\n<@${{ steps.resolve.outputs.slack_user_id }}>" + - type: mrkdwn + text: "*Commit SHA:*\n<${{ github.server_url }}/${{ github.repository }}/commit/${{ github.event.pull_request.head.sha }}|${{ github.event.pull_request.head.sha }}>" + - type: mrkdwn + text: "*Deployed to:*\n" + - type: mrkdwn + text: "*Logs:*\n" destroy: