diff --git a/package-lock.json b/package-lock.json index e8d7c77..cf09db5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13172,7 +13172,7 @@ }, "packages/branch-utilities": { "name": "@shiftcode/branch-utilities", - "version": "3.0.0", + "version": "3.1.0-pr30.0", "license": "MIT", "peerDependencies": { "tslib": "^2.3.0" @@ -13234,7 +13234,7 @@ }, "packages/publish-helper": { "name": "@shiftcode/publish-helper", - "version": "3.0.0", + "version": "3.0.1-pr30.0", "license": "MIT", "dependencies": { "conventional-changelog-angular": "^5.0.13", @@ -13245,7 +13245,7 @@ "publish-lib": "dist/publish-lib.js" }, "devDependencies": { - "@shiftcode/branch-utilities": "^3.0.0", + "@shiftcode/branch-utilities": "^3.1.0-pr30.0", "@types/yargs": "^17.0.32" }, "peerDependencies": { diff --git a/packages/branch-utilities/package.json b/packages/branch-utilities/package.json index 12b025d..8f0523d 100644 --- a/packages/branch-utilities/package.json +++ b/packages/branch-utilities/package.json @@ -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", diff --git a/packages/branch-utilities/src/lib/base.utils.spec.ts b/packages/branch-utilities/src/lib/base.utils.spec.ts index e3b0e20..fdaf49b 100644 --- a/packages/branch-utilities/src/lib/base.utils.spec.ts +++ b/packages/branch-utilities/src/lib/base.utils.spec.ts @@ -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({ stage: 'master', @@ -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({ + stage: 'pr1313', + isPr: true, + isProd: false, + name: 'on-branch-to-override-them-all', + branchName: '#1313-on-branch-to-override-them-all', + }) + }) }) describe('parseBranchName', () => { diff --git a/packages/branch-utilities/src/lib/base.utils.ts b/packages/branch-utilities/src/lib/base.utils.ts index e8c58b3..df60bf8 100644 --- a/packages/branch-utilities/src/lib/base.utils.ts +++ b/packages/branch-utilities/src/lib/base.utils.ts @@ -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) @@ -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). */ diff --git a/packages/branch-utilities/src/lib/types/sc-override-env-var.type.ts b/packages/branch-utilities/src/lib/types/sc-override-env-var.type.ts index ab5886c..d533d94 100644 --- a/packages/branch-utilities/src/lib/types/sc-override-env-var.type.ts +++ b/packages/branch-utilities/src/lib/types/sc-override-env-var.type.ts @@ -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 } diff --git a/packages/publish-helper/package.json b/packages/publish-helper/package.json index 17c05aa..9d5b38b 100644 --- a/packages/publish-helper/package.json +++ b/packages/publish-helper/package.json @@ -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", @@ -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": {