-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcounting_prawns.10s.js
executable file
·154 lines (129 loc) · 4.07 KB
/
counting_prawns.10s.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
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
#!/usr/local/bin/node
/*
*coding: utf-8
* <bitbar.title>Counting Prawns</bitbar.title>
* <bitbar.version>v1.0.0.beta</bitbar.version>
* <bitbar.author>stefano-nerder-saitta</bitbar.author>
* <bitbar.author.github>stefanosaittamrf</bitbar.author.github>
* <bitbar.desc>GitHub Assigned Pull Request Counter with fancy emoji :tada:</bitbar.desc>
* <bitbar.dependencies>node</bitbar.dependencies>
* <bitbar.abouturl>https://github.com/stefanosaittamrf/assigned-pr-counter-marfeel</bitbar.abouturl>
*/
const https = require('https');
const bitbar = require('bitbar');
// Configurable params
const githubConfig = {
accessToken: '',
username: 'YOUR_USERNAME',
owner: 'Marfeel',
repos: ['MarfeelXP', 'AliceTenants', 'Gutenberg', 'ProTenants'],
color: 'green',
alertColor: 'red',
};
const getMutipleOptions = (config) => {
const {
accessToken,
owner,
repos,
} = config;
return repos.map(repo => ({
hostname: 'api.github.com',
path: `/repos/${owner}/${repo}/pulls`,
method: 'GET',
headers: {
Authorization: `token ${accessToken}`,
'User-Agent': 'Counting-Prawns-App',
},
}));
};
const getPRs = (options) => (
new Promise((resolve, reject) => {
const req = https.request(options, (res) => {
let body = '';
res.on('data', (chunk) => {
body += chunk;
});
res.on('end', () => {
resolve(JSON.parse(body));
});
});
req.on('error', (e) => {
reject(e);
});
req.end();
})
);
const getPrDividedByRepos = () => {
const multipleOptions = getMutipleOptions(githubConfig);
/* TODO: before to call the API add a name of repo than concat */
const prDividedByRepos = multipleOptions.map(options => getPRs(options));
return Promise.all(prDividedByRepos);
};
const filterPrsByLoginNames = pr => {
const {
assignees,
} = pr;
const {
username,
} = githubConfig;
const loginNames = assignees.map(assignee => assignee.login);
return loginNames.includes(username);
};
const getPrDividedByReposAndAssignedToYou = () => (
getPrDividedByRepos().then(prDividedByRepos =>
prDividedByRepos.map(prs =>
prs.filter(pr => filterPrsByLoginNames(pr))
)
)
);
const getTotalPrAssignedToYou = (prDividedByReposAssignedToYou) => (
prDividedByReposAssignedToYou.reduce((acc, prDividedByRepos) => (
acc + prDividedByRepos.length
), 0)
);
const getEmoji = num => (num > 0 ? ':eyeglasses:' : ':palm_tree:');
const getColor = darkMode => (darkMode ? 'white' : 'black');
const getFormattedPrByRepo = (repo, darkMode, sep) => (
/* TODO: needs a spearator after each pr obj */
repo.map(pr => (
{
text: pr.title,
color: getColor(darkMode),
href: pr.html_url,
}
)).concat(sep)
);
const getStyledTitle = numberOfPr => `${getEmoji(numberOfPr)} ${numberOfPr}`;
/* TODO: This needs a better implementation. Needs refacor */
const getRepoName = prs => prs[0].base.repo.name;
/* TODO: This method should have been called as soon as i get the data, not here. Needs refacor */
const getCleanPrAssignedToYouDividedByRepos = prAssignedToYouDividedByRepos => (
prAssignedToYouDividedByRepos.filter(prAssignedToYou => prAssignedToYou.length)
);
const bitbarObjectBuilder = (prAssignedToYouDividedByRepos, bitbarConfigVars) => {
const totalPrAssignedToYou = getTotalPrAssignedToYou(prAssignedToYouDividedByRepos);
const cleanPrAssignedToYouDividedByRepos = getCleanPrAssignedToYouDividedByRepos(prAssignedToYouDividedByRepos);
const {
darkMode,
sep,
} = bitbarConfigVars;
const heading = {
text: getStyledTitle(totalPrAssignedToYou),
color: getColor(darkMode),
dropdown: false,
};
const formattedPr = cleanPrAssignedToYouDividedByRepos.map(prs => (
{
text: `${getRepoName(prs)} ${getStyledTitle(prs.length)}`,
color: getColor(darkMode),
submenu: getFormattedPrByRepo(prs, darkMode, sep),
}
));
return Array.of(heading, sep).concat(formattedPr);
};
const paintBitBar = () => {
getPrDividedByReposAndAssignedToYou().then((prAssignedToYou) => {
bitbar(bitbarObjectBuilder(prAssignedToYou, bitbar));
});
};
paintBitBar();