Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 | 5x 5x 5x 5x 5x 8x 8x 8x 5x 1x 1x 1x 8x 27x 8x 5x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 5x 8x 8x 8x 8x 8x 7x 7x 8x 8x 8x 8x 7x 8x 8x 45x 20x 25x 25x 24x 24x 11x 13x 5x 25x 25x 46x 15x 8x 8x 8x 7x 7x 7x 8x 8x 8x 7x 7x 8x 14x 14x 33x 10x 23x 9x 33x 23x 23x 27x 162x 162x 27x 12x 27x 27x 12x 27x 27x 4x 27x 27x 22x 22x 27x 54x 27x 27x 8x 8x 14x 27x 13x 27x 27x 27x 27x 35x 5x 27x 27x 14x 8x 3x 5x 5x 11x 6x 5x 5x 5x 8x 8x 6x 6x 8x 7x 7x 7x 7x 23x 23x 16x 16x 7x 23x 23x 130x 108x 22x 130x 3x 127x 31x 96x 66x 30x 30x 18x 12x 96x 96x 96x 96x 21x 21x 96x 24x 24x 11x 11x 13x 3x 3x 96x 4x 4x 96x 23x 73x 3x 3x 70x 63x 23x 16x 7x 16x 23x 68x 61x 61x 7x 54x 54x 8x 27x 21x 27x 27x 13x 12x 1x 27x 27x 27x 5x 5x 27x 1x 27x 27x 27x 13x 13x 12x 1x 27x 1x 27x 27x 27x 21x 21x 21x 21x 21x 35x 21x 35x 35x 21x 21x 21x 20x | import { strict as assert } from 'assert'; import { ProjectPlan, RetrieveProjectPlanOptions, YouTrackConfig, YouTrackIssue, } from './api-types'; import { assignDefined, OnlyOptionals } from './util'; import { httpGet, httpGetAll, httpGetAllWithOptions } from './you-track-http'; import * as RestApi from './you-track-rest'; import { CustomFieldActivityItem } from './you-track-rest'; /** * Retrieves, reconstructs, and returns a project plan consisting of the issues and their activities in a * [YouTrack saved search](https://www.jetbrains.com/help/youtrack/standalone/Saved-Search.html). * * This method requires that {@link handlePotentialOauthRedirect}() was called previously and obtained an OAuth token. * The authorization should be valid for the expected duration of the data retrieval. YouTrack * [does not](https://www.jetbrains.com/help/youtrack/standalone/OAuth-Authorization.html#AuthServerResponse) issue * refresh tokens, so communication with YouTrack will fail otherwise. * * @param baseUrl The YouTrack base URL. See also {@link httpGet}(). * @param youTrackConfig Configuration of YouTrack. * @param options Options for retrieving YouTrack issue data and building a project plan. * @return A promise that in case of success will be fulfilled with a project plan. In case of any failure, it will be * rejected with a {@link Failure}. */ export function retrieveProjectPlan( baseUrl: string, youTrackConfig: YouTrackConfig, options?: RetrieveProjectPlanOptions): Promise<ProjectPlan> { const actualYouTrackConfig: Required<YouTrackConfig> = assignDefined(newDefaultYouTrackConfig(), youTrackConfig); const actualOptions: Required<RetrieveProjectPlanOptions> = assignDefined(newDefaultRequestProjectPlanOptions(), options); return new YouTrackIssueActivities(baseUrl, actualYouTrackConfig, actualOptions).promise; } /** * Retrieves and returns the number of minutes in a work week, as configured in YouTrack. * * @param baseUrl The YouTrack base URL. See also {@link httpGet}(). * @return A promise that in case of success will be fulfilled with a the number of minutes. In case of any failure, it * will be rejected with a {@link Failure}. */ export async function getMinutesPerWorkWeek(baseUrl: string): Promise<number> { const queryParams = { fields: YouTrackFields.WORK_TIME_SETTINGS, }; const response: RestApi.WorkTimeSettings = await httpGet<RestApi.WorkTimeSettings>(baseUrl, RestApi.youTrackPath.WORK_TIME_SETTINGS, queryParams); return response.daysAWeek * response.minutesADay; } /** * Returns a new object with values for the optional properties of {@link YouTrackConfig}. */ function newDefaultYouTrackConfig(): OnlyOptionals<YouTrackConfig> { return { remainingEffortFieldId: '', remainingWaitFieldId: '', assigneeFieldId: '', otherCustomFieldIds: [], doesInwardDependOnOutward: true, overlaySavedQueryId: '', minStateChangeDurationMs: 0, defaultRemainingEffortMs: 0, defaultWaitTimeMs: 0, isSplittableFn: () => false, }; } /** * Returns a new object with values for the optional properties of {@link RetrieveProjectPlanOptions}. */ function newDefaultRequestProjectPlanOptions(): OnlyOptionals<RetrieveProjectPlanOptions> { return { progressCallback: () => { /* no-op */ }, omitIssueActivities: false, progressUpdateIntervalMs: 200, restBatchSize: 100, }; } const YOUTRACK_CUSTOM_FIELDS_CATEGORY = 'CustomFieldCategory'; /** * YouTrack REST API fields. */ const enum YouTrackFields { SORTED_ISSUES = 'count', CUSTOM_FIELD = 'fieldDefaults(bundle(id))', STATE_BUNDLES = 'id,values(id,name,isResolved)', SAVED_QUERY_NAME = 'name', ISSUES = 'created,customFields(projectCustomField(field(id)),value(id,minutes,name)),id,idReadable,' + 'links(direction,issues(id,idReadable),linkType(id)),parent(issues(id,idReadable)),resolved,' + 'subtasks(issues(id,idReadable)),summary,updated', OVERLAY_ORDER = 'id', ACTIVITIES_PAGE = 'afterCursor,hasAfter,activities(added(id,name,isResolved),field(id),' + 'removed(id,name,isResolved),target(id),timestamp)', WORK_TIME_SETTINGS = 'minutesADay,daysAWeek', } const enum ActiveState { UNKNOWN, ACTIVE, INACTIVE, } interface StateTransition { timestamp: number; activeState: ActiveState; } interface IssueId { /** * The human readable ID, consisting of project abbreviation and number. */ id: string; /** * The YouTrack-internal ID. */ internalId: string; } interface InternalYouTrackIssue extends YouTrackIssue { internalId: string; parentIssueId: IssueId | null; subtaskIssueIds: IssueId[]; dependencyIssueIds: IssueId[]; activeState: ActiveState; stateTransitions: StateTransition[]; lastUpdate?: number; } /** * Class that retrieves issues, their state changes, and issue links. */ class YouTrackIssueActivities { public readonly promise: Promise<ProjectPlan>; private readonly baseUrl_: string; private readonly config_: Required<YouTrackConfig>; private readonly options_: Required<RetrieveProjectPlanOptions>; private readonly dependsOnDirection_: 'INWARD' | 'OUTWARD'; private readonly issues_: InternalYouTrackIssue[] = []; private readonly idToIssueMap_ = new Map<string, InternalYouTrackIssue>(); /** * Map from id of the issue state (that is, the ID of the {@link StateBundleElement}) to whether that state is active. * * This property is only filled in {@link buildIdToActiveStateMap}() if * {@link RetrieveProjectPlanOptions.omitIssueActivities} is false. */ private readonly idToActiveState_: Map<string, ActiveState>; private readonly lowerCaseNameToActiveState_: {[lowerCaseName: string]: ActiveState} = {}; private readonly projectPlan_: ProjectPlan; private numIssues_: number = 0; private savedQueryName_: string = ''; private numTasksWithDetails_: number = 0; /** * Timestamp of the first activity in the activity log. * * This property is only filled in {@link parseActivityItems}() if * {@link RetrieveProjectPlanOptions.omitIssueActivities} is false. */ private minTimeStamp_: number = 0; private maxUpdateTimeStamp_: number = 0; private maxActivityTimeStamp_: number = 0; private lastProgressUpdate_: number = 0; /** * Constructor that will already start the communication with the YouTrack server. */ constructor( baseUrl: string, youTrackConfig: Required<YouTrackConfig>, options: Required<RetrieveProjectPlanOptions>) { this.baseUrl_ = baseUrl; this.config_ = youTrackConfig; this.options_ = options; this.dependsOnDirection_ = this.config_.doesInwardDependOnOutward ? 'INWARD' : 'OUTWARD'; this.idToActiveState_ = youTrackConfig.inactiveStateIds .reduce((map, inactiveState) => map.set(inactiveState, ActiveState.INACTIVE), new Map<string, ActiveState>()); this.projectPlan_ = { issues: [], warnings: [], }; this.promise = this.run(); } private async run(): Promise<ProjectPlan> { await this.retrieveNumIssues(); await this.retrieveSavedQueryName(); if (!this.options_.omitIssueActivities) { const stateBundleId: string = await this.getStateBundleId(); await this.buildIdToActiveStateMap(stateBundleId); } this.progressUpdate(); await this.retrieveIssues(); await this.retrieveOverlayOrder(); if (!this.options_.omitIssueActivities) { await this.retrieveActivities(); } this.finalizeSchedule(); return this.projectPlan_; } /** * If sufficient time has elapsed since the last progress update, send a new message to the thread that spawned this * worker. */ private progressUpdate(): void { if (Date.now() - this.lastProgressUpdate_ < this.options_.progressUpdateIntervalMs) { return; } let percentageDone = 0; if (this.numIssues_ > 0) { percentageDone = 1 + 9 * Math.min(1, this.numTasksWithDetails_ / this.numIssues_); if (this.minTimeStamp_ > 0) { percentageDone += 90 * Math.min(1, (this.maxActivityTimeStamp_ - this.minTimeStamp_) / (this.maxUpdateTimeStamp_ - this.minTimeStamp_) ); } else if (this.options_.omitIssueActivities) { percentageDone *= 10; } } this.lastProgressUpdate_ = Date.now(); this.options_.progressCallback(percentageDone); } private newHttpRequest<T>(resourcePath: string, queryParams: {[param: string]: string}): Promise<T> { return httpGet(this.baseUrl_, resourcePath, queryParams); } private getAll<T>(resourcePath: string, queryParams: {[param: string]: string}, processBatch: (batch: T[]) => void): Promise<void> { return httpGetAllWithOptions(this.baseUrl_, resourcePath, queryParams, this.options_.restBatchSize, processBatch, undefined); } private async retrieveNumIssues(): Promise<void> { const queryParams = { folderId: this.config_.savedQueryId, fields: YouTrackFields.SORTED_ISSUES, }; const response: RestApi.SortedIssuesResponse = await this.newHttpRequest(RestApi.youTrackPath.SORTED_ISSUES, queryParams); this.numIssues_ = response.count; } private async getStateBundleId(): Promise<string> { const queryParams = {fields: YouTrackFields.CUSTOM_FIELD}; const customField: RestApi.CustomField = await this.newHttpRequest(RestApi.youTrackPath.CUSTOM_FIELD(this.config_.stateFieldId), queryParams); return (customField.fieldDefaults as RestApi.StateBundleCustomFieldDefaults).bundle.id; } private async retrieveSavedQueryName(): Promise<void> { const queryParams = {fields: YouTrackFields.SAVED_QUERY_NAME}; const savedQuery: RestApi.SavedQuery = await this.newHttpRequest(RestApi.youTrackPath.SAVED_QUERY(this.config_.savedQueryId), queryParams); this.savedQueryName_ = savedQuery.name; } private async buildIdToActiveStateMap(stateBundleId: string): Promise<void> { const queryParams: {fields: string} = { fields: YouTrackFields.STATE_BUNDLES, }; await this.getAll<RestApi.StateBundle>(RestApi.youTrackPath.STATE_BUNDLES, queryParams, (restStateBundles) => { for (const restStateBundle of restStateBundles) { const isRelevantStateBundle = restStateBundle.id === stateBundleId; for (const restElement of restStateBundle.values) { if (restElement.isResolved) { this.idToActiveState_.set(restElement.id, ActiveState.INACTIVE); } else if (isRelevantStateBundle && !this.config_.inactiveStateIds.includes(restElement.id)) { this.idToActiveState_.set(restElement.id, ActiveState.ACTIVE); } if (isRelevantStateBundle) { // In this case, restElement.id was added to this.idToActiveState_: Either (a) in the constructor (if // this.config_.inactiveStateIds.includes(restElement.id)) or (b) in one of the two if-branches above. assert(this.idToActiveState_.has(restElement.id)); this.lowerCaseNameToActiveState_[restElement.name.toLowerCase()] = this.idToActiveState_.get(restElement.id)!; } } } }); } private parseIssueCustomFields(issue: InternalYouTrackIssue, restIssue: RestApi.Issue): void { for (const customField of restIssue.customFields) { const fieldId = customField.projectCustomField.field.id; switch (fieldId) { case this.config_.remainingEffortFieldId: if (RestApi.isPeriodIssueCustomField(customField) && customField.value !== null) { issue.remainingEffortMs = customField.value.minutes * 60 * 1000; } break; case this.config_.remainingWaitFieldId: if (RestApi.isPeriodIssueCustomField(customField) && customField.value !== null) { issue.remainingWaitTimeMs = customField.value.minutes * 60 * 1000; } break; case this.config_.assigneeFieldId: if (RestApi.isSingleUserIssueCustomField(customField) && customField.value !== null) { issue.assignee = customField.value.id; } break; case this.config_.stateFieldId: if (RestApi.isStateIssueCustomField(customField) && customField.value !== null) { issue.state = customField.value.id; issue.activeState = this.stateBundleElementIdToActiveState(customField.value); } break; default: if (RestApi.isSingleEnumIssueCustomField(customField) && customField.value !== null && this.config_.otherCustomFieldIds.find((id) => id === fieldId)) { issue.customFields[fieldId] = customField.value.id; } } } } private async retrieveIssues(): Promise<void> { const queryParams = {fields: YouTrackFields.ISSUES}; await this.getAll<RestApi.Issue>( RestApi.youTrackPath.ISSUES(this.config_.savedQueryId), queryParams, (restIssues) => { for (const restIssue of restIssues) { const issue: InternalYouTrackIssue = { id: restIssue.idReadable, remainingEffortMs: this.config_.defaultRemainingEffortMs, remainingWaitTimeMs: this.config_.defaultWaitTimeMs, splittable: false, dependencies: [], assignee: '', summary: restIssue.summary, issueActivities: [], resolved: restIssue.resolved !== null ? restIssue.resolved : Number.MAX_SAFE_INTEGER, state: '', parent: '', customFields: {}, internalId: restIssue.id, parentIssueId: restIssue.parent.issues.length > 0 ? { id: restIssue.parent.issues[0].idReadable, internalId: restIssue.parent.issues[0].id, } : null, subtaskIssueIds: restIssue.subtasks.issues.map((subissue) => ({ id: subissue.idReadable, internalId: subissue.id, })), dependencyIssueIds: [], activeState: ActiveState.UNKNOWN, stateTransitions: [{timestamp: restIssue.created, activeState: ActiveState.UNKNOWN}], lastUpdate: restIssue.updated, }; this.issues_.push(issue); this.idToIssueMap_.set(restIssue.id, issue); this.parseIssueCustomFields(issue, restIssue); for (const restLink of restIssue.links) { if (restLink.direction === this.dependsOnDirection_ && restLink.linkType.id === this.config_.dependsLinkTypeId) { issue.dependencyIssueIds.push(...restLink.issues.map((linkedIssue) => ({ id: linkedIssue.idReadable, internalId: linkedIssue.id, }))); } } ++this.numTasksWithDetails_; this.maxUpdateTimeStamp_ = Math.max(this.maxUpdateTimeStamp_, restIssue.updated); } this.progressUpdate(); } ); } private async retrieveOverlayOrder(): Promise<void> { if (this.config_.overlaySavedQueryId.length === 0) { return; } const queryParams = {fields: YouTrackFields.OVERLAY_ORDER}; type IdIssue = Pick<RestApi.Issue, 'id'>; const issues = await httpGetAll<IdIssue>(this.baseUrl_, RestApi.youTrackPath.ISSUES(this.config_.overlaySavedQueryId), queryParams, this.options_.restBatchSize); const overlayIds = issues.map((issue) => issue.id).filter((id) => this.idToIssueMap_.has(id)); const idToOverlayIdx = overlayIds.reduce((map, id, idx) => map.set(id, idx), new Map<string, number>()); let issueIdx = 0; let overlayIdx = 0; while (issueIdx < this.issues_.length && overlayIdx < overlayIds.length) { const issue: InternalYouTrackIssue = this.issues_[issueIdx]; if (idToOverlayIdx.has(issue.internalId)) { this.issues_[issueIdx] = this.idToIssueMap_.get(overlayIds[overlayIdx])!; ++overlayIdx; } ++issueIdx; } } private async retrieveActivities(): Promise<void> { const queryParams: {[param: string]: string} = { fields: YouTrackFields.ACTIVITIES_PAGE, categories: YOUTRACK_CUSTOM_FIELDS_CATEGORY, issueQuery: `saved search: {${this.savedQueryName_}}`, $top: this.options_.restBatchSize.toString(), }; let busy = true; let promise = this.newHttpRequest<RestApi.ActivityCursorPage>(RestApi.youTrackPath.ACTIVITIES_PAGE, queryParams); do { const activitiesPage = await promise; if (activitiesPage.hasAfter) { queryParams.cursor = activitiesPage.afterCursor!; promise = this.newHttpRequest<RestApi.ActivityCursorPage>(RestApi.youTrackPath.ACTIVITIES_PAGE, queryParams); } else { busy = false; } // The type-cast is fine because we set queryParams.categories accordingly. this.parseActivityItems(activitiesPage.activities as CustomFieldActivityItem[]); this.progressUpdate(); } while (busy); } private stateBundleElementIdToActiveState( arrayOrObject?: RestApi.StateBundleElement | RestApi.StateBundleElement[]): ActiveState { let object: RestApi.StateBundleElement | undefined; if (arrayOrObject instanceof Array) { object = arrayOrObject.length > 0 ? arrayOrObject[0] : undefined; } else { object = arrayOrObject; } if (object === undefined) { return ActiveState.UNKNOWN; } if (object.isResolved) { return ActiveState.INACTIVE; } else if (this.idToActiveState_.has(object.id)) { return this.idToActiveState_.get(object.id)!; } const lowerCaseName = object.name.toLowerCase(); if (lowerCaseName in this.lowerCaseNameToActiveState_) { return this.lowerCaseNameToActiveState_[lowerCaseName]; } return ActiveState.UNKNOWN; } private addStateTransition(stateTransitions: StateTransition[], newStateTransition: StateTransition, replacementForPreviousUnknown: ActiveState): void { const a = stateTransitions; let n = a.length; const c = this.config_.minStateChangeDurationMs; // By definition, a is the array [a[0], ..., a[n - 1]]. With the abbreviating notation a[i].s ∈ {A,I,U} and // a[i].t (instead of a[i].activeState and a[i].timestamp, respectively), if the following invariants hold here // then they also hold when the function returns. // (And the fact that the invariant holds the first time this method is called follows from method retrieveIssues(). // There, the array a was initialized with exactly one element such that a[0].s = U.) // Throughout the explanatory comments, let n denote the length of a (even at the end of the function where the // source code does not reassign n). // // 1. a[0].s, ..., a[n - 2].s ∈ {A,I}. // 2. a[i - 1].s != a[i].s for all i = 1, ..., n - 1 // 3. n = 1 => (a[0].s ∈ {A,U} and n > 1 => a[0].s = A) // 4. a[i - 1].t < a[i].t for all i = 1, ..., n - 1 // 5. a[i].t - a[i - 1].t >= c for all i = 1, ..., n - 3 // 6. a[n - 2].t - a[n - 3].t < c => (a[n - 1].t - a[n - 2].t < c and a[n - 2].s = I and a[n - 1].s = U) // 7. a[n - 1].t - a[n - 2].t < c => a[n - 1].s ∈ {I,U} // In the following, we first prepare a so that newStateTransition could be added without violating the // invariants. Let s ∈ {A,I,U} denote the new state and t the new timestamp. That is, // s = newStateTransition.activeState and t = newStateTransition.timestamp. // The behavior of this function satisfies (with a and n denoting the respective values upon return, and a' and n' // their current values, respectively): // I. a[i].t - a[i-1].t < c => t - a[i].t < c // II. (a'[n - 1].s ∈ {A,I} and s != a'[n - 1].s and t >= a'[n - 1].t + c) // => (a' = a[0 .. n - 2] and a[n - 1] = (t, s)) // Goal: Preserve invariant 4. while (n > 0 && a[n - 1].timestamp >= newStateTransition.timestamp) { a.pop(); --n; } // (Condition POST-WHILE:) We now have a[n - 1].t <= t. Also, all invariants hold. // Goal: Preserve invariant 1. if (n > 0 && a[n - 1].activeState === ActiveState.UNKNOWN) { a[n - 1].activeState = replacementForPreviousUnknown !== ActiveState.UNKNOWN ? replacementForPreviousUnknown : ActiveState.ACTIVE; // We now have (stronger than invariant 1!): a[0].s, ..., a[n - 1].s ∈ {A,I}. // // Goals: // - Restore invariant 3 (if-condition before '||') // - Restore invariant 2 (if-condition after '||') // - Preserve invariant 5,6,7 (else-if-condition) if ((n === 1 && a[0].activeState === ActiveState.INACTIVE) || (n > 1 && a[n - 1].activeState === a[n - 2].activeState)) { a.pop(); --n; } else if (n > 2 && a[n - 2].activeState === ActiveState.INACTIVE && a[n - 2].timestamp - a[n - 3].timestamp < c) { // Note we have here: a[n - 1].s = A. a.length -= 2; n -= 2; // We now have a[i].t - a[i - 1].t >= c for all i. Also, a[n - 1].s = A. } } // (Condition NOUNKNOWN:) All invariants hold, and additionally a[n - 1].s ∈ {A,I} and a[i].t - a[i - 1].t >= c // for i = 1, ..., n - 2. // Goal: Preserve invariants 5,6,7. if (n > 1 && a[n - 1].activeState === ActiveState.INACTIVE && a[n - 1].timestamp - a[n - 2].timestamp < c && newStateTransition.timestamp - a[n - 1].timestamp >= c) { a.length -= 2; n -= 2; } // (Condition GAPS1:) All invariants hold. It also holds now that (a[n - 1].t - a[n - 2].t < c) implies that the // previous if-block was not entered (because 2 elements were removed from the list and therefore // condition NOUNKNOWN would imply a[n - 1].t - a[n - 2].c >= c). Therefore one of the following holds: // (1.) a[n - 1].s ∈ {A,U}, which together with condition NOUNKNOWN implies a[n - 1].s = A, or // (2.) t - a[n - 1].t < c. // Goal: Preserve invariant 2. if (n > 0 && newStateTransition.activeState === a[n - 1].activeState) { // All invariants still hold (due to condition GAPS1). return; } // (Condition ALT:) All invariants hold. We also have now s != a[n - 1].s. // Goal: Preserve invariants 5,6,7. if (n > 1 && newStateTransition.activeState === ActiveState.ACTIVE && newStateTransition.timestamp - a[n - 1].timestamp < c) { a.pop(); // Since n > 1, we have (due to invariant 3) that a[0].s = A. Therefore, invariant 3 remains // valid. Other invariants trivially hold, because the last line cannot have invalidated them. return; } // (Condition GAPS2:) All invariants hold. We also have now that n > 1 implies that (1.) s ∈ {I,U} or // (2.) t - a[n - 1] >= c. // Goal: Preserve invariant 3. if (n > 0 || newStateTransition.activeState !== ActiveState.INACTIVE) { a.push(newStateTransition); // Let n = a.length (which changed in the last line). // - Invariant 1 continues to hold because of condition NOUNKNOWN. // - Invariant 2 continues to hold because of condition ALT. // - Regarding invariant 3, if n = 1, then a[0] = s ∈ {A,U} because of the if-condition. If n = 2, then // a[0].s ∈ {A,U} due to invariant 3 holding before, and therefore a[0].s = A due to condition NOUNKNOWN. // If n > 2, then a[0].s = A because invariant 3 held before the last line. // - Invariant 4 holds because a[n - 1].t <= t (due to condition POST-WHILE). // - Invariant 5 holds because of condition NOUNKNOWN. // - Regarding invariant 6, only the case n > 3 is meaningful (i.e., not a vacuous truth). Suppose that // a[n - 2].t - a[n - 3].t < c. Since invariant 7 held before, we know a[n - 2].s ∈ {I,U}. Together with // condition NOUNKNOWN, this implies a[n - 2].s = I. Condition GAPS2 implies a[n - 1].s = s ∈ {I,U}, and // condition ALT strengthens this to a[n - 1].s = U. Finally, due to condition GAPS1 (note that // a[n - 2].s ∉ {A,U}), we have a[n - 1].t - a[n - 2].t < c. // - Regarding invariant 7, suppose a[n - 1].t - a[n - 2].t < c. Then s ∈ {I,U} due to condition GAPS2. } } private parseActivityItems(activityItems: RestApi.CustomFieldActivityItem[]): void { if (activityItems.length > 0) { if (this.minTimeStamp_ === 0) { this.minTimeStamp_ = activityItems[0].timestamp; } this.maxActivityTimeStamp_ = Math.max(this.maxActivityTimeStamp_, activityItems[activityItems.length - 1].timestamp); } for (const activityItem of activityItems) { if (activityItem.field.id === this.config_.stateFieldId) { // In theory, multiple users can own saved queries with the same name, in which case we may see more issues here // than we need to. const issue = this.idToIssueMap_.get(activityItem.target.id); if (issue === undefined) { continue; } const newStateTransition = { timestamp: activityItem.timestamp, activeState: this.stateBundleElementIdToActiveState(activityItem.added as RestApi.StateBundleElement[]), }; this.addStateTransition(issue.stateTransitions, newStateTransition, this.stateBundleElementIdToActiveState(activityItem.removed as RestApi.StateBundleElement[])); } } } private finalizeSchedule(): void { for (const issue of this.issues_) { if (!this.options_.omitIssueActivities) { this.finalizeIssueActivities(issue); } let parent: string = ''; if (issue.parentIssueId !== null) { if (this.idToIssueMap_.has(issue.parentIssueId.internalId)) { parent = issue.parentIssueId.id; } else { this.projectPlan_.warnings.push({ description: `Issue ${issue.id} is a subtask of ${issue.parentIssueId.id}, which is not contained ` + `in saved search “${this.savedQueryName_}”.`, issueId: issue.id, }); } } const dependencies: string[] = []; const unknownDependencies: string[] = []; for (const issueId of issue.dependencyIssueIds) { const pushTo = this.idToIssueMap_.has(issueId.internalId) ? dependencies : unknownDependencies; pushTo.push(issueId.id); } if (unknownDependencies.length > 0) { this.projectPlan_.warnings.push({ description: `Issue ${issue.id} depends on ${unknownDependencies.join(', ')}, which is/are not ` + `contained in saved search “${this.savedQueryName_}”.`, issueId: issue.id, }); } let remainingEffortMs = issue.resolved === Number.MAX_SAFE_INTEGER ? issue.remainingEffortMs : 0; const unknownSubtasks: string[] = []; for (const issueId of issue.subtaskIssueIds) { const subIssue: InternalYouTrackIssue | undefined = this.idToIssueMap_.get(issueId.internalId); if (subIssue !== undefined) { remainingEffortMs = Math.max(0, remainingEffortMs - subIssue.remainingEffortMs); } else { unknownSubtasks.push(issueId.id); } } if (unknownSubtasks.length > 0) { this.projectPlan_.warnings.push({ description: `Issue ${issue.id} is a parent for ${unknownSubtasks.join(', ')}, which is/are not ` + `contained in saved search “${this.savedQueryName_}”.`, issueId: issue.id, }); } const youTrackIssue: YouTrackIssue = { id: issue.id, summary: issue.summary, issueActivities: issue.issueActivities, resolved: issue.resolved, state: issue.state, assignee: issue.assignee, parent, customFields: issue.customFields, remainingEffortMs, remainingWaitTimeMs: issue.resolved === Number.MAX_SAFE_INTEGER ? issue.remainingWaitTimeMs : 0, splittable: issue.splittable, dependencies, }; youTrackIssue.splittable = this.config_.isSplittableFn(youTrackIssue); this.projectPlan_.issues.push(youTrackIssue); } } private finalizeIssueActivities(issue: InternalYouTrackIssue): void { // The following 3 issue properties have been set before in finishedIssues(). const newStateTransition: StateTransition = { timestamp: issue.lastUpdate!, activeState: issue.activeState, }; this.addStateTransition(issue.stateTransitions, newStateTransition, issue.activeState); // Using the definitions in addStateTransition(), we could at this point still have a[n - 1].s = U or // a[n - 1].t - a[n - 2].t < c. We therefore add the following final transition for "clean up". // Note that we err on the side of interpreting state UNKNOWN as ACTIVE. const finalStateTransition: StateTransition = { timestamp: Number.MAX_SAFE_INTEGER, activeState: issue.activeState === ActiveState.UNKNOWN ? ActiveState.ACTIVE : issue.activeState, }; this.addStateTransition(issue.stateTransitions, finalStateTransition, ActiveState.ACTIVE); assert( issue.stateTransitions .filter((stateTransition) => stateTransition.activeState === ActiveState.UNKNOWN).length === 0, 'issue.stateTransitions should no longer contain elements with activeState === ActiveState.UNKNOWN' ); assert( issue.stateTransitions .map((stateTransition) => stateTransition.timestamp) .reduce( ([min, previousTimestamp], timestamp) => [Math.min(min, timestamp - previousTimestamp), timestamp], [Number.MAX_SAFE_INTEGER, -this.config_.minStateChangeDurationMs] )[0] >= this.config_.minStateChangeDurationMs, 'issue.stateTransitions should no longer have consecutive elements within less than minStateChangeDurationMs' ); const a = issue.stateTransitions; const n = a.length; for (let i = 0; i < n; i += 2) { issue.issueActivities.push({ assignee: issue.assignee, start: a[i].timestamp, end: i < n - 1 ? a[i + 1].timestamp : Number.MAX_SAFE_INTEGER, isWaiting: false, }); } } } |