-
Notifications
You must be signed in to change notification settings - Fork 149
95 lines (90 loc) · 3.46 KB
/
notifications.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
name: Send Notifications to Slack
on:
pull_request:
types: [opened, reopened]
issues:
types: [opened]
issue_comment:
types: [created]
jobs:
issue-notifications:
name: Send Notifications
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v7
id: sanitize-title
with:
script: |
const isPR = !!context.payload.pull_request;
const isIssue = !!context.payload.issue;
const item = isPR ? context.payload.pull_request : isIssue ? context.payload.issue : context.payload.issue_comment.issue;
// Sanitization functions
const sanitizeTitle = (title) => {
return title
// Remove potential markdown formatting
.replace(/[*_~`]/g, '')
// Remove potential HTML tags
.replace(/<[^>]*>/g, '')
// Remove multiple spaces
.replace(/\s{2,}/g, ' ')
// Trim whitespace
.trim()
// Enforce max length of 100
.substring(0, 100);
};
// Escape special characters for Slack
const escapeForSlack = (text) => {
return text
.replace(/"/g, '"')
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/[@]/g, '\\@')
.replace(/>/g, '>')
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>');
};
const sanitizedTitle = escapeForSlack(sanitizeTitle(item.title));
console.log('Sanitized Title: ', sanitizedTitle);
core.setOutput('safe-title', sanitizedTitle);
- name: Send notifications on Pull Request
if: ${{ github.event_name == 'pull_request'}}
id: slack_PR
uses: slackapi/slack-github-action@v1.26.0
with:
payload: |
{
"Notification Type": "Pull Request",
"Notification URL":"${{ github.event.pull_request.html_url }}",
"GitHub Repo": "${{ github.repository }}",
"Notification Title": "${{ steps.sanitize-title.outputs.safe-title }}"
}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
- name: Send notification on new issues
if: ${{github.event_name == 'issues'}}
id: slack_issue
uses: slackapi/slack-github-action@v1.26.0
with:
payload: |
{
"Notification Type": "Issue",
"Notification URL":"${{ github.event.issue.html_url }}",
"GitHub Repo": "${{ github.repository }}",
"Notification Title": "${{ steps.sanitize-title.outputs.safe-title }}"
}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
- name: Send notification on Issues and Pull Requests Comments
if: ${{github.event_name == 'issue_comment'}}
id: slack_issue_comment
uses: slackapi/slack-github-action@v1.26.0
with:
payload: |
{
"Notification Type": "Issue comment",
"Notification URL":"${{ github.event.comment.html_url }}",
"GitHub Repo": "${{ github.repository }}",
"Notification Title": "${{ steps.sanitize-title.outputs.safe-title }}"
}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}