-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathStoriesToWord.html
341 lines (325 loc) · 11.1 KB
/
StoriesToWord.html
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Stories to Word</title>
<link rel="icon" type="image/x-icon" href="favicon.ico" />
<link
href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"
rel="stylesheet"
type="text/css"
/>
<script src="config.js"></script>
<script src="https://momentjs.com/downloads/moment.js"></script>
<script src="https://unpkg.com/[email protected]/build/index.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.8/FileSaver.js"></script>
<style>
#spinner {
width: 80px;
height: 80px;
left: 0px;
top: 0px;
opacity: 1;
}
.code {
font-family: "Courier New";
font-weight: bolder;
padding: 4px;
margin: 2px;
}
.box {
margin: 2px;
padding: 2px;
width: 40px;
height: 40px;
border: 1px solid grey;
}
</style>
</head>
<body>
<div class="pl-5 m-2 mt-4 text-center">
<h3>Projet</h3>
<select id="selProj" class="m-3"></select>
<div>
<h3>Format du document:</h3>
<input name="rbtFormat" id="rbtAnalysis" type="radio" checked />
Analyse<br />
<input name="rbtFormat" id="rbtTests" type="radio" /> Rapport de test<br />
</div>
<div id="spnLoading" class="d-none">
<img
src="https://media2.giphy.com/media/3oEjI6SIIHBdRxXI40/giphy.gif"
alt="Loading GIF by Mashable"
id="spinner"
/>
Préparation ...
</div>
<br />
<button id="cmdSaveAsWord" type="button" onclick="generate()" class="p-3 m-3">Enregistrer</button>
</div>
<div style="font-size: x-small">V3.0</div>
</body>
</html>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>
moment.locale();
// ============ Initialization of output document =================
const docContent = {
styles: {
paragraphStyles: [
{
id: "Normal",
name: "Normal",
basedOn: "Normal",
next: "Normal",
quickFormat: true,
run: {
font: "Calibri"
}
}
]
},
sections: [
{
properties: {},
children: []
}
]
};
async function generate() {
await loadProject();
docx.Packer.toBlob(new docx.Document(docContent)).then((blob) => {
saveAs(blob, selProj.value + ".docx");
});
}
// ============ Initialization of IceScrum interface =================
// Init dropdown list from either an array of project IDs or a single ID
if (typeof iceScrumToken == "undefined") {
window.location = "index.html";
} else if (typeof projects !== "undefined") {
for (project of projects) {
let opt = document.createElement("option");
opt.text = project.projectKey;
opt.value = project.projectKey;
selProj.appendChild(opt);
}
} else {
window.location = "index.html";
}
async function loadProject() {
API_project_url = API_base_url + selProj.value;
// Project name
await fetch(API_project_url + "/?icescrum-token=" + iceScrumToken)
.then(function (response) {
return response.json();
})
.then(async function (data) {
spnLoading.classList.remove("d-none");
document.title = "Projet " + data.name;
if (rbtAnalysis.checked) {
await buildAnalysisContent(docContent.sections[0]);
} else {
await buildTestReportContent(docContent.sections[0]);
}
});
}
// Build the docx js content in the "Functional Analysis" format, adds it to the section passed
async function buildAnalysisContent(section) {
// Get all stories
await fetch(API_project_url + "/story?icescrum-token=" + iceScrumToken)
.then(function (response) {
return response.json();
})
.then(async function (data) {
// Process each story
for (story of data) {
await fetch(API_project_url + "/story/" + story.id + "?icescrum-token=" + iceScrumToken)
.then(function (response) {
return response.json();
})
.then(async function (story) {
// Create one table per story
let testRows = [];
await testsDoc(story.id, testRows, false); // prepare all tests as array of rows, without result
if (story.state > 1) {
// story is beyond the sandbox, keep it
if (testRows.length > 0 && story.description != null && story.acceptanceTests_count > 0) {
// Prepare the table
const storyDoc = new docx.Table({
rows: [
new docx.TableRow({
children: [
new docx.TableCell({
children: [new docx.Paragraph(story.description)]
})
]
}),
new docx.TableRow({
children: [
new docx.TableCell({
children: [
new docx.Paragraph({
text: "Tests d'acceptance: ",
alignment: docx.AlignmentType.CENTER
}),
new docx.Table({
rows: testRows,
borders: docx.TableBorders.NONE
})
]
})
]
})
]
});
// Stuff all this into the document
section.children.push(
new docx.Paragraph({
text: story.name,
heading: docx.HeadingLevel.HEADING_3
})
);
section.children.push(
new docx.Paragraph({
text: "(Auteur: " + story.creator.firstName + " " + story.creator.lastName + ")"
})
);
section.children.push(storyDoc);
section.children.push(new docx.Paragraph(""));
} else {
alert("La story '" + story.name + "' est incomplète et a donc été ignorée");
}
}
})
.catch(function (err) {
console.log(err);
});
}
spnLoading.classList.add("d-none");
})
.catch(function (erreur) {
alert(
"Erreur de connexion au serveur IceScrum\n\nSi internet est ok (=vous pouvez atteindre icescrum.cpnv.ch avec votre navigateur), alors c'est probablement que le code du projet est faux ou que votre token est pourri"
);
window.location = "index.html";
});
}
// Build the docx js content in the "Test Report" format, adds it to the section passed
async function buildTestReportContent(section) {
// Get all sprints
await fetch(API_project_url + "/sprint?icescrum-token=" + iceScrumToken)
.then(function (response) {
return response.json();
})
.then((data) => appendSprints(data, section))
.catch(function (erreur) {
alert(
"Erreur de connexion au serveur IceScrum\n\nSi internet est ok (=vous pouvez atteindre icescrum.cpnv.ch avec votre navigateur), alors c'est probablement que le code du projet est faux ou que votre token est pourri"
);
window.location = "index.html";
});
}
// Add each sprint as a Heading 3 to the section
async function appendSprints(sprints, section) {
for (sprint of sprints) {
section.children.push(
new docx.Paragraph({
text: "Sprint " + sprint.orderNumber,
heading: docx.HeadingLevel.HEADING_3
})
);
for (story_id of sprint.stories_ids) {
await appendTestsOfStory(story_id.id, section);
}
}
spnLoading.classList.add("d-none");
}
// Add the tests of one story to the section
async function appendTestsOfStory(storyid, section) {
await fetch(API_project_url + "/story/" + storyid + "?icescrum-token=" + iceScrumToken)
.then(function (response) {
return response.json();
})
.then(async function (story) {
section.children.push(
new docx.Paragraph({
text: story.name,
heading: docx.HeadingLevel.HEADING_4
})
);
// Create one table per story
let testRows = [];
await testsDoc(story.id, testRows, true); // prepare all tests as array of rows
section.children.push(
new docx.Table({
rows: testRows
})
);
})
.catch(function (err) {
console.log(err);
});
}
// Takes the tests of the given story and put them in an array of docx rows
// addResult is a bool that tells to add a cell with the result
async function testsDoc(storyid, rows, addResult) {
await new Promise((r) => setTimeout(r, 500)); // cooldown for IceScrum. Without it, the API starts throwing CORS exceptions after a dozen requests
await fetch(API_project_url + "/acceptanceTest/story/" + storyid + "?icescrum-token=" + iceScrumToken)
.then(function (response) {
return response.json();
})
.then(function (tests) {
for (test of tests) {
if (addResult) {
switch (test.state) {
case 10:
resultCell = new docx.TableCell({
children: [new docx.Paragraph("OK"), new docx.Paragraph(moment(test.lastUpdated).format("D MMM"))]
});
break;
case 5:
resultCell = new docx.TableCell({
children: [new docx.Paragraph("ko"), new docx.Paragraph(moment(test.lastUpdated).format("D MMM"))]
});
break;
default:
resultCell = new docx.TableCell({
children: [new docx.Paragraph("???")]
});
}
rows.push(
new docx.TableRow({
children: [
new docx.TableCell({
children: [new docx.Paragraph(test.name)]
}),
new docx.TableCell({
children: [new docx.Paragraph(test.description)]
}),
resultCell
]
})
);
} else {
if (test.description != null) {
rows.push(
new docx.TableRow({
children: [
new docx.TableCell({
children: [new docx.Paragraph(test.name)]
}),
new docx.TableCell({
children: [new docx.Paragraph(test.description)]
})
]
})
);
}
}
}
})
.catch(function (err) {
console.log(err);
});
}
</script>