From 483ee90f2e76d670f595e56315e445badd2c265e Mon Sep 17 00:00:00 2001 From: OJ Kwon Date: Wed, 4 Jan 2017 13:43:15 -0800 Subject: [PATCH] style(providers): update type definition for ci providers --- source/ci_source/providers/Circle.js | 54 ------------------------- source/ci_source/providers/Circle.ts | 51 +++++++++++++++++++++++ source/ci_source/providers/Fake.ts | 23 +++++++++++ source/ci_source/providers/Jenkins.ts | 33 +++++++++++++++ source/ci_source/providers/Semaphore.ts | 23 +++++++++++ source/ci_source/providers/Travis.ts | 23 +++++++++++ source/ci_source/providers/index.ts | 10 +++++ 7 files changed, 163 insertions(+), 54 deletions(-) delete mode 100644 source/ci_source/providers/Circle.js create mode 100644 source/ci_source/providers/Circle.ts create mode 100644 source/ci_source/providers/Fake.ts create mode 100644 source/ci_source/providers/Jenkins.ts create mode 100644 source/ci_source/providers/Semaphore.ts create mode 100644 source/ci_source/providers/Travis.ts create mode 100644 source/ci_source/providers/index.ts diff --git a/source/ci_source/providers/Circle.js b/source/ci_source/providers/Circle.js deleted file mode 100644 index aa349ef54..000000000 --- a/source/ci_source/providers/Circle.js +++ /dev/null @@ -1,54 +0,0 @@ -// @flow -"use strict" - -import type { Env } from "../ci_source" -import { ensureEnvKeysExist, ensureEnvKeysAreInt } from "../ci_source_helpers" - -export default class Circle { - env: Env - constructor(env: Env) { this.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"]) - } - - _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"] } -} diff --git a/source/ci_source/providers/Circle.ts b/source/ci_source/providers/Circle.ts new file mode 100644 index 000000000..4ff0b1c4d --- /dev/null +++ b/source/ci_source/providers/Circle.ts @@ -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']; } +} diff --git a/source/ci_source/providers/Fake.ts b/source/ci_source/providers/Fake.ts new file mode 100644 index 000000000..4f7a506de --- /dev/null +++ b/source/ci_source/providers/Fake.ts @@ -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 { return ['github']; } +} diff --git a/source/ci_source/providers/Jenkins.ts b/source/ci_source/providers/Jenkins.ts new file mode 100644 index 000000000..87bc7d30a --- /dev/null +++ b/source/ci_source/providers/Jenkins.ts @@ -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 { + return ['github']; + } +} diff --git a/source/ci_source/providers/Semaphore.ts b/source/ci_source/providers/Semaphore.ts new file mode 100644 index 000000000..54049829f --- /dev/null +++ b/source/ci_source/providers/Semaphore.ts @@ -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']; } +} diff --git a/source/ci_source/providers/Travis.ts b/source/ci_source/providers/Travis.ts new file mode 100644 index 000000000..9e58e06d4 --- /dev/null +++ b/source/ci_source/providers/Travis.ts @@ -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']; } +} diff --git a/source/ci_source/providers/index.ts b/source/ci_source/providers/index.ts new file mode 100644 index 000000000..d8e80c608 --- /dev/null +++ b/source/ci_source/providers/index.ts @@ -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 = [Travis, Circle, Semaphore, Jenkins, FakeCI]; +export { + providers +};