Skip to content

Commit

Permalink
Merge pull request #31 from shiftcode/#30-branch-name-and-is-pr-overr…
Browse files Browse the repository at this point in the history
…ides

feat(branch-utilities): added branchName and isPr override via ENV
  • Loading branch information
patricksuter authored Jul 26, 2024
2 parents 56a52c8 + cf671a4 commit 0d97ba8
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 8 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/branch-utilities/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shiftcode/branch-utilities",
"version": "3.0.0",
"version": "3.1.0-pr30.0",
"description": "Utilities for local and ci to get branch name and stage",
"repository": "https://github.com/shiftcode/sc-commons-public",
"license": "MIT",
Expand Down
17 changes: 17 additions & 0 deletions packages/branch-utilities/src/lib/base.utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ describe('base utils', () => {
test('works when master run locally with override', () => {
const env: CustomScOverrideEnv = {
SC_OVERRIDE: 'true',
SC_OVERRIDE_BRANCH_NAME: undefined,
SC_OVERRIDE_IS_PR: undefined,
}
expect(getBranchInfo(env, 'master')).toEqual(<BranchInfo>{
stage: 'master',
Expand Down Expand Up @@ -61,6 +63,21 @@ describe('base utils', () => {
name: 'master',
})
})

test('works with env var overrides', () => {
const env: CustomScOverrideEnv = {
SC_OVERRIDE: 'true',
SC_OVERRIDE_BRANCH_NAME: '#1313-on-branch-to-override-them-all',
SC_OVERRIDE_IS_PR: 'true',
}
expect(getBranchInfo(env, 'master')).toEqual(<BranchInfo>{
stage: 'pr1313',
isPr: true,
isProd: false,
name: 'on-branch-to-override-them-all',
branchName: '#1313-on-branch-to-override-them-all',
})
})
})

describe('parseBranchName', () => {
Expand Down
21 changes: 20 additions & 1 deletion packages/branch-utilities/src/lib/base.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ export function createStageInfo(stage: string): StageInfo {
*/
export function getBranchInfo(env: unknown, branchName?: string): BranchInfo {
let isPr = false
if (isGithubWorkflow(env)) {

if (isFullBranchOverrideDefined(env)) {
// full branch name override via env vars SC_OVERRIDE_BRANCH_NAME and SC_OVERRIDE_IS_PR
branchName = getBranchNameOverride(env)
isPr = getIsPrOverride(env)
} else if (isGithubWorkflow(env)) {
// github workflow environment
branchName = branchName ?? getGithubBranchName(env)
isPr = isGithubPullRequest(env)
Expand Down Expand Up @@ -75,6 +80,20 @@ export function getBranchInfo(env: unknown, branchName?: string): BranchInfo {
}
}

export function isFullBranchOverrideDefined(envVars: unknown): envVars is CustomScOverrideEnv {
return envVars !== null
&& typeof (envVars as CustomScOverrideEnv).SC_OVERRIDE_BRANCH_NAME ==='string'
&& typeof (envVars as CustomScOverrideEnv).SC_OVERRIDE_IS_PR ==='string'
}

export function getBranchNameOverride(env: CustomScOverrideEnv): string {
return env.SC_OVERRIDE_BRANCH_NAME
}

export function getIsPrOverride(env: CustomScOverrideEnv): boolean {
return env.SC_OVERRIDE_IS_PR === 'true'
}

/**
* @return Returns the branch name. The name is resolved depending on provided environment (github actions | local).
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
export interface CustomScOverrideEnv {
/** stringified object of type {@link GitHubContext} */
/** flag to allow usage of master branch locally */
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents,@typescript-eslint/naming-convention
SC_OVERRIDE: 'true' | any
/** flag to override branchName for getBranchInfo(), needs to be used in conjunction with SC_OVERRIDE_IS_PR */
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents,@typescript-eslint/naming-convention
SC_OVERRIDE_BRANCH_NAME: string | any
/** flag to override isPr getBranchInfo(), needs to be used in conjunction with SC_OVERRIDE_BRANCH_NAME */
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents,@typescript-eslint/naming-convention
SC_OVERRIDE_IS_PR: string | any
}
4 changes: 2 additions & 2 deletions packages/publish-helper/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shiftcode/publish-helper",
"version": "3.0.0",
"version": "3.0.1-pr30.0",
"description": "scripts for conventional (pre)releases",
"repository": "https://github.com/shiftcode/sc-commons-public",
"license": "MIT",
Expand Down Expand Up @@ -30,7 +30,7 @@
"yargs": "^17.7.2"
},
"devDependencies": {
"@shiftcode/branch-utilities": "^3.0.0",
"@shiftcode/branch-utilities": "^3.1.0-pr30.0",
"@types/yargs": "^17.0.32"
},
"peerDependencies": {
Expand Down

0 comments on commit 0d97ba8

Please sign in to comment.