-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
295 lines (267 loc) · 10 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import { Octokit, App } from "octokit";
import * as yaml from 'js-yaml';
import * as fs from 'fs';
import 'dotenv/config'
import moment from 'moment';
const octokit = new Octokit({
userAgent: "my-app/v1.2.3",
auth: process.env.GITHUB_PERSONAL_TOKEN
});
moment.locale('fr');
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
/*
Get the value that is under the ### <title> specified by lineNumber until finding another title or the end of the body
*/
const getSectionValueFromBodyIssue = (bodyLines, lineNumber) => {
let value = "";
let i = lineNumber;
if (!bodyLines[i].startsWith("###")) {
console.error("not having a section title at line " + i);
return null;
}
i++;
while (i < bodyLines.length && !bodyLines[i].startsWith("###")) {
value += bodyLines[i] === '' ? '' : bodyLines[i] + "\n";
i++;
}
return value.trim();
}
/*
parse the body of an issues to get the author, need, time charge, description and DoD
*/
const parseIssueBody = (body) => {
const lines = body.split("\n");
const sections = {
"En tant que": "actor",
"Je veux": "need",
"Estimation du temps": "timeCharge",
"Description": "description",
"Definition of Done (DoD)": "dod",
};
let data = {};
lines.forEach((line, i) => {
if (line.startsWith("###")) {
const section = sections[line.substring(4).trim()];
if (section) {
data[section] = getSectionValueFromBodyIssue(lines, i);
}
}
});
return data;
};
const getMilestoneIssues = async (owner, repo, milestoneNumber) => {
const iterator = octokit.paginate.iterator(octokit.rest.issues.listForRepo, {
owner: owner,
repo: repo,
state: "all",
per_page: 100,
});
let total_issues = [];
for await (const { data: issues } of iterator) {
total_issues = total_issues.concat(issues);
}
return total_issues.filter(i => i.milestone && i.milestone.number === milestoneNumber && !("pull_request" in i));
};
const getSettings = (configFile) => {
const config = yaml.load(fs.readFileSync(configFile, 'utf8'));
return {
members: config.members.map(m => ({name: m.name, ghUsername: m.ghUsername})),
repository: config.repository,
doc: {
title: config.doc.title,
object: config.doc.object,
author: config.doc.author,
manager: config.doc.manager,
email: config.doc.email,
keywords: config.doc.keywords,
promo: config.doc.promo,
ver: config.doc.versions,
},
sprint: config.sprint,
lastSprintSummary: config.lastSprintSummary,
progressReport: {
summary: config.progressReport.summary,
blockingPoints: config.progressReport.blockingPoints,
conclusion: config.progressReport.conclusion,
members: config.members.map(m => ({name: m.name, ghUsername: m.ghUsername, tasks: [], chargeDone: 0, chargeTotal: 0})),
},
projects: []
}
}
const getProjects = async (owner, repo) => {
return (await octokit.rest.projects.listForRepo({
owner,
repo,
}));
}
const getProjectColumns = async (id) => {
return (await octokit.rest.projects.listColumns({
project_id: id,
}));
}
const getColumnCards = async (id) => {
return (await octokit.rest.projects.listCards({
column_id: id,
}));
}
const getProjectIssues = async (id) => {
let t = await Promise.all((await getProjectColumns(id)).data.flatMap(async c => {
const b = (await getColumnCards(c.id)).data;
return b;
}));
return t.flat();
}
const reorderProjectIssuesByLabel = (issues) => {
const reordered = {};
issues.forEach(i => {
if (i.labels.length > 0) {
const label = i.labels[0];
if (!reordered[label]) {
reordered[label] = [];
}
reordered[label].push(i);
}
});
return reordered;
}
const getTimeChargeData = (timeChargeDatyBody) => {
const l = timeChargeDatyBody;
let timeCharge = 0;
let timeChargeDone = 0;
const parts = l.split("/");
if (parts.length === 3) {
const [a, b, _c] = parts;
timeCharge += parseFloat(b);
timeChargeDone += parseFloat(a);
return {timeCharge, timeChargeDone: timeChargeDone < 0 ? 0 : timeChargeDone};
}
timeCharge += parseFloat(l);
return {timeCharge, timeChargeDone};
}
export const getDataFromIssues = async (configFile) => {
const data = getSettings(configFile);
let issues = (await getMilestoneIssues(data.repository.owner, data.repository.repo, parseInt(data.repository.milestoneNum)));
data.ignoredIssues = [];
issues = issues.filter(i => {
if (!i.labels.length) return true;
if (!i.labels.some(l => data.repository.ignoredLabels.includes(l.name))) {
return true;
}
data.ignoredIssues.push(i);
return false;
})
let stories = issues.map((issue) => {
try {
const parsed = parseIssueBody(issue.body);
const {timeCharge, timeChargeDone} = getTimeChargeData(parsed.timeCharge);
issue.assignees.forEach(a => {
const member = data.progressReport.members.find(m => m.ghUsername === a.login);
if (!member) {
return;
}
if (timeChargeDone > 0) {
member.chargeDone += timeChargeDone / issue.assignees.length;
} else if (issue.state === "closed") {
member.chargeDone += timeCharge / issue.assignees.length;
}
member.chargeTotal += timeCharge / issue.assignees.length;
});
return {
id: issue.number,
num: '',
name: issue.title,
actor: parsed.actor,
need: parsed.need,
description: parsed.description.split('\n').map(l => ({line: l})),
dod: parsed.dod.split('\n').map(l => ({line: l})),
// timeCharge ex : "2J/H", "0.6 J/H"
charge: timeCharge,
done: issue.state === 'closed',
labels: issue.labels.map(l => l.name),
assignees: issue.assignees.map(a => data.members.find(m => m.ghUsername === a.login)?.name ?? a.login).join(', '),
}}
catch (e) {
console.log("Error parsing issue " + issue.number);
console.log(e);
return null;
}}).filter(i => i !== null);
const projects = (await getProjects(data.repository.owner, data.repository.repo)).data;
const projectIssues = await Promise.all(projects.map(async p => {
const taskObj = reorderProjectIssuesByLabel((await getProjectIssues(p.id)).map((c) => {
return stories.find(s => parseInt(c.content_url.split("/").pop()) === s.id);
}).filter(s => s !== undefined));
if (taskObj.length === 0) {
return null;
}
let taskInc = 0;
const projectTasks = Object.entries(taskObj).map(([taskName, taskStories]) => {
taskInc++;
let inc = 0;
let tasksStoriesNum = [];
// updating by reference stories num
taskStories.forEach(s => {
inc++;
s.num = `${p.name} - ${taskInc}.${inc}`;
tasksStoriesNum.push(`${taskInc}.${inc}`);
});
return ({
name: taskName,
stories: taskStories.map((v, i) => ({...v, num: tasksStoriesNum[i]})),
num: taskInc,
charge: taskStories.reduce((acc, s) => acc + s.charge, 0),
});
});
return {
tasks: projectTasks,
name: p.name,
charge: projectTasks.reduce((acc, t) => acc + t.charge, 0),
};
}));
data.projects = projectIssues;
data.sprintCharge = projectIssues.reduce((acc, s) => acc + s.charge, 0);
data.stories = stories.sort((a, b) => {
// elements with num are at the start of the list
if (a.num != '' && b.num == '') return -1;
if (a.num == '' && b.num != '') return 1;
if (a.num == '' && b.num == '') return 0;
return a.num.localeCompare(b.num);
});
//data.projects = projectsInfo.filter(pI => {
// return pI.tasks.filter(t => t.stories.length > 0).length > 0;
//});
data.progressReport.members.map(m => {
const memberIssues = issues.filter(i => i.assignees.map(a => a.login).includes(m.ghUsername));
m.tasks = memberIssues.map(issue => ({name: issue.title, done: issue.state === 'closed'}));
return m;
})
{
const parseDateFormat = "DD-MM-YYYY";
const dateStart = moment(data.sprint.start, parseDateFormat);
const dateEnd = moment(data.sprint.end, parseDateFormat);
const displayFormat = (dateStart.format("YYYY") === dateEnd.format("YYYY")) ? 'MMMM' : 'MMMM YYYY';
data.sprint.displayTimePeriod = `${capitalizeFirstLetter(dateStart.format(displayFormat))} - ${capitalizeFirstLetter(dateEnd.format('MMMM YYYY'))}`;
}
return data;
}
// print the summary of the PLD in a table
// one member per line, with the number of tasks done and the number of tasks total and their charge in J/H
// print ignored issues and if they're closed or not
export const printSummarizePLD = (data) => {
console.log("Summary of the PLD, milestone : " + data.repository.milestoneNum);
console.log(`
Number of stories: ${data.stories.length}
${data.sprint.displayTimePeriod}
${data.sprintCharge} J/H
`);
console.log(`Ignored issues (${data.ignoredIssues.length})`);
data.ignoredIssues.forEach(i => {
console.log(`${i.state.padEnd(5)} - ${i.title}`);
});
console.log("");
console.log(`Members (${data.progressReport.members.length})`);
data.progressReport.members.forEach(m => {
console.log(`${m.name.padEnd(30)} - ${m.chargeDone.toString().padStart(3)} / ${m.chargeTotal.toString().padEnd(3)} J/H ${m.tasks.length.toString().padEnd(2)} tasks`);
});
}