|
| 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 } from '../api/api'; |
| 7 | +import { ActionBase } from '../common/ActionBase'; |
| 8 | +import { daysAgoToHumanReadbleDate, daysAgoToTimestamp, safeLog } from '../common/utils'; |
| 9 | + |
| 10 | +export class AddComment extends ActionBase { |
| 11 | + constructor( |
| 12 | + private github: GitHub, |
| 13 | + private createdAfter: string, |
| 14 | + private afterDays: number, |
| 15 | + labels: string, |
| 16 | + private addComment: string, |
| 17 | + private addLabels?: string, |
| 18 | + private removeLabels?: string, |
| 19 | + private setMilestoneId?: string, |
| 20 | + milestoneName?: string, |
| 21 | + milestoneId?: string, |
| 22 | + ignoreLabels?: string, |
| 23 | + ignoreMilestoneNames?: string, |
| 24 | + ignoreMilestoneIds?: string, |
| 25 | + minimumVotes?: number, |
| 26 | + maximumVotes?: number, |
| 27 | + involves?: string |
| 28 | + ) { |
| 29 | + super(labels, milestoneName, milestoneId, ignoreLabels, ignoreMilestoneNames, ignoreMilestoneIds, minimumVotes, maximumVotes, involves); |
| 30 | + } |
| 31 | + |
| 32 | + async run() { |
| 33 | + const updatedTimestamp = this.afterDays ? daysAgoToHumanReadbleDate(this.afterDays) : undefined; |
| 34 | + const query = this.buildQuery( |
| 35 | + (updatedTimestamp ? `updated:<${updatedTimestamp} ` : "") + |
| 36 | + (this.createdAfter ? `created:>${this.createdAfter} ` : "") + |
| 37 | + "is:open is:unlocked"); |
| 38 | + |
| 39 | + const addLabelsSet = this.addLabels ? this.addLabels.split(',') : []; |
| 40 | + const removeLabelsSet = this.removeLabels ? this.removeLabels.split(',') : []; |
| 41 | + |
| 42 | + for await (const page of this.github.query({ q: query })) { |
| 43 | + for (const issue of page) { |
| 44 | + const hydrated = await issue.getIssue(); |
| 45 | + if (hydrated.open && this.validateIssue(hydrated) |
| 46 | + // TODO: Verify updated timestamp |
| 47 | + ) { |
| 48 | + if (this.addComment) { |
| 49 | + safeLog(`Posting comment on issue ${hydrated.number}`); |
| 50 | + await issue.postComment(this.addComment); |
| 51 | + } |
| 52 | + if (removeLabelsSet.length > 0) { |
| 53 | + for (const removeLabel of removeLabelsSet) { |
| 54 | + if (removeLabel && removeLabel.length > 0) { |
| 55 | + safeLog(`Removing label on issue ${hydrated.number}: ${removeLabel}`); |
| 56 | + await issue.removeLabel(removeLabel); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + if (addLabelsSet.length > 0) { |
| 61 | + for (const addLabel of addLabelsSet) { |
| 62 | + if (addLabel && addLabel.length > 0) { |
| 63 | + safeLog(`Adding label on issue ${hydrated.number}: ${addLabel}`); |
| 64 | + await issue.addLabel(addLabel); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + if (this.setMilestoneId != undefined) { |
| 69 | + safeLog(`Setting milestone of issue ${hydrated.number} to id ${+this.setMilestoneId}`); |
| 70 | + await issue.setMilestone(+this.setMilestoneId); |
| 71 | + } |
| 72 | + safeLog(`Processing issue ${hydrated.number}.`); |
| 73 | + } else { |
| 74 | + if (!hydrated.open) { |
| 75 | + safeLog(`Issue ${hydrated.number} is not open. Ignoring`); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments