-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.yml
109 lines (99 loc) · 3.58 KB
/
action.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
name: Notify Slack
description: Send notification to Slack
inputs:
channel_id:
description: Slack Channel id or name
default: C0P46V3A8 #slack-test
required: false
slack_bot_token:
description: Slack Bot Token
required: true
include_commit_info:
description: Include commit information in slack notification
default: 'false'
required: false
include_custom_message:
description: Include custom message in slack notification
default: 'false'
required: false
custom_status:
description: Custom status to use in slack notification
required: false
custom_message:
description: Custom message to include in slack notification
required: false
runs:
using: composite
steps:
- name: Checkout
uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # v4.1.2
- name: Set commit info
id: set_commit_info
if: inputs.include_commit_info == 'true'
shell: bash
run: |
commit_sha=${{ github.event.pull_request.head.sha || github.sha }}
commit_message=$(git log -n 1 --pretty=format:%s $commit_sha)
commit_author_name=$(git log -n 1 --pretty=format:%aN $commit_sha)
commit_author_email=$(git log -n 1 --pretty=format:%aE $commit_sha)
commit_url="${{ github.server_url }}/${{ github.repository }}/commit/$commit_sha"
api_url="https://slack.com/api/users.lookupByEmail?token=${{ inputs.slack_bot_token }}&email=$commit_author_email"
slack_id=$(curl $api_url | jq -r '.user.id')
if [[ -z $slack_id || $slack_id == "null" ]]; then
user_tag="<mailto:$commit_author_email|$commit_author_name>"
else
user_tag="<@$slack_id>"
fi
echo "COMMIT_INFO=*Commit:*\n<$commit_url|$commit_message>" >> $GITHUB_OUTPUT
echo "AUTHOR_INFO=*Author:*\n$user_tag" >> $GITHUB_OUTPUT
- name: Create payload
shell: python
run: |
import json
job_status = "${{ inputs.custom_status || job.status }}".upper()
run_url = "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"
run_title = "${{ github.repository }} » ${{ github.ref_name }} » ${{ github.workflow }}"
message_title = f"*{job_status}*: <{run_url}|{run_title}>"
payload = {}
blocks = []
payload['text'] = message_title
blocks.append({
"type": "section",
"text": {
"type": "mrkdwn",
"text": message_title
}
})
if "${{ inputs.include_commit_info }}" == 'true':
blocks.append({
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "${{ steps.set_commit_info.outputs.COMMIT_INFO }}"
},
{
"type": "mrkdwn",
"text": "${{ steps.set_commit_info.outputs.AUTHOR_INFO }}"
}
]
})
if "${{ inputs.include_custom_message }}" == 'true':
blocks.append({
"type": "section",
"text": {
"type": "mrkdwn",
"text": "${{ inputs.custom_message }}"
}
})
payload['blocks'] = blocks
print(payload)
with open('payload.json', 'w') as f:
json.dump(payload, f)
- name: Send message
uses: slackapi/slack-github-action@6c661ce58804a1a20f6dc5fbee7f0381b469e001 # v1.25.0
env:
SLACK_BOT_TOKEN: ${{ inputs.slack_bot_token }}
with:
channel-id: ${{ inputs.channel_id }}
payload-file-path: payload.json