|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See LICENSE in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import { GitHub, Issue } from '../api/api' |
| 7 | +import { ActionBase } from '../common/ActionBase' |
| 8 | + |
| 9 | +export class Reopener extends ActionBase { |
| 10 | + constructor( |
| 11 | + private github: GitHub, |
| 12 | + private alsoApplyToOpenIssues: boolean, |
| 13 | + private addLabels?: string, |
| 14 | + private removeLabels?: string, |
| 15 | + private reopenComment?: string, |
| 16 | + private setMilestoneId?: string, |
| 17 | + labels?: string, |
| 18 | + milestoneName?: string, |
| 19 | + milestoneId?: string, |
| 20 | + ignoreLabels?: string, |
| 21 | + ignoreMilestoneNames?: string, |
| 22 | + ignoreMilestoneIds?: string, |
| 23 | + minimumVotes?: number, |
| 24 | + maximumVotes?: number |
| 25 | + ) |
| 26 | + { |
| 27 | + super(labels, milestoneName, milestoneId, ignoreLabels, ignoreMilestoneNames, ignoreMilestoneIds, minimumVotes, maximumVotes); |
| 28 | + } |
| 29 | + |
| 30 | + async run() { |
| 31 | + const addLabelsSet = this.addLabels ? this.addLabels.split(',') : []; |
| 32 | + const removeLabelsSet = this.removeLabels ? this.removeLabels.split(',') : []; |
| 33 | + |
| 34 | + console.log(`alsoApplyToOpenIssues: ${this.alsoApplyToOpenIssues}`); |
| 35 | + |
| 36 | + const query = this.buildQuery((this.alsoApplyToOpenIssues ? "": "is:closed ") + "is:unlocked"); |
| 37 | + |
| 38 | + for await (const page of this.github.query({ q: query })) { |
| 39 | + await Promise.all( |
| 40 | + page.map(async (issue) => { |
| 41 | + const hydrated = await issue.getIssue() |
| 42 | + if (!hydrated.locked && (this.alsoApplyToOpenIssues || hydrated.open === false) && this.validateIssue(hydrated) |
| 43 | + // TODO: Verify closed and updated timestamps |
| 44 | + ) { |
| 45 | + if (hydrated.open === false) { |
| 46 | + console.log(`Reopening issue ${hydrated.number}`) |
| 47 | + await issue.reopenIssue() |
| 48 | + } |
| 49 | + if (this.setMilestoneId != undefined) { |
| 50 | + console.log(`Setting milestone of issue ${hydrated.number} to id ${+this.setMilestoneId}`) |
| 51 | + await issue.setMilestone(+this.setMilestoneId) |
| 52 | + } |
| 53 | + if (removeLabelsSet.length > 0) { |
| 54 | + for (const removeLabel of removeLabelsSet) { |
| 55 | + if (removeLabel && removeLabel.length > 0) { |
| 56 | + console.log(`Removing label on issue ${hydrated.number}: ${removeLabel}`) |
| 57 | + await issue.removeLabel(removeLabel) |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + if (addLabelsSet.length > 0) { |
| 62 | + for (const addLabel of addLabelsSet) { |
| 63 | + if (addLabel && addLabel.length > 0) { |
| 64 | + console.log(`Adding label on issue ${hydrated.number}: ${addLabel}`) |
| 65 | + await issue.addLabel(addLabel) |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + if (this.reopenComment) { |
| 70 | + console.log(`Posting comment to issue ${hydrated.number}.`) |
| 71 | + await issue.postComment(this.reopenComment) |
| 72 | + } |
| 73 | + } else { |
| 74 | + if (hydrated.locked) { |
| 75 | + console.log(`Issue ${hydrated.number} is locked. Ignoring`) |
| 76 | + } else if (!this.alsoApplyToOpenIssues && hydrated.open) { |
| 77 | + console.log(`Issue ${hydrated.number} is open. Ignoring`) |
| 78 | + } |
| 79 | + } |
| 80 | + }), |
| 81 | + ) |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments