Skip to content

Commit

Permalink
Merge pull request #960 from gzaripov/master
Browse files Browse the repository at this point in the history
Add option to not publish checks in danger ci
  • Loading branch information
orta committed Nov 16, 2019
2 parents fb1c50b + 4d36bf7 commit 54d4c8a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 12 deletions.
1 change: 1 addition & 0 deletions source/commands/ci/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export const runRunner = async (app: SharedCLI, config?: Partial<RunnerConfig>)
passURLForDSL: app.passURLForDSL || false,
disableGitHubChecksSupport: !app.useGithubChecks,
failOnErrors: app.failOnErrors,
noPublishCheck: !app.publishCheck,
}

const processName = (app.process && app.process.split(" ")) || undefined
Expand Down
6 changes: 5 additions & 1 deletion source/commands/danger-ci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 21 additions & 7 deletions source/runner/Executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down Expand Up @@ -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)
}
}

/**
Expand Down
39 changes: 35 additions & 4 deletions source/runner/_tests/_executor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const defaultConfig: ExecutorOptions = {
dangerID: "123",
passURLForDSL: false,
failOnErrors: false,
noPublishCheck: false,
}

class FakeProcces {
Expand Down Expand Up @@ -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<any>((_, reject) => reject()))
const apiFailureMock = jest.fn().mockReturnValue(
new Promise<any>((_, reject) => reject())
)
platform.createInlineComment = apiFailureMock

let results = await exec.sendInlineComments(singleViolationSingleFileResults, dsl.git, [])
Expand Down Expand Up @@ -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: [],
}
Expand All @@ -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: [],
}
Expand Down Expand Up @@ -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()
})
})

0 comments on commit 54d4c8a

Please sign in to comment.