Skip to content
This repository has been archived by the owner on May 5, 2023. It is now read-only.

Commit

Permalink
Add GitHub AutoPR branch cleanup script (#4904)
Browse files Browse the repository at this point in the history
* Add GitHub AutoPR branch cleanup script

* Fix octokit breaking changes

* Remove TypeScript compilation from Travis CI
  • Loading branch information
kpajdzik committed Mar 18, 2019
1 parent 6d9c27c commit 07196c3
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 4 deletions.
85 changes: 85 additions & 0 deletions .scripts/github.ts
@@ -0,0 +1,85 @@
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for
* license information.
*/

import Octokit, { PullRequestsListParams } from '@octokit/rest';
import { exec as execWithCallback } from "child_process";

const _repositoryOwner = "Azure";

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.log(text);
}
}

function getToken(): string {
const token: string = process.env.SDK_GEN_GITHUB_TOKEN || "";
_validatePersonalAccessToken(token);

return token;
}

function getAuthenticatedClient(): Octokit {
const octokit = new Octokit();
octokit.authenticate({ type: "token", token: getToken() });
return octokit;
}

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

async function cleanBranches() {
const octokit = getAuthenticatedClient();
const params: PullRequestsListParams = {
owner: _repositoryOwner,
repo: "azure-sdk-for-node",
state: "open"
}

let pullRequestsResponse = await octokit.pullRequests.list(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);
}
4 changes: 0 additions & 4 deletions .travis.yml
Expand Up @@ -3,7 +3,3 @@ sudo: false
node_js:
- "8"
- "6"

before_script:
- npm i -g typescript
- tsc
3 changes: 3 additions & 0 deletions package.json
Expand Up @@ -37,6 +37,7 @@
"uuid": "^3.0.1"
},
"devDependencies": {
"@octokit/rest": "^15.17.0",
"@types/request": "^2.0.3",
"adal-node": "^0.1.22",
"async": "^2.5.0",
Expand Down Expand Up @@ -80,6 +81,8 @@
"random-js": "1.0.4",
"should": "^8.3.1",
"sinon": "^2.1.0",
"tslib": "^1.9.3",
"typescript": "^3.1.6",
"xmlbuilder": "0.4.3",
"yargs": "3.29.0"
},
Expand Down
20 changes: 20 additions & 0 deletions tsconfig.json
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"target": "ES2015", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"lib": ["es2017"], /* Specify library files to be included in the compilation. */
"importHelpers": true, /* Import emit helpers from 'tslib'. */
"strict": true, /* Enable all strict type-checking options. */
"noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
"strictNullChecks": true, /* Enable strict null checks. */
"strictFunctionTypes": true, /* Enable strict checking of function types. */
"strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
"noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
"alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
"noUnusedLocals": true, /* Report errors on unused locals. */
"noUnusedParameters": true, /* Report errors on unused parameters. */
"noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
"noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
}
}

0 comments on commit 07196c3

Please sign in to comment.