Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean unnecessary scripts to enable Node 12 #3037

Merged
merged 4 commits into from
May 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 44 additions & 31 deletions .scripts/clean-autopr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,53 @@
*/

import { exec as execWithCallback } from "child_process";
import { getAuthenticatedClient } from "./github";
import { PullRequestsGetAllParams } from "@octokit/rest";
import Octokit from "@octokit/rest";

const _repositoryOwner = "Azure";
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
kpajdzik marked this conversation as resolved.
Show resolved Hide resolved
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: PullRequestsGetAllParams = {
owner: _repositoryOwner,
const params: Octokit.PullsListParams = {
owner: "Azure",
repo: "azure-sdk-for-js",
state: "open"
state: "open",
per_page: 100
}

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}]`);
}
}
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`);

if (octokit.hasFirstPage(pullRequestsResponse)) {
pullRequestsResponse = await octokit.getNextPage(pullRequestsResponse);
} else {
break;
for (const branch of autoPullRequests) {
try {
await exec(`git push origin :${branch}`);
} catch (err) {
console.log(`Branch ${branch} doesn't exist. Skipping. Error: [${err}]`);
}
} while (true);
}

try {
cleanBranches();
} catch (err) {
console.error(err);
}
}

async function exec(command: string): Promise<any> {
Expand All @@ -59,3 +66,9 @@ async function exec(command: string): Promise<any> {
});
});
}

try {
cleanBranches();
} catch (err) {
console.error(err);
}
Loading