Skip to content

Commit

Permalink
added environment variable DANGER_SKIP_WHEN_EMPTY
Browse files Browse the repository at this point in the history
  • Loading branch information
ivankatliarchuk committed Oct 29, 2022
1 parent 2bcccbd commit 6b47827
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 17 deletions.
10 changes: 5 additions & 5 deletions source/commands/danger-process.ts
Expand Up @@ -45,17 +45,17 @@ program
.allowUnknownOption(true)

setSharedArgs(program)
program.action(process_name => (subprocessName = process_name)).parse(process.argv)
program.action((process_name) => (subprocessName = process_name)).parse(process.argv)

// The dynamic nature of the program means typecasting a lot
// use this to work with dynamic properties
const app = (program as any) as SharedCLI
const app = program as any as SharedCLI

if (process.env["DANGER_VERBOSE"] || app.verbose) {
global.verbose = true
}

getRuntimeCISource(app).then(source => {
getRuntimeCISource(app).then((source) => {
// This does not set a failing exit code
if (source && !source.isPR) {
console.log("Skipping Danger due to this run not executing on a PR.")
Expand All @@ -67,13 +67,13 @@ getRuntimeCISource(app).then(source => {
if (!platform) {
console.log(chalk.red(`Could not find a source code hosting platform for ${source.name}.`))
console.log(
`Currently Danger JS only supports GitHub and BitBucket Server, if you want other platforms, consider the Ruby version or help out.`
`Platform '${source.name}' is not supported with Danger JS, if you want other platforms, consider the Ruby version or help out.`
)
process.exitCode = 1
}

if (platform) {
jsonDSLGenerator(platform, source, app).then(dangerJSONDSL => {
jsonDSLGenerator(platform, source, app).then((dangerJSONDSL) => {
if (!subprocessName) {
// Just pipe it out to the CLI
const processInput = prepareDangerDSL(dangerJSONDSL)
Expand Down
6 changes: 3 additions & 3 deletions source/platforms/gitlab/_tests/_gitlab_api.test.ts
Expand Up @@ -181,9 +181,9 @@ describe("GitLab API", () => {
expected: "",
},
]
for (let el in parameters) {
let result = await api.getFileContents(parameters[el].filePath, api.repoMetadata.repoSlug, parameters[el].ref)
expect(result).toContain(parameters[el].expected)
for (let el of parameters) {
let result = await api.getFileContents(el.filePath, api.repoMetadata.repoSlug, el.ref)
expect(result).toContain(el.expected)
}
nockDone()
})
Expand Down
24 changes: 15 additions & 9 deletions source/runner/Executor.ts
Expand Up @@ -262,16 +262,22 @@ export class Executor {
let issueURL = undefined

if (!hasMessages || this.options.removePreviousComments) {
if (!hasMessages) {
this.log(`Found no issues or messages from Danger. Removing any existing messages on ${this.platform.name}.`)
if (process.env["DANGER_SKIP_WHEN_EMPTY"] === "true") {
this.d("Skip posting to platform:", results)
} else {
this.log(`'removePreviousComments' option specified. Removing any existing messages on ${this.platform.name}.`)
}
await this.platform.deleteMainComment(dangerID)
const previousComments = await this.platform.getInlineComments(dangerID)
for (const comment of previousComments) {
if (comment && comment.ownedByDanger) {
await this.deleteInlineComment(comment)
if (!hasMessages) {
this.log(`Found no issues or messages from Danger. Removing any existing messages on ${this.platform.name}.`)
} else {
this.log(
`'removePreviousComments' option specified. Removing any existing messages on ${this.platform.name}.`
)
}
await this.platform.deleteMainComment(dangerID)
const previousComments = await this.platform.getInlineComments(dangerID)
for (const comment of previousComments) {
if (comment && comment.ownedByDanger) {
await this.deleteInlineComment(comment)
}
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions source/runner/_tests/_executor.test.ts
Expand Up @@ -112,6 +112,28 @@ describe("setup", () => {
expect(platform.deleteMainComment).toBeCalled()
})

it("Configure to Skip a post deletion when there are no messages", async () => {
const platform = new FakePlatform()
const exec = new Executor(new FakeCI({}), platform, inlineRunner, defaultConfig, new FakeProcces())
let parameters: { skip: boolean; times: number }[] = [
{ skip: true, times: 0 },
{ skip: false, times: 1 },
]
for (let el of parameters) {
if (el.skip) {
process.env.DANGER_SKIP_WHEN_EMPTY = "true"
} else {
process.env.DANGER_SKIP_WHEN_EMPTY = "false"
}
const dsl = await defaultDsl(platform)
platform.deleteMainComment = jest.fn()
await exec.handleResults(emptyResults, dsl.git)

expect(process.env.DANGER_SKIP_WHEN_EMPTY).toBeDefined()
expect(platform.deleteMainComment).toBeCalledTimes(el.times)
}
})

it("Deletes a post when 'removePreviousComments' option has been specified", async () => {
const platform = new FakePlatform()
const exec = new Executor(
Expand Down

0 comments on commit 6b47827

Please sign in to comment.