-
-
Notifications
You must be signed in to change notification settings - Fork 363
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1103 from danger/fb/codemagic
[Feature] Add Codemagic as a new CI Source
- Loading branch information
Showing
7 changed files
with
125 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { Env, CISource } from "../ci_source" | ||
import { ensureEnvKeysExist, ensureEnvKeysAreInt } from "../ci_source_helpers" | ||
|
||
/** | ||
* Codemagic.io CI Integration | ||
* | ||
* Environment Variables Documented: https://docs.codemagic.io/building/environment-variables/ | ||
* Notice a bug in the docs?: https://github.com/codemagic-ci-cd/codemagic-docs | ||
* | ||
* Need support/advice? https://slack.codemagic.io/ | ||
*/ | ||
export class Codemagic implements CISource { | ||
constructor(private readonly env: Env) {} | ||
|
||
get name(): string { | ||
return "Codemagic" | ||
} | ||
|
||
get isCI(): boolean { | ||
// Codemagic developer relations confirmed this is fine to use for this purpose | ||
return ensureEnvKeysExist(this.env, ["FCI_BUILD_ID"]) | ||
} | ||
|
||
get isPR(): boolean { | ||
const mustHave = ["FCI_PULL_REQUEST", "FCI_REPO_SLUG", "FCI_PROJECT_ID", "FCI_BUILD_ID"] | ||
const mustBeInts = ["BUILD_NUMBER", "FCI_PULL_REQUEST_NUMBER"] | ||
return ( | ||
ensureEnvKeysExist(this.env, mustHave) && | ||
ensureEnvKeysAreInt(this.env, mustBeInts) && | ||
this.env.FCI_PULL_REQUEST === "true" | ||
) | ||
} | ||
|
||
get pullRequestID(): string { | ||
return this.env.FCI_PULL_REQUEST_NUMBER | ||
} | ||
|
||
get repoSlug(): string { | ||
return this.env.FCI_REPO_SLUG | ||
} | ||
|
||
get ciRunURL() { | ||
const { FCI_BUILD_ID, FCI_PROJECT_ID } = process.env | ||
return `https://codemagic.io/app/${FCI_PROJECT_ID}/build/${FCI_BUILD_ID}` | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Codemagic } from "../Codemagic" | ||
import { getCISourceForEnv } from "../../get_ci_source" | ||
|
||
const correctEnv = { | ||
FCI_PROJECT_ID: "abcdef123-app-uuid", | ||
FCI_BUILD_ID: "abcdef123-build-uuid", | ||
FCI_REPO_SLUG: "danger/danger-js", | ||
FCI_PULL_REQUEST: "true", | ||
FCI_PULL_REQUEST_NUMBER: "2", | ||
BUILD_NUMBER: "42", | ||
} | ||
|
||
describe("being found when looking for CI", () => { | ||
it("finds Codemagic with the right ENV", () => { | ||
const ci = getCISourceForEnv(correctEnv) | ||
expect(ci).toBeInstanceOf(Codemagic) | ||
}) | ||
}) | ||
|
||
describe(".isCI", () => { | ||
it("validates when all Codemagic environment vars are set", () => { | ||
const codemagic = new Codemagic(correctEnv) | ||
expect(codemagic.isCI).toBeTruthy() | ||
}) | ||
|
||
it("does not validate without env", () => { | ||
const codemagic = new Codemagic({}) | ||
expect(codemagic.isCI).toBeFalsy() | ||
}) | ||
}) | ||
|
||
describe(".isPR", () => { | ||
it("validates when all codemagic environment vars are set", () => { | ||
const codemagic = new Codemagic(correctEnv) | ||
expect(codemagic.isPR).toBeTruthy() | ||
}) | ||
|
||
it("does not validate outside of codemagic", () => { | ||
const codemagic = new Codemagic({}) | ||
expect(codemagic.isPR).toBeFalsy() | ||
}) | ||
|
||
const envs = [ | ||
"FCI_PULL_REQUEST", | ||
"FCI_REPO_SLUG", | ||
"FCI_PROJECT_ID", | ||
"FCI_BUILD_ID", | ||
"BUILD_NUMBER", | ||
"FCI_PULL_REQUEST_NUMBER", | ||
] | ||
envs.forEach((key: string) => { | ||
let env = Object.assign({}, correctEnv) | ||
delete env[key] | ||
|
||
it(`does not validate when ${key} is missing`, () => { | ||
const codemagic = new Codemagic(env) | ||
expect(codemagic.isCI && codemagic.isPR).toBeFalsy() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters