diff --git a/source/commands/ci/runner.ts b/source/commands/ci/runner.ts index 574399236..7209eaf43 100644 --- a/source/commands/ci/runner.ts +++ b/source/commands/ci/runner.ts @@ -67,6 +67,7 @@ export const runRunner = async (app: SharedCLI, config?: Partial) passURLForDSL: app.passURLForDSL || false, disableGitHubChecksSupport: !app.useGithubChecks, failOnErrors: app.failOnErrors, + noPublishCheck: !app.publishCheck, } const processName = (app.process && app.process.split(" ")) || undefined diff --git a/source/commands/danger-ci.ts b/source/commands/danger-ci.ts index 5298699b7..f21d17de2 100755 --- a/source/commands/danger-ci.ts +++ b/source/commands/danger-ci.ts @@ -5,7 +5,11 @@ import program from "commander" import setSharedArgs, { SharedCLI } from "./utils/sharedDangerfileArgs" import { runRunner } from "./ci/runner" -program.usage("[options]").description("Runs a Dangerfile in JavaScript or TypeScript.") +program + .usage("[options]") + .description("Runs a Dangerfile in JavaScript or TypeScript.") + .option("--no-publish-check", "Dont add danger check to PR", false) + setSharedArgs(program).parse(process.argv) const app = (program as any) as SharedCLI diff --git a/source/runner/Executor.ts b/source/runner/Executor.ts index 214fef1a8..384f6668f 100644 --- a/source/runner/Executor.ts +++ b/source/runner/Executor.ts @@ -56,6 +56,8 @@ export interface ExecutorOptions { disableGitHubChecksSupport?: boolean /** Fail if danger report contains failures */ failOnErrors?: boolean + /** Dont add danger check to PR */ + noPublishCheck?: boolean } // This is still badly named, maybe it really should just be runner? @@ -303,22 +305,34 @@ export class Executor { } } + if (!this.options.noPublishCheck) { + await this.updatePrStatus(!failed, issueURL, results, dangerID) + } + + // More info, is more info. + if (this.options.verbose) { + await this.handleResultsPostingToSTDOUT(results) + } + } + + async updatePrStatus( + passed: boolean | "pending", + issueURL: string | undefined, + results: DangerResults, + dangerID: string + ) { const urlForInfo = issueURL || this.ciSource.ciRunURL - const successPosting = await this.platform.updateStatus(!failed, messageForResults(results), urlForInfo, dangerID) + const successPosting = await this.platform.updateStatus(passed, messageForResults(results), urlForInfo, dangerID) + if (!successPosting && this.options.verbose) { this.log("Could not add a commit status, the GitHub token for Danger does not have access rights.") this.log("If the build fails, then danger will use a failing exit code.") } - if (!successPosting && failed) { + if (!successPosting && !passed) { this.d("Failing the build due to handleResultsPostingToPlatform not successfully setting a commit status") process.exitCode = 1 } - - // More info, is more info. - if (this.options.verbose) { - await this.handleResultsPostingToSTDOUT(results) - } } /** diff --git a/source/runner/_tests/_executor.test.ts b/source/runner/_tests/_executor.test.ts index a29e5cc4b..221057e01 100644 --- a/source/runner/_tests/_executor.test.ts +++ b/source/runner/_tests/_executor.test.ts @@ -26,6 +26,7 @@ const defaultConfig: ExecutorOptions = { dangerID: "123", passURLForDSL: false, failOnErrors: false, + noPublishCheck: false, } class FakeProcces { @@ -171,7 +172,9 @@ describe("setup", () => { const platform = new FakePlatform() const exec = new Executor(new FakeCI({}), platform, inlineRunner, defaultConfig, new FakeProcces()) const dsl = await defaultDsl(platform) - const apiFailureMock = jest.fn().mockReturnValue(new Promise((_, reject) => reject())) + const apiFailureMock = jest.fn().mockReturnValue( + new Promise((_, reject) => reject()) + ) platform.createInlineComment = apiFailureMock let results = await exec.sendInlineComments(singleViolationSingleFileResults, dsl.git, []) @@ -267,7 +270,10 @@ describe("setup", () => { const dsl = await defaultDsl(platform) const previousResults = { fails: [], - warnings: [{ message: "1", file: "1.swift", line: 1 }, { message: "2", file: "2.swift", line: 2 }], + warnings: [ + { message: "1", file: "1.swift", line: 1 }, + { message: "2", file: "2.swift", line: 2 }, + ], messages: [], markdowns: [], } @@ -291,13 +297,19 @@ describe("setup", () => { const dsl = await defaultDsl(platform) const previousResults = { fails: [], - warnings: [{ message: "1", file: "1.swift", line: 1 }, { message: "2", file: "2.swift", line: 2 }], + warnings: [ + { message: "1", file: "1.swift", line: 1 }, + { message: "2", file: "2.swift", line: 2 }, + ], messages: [], markdowns: [], } const newResults = { fails: [], - warnings: [{ message: "1", file: "1.swift", line: 2 }, { message: "2", file: "2.swift", line: 3 }], + warnings: [ + { message: "1", file: "1.swift", line: 2 }, + { message: "2", file: "2.swift", line: 3 }, + ], messages: [], markdowns: [], } @@ -370,4 +382,23 @@ describe("setup", () => { await exec.handleResults(failsResults, dsl.git) expect(platform.updateStatus).toBeCalledWith(expect.anything(), expect.anything(), ci.ciRunURL, expect.anything()) }) + + it("Doesn't update status when check publishing feature is disabled", async () => { + const platform = new FakePlatform() + const ci: any = new FakeCI({}) + ci.ciRunURL = "https://url.com" + + const config = { + ...defaultConfig, + noPublishCheck: true, + } + + const exec = new Executor(ci, platform, inlineRunner, config, new FakeProcces()) + const dsl = await defaultDsl(platform) + platform.updateOrCreateComment = jest.fn() + platform.updateStatus = jest.fn() + + await exec.handleResults(failsResults, dsl.git) + expect(platform.updateStatus).not.toBeCalled() + }) })