Skip to content

Commit

Permalink
feat: naming strategy supporting CI names
Browse files Browse the repository at this point in the history
  • Loading branch information
moltar committed Sep 14, 2022
1 parent 4049009 commit 0648553
Showing 1 changed file with 81 additions and 1 deletion.
82 changes: 81 additions & 1 deletion src/naming-strategy/tokens.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,105 @@
import assert from 'assert';
import { Aws } from 'aws-cdk-lib';
import ci from 'env-ci';

function fromCI(name: 'branch' | 'commit' | 'build' | 'job' | 'pr' | 'slug'): IStaticWebsitePreviewNamingToken {
const c = ci();

if (name in c) {
const val = c[name];

if (val) {
return new StaticWebsitePreviewNamingToken(val);
}
}

throw new Error(`The value for "${name}" is not set.`);
}

export interface IStaticWebsitePreviewNamingToken {
token: string;
}

export class StaticWebsitePreviewNamingToken implements IStaticWebsitePreviewNamingToken {
/**
* Provide an arbitrary string.
*/
static fromString(val: string): IStaticWebsitePreviewNamingToken {
return new StaticWebsitePreviewNamingToken(val);
}

/**
* AWS Account ID into which this construct is deployed.
*/
static get AWS_ACCOUNT_ID(): IStaticWebsitePreviewNamingToken {
return new StaticWebsitePreviewNamingToken(Aws.ACCOUNT_ID);
}

/**
* AWS region into which this construct is deployed.
*/
static get AWS_REGION(): IStaticWebsitePreviewNamingToken {
return new StaticWebsitePreviewNamingToken(Aws.REGION);
}

/**
* AWS stack name into which this construct is deployed.
*/
static get AWS_STACK_NAME(): IStaticWebsitePreviewNamingToken {
return new StaticWebsitePreviewNamingToken(Aws.STACK_NAME);
}

constructor (public readonly token: string) {}
/**
* Git branch being built or targeted by a Pull Request.
*
* @see https://github.com/semantic-release/env-ci#supported-variables
*/
static get CI_BRANCH_NAME(): IStaticWebsitePreviewNamingToken {
return fromCI('branch');
}

/**
* Commit sha that triggered the CI build.
*
* @see https://github.com/semantic-release/env-ci#supported-variables
*/
static get CI_COMMIT_SHA(): IStaticWebsitePreviewNamingToken {
return fromCI('commit');
}

/**
* CI service build number.
*
* @see https://github.com/semantic-release/env-ci#supported-variables
*/
static get CI_BUILD(): IStaticWebsitePreviewNamingToken {
return fromCI('build');
}

/**
* CI service job number.
*
* @see https://github.com/semantic-release/env-ci#supported-variables
*/
static get CI_JOB(): IStaticWebsitePreviewNamingToken {
return fromCI('job');
}

/**
* Pull Request number (only for builds triggered by a Pull Request).
*
* @see https://github.com/semantic-release/env-ci#supported-variables
*/
static get CI_PR(): IStaticWebsitePreviewNamingToken {
return fromCI('pr');
}

/**
* The slug (in form: owner_name/repo_name) of the repository currently being built.
*/
static get CI_SLUG(): IStaticWebsitePreviewNamingToken {
return fromCI('slug');
}

constructor (public readonly token: string = '') {}
}

0 comments on commit 0648553

Please sign in to comment.