This repository has been archived by the owner on May 5, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add GitHub AutoPR branch cleanup script (#4904)
* Add GitHub AutoPR branch cleanup script * Fix octokit breaking changes * Remove TypeScript compilation from Travis CI
- Loading branch information
Showing
4 changed files
with
108 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,3 @@ sudo: false | |
node_js: | ||
- "8" | ||
- "6" | ||
|
||
before_script: | ||
- npm i -g typescript | ||
- tsc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'. */ | ||
} | ||
} |