-
Notifications
You must be signed in to change notification settings - Fork 467
153 lines (137 loc) · 5.25 KB
/
cicd_scheduled_notify-seated-prs.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
name: 'Notify about seated PRs'
on:
schedule:
- cron: '0 10 * * *'
workflow_dispatch:
push:
branches:
- 28718-notify-seated-prs
jobs:
resolve-seated-prs:
runs-on: ubuntu-22.04
outputs:
seated_prs: ${{ steps.fetch-seated-prs.outputs.seated_prs }}
members: ${{ steps.fetch-seated-prs.outputs.members }}
env:
REPO: core
PR_DAY_THRESHOLD: 1
DRAFT_PR_DAY_THRESHOLD: 1
steps:
- run: echo 'GitHub context'
env:
GITHUB_CONTEXT: ${{ toJson(github) }}
- name: Filter execution
uses: actions/github-script@v7
with:
result-encoding: string
script: |
const day = new Date().getDay();
console.log(new Date());
if (day === 0 || day === 6) {
console.log('It\'s (happy Ⓡ Freddy Rodriguez) weekend, not sending any notifications');
//process.exit(1);
}
- id: fetch-seated-prs
name: Fetch Seated PRs
if: success()
uses: actions/github-script@v7
with:
result-encoding: string
retries: 3
retry-exempt-status-codes: 400,401
script: |
const millisPerDay = 1 * 60 * 60 * 1000;
const prDayThreshold = ${{ env.PR_DAY_THRESHOLD }} * millisPerDay;
const draftPrDayThreshold = ${{ env.DRAFT_PR_DAY_THRESHOLD }} * millisPerDay;
const now = new Date();
const seatedPrs = {};
const excludedUsers = ['dependabot[bot]']
const fetchOpenPrs = async () => {
const opts = github.rest.pulls.list.endpoint.merge({
...{
owner: '${{ github.repository_owner }}',
repo: '${{ env.REPO }}',
per_page: 100
}
});
return await github.paginate(opts);
};
const isPrSeated = (pr) => {
const createdAt = new Date(Date.parse(pr.created_at));
console.log(`Now: ${now} / CreatedAt: ${createdAt}`);
const delta = now.getTime() - createdAt.getTime();
const threshold = pr.draft ? draftPrDayThreshold : prDayThreshold;
return delta >= threshold;
};
const addPr = (pr, login) => {
if (!isPrSeated(pr)) {
return;
}
if (!seatedPrs[login]) {
seatedPrs[login] = [];
}
seatedPrs[login].push({
pr_number: pr.number,
url: pr.html_url,
draft: pr.draft,
created_at: pr.created_at,
updated_at: pr.updated_at
});
};
const handlePr = (pr) => {
const login = pr.user.login;
if (excludedUsers.includes(login) || login !== 'victoralfaro-dotcms') {
return;
}
addPr(pr, login);
};
const prs = await fetchOpenPrs();
console.log(`PRs size: [${prs.length}]`);
prs.forEach(handlePr);
const members = Object.keys(seatedPrs);
console.log(`Seated PRs size: [${Object.keys(seatedPrs).length}]`);
console.log(JSON.stringify(seatedPrs, null, 2));
console.log(`Users: ${JSON.stringify(members)}`);
core.setOutput('seated_prs', JSON.stringify(seatedPrs));
core.setOutput('members', members.join(' '));
slack-channel-resolver:
name: Resolve Slack Channel
needs: resolve-seated-prs
if: success() && needs.resolve-seated-prs.outputs.members
uses: ./.github/workflows/utility_slack-channel-resolver.yml
with:
github_users: ${{ needs.resolve-seated-prs.outputs.members }}
secrets:
CI_MACHINE_USER: ${{ secrets.CI_MACHINE_USER }}
CI_MACHINE_TOKEN: ${{ secrets.CI_MACHINE_TOKEN }}
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
notify-seated-prs:
runs-on: ubuntu-22.04
needs: [resolve-seated-prs, slack-channel-resolver]
if: success()
name: Notifying team member ${{ matrix.member }}
strategy:
fail-fast: false
matrix:
member: ${{ fromJSON(needs.slack-channel-resolver.outputs.channel_ids) }}
steps:
- name: Build Message
uses: actions/github-script@v7
with:
result-encoding: string
script: |
console.log('${{ needs.resolve-seated-prs.outputs.seated_prs }}');
const seatedPrs = '${{ fromJSON(needs.resolve-seated-prs.outputs.seated_prs) }}'
console.log(JSON.stringify(seatedPrs, null, 2));
- name: Notify member
if: success()
shell: bash
run: |
channel=${{ matrix.member }}
message="The following github issue has been marked as a Front-end issue: ${{ env.ISSUE_URL }}"
# curl -X POST \
# -H "Content-type: application/json" \
# -H "Authorization: Bearer ${{ secrets.SLACK_BOT_TOKEN }}" \
# -d "{ \"channel\":\"${channel}\",\"text\":\"${message}\"}" \
# -s \
# https://slack.com/api/chat.postMessage