-
Notifications
You must be signed in to change notification settings - Fork 20
/
index.js
105 lines (98 loc) · 3.64 KB
/
index.js
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
const core = require('@actions/core');
const axios = require('axios');
const {
getPullRequestsToReview,
getPullRequestsWithoutLabel,
getPullRequestsReviewersCount,
createPr2UserArray,
checkGithubProviderFormat,
prettyMessage,
stringToObject,
getTeamsMentions,
formatSlackMessage,
formatRocketMessage,
formatTeamsMessage,
} = require('./functions');
const { GITHUB_TOKEN, GITHUB_REPOSITORY, GITHUB_API_URL } = process.env;
const AUTH_HEADER = {
Authorization: `token ${GITHUB_TOKEN}`,
};
const PULLS_ENDPOINT = `${GITHUB_API_URL}/repos/${GITHUB_REPOSITORY}/pulls`;
/**
* Get Pull Requests from GitHub repository
* @return {Promise} Axios promise
*/
async function getPullRequests() {
return axios({
method: 'GET',
url: PULLS_ENDPOINT,
headers: AUTH_HEADER,
});
}
/**
* Send notification to a channel
* @param {String} webhookUrl Webhook URL
* @param {String} messageData Message data object to send into the channel
* @return {Promise} Axios promise
*/
async function sendNotification(webhookUrl, messageData) {
return axios({
method: 'POST',
url: webhookUrl,
data: messageData,
});
}
/**
* Main function for the GitHub Action
*/
async function main() {
try {
const webhookUrl = core.getInput('webhook-url');
const provider = core.getInput('provider');
const channel = core.getInput('channel');
const github2providerString = core.getInput('github-provider-map');
const ignoreLabel = core.getInput('ignore-label');
core.info('Getting open pull requests...');
const pullRequests = await getPullRequests();
const totalReviewers = await getPullRequestsReviewersCount(pullRequests.data);
core.info(`There are ${pullRequests.data.length} open pull requests and ${totalReviewers} reviewers`);
const pullRequestsToReview = getPullRequestsToReview(pullRequests.data);
const pullRequestsWithoutLabel = getPullRequestsWithoutLabel(pullRequestsToReview, ignoreLabel);
core.info(`There are ${pullRequestsWithoutLabel.length} pull requests waiting for reviews`);
if (pullRequestsWithoutLabel.length) {
const pr2user = createPr2UserArray(pullRequestsWithoutLabel);
if (github2providerString && !checkGithubProviderFormat(github2providerString)) {
return core.setFailed(`The github-provider-map string is not in correct format: "name1:id1,name2:id2,..."`);
}
const github2provider = stringToObject(github2providerString);
const messageText = prettyMessage(pr2user, github2provider, provider);
let messageObject;
switch (provider) {
case 'slack':
messageObject = formatSlackMessage(channel, messageText);
break;
case 'rocket':
messageObject = formatRocketMessage(channel, messageText);
break;
case 'msteams': {
const msTeamsMentions = getTeamsMentions(github2provider, pr2user);
messageObject = formatTeamsMessage(messageText, msTeamsMentions);
break;
}
}
const resNotification = await sendNotification(webhookUrl, messageObject);
// https://github.com/MicrosoftDocs/msteams-docs/issues/402
// If MS Teams fails, it might return still 200 OK, but data is not 1:
if (provider === 'msteams' && resNotification.data !== 1) {
core.info('Error: MS Teams notification failed.');
core.info(`Debugging: request body sent:\n${resNotification.config?.data}`);
return core.setFailed(resNotification.data);
}
core.info(`Notification sent successfully!`);
core.info(`Debugging: request body sent:\n${resNotification.config?.data}`);
}
} catch (error) {
core.setFailed(error.message);
}
}
main();