Last active
July 13, 2019 15:38
-
-
Save jdx/5b0295687fdff8822992fb9e8208bc88 to your computer and use it in GitHub Desktop.
a set of scripts to migrate github status checks from legacy travis to github app (run with ts-node)
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
// run this second | |
import GitHub from '@octokit/rest' | |
import * as _ from 'lodash' | |
import * as fs from 'fs-extra' | |
import pMap from 'p-map' | |
import repos from '../repos.json' | |
let statuses: {[repo: string]: string[]} | |
try { | |
statuses = fs.readJSONSync('./repo-status.json') | |
} catch { | |
statuses = {} | |
} | |
const gh = new GitHub() | |
const GH_TOKEN = process.env.GH_TOKEN | |
if (!GH_TOKEN) throw new Error('set GH_TOKEN') | |
gh.authenticate({ | |
type: 'token', | |
token: GH_TOKEN | |
}) | |
async function getChecks(repo: string): Promise<string[]> { | |
try { | |
const {data: checks} = await gh.repos.getProtectedBranchRequiredStatusChecks({ | |
owner: 'heroku', | |
repo, | |
branch: 'master', | |
}) | |
return checks.contexts | |
} catch (err) { | |
if (err.code === 404) return [] | |
throw err | |
} | |
} | |
async function run() { | |
await pMap(repos, async repo => { | |
if (statuses[repo]) return | |
const checks = await getChecks(repo) | |
console.error(repo, checks) | |
statuses[repo] = checks | |
const sortedStatus: {[k: string]: string[]} = {} | |
for (let [k, v] of _.sortBy(Object.entries(statuses), s => s[0])) { | |
sortedStatus[k] = v | |
} | |
await fs.writeJSON('./repo-status.json', sortedStatus, {spaces: 2}) | |
}, {concurrency: 8}) | |
} | |
run() | |
.catch(err => { | |
console.error(err) | |
process.exit(1) | |
}) |
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
import GitHub from '@octokit/rest' | |
import * as fs from 'fs-extra' | |
const gh = new GitHub() | |
const GH_TOKEN = process.env.GH_TOKEN | |
if (!GH_TOKEN) throw new Error('set GH_TOKEN') | |
gh.authenticate({ | |
type: 'token', | |
token: GH_TOKEN | |
}) | |
async function * getAllRepos(response?: GitHub.Response<GitHub.ReposGetForOrgResponseItem[]>): AsyncIterableIterator<GitHub.ReposGetForOrgResponseItem> { | |
response = response || await gh.repos.getForOrg({ | |
org: 'heroku' | |
}) | |
for (let repo of response.data) { | |
yield repo | |
} | |
if (gh.hasNextPage(response)) { | |
for await (let repo of getAllRepos(await gh.getNextPage(response))) { | |
yield repo | |
} | |
} | |
} | |
async function run() { | |
let repos = [] | |
for await (let repo of getAllRepos()) { | |
console.error(repo.name) | |
repos.push(repo.name) | |
} | |
repos.sort() | |
await fs.writeJSON('repos.json', repos, {spaces: 2}) | |
} | |
run() | |
.catch(err => { | |
console.error(err) | |
process.exit(1) | |
}) |
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
// run this last | |
import GitHub from '@octokit/rest' | |
import statuses from '../repo-status.json' | |
const gh = new GitHub() | |
const GH_TOKEN = process.env.GH_TOKEN | |
if (!GH_TOKEN) throw new Error('set GH_TOKEN') | |
gh.authenticate({ | |
type: 'token', | |
token: GH_TOKEN | |
}) | |
async function getChecks(repo: string): Promise<string[]> { | |
try { | |
const {data: checks} = await gh.repos.getProtectedBranchRequiredStatusChecks({ | |
owner: 'heroku', | |
repo, | |
branch: 'master', | |
}) | |
return checks.contexts | |
} catch (err) { | |
if (err.code === 404) return [] | |
throw err | |
} | |
} | |
async function run() { | |
for await (let [repo, checks] of Object.entries(statuses)) { | |
if (checks.includes('continuous-integration/travis-ci') || | |
checks.includes('continuous-integration/travis-ci/push') || | |
checks.includes('continuous-integration/travis-ci/pr')) { | |
console.log(repo, checks) | |
checks = checks.map(c => c === 'continuous-integration/travis-ci' ? 'Travis CI - Pull Request' : c) | |
.map(c => c === 'continuous-integration/travis-ci/push' ? 'Travis CI - Branch' : c) | |
.map(c => c === 'continuous-integration/travis-ci/pr' ? 'Travis CI - Pull Request' : c) | |
await gh.repos.updateProtectedBranchRequiredStatusChecks({ | |
owner: 'heroku', | |
repo, | |
branch: 'master', | |
contexts: checks, | |
}) | |
console.log(repo, await getChecks(repo)) | |
} | |
} | |
} | |
run() | |
.catch(err => { | |
console.error(err) | |
process.exit(1) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment