Skip to content

Commit

Permalink
Merge pull request #1332 from ivankatliarchuk/issue_1330
Browse files Browse the repository at this point in the history
Gitlab: added environment variable DANGER_SKIP_WHEN_EMPTY
  • Loading branch information
orta committed Nov 1, 2022
2 parents 2bcccbd + 886616b commit 0c8804f
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 16 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Expand Up @@ -16,8 +16,8 @@

<!-- Your comment below this -->
- Append random string to danger-results.json and danger-dsl.json files to better support concurrent processes #1311

- Gitlab package moved to a new home "@gitbreaker/*" [#1301](https://github.com/danger/danger-js/issues/1301) [@ivankatliarchuk]
- Gitlab: add support for skipping remove of messages when empty [#1330](https://github.com/danger/danger-js/issues/1330) [@ivankatliarchuk]
- Gitlab: package moved to a new home "@gitbreaker/*" [#1301](https://github.com/danger/danger-js/issues/1301) [@ivankatliarchuk]
- GitLab: Improve support for MRs from forks [#1319](https://github.com/danger/danger-js/pull/1319) [@ivankatliarchuk]
- GitLab: Added provider tests [#1319](https://github.com/danger/danger-js/pull/1319) [@ivankatliarchuk]

Expand Down
4 changes: 2 additions & 2 deletions source/commands/danger-process.ts
Expand Up @@ -49,7 +49,7 @@ program.action(process_name => (subprocessName = process_name)).parse(process.ar

// 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
Expand All @@ -67,7 +67,7 @@ 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
}
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.log(`Skip posting to platform ${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)
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 0c8804f

Please sign in to comment.