-
Notifications
You must be signed in to change notification settings - Fork 8
71 lines (63 loc) · 2.28 KB
/
Copy pathapp-notify-slack-github.yml
File metadata and controls
71 lines (63 loc) · 2.28 KB
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
name: lomi · notify · slack · github
on:
issues:
types: [opened]
pull_request:
types: [opened]
workflow_dispatch:
jobs:
notify-devops:
runs-on: ubuntu-latest
steps:
- name: Notify #devops on Slack
uses: actions/github-script@v7
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
with:
script: |
const webhook = process.env.SLACK_WEBHOOK_URL;
if (!webhook) {
core.info('SLACK_WEBHOOK_URL not set; skipping notification');
return;
}
let payload;
if (context.eventName === 'workflow_dispatch') {
payload = {
blocks: [
{
type: 'section',
text: {
type: 'mrkdwn',
text: `:white_check_mark: *GitHub → Slack test* for \`${context.repo.owner}/${context.repo.repo}\`\nNotifications for new issues and pull requests will post to *#devops*.`,
},
},
],
};
} else {
const isIssue = context.eventName === 'issues';
const item = isIssue ? context.payload.issue : context.payload.pull_request;
const kind = isIssue ? 'Issue' : 'Pull request';
const emoji = isIssue ? ':issue-opened:' : ':pull-request:';
payload = {
blocks: [
{
type: 'section',
text: {
type: 'mrkdwn',
text: `${emoji} *New ${kind}* in \`${context.repo.owner}/${context.repo.repo}\`\n<${item.html_url}|${item.title}>\nOpened by *${item.user.login}*`,
},
},
],
};
}
const response = await fetch(webhook, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload),
});
const body = await response.text();
if (!response.ok) {
core.setFailed(`Slack webhook failed (${response.status}): ${body}`);
return;
}
core.info('Posted notification to #devops');