Skip to content

Commit

Permalink
style(providers): update type definition for ci providers
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonoj committed Jan 4, 2017
1 parent e05595d commit 483ee90
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 54 deletions.
54 changes: 0 additions & 54 deletions source/ci_source/providers/Circle.js

This file was deleted.

51 changes: 51 additions & 0 deletions source/ci_source/providers/Circle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Env, CISource } from '../ci_source';
import { ensureEnvKeysExist, ensureEnvKeysAreInt } from '../ci_source_helpers';

export class Circle implements CISource {
constructor(private readonly env: Env) {
}

get name(): string { return 'Circle CI'; }

get isCI(): boolean {
return ensureEnvKeysExist(this.env, ['CIRCLE_BUILD_NUM']);
}

get isPR(): boolean {
if (ensureEnvKeysExist(this.env, ['CI_PULL_REQUEST'])) {
return true;
}

const mustHave = ['CIRCLE_CI_API_TOKEN', 'CIRCLE_PROJECT_USERNAME', 'CIRCLE_PROJECT_REPONAME', 'CIRCLE_BUILD_NUM'];
return ensureEnvKeysExist(this.env, mustHave) && ensureEnvKeysAreInt(this.env, ['CIRCLE_PR_NUMBER']);
}

private _prParseURL(): {owner?: string, reponame?: string, id?: string} {
const prUrl = this.env.CI_PULL_REQUEST || '';
const splitSlug = prUrl.split('/');
if (splitSlug.length === 7) {
const owner = splitSlug[3];
const reponame = splitSlug[4];
const id = splitSlug[6];
return {owner, reponame, id};
};
return {};
}

get pullRequestID(): string {
if (this.env.CIRCLE_PR_NUMBER) {
return this.env.CIRCLE_PR_NUMBER;
} else {
const {id} = this._prParseURL();
return id || '';
}
}

get repoSlug(): string {
const {owner, reponame} = this._prParseURL();
return (owner && reponame) ? `${owner}/${reponame}` : '';
}

get repoURL(): string { return this.env.CIRCLE_REPOSITORY_URL; }
get supportedPlatforms(): string[] { return ['github']; }
}
23 changes: 23 additions & 0 deletions source/ci_source/providers/Fake.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Env, CISource } from '../ci_source';
import { ensureEnvKeysExist } from '../ci_source_helpers';

export class FakeCI implements CISource {
private readonly env: Env;

constructor(env: Env) {
const defaults = {
repo: env.DANGER_TEST_REPO || 'artsy/emission',
pr: env.DANGER_TEST_PR || '327'
};

this.env = {...env, ...defaults};
}
get name(): string { return 'Fake Testing CI'; }

get isCI(): boolean { return ensureEnvKeysExist(this.env, ['DANGER_FAKE_CI']); }
get isPR(): boolean { return true; }

get pullRequestID(): string { return this.env.pr; }
get repoSlug(): string { return this.env.repo; }
get supportedPlatforms(): Array<string> { return ['github']; }
}
33 changes: 33 additions & 0 deletions source/ci_source/providers/Jenkins.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Env, CISource } from '../ci_source';
import { ensureEnvKeysExist, ensureEnvKeysAreInt } from '../ci_source_helpers';

export class Jenkins implements CISource {
constructor(private readonly env: Env) {
}

get name(): string {
return 'Jenkins';
}

get isCI(): boolean {
return ensureEnvKeysExist(this.env, ['JENKINS_URL']);
}

get isPR(): boolean {
const mustHave = ['JENKINS_URL', 'ghprbPullId', 'ghprbGhRepository'];
const mustBeInts = ['ghprbPullId'];
return ensureEnvKeysExist(this.env, mustHave) && ensureEnvKeysAreInt(this.env, mustBeInts);
}

get pullRequestID(): string {
return this.env.ghprbPullId;
}

get repoSlug(): string {
return this.env.ghprbGhRepository;
}

get supportedPlatforms(): Array<string> {
return ['github'];
}
}
23 changes: 23 additions & 0 deletions source/ci_source/providers/Semaphore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Env, CISource } from '../ci_source';
import { ensureEnvKeysExist, ensureEnvKeysAreInt } from '../ci_source_helpers';

export class Semaphore implements CISource {
constructor(private readonly env: Env) {
}

get name(): string { return 'Travis CI'; }

get isCI(): boolean {
return ensureEnvKeysExist(this.env, ['SEMAPHORE']);
}

get isPR(): boolean {
const mustHave = ['SEMAPHORE_REPO_SLUG'];
const mustBeInts = ['PULL_REQUEST_NUMBER'];
return ensureEnvKeysExist(this.env, mustHave) && ensureEnvKeysAreInt(this.env, mustBeInts);
}

get pullRequestID(): string { return this.env.PULL_REQUEST_NUMBER; }
get repoSlug(): string { return this.env.SEMAPHORE_REPO_SLUG; }
get supportedPlatforms(): string[] { return ['github']; }
}
23 changes: 23 additions & 0 deletions source/ci_source/providers/Travis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Env, CISource } from '../ci_source';
import { ensureEnvKeysExist, ensureEnvKeysAreInt } from '../ci_source_helpers';

export class Travis implements CISource {
constructor(private readonly env: Env) {
}

get name(): string { return 'Travis CI'; }

get isCI(): boolean {
return ensureEnvKeysExist(this.env, ['HAS_JOSH_K_SEAL_OF_APPROVAL']);
}

get isPR(): boolean {
const mustHave = ['HAS_JOSH_K_SEAL_OF_APPROVAL', 'TRAVIS_PULL_REQUEST', 'TRAVIS_REPO_SLUG'];
const mustBeInts = ['TRAVIS_PULL_REQUEST'];
return ensureEnvKeysExist(this.env, mustHave) && ensureEnvKeysAreInt(this.env, mustBeInts);
}

get pullRequestID(): string { return this.env.TRAVIS_PULL_REQUEST; }
get repoSlug(): string { return this.env.TRAVIS_REPO_SLUG; }
get supportedPlatforms(): string[] { return ['github']; }
}
10 changes: 10 additions & 0 deletions source/ci_source/providers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {Travis} from './Travis';
import {Circle} from './Circle';
import {Semaphore} from './Semaphore';
import {Jenkins} from './Jenkins';
import {FakeCI} from './Fake';

const providers: Array<any> = [Travis, Circle, Semaphore, Jenkins, FakeCI];
export {
providers
};

0 comments on commit 483ee90

Please sign in to comment.