Skip to content

Commit

Permalink
SC-16085 Fix empty actor
Browse files Browse the repository at this point in the history
  • Loading branch information
yanis-martin-sonarsource1 committed Oct 3, 2024
1 parent cf3cce9 commit a71860b
Showing 1 changed file with 95 additions and 63 deletions.
158 changes: 95 additions & 63 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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>"
}
]
}
]
}
]
}

0 comments on commit a71860b

Please sign in to comment.