-
-
Notifications
You must be signed in to change notification settings - Fork 40
161 lines (134 loc) · 6.36 KB
/
slack-ocwm-reminder.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
154
155
156
157
158
159
160
161
name: Send Office Hours Reminders
on:
schedule:
# Europe/Americas: Last Friday of previous month, first Friday (if before Tuesday), and day of (first Tuesday), even months
- cron: '0 9 25-31 5,7,9,11,1,3 5' # Last Friday of odd months at 9 UTC
- cron: '0 9 1-7 6,8,10,12,2,4 5' # First Friday at 9 UTC in even months
- cron: '0 9 1-7 6,8,10,12,2,4 2' # First Tuesday at 9 UTC in even months
# APAC/Americas: Last Friday of previous month, first Friday (if before Tuesday), and day of (first Tuesday), odd months
- cron: '0 17 25-31 4,6,8,10,12,2 5' # Last Friday of even months at 17 UTC
- cron: '0 17 1-7 7,9,11,1,3,5 5' # First Friday at 17 UTC in odd months
- cron: '0 17 1-7 7,9,11,1,3,5 2' # First Tuesday at 17 UTC in odd months
jobs:
send-reminders:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
- name: Get GitHub App Token
uses: actions/create-github-app-token@v1
id: get_workflow_token
with:
app-id: ${{ vars.APP_ID }}
private-key: ${{ secrets.PRIVATE_KEY }}
- name: Install dependencies
run: npm install @octokit/[email protected]
- name: Send reminders
uses: actions/github-script@v7
env:
GITHUB_TOKEN: ${{ steps.get_workflow_token.outputs.token }}
OWNER: ${{ vars.ORGANISATION }}
REPO: 'community'
OCWM_LABEL: ${{ vars.OCWM_LABEL }}
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_OFFICEHOURS }}
with:
script: |
const { Octokit } = require("@octokit/core");
const github = new Octokit({ auth: process.env.GITHUB_TOKEN });
function getFirstTuesdayOfMonth(year, month) {
let date = new Date(Date.UTC(year, month, 1));
while (date.getUTCDay() !== 2) {
date.setUTCDate(date.getUTCDate() + 1);
}
return date;
}
function getLastTuesdayOfMonth(year, month) {
let date = new Date(Date.UTC(year, month + 1, 0)); // Last day of the month
while (date.getUTCDay() !== 2) {
date.setUTCDate(date.getUTCDate() - 1);
}
return date;
}
function isLastFridayOfMonth(date) {
const lastDayOfMonth = new Date(Date.UTC(date.getUTCFullYear(), date.getUTCMonth() + 1, 0));
const lastFriday = new Date(lastDayOfMonth);
lastFriday.setUTCDate(lastFriday.getUTCDate() - (lastFriday.getUTCDay() + 2) % 7);
return date.getUTCDate() === lastFriday.getUTCDate();
}
function isFirstFridayOfMonth(date) {
return date.getUTCDay() === 5 && date.getUTCDate() <= 7;
}
try {
const today = new Date();
const currentMonth = today.getUTCMonth();
const isEuropeAmericas = [5, 7, 9, 11, 1, 3].includes(currentMonth+1);
const isFriday = today.getUTCDay() === 5;
const isTuesday = today.getUTCDay() === 2;
const isLastFriday = isLastFridayOfMonth(today);
const isFirstFriday = isFirstFridayOfMonth(today);
console.log(`Current date: ${today.toISOString()}, Month: ${currentMonth + 1}, Is Europe/Americas: ${isEuropeAmericas}, Is Friday: ${isFriday}, Is Tuesday: ${isTuesday}, Is Last Friday: ${isLastFriday}, Is First Friday: ${isFirstFriday}`);
let timezone, time, date;
if (isEuropeAmericas) {
timezone = "Europe/Americas friendly";
time = "10:00 BST";
} else {
timezone = "APAC/Americas friendly";
time = "10:00 PT";
}
const firstTuesdayThisMonth = getFirstTuesdayOfMonth(today.getUTCFullYear(), currentMonth);
const lastTuesdayLastMonth = getLastTuesdayOfMonth(today.getUTCFullYear(), currentMonth);
let shouldSendReminder = false;
if (isFriday) {
if(isLastFriday && today > lastTuesdayLastMonth){
shouldSendReminder = true;
const firstTuesdayNextMonth = getFirstTuesdayOfMonth(today.getUTCFullYear(), currentMonth + 1);
date = firstTuesdayNextMonth.toISOString().split('T')[0];
} else if (isFirstFriday && today < firstTuesdayThisMonth) {
shouldSendReminder = true;
date = firstTuesdayThisMonth.toISOString().split('T')[0];
}
} else if (isTuesday) {
shouldSendReminder = true;
date = "today";
}
if (!shouldSendReminder) {
console.log("Not the correct day for sending a reminder");
return;
}
console.log(`Calculated date for reminder: ${date}`);
// Fetch the latest open Office Hours issue
const targetLabel = encodeURIComponent(process.env.OCWM_LABEL);
const { data: workMeetings } = await github.request(`GET /repos/${process.env.OWNER}/${process.env.REPO}/issues?labels=${targetLabel}&per_page=1&state=open`);
if (workMeetings.length === 0) {
console.log("No open Office Hours issue found");
return;
}
const issueNumber = workMeetings[0].number;
const issueLink = `https://github.com/${process.env.OWNER}/${process.env.REPO}/issues/${issueNumber}`;
console.log(`Found issue: ${issueLink}`);
// Prepare the message for Slack
const message = {
issue: issueLink,
date: date,
timezone: timezone,
time: time
};
console.log(`Sending message to Slack: ${JSON.stringify(message)}`);
// Send the message to Slack
const response = await fetch(process.env.SLACK_WEBHOOK, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(message)
});
if (!response.ok) {
throw new Error(`Failed to send Slack message: ${response.statusText}`);
}
console.log(`Reminder sent for ${timezone} session on ${date}`);
} catch (error) {
console.error(`An error occurred: ${error.message}`);
core.setFailed(error.message);
}