forked from DavideViolante/pr-reviews-reminder-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.js
85 lines (81 loc) · 2.46 KB
/
functions.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
/**
* Filter Pull Requests to get the one to review
* @param {Array} pullRequests Pull Requests to filter
* @return {Array} Pull Requests to review
*/
function getPullRequestsToReview(pullRequests) {
return pullRequests.filter((pr) => pr.requested_reviewers.length || pr.requested_teams.length);
}
/**
* Create an Array of Objects with { url, title, login } properties from a list of Pull Requests
* @param {Array} pullRequestsToReview Pull Requests
* @return {Array} Array of Objects with { url, title, login } properties
*/
function createPr2UserArray(pullRequestsToReview) {
const pr2user = [];
for (const pr of pullRequestsToReview) {
for (const user of pr.requested_reviewers) {
pr2user.push({
url: pr.html_url,
title: pr.title,
login: user.login,
});
}
for (const team of pr.requested_teams) {
pr2user.push({
url: pr.html_url,
title: pr.title,
login: team.slug,
});
}
}
return pr2user;
}
/**
* Convert a string like "name1:ID123,name2:ID456" to an Object { name1: "ID123", name2: "ID456"}
* @param {String} str String to convert to Object
* @return {Object} Object with usernames as properties and IDs as values
*/
function stringToObject(str) {
const map = {};
if (!str) {
return map;
}
const users = str.split(',');
users.forEach((user) => {
const [github, provider] = user.split(':');
map[github] = provider;
});
return map;
}
/**
* Create a pretty message to print
* @param {Array} pr2user Array of Object with these properties { url, title, login }
* @param {String} github2provider String containing usernames and IDs as "username:id,..."
* @param {String} provider Service to use: slack or msteams
* @return {String} Pretty message to print
*/
function prettyMessage(pr2user, github2provider, provider) {
let message = '';
for (const obj of pr2user) {
const mention = github2provider[obj.login] ?
`<@${github2provider[obj.login]}>` :
`@${obj.login}`;
switch (provider) {
case 'slack':
message += `Hey ${mention}, the PR "${obj.title}" is waiting for your review: ${obj.url}\n`;
break;
case 'msteams':
// eslint-disable-next-line max-len
message += `Hey ${mention}, the PR "${obj.title}" is waiting for your review: [${obj.url}](${obj.url}) \n`;
break;
}
}
return message;
}
module.exports = {
getPullRequestsToReview,
createPr2UserArray,
stringToObject,
prettyMessage,
};