Add GitHub AutoPR branches cleanup script (#1559)

This commit is contained in:
Kamil Pajdzik 2019-03-14 09:40:47 -07:00 коммит произвёл GitHub
Родитель a1c7a11b6b
Коммит e20e4a4248
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
2 изменённых файлов: 66 добавлений и 5 удалений

61
.scripts/clean-autopr.ts Normal file
Просмотреть файл

@ -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<any> {
console.log(`Executing ${command}`);
return new Promise((resolve, reject) => {
execWithCallback(command, (error, stdout) => {
if (error) {
reject(error);
}
resolve(stdout);
});
});
}

Просмотреть файл

@ -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<Response<PullRequestsCreateReviewRequestResponse>>((resolve, reject) => {
return new Promise<Response<PullRequestsCreateReviewRequestResponse>>((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<string> {
await createNewUniqueBranch(repository, `generated/${packageName}`, true);