Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(branch-utilities): added branchName and isPr override via ENV #31

Merged
merged 7 commits into from
Jul 26, 2024
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 en vars SC_OVERRIDE_BRANCH_NAME and SC_OVERRIDE_IS_PR
patricksuter marked this conversation as resolved.
Show resolved Hide resolved
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_IS_PR */
patricksuter marked this conversation as resolved.
Show resolved Hide resolved
// eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents,@typescript-eslint/naming-convention
SC_OVERRIDE_IS_PR: string | any
}
Loading