-
Notifications
You must be signed in to change notification settings - Fork 1
/
schedule-prepare-issues.js
421 lines (339 loc) · 12.1 KB
/
schedule-prepare-issues.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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
import {
makeObjectMapByKey,
makeFilterByPropertyNotEqualToOneOfValues,
groupByKey,
splitByHavingPropertyValue,
partition,
stringToArray
} from "./helpers.js";
import {estimateExtraPoints, sampleExtraPoints} from "./shared/confidence.js";
import {WorkPlans} from "./work-plans.js";
import { defineFeatureFlag } from "./feature-flag.js";
const useParentBlockers = defineFeatureFlag("useParentBlockers",`
Turns on an awareness of epic parent's blockers.
For example, lets say Initiatives contain Epics.
If Initiative A blocks Initaitive B, then every epic in Initiative A will block
ever epic in Initiative B.
This creates a lof of dependency blockers in the tool.
`, false)
export function prepareIssues(issuesSource, {
// Decides what the project key is from an issue
getTeamKey = getTeamKeyDefault,
// returns the number of work days per sprint
getDaysPerSprint = (teamKey) => 10,
// Returns a team's velocity
getVelocity = (key) => 21,
// Returns a confidence for a particular issue (from 0-100)
getConfidence = getConfidenceDefault,
// Returns an estimate of the amount of work
getEstimate = getEstimateDefault,
issueFilter = issueFilterDefault,
getParentKey = (issue) => issue["Custom field (Epic Link)"],
// returns an array of keys that the issue blocks
getBlockingKeys = (issue) => stringToArray(issue.linkedIssues.blocks) || [],
// Called back when an issue isn't used
onIgnoredIssues = (issues, reason) => {},
uncertaintyWeight = 100,
// True will randomly select an issue timing based on the probability distribution
// False will always use the same extra timing. There is no reason to do a montecarlo with this configuration.
probablisticallySelectIssueTiming = true,
getParallelWorkLimit = (teamKey) => {
return 1;
}
}) {
window.issuesSource = issuesSource;
// Copy issues
const issues = issuesSource.map( issue => {
return {...issue}
});
// Remove issues that we don't care about (done issues and epics without parents)
const {truthy: interestingIssues, falsy: filteredOut} = partition( issues, issueFilterDefault);
onIgnoredIssues(filteredOut, "filtered-out");
const issueByKey = makeObjectMapByKey(interestingIssues, "Issue key");
// Group issues by type
const issuesByType = Object.groupBy(interestingIssues, issue => issue["Issue Type"].name)
const interestingEpics = issuesByType.Epic || [];
const issuesByTeam = groupByKey(interestingIssues, getTeamKey);
var projectIds = Object.keys(issuesByTeam);
const workByTeams = makeObjectMapByKey(projectIds.map( (teamKey, i) => {
return {
teamKey,
workPlans: new WorkPlans(getParallelWorkLimit(teamKey)),
velocity: getVelocity(teamKey)
}
}), "teamKey" );
// Warning: Mutations of data start here!
// Make parent-child relationship
associateParentAndChildren(interestingIssues, getParentKey, issueByKey);
// add timing to epics
interestingEpics.forEach( issue => {
// mutation!
issue.work = createWork(issue,
workByTeams,
{getTeamKey, getConfidence, getEstimate, uncertaintyWeight, getDaysPerSprint, getParallelWorkLimit, probablisticallySelectIssueTiming});
});
// start building the block tree of direct locks
//directLinkBlocks(interestingIssues, issueByKey, getBlockingKeys);
// allow a parent to block other parents with children
if(useParentBlockers()) {
linkIndirectBlocks(interestingEpics, issueByKey, getBlockingKeys);
} else {
linkDirectBlocks(interestingEpics, issueByKey, getBlockingKeys);
}
checkForCycles(interestingEpics);
return {preparedIssues: interestingEpics, issuesByKey: issueByKey, workByTeams};
}
const removeDone = makeFilterByPropertyNotEqualToOneOfValues("Status", ["Done"]);
function issueFilterDefault(issue){
return removeDone(issue);
}
function getDaysOfWork(usedEstimate, extraPoints, pointsPerDay){
return Math.max( Math.round( (usedEstimate + extraPoints) / pointsPerDay), 1)
}
export function isConfidenceValid(value){
return value && value > 0 && value <=100;
}
function createWork(issue, workByTeams,
{
getTeamKey,
getConfidence,
getEstimate,
uncertaintyWeight,
getDaysPerSprint,
getParallelWorkLimit,
probablisticallySelectIssueTiming
}) {
if(issue.work) {
return issue.work;
}
var teamKey = getTeamKey(issue);
var team = workByTeams[teamKey];
var confidence = getConfidence(issue);
var estimate = getEstimate(issue);
if(!confidence || !estimate) {
//debugger; Template Void, Refund features beyond Store 1
}
var canEstimate = isConfidenceValid( confidence ) && estimate !== undefined;
var pointsPerDay = team.velocity / getDaysPerSprint(teamKey) / getParallelWorkLimit(teamKey);
const usedEstimate = (estimate != undefined ? estimate : team.velocity );
const usedConfidence = ( isConfidenceValid(confidence) ? confidence : 50 );
var estimatedDaysOfWork = Math.max( Math.round( (usedEstimate) / pointsPerDay), 1);
const extraPoints = probablisticallySelectIssueTiming ?
sampleExtraPoints(usedEstimate, usedConfidence):
estimateExtraPoints(usedEstimate, usedConfidence, uncertaintyWeight);
var daysOfWork = getDaysOfWork(usedEstimate, extraPoints, pointsPerDay);
// We always want deterministic days of work, this makes controling for the order easier
let deterministicDaysOfWork;
if(probablisticallySelectIssueTiming) {
deterministicDaysOfWork = getDaysOfWork(usedEstimate,
estimateExtraPoints(usedEstimate, usedConfidence, uncertaintyWeight),
pointsPerDay);
} else {
deterministicDaysOfWork = daysOfWork;
}
var work = {
isDefaultValue: !canEstimate,
confidence,
estimate,
usedEstimate,
usedConfidence,
extraPoints,
extraDays: daysOfWork - estimatedDaysOfWork,
estimatedDaysOfWork,
daysOfWork: daysOfWork,
deterministicDaysOfWork,
deterministicExtraDays: deterministicDaysOfWork - estimatedDaysOfWork,
issue: issue,
team: team,
isScheduled: false,
pointsPerDay
}
return work;
}
function getParentBlocks(issueWithDirectBlocks){
let parent;
allBlocks = [];
while(parent = issue._parent) {
const children = getNestedEpics(parent.blocks);
allBlocks.push(...children);
}
return [...new Set(allBlocks)];
}
function getNestedEpics(blockedIssues){
const blockedEpics = [];
for( let blockedIssue of blockedIssues) {
if(blockedIssue["Issue Type"].name === "Epic") {
blockedEpics.push(blockedIssue);
}
else if(blockedIssue._children) {
blockedEpics.push(...getNestedEpics(blockedIssue._children))
}
}
return blockedEpics;
}
function makeGetDirectBlocks(issueByKey, getBlockingKeys){
return function(issue){
return (stringToArray( getBlockingKeys(issue) || [])).map( (blockKey)=> {
return issueByKey[blockKey];
}).filter( x=> x);
}
}
// hierarchyLevel = 1 for epic
function getIndirectAndDirectBlocks(issue, getDirectBlocks){
const allBlocks = [];
const directBlocks = getDirectBlocks(issue);
const directHierarchyOneBlocks = getHierarchyLevelChildren(directBlocks, 1);
allBlocks.push(...directHierarchyOneBlocks);
let parent = issue;
while(parent = parent._parent) {
let parentDirectBlocks = getDirectBlocks(parent);
const parentDirectHierarchyOneBlocks = getHierarchyLevelChildren(parentDirectBlocks, 1);
allBlocks.push(...parentDirectHierarchyOneBlocks);
}
return allBlocks;
}
function getHierarchyLevelChildren(issues, level) {
const children = [];
for( let issue of issues) {
if(issue["Issue Type"].hierarchyLevel === level) {
children.push(issue);
}
else if(issue._children && issue["Issue Type"].hierarchyLevel > level) {
children.push(...getHierarchyLevelChildren(issue._children, level));
}
}
return children;
}
function linkDirectBlocks(issues, issueByKey, getBlockingKeys){
issues.forEach((issue)=> {
const issueBlocks = (stringToArray( getBlockingKeys(issue) || [])).map( (blockKey)=> {
const blocked = issueByKey[blockKey];
if(blocked && blocked["Issue Type"].name !== issue["Issue Type"].name) {
console.log(issue["Issue Type"].name, issue.Summary,"is blocking", blocked["Issue Type"].name, blocked.Summary, ". This is ignored");
return undefined;
}
return blocked
}).filter( x=> x);
issue.blocks = issueBlocks;
issue.blocks.forEach( (blocker)=> {
if(!blocker.blockedBy){
blocker.blockedBy = [];
}
blocker.blockedBy.push(issue);
})
});
}
function linkIndirectBlocks(issues, issueByKey, getBlockingKeys) {
const getDirectBlocks = makeGetDirectBlocks(issueByKey, getBlockingKeys);
issues.forEach((issue)=> {
const allBlocks = getIndirectAndDirectBlocks(issue, getDirectBlocks);
issue.blocks = allBlocks;
issue.blocks.forEach( (blocker)=> {
if(!blocker.blockedBy){
blocker.blockedBy = [];
}
blocker.blockedBy.push(issue);
})
});
}
function checkForCycles(issues) {
issues.forEach((issue) => {
// collect all the recursive blocks, make sure it doesn't block itself ...
checkCycle([issue]);
})
}
function checkCycle(issuePath) {
var last = issuePath[issuePath.length-1];
var issuePathSet = new Set(issuePath);
last.blocks.forEach( (blocked)=> {
if(issuePathSet.has(blocked)) {
console.log(issuePath, blocked);
throw "There is a cycle of dependencies. Aborting."
} else {
checkCycle([...issuePath, blocked]);
}
})
}
// NOT NEEDED
function blockedByDepth(issue) {
if(issue.blockedByDepth !== undefined) {
return issue.blockedByDepth;
}
if(!issue.blockedBy || !issue.blockedBy.length) {
return issue.blockedByDepth = 0;
} else {
return issue.blockedByDepth = issue.blockedBy.reduce( (sum, issue)=> {
return sum + blockedByDepth(issue);
},1)
}
}
// NOT NEEDED
function blocksDepth(issue) {
if(issue.blocksDepth !== undefined) {
return issue.blocksDepth;
}
if(!issue.blocks || !issue.blocks.length) {
return issue.blocksDepth = 0;
} else {
return issue.blocksDepth = issue.blocks.reduce( (sum, issue)=> {
return sum + blocksDepth(issue);
},1)
}
}
function associateParentAndChildren(issues, getParentKey, issuesByKey) {
const issuesByParentKey = groupByKey(issues,getParentKey);
for(let parentKey in issuesByParentKey) {
if(parentKey) {
const issue = issuesByKey[parentKey];
const children = issuesByParentKey[parentKey];
if(issue) {
issue._children = children;
children.forEach( child => child._parent = issue );
} else {
//console.log("Unable to find epic", epicKey, "perhaps it is marked as done but has an issue not done");
}
}
}
}
// WEIRD STUFF
function getTeamKeyDefault(issue) {
var matches = issue["Summary"].match( /M\d: ([^:]+): / )
if(matches) {
return issue["Project key"]+":"+matches[1]
} else {
return issue["Project key"];
}
}
function getConfidenceDefault(issue) {
const outerValue = issue["Custom field (Confidence)"];
if(Array.isArray(outerValue)) {
for(let rawValue of outerValue) {
let converted = parseInt(rawValue, 10);
if(converted !== 0 && converted !== "") {
return val;
}
}
} else if(val !== undefined) {
var val = parseInt(outerValue, 10);
if(val !== 0) {
return val;
}
}
return undefined;
}
export function getEstimateDefault(issue) {
var rawValue;
if(issue["Custom field (Story Points)"]){
rawValue = parseInt(issue["Custom field (Story Points)"], 10);
}
if( issue["Custom field (Story point estimate)"] ) {
for( let value of issue["Custom field (Story point estimate)"] ){
if(value) {
rawValue = parseInt(value, 10);
}
}
}
if(rawValue !== undefined && rawValue !== 0 && rawValue !== "") {
return rawValue;
}
}