From 6ebd330c40e9cf80c8eb58d6487bee9c9101ee38 Mon Sep 17 00:00:00 2001 From: Jonas Boberg Date: Mon, 11 Oct 2021 14:33:02 +0900 Subject: [PATCH 1/2] Add support for Xcode Cloud CI. Closes #1160 --- README.md | 2 +- source/ci_source/providers/XcodeCloud.ts | 46 ++++++++++ .../providers/_tests/_xcodeCloud.test.ts | 84 +++++++++++++++++++ source/ci_source/providers/index.ts | 3 + 4 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 source/ci_source/providers/XcodeCloud.ts create mode 100644 source/ci_source/providers/_tests/_xcodeCloud.test.ts diff --git a/README.md b/README.md index 46d4ccbba..d396bd273 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ You can use Danger to codify your team's norms, leaving humans to think about ha Danger JS works with GitHub, BitBucket Server, BitBucket Cloud for code review, then with: Travis CI, GitLab CI, Semaphore, Circle CI, GitHub Actions, Jenkins, Docker Cloud, Bamboo, Bitrise, surf-build, Codeship, Drone, Buildkite, Nevercode, buddybuild, Buddy.works, TeamCity, Visual Studio Team Services, Screwdriver, Concourse, Netlify, CodeBuild, -Codefresh, AppCenter, BitBucket Pipelines, Cirrus CI, or Codemagic. +Codefresh, AppCenter, BitBucket Pipelines, Cirrus CI, Codemagic or Xcode Cloud. [![npm](https://img.shields.io/npm/v/danger.svg)](https://www.npmjs.com/package/danger) [![Build Status](https://travis-ci.org/danger/danger-js.svg?branch=main)](https://travis-ci.org/danger/danger-js) diff --git a/source/ci_source/providers/XcodeCloud.ts b/source/ci_source/providers/XcodeCloud.ts new file mode 100644 index 000000000..86f4067a8 --- /dev/null +++ b/source/ci_source/providers/XcodeCloud.ts @@ -0,0 +1,46 @@ +import { Env, CISource } from "../ci_source" +import { ensureEnvKeysExist } from "../ci_source_helpers" + +/** + * ### CI Setup + * + * Install dependencies and add a danger step to the custom build scripts. + * See the Xcode Cloud documentation [here](https://developer.apple.com/documentation/xcode/writing-custom-build-scripts) + * + * ### Token Setup + * + * Setup the acesss token (for github `DANGER_GITHUB_API_TOKEN`) environment variable for your workflow. + * See the Xcode Cloud documentation [here](https://developer.apple.com/documentation/xcode/xcode-cloud-workflow-reference#Custom-Environment-Variables) + */ +export class XcodeCloud implements CISource { + constructor(private readonly env: Env) { } + + get name(): string { + return "Xcode Cloud" + } + + get isCI(): boolean { + const mustHave = ["CI", "CI_XCODEBUILD_ACTION"] + return ( + ensureEnvKeysExist(this.env, mustHave) && + this.env.CI == "TRUE" + ) + } + + get isPR(): boolean { + const mustHave = ["CI_PULL_REQUEST_NUMBER", "CI_PULL_REQUEST_TARGET_REPO"] + return ensureEnvKeysExist(this.env, mustHave) + } + + get repoSlug(): string { + return this.env.CI_PULL_REQUEST_TARGET_REPO + } + + get pullRequestID(): string { + return this.env.CI_PULL_REQUEST_NUMBER + } + + get commitHash(): string { + return this.env.CI_COMMIT + } +} diff --git a/source/ci_source/providers/_tests/_xcodeCloud.test.ts b/source/ci_source/providers/_tests/_xcodeCloud.test.ts new file mode 100644 index 000000000..22aa3acb1 --- /dev/null +++ b/source/ci_source/providers/_tests/_xcodeCloud.test.ts @@ -0,0 +1,84 @@ +import { XcodeCloud } from "../XcodeCloud" +import { getCISourceForEnv } from "../../get_ci_source" + +const correctEnv = { + CI: "TRUE", + CI_XCODEBUILD_ACTION: "build", + CI_PULL_REQUEST_TARGET_REPO: "someone/something", + CI_PULL_REQUEST_NUMBER: "999", +} + +describe("being found when looking for CI", () => { + it("finds XcodeCloud with the right ENV", () => { + const ci = getCISourceForEnv(correctEnv) + expect(ci).toBeInstanceOf(XcodeCloud) + }) +}) + +describe(".isCI", () => { + it("validates when all XcodeCloud environment vars are set", () => { + const xcodeCloud = new XcodeCloud(correctEnv) + expect(xcodeCloud.isCI).toBeTruthy() + }) + + it("does not validate", () => { + const xcodeCloud = new XcodeCloud({}) + expect(xcodeCloud.isCI).toBeFalsy() + }) +}) + +describe(".isPR", () => { + it("validates when all XcodeCloud environment vars are set", () => { + const xcodeCloud = new XcodeCloud(correctEnv) + expect(xcodeCloud.isPR).toBeTruthy() + }) + + it("does not validate outside of XcodeCloud", () => { + const xcodeCloud = new XcodeCloud({}) + expect(xcodeCloud.isPR).toBeFalsy() + }) + + const envs = ["CI_PULL_REQUEST_TARGET_REPO", "CI_PULL_REQUEST_NUMBER"] + envs.forEach((key: string) => { + let env = { + CI_PULL_REQUEST_TARGET_REPO: "someone/something", + CI_PULL_REQUEST_NUMBER: "999", + } + env[key] = null + + it(`does not validate when ${key} is missing`, () => { + const xcodeCloud = new XcodeCloud(env) + expect(xcodeCloud.isPR).toBeFalsy() + }) + }) +}) + +describe(".pullRequestID", () => { + it("pulls it out of the env", () => { + const xcodeCloud = new XcodeCloud({ CI_PULL_REQUEST_NUMBER: "999" }) + expect(xcodeCloud.pullRequestID).toEqual("999") + }) +}) + +describe(".repoSlug", () => { + it("pulls it out of the env", () => { + const xcodeCloud = new XcodeCloud({ CI_PULL_REQUEST_TARGET_REPO: "someone/something" }) + expect(xcodeCloud.repoSlug).toEqual("someone/something") + }) +}) + +describe("commit hash", () => { + it("returns correct commit hash when present", () => { + const env = { + ...correctEnv, + CI_COMMIT: "1234abc", + } + const xcodeCloud = new XcodeCloud(env) + expect(xcodeCloud.commitHash).toEqual("1234abc") + }) + + it("returns no commit hash when not present", () => { + const xcodeCloud = new XcodeCloud(correctEnv) + expect(xcodeCloud.commitHash).toBeUndefined() + }) + }) diff --git a/source/ci_source/providers/index.ts b/source/ci_source/providers/index.ts index a9eea6173..fdcb1a2a4 100644 --- a/source/ci_source/providers/index.ts +++ b/source/ci_source/providers/index.ts @@ -26,6 +26,7 @@ import { Surf } from "./Surf" import { TeamCity } from "./TeamCity" import { Travis } from "./Travis" import { VSTS } from "./VSTS" +import { XcodeCloud } from "./XcodeCloud" const providers = [ FakeCI, @@ -56,6 +57,7 @@ const providers = [ Cirrus, Bamboo, Codemagic, + XcodeCloud, ] // Mainly used for Dangerfile linting @@ -85,6 +87,7 @@ const realProviders = [ Cirrus, Bamboo, Codemagic, + XcodeCloud, ] export { providers, realProviders } From 2860fcca987d2ad91008a4ccd1a49fd17a796231 Mon Sep 17 00:00:00 2001 From: Jonas Boberg Date: Mon, 11 Oct 2021 16:40:19 +0900 Subject: [PATCH 2/2] Fix test indentation. --- .../providers/_tests/_xcodeCloud.test.ts | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/source/ci_source/providers/_tests/_xcodeCloud.test.ts b/source/ci_source/providers/_tests/_xcodeCloud.test.ts index 22aa3acb1..325701bed 100644 --- a/source/ci_source/providers/_tests/_xcodeCloud.test.ts +++ b/source/ci_source/providers/_tests/_xcodeCloud.test.ts @@ -68,17 +68,17 @@ describe(".repoSlug", () => { }) describe("commit hash", () => { - it("returns correct commit hash when present", () => { - const env = { - ...correctEnv, - CI_COMMIT: "1234abc", - } - const xcodeCloud = new XcodeCloud(env) - expect(xcodeCloud.commitHash).toEqual("1234abc") - }) + it("returns correct commit hash when present", () => { + const env = { + ...correctEnv, + CI_COMMIT: "1234abc", + } + const xcodeCloud = new XcodeCloud(env) + expect(xcodeCloud.commitHash).toEqual("1234abc") + }) - it("returns no commit hash when not present", () => { - const xcodeCloud = new XcodeCloud(correctEnv) - expect(xcodeCloud.commitHash).toBeUndefined() - }) + it("returns no commit hash when not present", () => { + const xcodeCloud = new XcodeCloud(correctEnv) + expect(xcodeCloud.commitHash).toBeUndefined() }) +})