From e20e4a42480fa203cf4c267feadc56ea2f88def7 Mon Sep 17 00:00:00 2001 From: Kamil Pajdzik <41709775+kpajdzik@users.noreply.github.com> Date: Thu, 14 Mar 2019 09:40:47 -0700 Subject: [PATCH] Add GitHub AutoPR branches cleanup script (#1559) --- .scripts/clean-autopr.ts | 61 ++++++++++++++++++++++++++++++++++++++++ .scripts/github.ts | 10 +++---- 2 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 .scripts/clean-autopr.ts diff --git a/.scripts/clean-autopr.ts b/.scripts/clean-autopr.ts new file mode 100644 index 00000000000..71015f35206 --- /dev/null +++ b/.scripts/clean-autopr.ts @@ -0,0 +1,61 @@ +/** + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for + * license information. + */ + +import { exec as execWithCallback } from "child_process"; +import { getAuthenticatedClient } from "./github"; +import { PullRequestsGetAllParams } from "@octokit/rest"; + +const _repositoryOwner = "Azure"; + +async function cleanBranches() { + const octokit = getAuthenticatedClient(); + const params: PullRequestsGetAllParams = { + owner: _repositoryOwner, + repo: "azure-sdk-for-js", + state: "open" + } + + let pullRequestsResponse = await octokit.pullRequests.getAll(params); + + do { + const autoPullRequests = pullRequestsResponse.data.filter(pr => pr.title.startsWith("[AutoPR")).map(pr => pr.head.ref); + console.log(JSON.stringify(autoPullRequests, undefined, " ")); + console.log(JSON.stringify(autoPullRequests.length, undefined, " ")); + + for (const branch of autoPullRequests) { + try { + await exec(`git push origin :${branch}`); + } catch (err) { + console.log(`Branch ${branch} doesn't exist. Skipping. Error: [${err}]`); + } + } + + if (octokit.hasFirstPage(pullRequestsResponse)) { + pullRequestsResponse = await octokit.getNextPage(pullRequestsResponse); + } else { + break; + } + } while (true); +} + +try { + cleanBranches(); +} catch (err) { + console.error(err); +} + +async function exec(command: string): Promise { + console.log(`Executing ${command}`); + return new Promise((resolve, reject) => { + execWithCallback(command, (error, stdout) => { + if (error) { + reject(error); + } + + resolve(stdout); + }); + }); +} diff --git a/.scripts/github.ts b/.scripts/github.ts index 5ecbfd01783..c1fed2965f3 100644 --- a/.scripts/github.ts +++ b/.scripts/github.ts @@ -12,7 +12,7 @@ import { Logger } from './logger'; const _repositoryOwner = "Azure"; const _logger = Logger.get(); -function getAuthenticatedClient(): Octokit { +export function getAuthenticatedClient(): Octokit { const octokit = new Octokit(); octokit.authenticate({ type: "token", token: getToken() }); return octokit; @@ -70,10 +70,10 @@ export async function requestPullRequestReview(repositoryName: string, prId: num owner: _repositoryOwner, repo: repositoryName, number: prId, - reviewers: [ "daschult", "amarzavery", "sergey-shandar" ] + reviewers: ["daschult", "amarzavery", "sergey-shandar"] }; - return new Promise>((resolve, reject) => { + return new Promise>((resolve, reject) => { octokit.pullRequests.createReviewRequest(params, (error, response) => { if (error) { reject(error); @@ -81,7 +81,7 @@ export async function requestPullRequestReview(repositoryName: string, prId: num resolve(response); } }); - }); + }); } export async function commitAndCreatePullRequest( @@ -90,7 +90,7 @@ export async function commitAndCreatePullRequest( commitMessage: string, repositoryName: string, pullRequestTitle: string, - pullRequestDescription:string, + pullRequestDescription: string, validate?: ValidateFunction, validateEach?: string | ValidateEachFunction): Promise { await createNewUniqueBranch(repository, `generated/${packageName}`, true);