-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
clean-autopr.ts
74 lines (62 loc) · 2.15 KB
/
clean-autopr.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/**
* 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 Octokit from "@octokit/rest";
export function getToken(): string {
const token: string = process.env.SDK_GEN_GITHUB_TOKEN || "";
_validatePersonalAccessToken(token);
return token;
}
function _validatePersonalAccessToken(token: string): void {
if (!token) {
const text =
`Github personal access token was not found as a script parameter or as an
environmental variable. Please visit https://github.com/settings/tokens,
generate new token with "repo" scope and pass it with -token switch or set
it as environmental variable named SDK_GEN_GITHUB_TOKEN.`
console.error(text);
}
}
export function getAuthenticatedClient(): Octokit {
const octokit = new Octokit({ auth: getToken()});
return octokit;
}
async function cleanBranches() {
const octokit = getAuthenticatedClient();
const params: Octokit.PullsListParams = {
owner: "Azure",
repo: "azure-sdk-for-js",
state: "open",
per_page: 100
}
let pullRequestsResponse = await octokit.pulls.list(params);
const autoPullRequests = pullRequestsResponse.data.filter(pr => pr.title.startsWith("[AutoPR")).map(pr => pr.head.ref);
console.log(JSON.stringify(autoPullRequests, undefined, " "));
console.log(`Found ${autoPullRequests.length} branches`);
for (const branch of autoPullRequests) {
try {
await exec(`git push origin :${branch}`);
} catch (err) {
console.log(`Branch ${branch} doesn't exist. Skipping. 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);
});
});
}
try {
cleanBranches();
} catch (err) {
console.error(err);
}