Skip to content

Commit

Permalink
Add support for Xcode Cloud CI.
Browse files Browse the repository at this point in the history
Closes #1160
  • Loading branch information
bobergj committed Oct 11, 2021
1 parent a2548c2 commit 6ebd330
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
46 changes: 46 additions & 0 deletions source/ci_source/providers/XcodeCloud.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
84 changes: 84 additions & 0 deletions source/ci_source/providers/_tests/_xcodeCloud.test.ts
Original file line number Diff line number Diff line change
@@ -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()
})
})
3 changes: 3 additions & 0 deletions source/ci_source/providers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -56,6 +57,7 @@ const providers = [
Cirrus,
Bamboo,
Codemagic,
XcodeCloud,
]

// Mainly used for Dangerfile linting
Expand Down Expand Up @@ -85,6 +87,7 @@ const realProviders = [
Cirrus,
Bamboo,
Codemagic,
XcodeCloud,
]

export { providers, realProviders }

0 comments on commit 6ebd330

Please sign in to comment.