Skip to content

Commit b39c517

Browse files
committed
feat(package): add stage-override-to-pr-base script
1 parent fc2a0fc commit b39c517

4 files changed

Lines changed: 49 additions & 0 deletions

File tree

packages/branch-utilities/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
"default": "./dist/index.js"
1414
}
1515
},
16+
"bin": {
17+
"stage-override-to-pr-base": "./dist/stage-override-to-pr-base.js"
18+
},
1619
"scripts": {
1720
"prebuild": "rm -rf ./dist",
1821
"build": "tsc",
@@ -28,6 +31,9 @@
2831
"test:ci": "npm run test",
2932
"test:watch": "npm run test -- --watch"
3033
},
34+
"dependencies": {
35+
"yargs": "^17.7.2"
36+
},
3137
"peerDependencies": {
3238
"tslib": "^2.3.0"
3339
},
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './stage-override-to-pr-base.function.js'
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { execReturn } from '../lib/helpers.js'
2+
3+
export interface StageOverrideToPrBaseOptions {
4+
branchNameOverride?: string
5+
}
6+
7+
export async function stageOverrideToPrBase(
8+
options: StageOverrideToPrBaseOptions | Promise<StageOverrideToPrBaseOptions>,
9+
): Promise<void> {
10+
const opts = await options
11+
12+
const gitHubPrJson = execReturn('gh pr list --json headRefName,title,isDraft,closed')
13+
const gitHubPrs = JSON.parse(gitHubPrJson)
14+
15+
const branchNameOverride = opts.branchNameOverride || process.env['GITHUB_BASE_REF']
16+
const isPrOverride = !!gitHubPrs.find((pr: any) => pr.headRefName === branchNameOverride)
17+
18+
// export to env
19+
process.stdout.write(`
20+
export SC_OVERRIDE_BRANCH_NAME=${branchNameOverride}
21+
export SC_OVERRIDE_IS_PR=${isPrOverride}
22+
`)
23+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env node
2+
/* eslint-disable no-console */
3+
import yargs from 'yargs'
4+
import { stageOverrideToPrBase } from './scripts/index.js'
5+
6+
const args = yargs(process.argv)
7+
.option('branchNameOverride', {
8+
alias: 'b',
9+
type: 'string',
10+
description: 'branch name override to be used as PR base',
11+
}).argv
12+
13+
try {
14+
const responseMsg = await stageOverrideToPrBase(args)
15+
console.info(responseMsg)
16+
}catch (err){
17+
console.error(err)
18+
process.exit(1)
19+
}

0 commit comments

Comments
 (0)