grafana/toolkit: Modify close milestone task to remove label from more than 100 pull requests and add dry run option (#25108)

* Fix close milestone to remove label from all the required pull requests

There used to be a limit to 100 pull requests.

* Add dry run option in close-milestone task
This commit is contained in:
Sofia Papagiannaki 2020-05-26 17:26:16 +03:00 committed by GitHub
parent 170abf2e3d
commit 139be3d7ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 35 deletions

View File

@ -104,6 +104,7 @@ export const run = (includeInternalScripts = false) => {
program
.command('close-milestone')
.option('-m, --milestone <milestone>', 'Specify milestone')
.option('--dryRun', 'Only simulate actions')
.description('Helps ends a milestone by removing the cherry-pick label and closing it')
.action(async cmd => {
if (!cmd.milestone) {
@ -113,6 +114,7 @@ export const run = (includeInternalScripts = false) => {
await execTask(closeMilestoneTask)({
milestone: cmd.milestone,
dryRun: !!cmd.dryRun,
});
});

View File

@ -3,9 +3,10 @@ import GithubClient from '../utils/githubClient';
interface CloseMilestoneOptions {
milestone: string;
dryRun: boolean;
}
const closeMilestoneTaskRunner: TaskRunner<CloseMilestoneOptions> = async ({ milestone }) => {
const closeMilestoneTaskRunner: TaskRunner<CloseMilestoneOptions> = async ({ milestone, dryRun }) => {
const githubClient = new GithubClient({ required: true });
const cherryPickLabel = 'cherry-pick needed';
@ -16,6 +17,10 @@ const closeMilestoneTaskRunner: TaskRunner<CloseMilestoneOptions> = async ({ mil
return;
}
if (dryRun) {
console.log('dry run is enabled');
}
const milestoneRes = await client.get(`/milestones/${milestone}`, {});
const milestoneState = milestoneRes.data.state;
@ -27,7 +32,10 @@ const closeMilestoneTaskRunner: TaskRunner<CloseMilestoneOptions> = async ({ mil
console.log('fetching issues/PRs of the milestone ⏬');
// Get all the issues/PRs with the label cherry-pick
let totalIssues = 0;
while (true) {
// Get first 100 issues/PRs with the label cherry-pick
// Every pull request is actually an issue
const issuesRes = await client.get('/issues', {
params: {
@ -39,15 +47,18 @@ const closeMilestoneTaskRunner: TaskRunner<CloseMilestoneOptions> = async ({ mil
});
if (issuesRes.data.length < 1) {
console.log('no issues to remove label from');
} else {
console.log(`found ${issuesRes.data.length} issues to remove the cherry-pick label from 🔎`);
break;
}
const comparativeStr = totalIssues === 0 ? ' ' : ' more ';
console.log(`found ${issuesRes.data.length}${comparativeStr}issues to remove the cherry-pick label from 🔎`);
totalIssues += issuesRes.data.length;
for (const issue of issuesRes.data) {
// the reason for using stdout.write is for achieving 'action -> result' on
// the same line
process.stdout.write(`🔧removing label from issue #${issue.number} 🗑...`);
if (!dryRun) {
const resDelete = await client.delete(`/issues/${issue.number}/labels/${cherryPickLabel}`, {});
if (resDelete.status === 200) {
process.stdout.write('done ✅\n');
@ -55,9 +66,16 @@ const closeMilestoneTaskRunner: TaskRunner<CloseMilestoneOptions> = async ({ mil
console.log('failed ❌');
}
}
}
}
console.log(`cleaned up ${issuesRes.data.length} issues/prs ⚡️`);
if (totalIssues === 0) {
console.log('no issues to remove label from');
} else {
console.log(`cleaned up ${totalIssues} issues/prs ⚡️`);
}
if (!dryRun) {
const resClose = await client.patch(`/milestones/${milestone}`, {
state: 'closed',
});
@ -68,6 +86,7 @@ const closeMilestoneTaskRunner: TaskRunner<CloseMilestoneOptions> = async ({ mil
console.log('failed to close the milestone, response:');
console.log(resClose);
}
}
};
export const closeMilestoneTask = new Task<CloseMilestoneOptions>(