danger-js icon indicating copy to clipboard operation
danger-js copied to clipboard

[Feature Request]: Optional exit code if *warnings* are found

Open Taucher2003 opened this issue 3 years ago • 10 comments

Hey, I think it would be nice if we could define an exit code if ⚠️ warnings, but no fails are found.

There is already an option to let the process exit with code 1 if fails are found.
GitLab allows to define exit codes with which the job is allowed to fail. (https://docs.gitlab.com/ee/ci/yaml/#allow_failureexit_codes)

If we can run danger in a way, it would exit with code 2 if warnings are found, we can display the danger job in the GitLab Pipeline as passed with warnings instead of just passed.

Taucher2003 avatar Oct 04 '21 18:10 Taucher2003

I tried adding --failOnErrors into the yarn danger ci so it became yarn danger ci --failOnErrors after reading into this https://github.com/danger/danger-js/blob/main/source/runner/Executor.ts#L58

for me it did the trick.

gunturaf avatar Mar 23 '22 12:03 gunturaf

Hey @gunturaf

This works for errors/fails, I'm talking about warnings. In combination with a correct GitLab Pipeline Setup, it would enable us to have the warning status represented in the pipeline itself. As warnings don't fail the build, the job is just marked as "passed" and we can't mark it as "passed with warnings".

Taucher2003 avatar Mar 23 '22 17:03 Taucher2003

oh right... sorry my bad

gunturaf avatar Mar 24 '22 02:03 gunturaf

@Taucher2003 I just added this to our Dangerfile (at my company). If you don't have any async checks, you can just check results.warnings at the end of your Dangerfile, and add a if (results?.warnings?.length > 0) { fail(…); }

If you do have async checks it's a little bit clunkier, but it's only about 100 lines of code.

fbartho avatar Apr 08 '22 16:04 fbartho

you can just check results.warnings at the end of your Dangerfile, and add a if (results?.warnings?.length > 0) { fail(…); }

Still not solving the problem, that just adds a fail if there are some warnings. It does not solve the problem of detecting the case when only warnings but no fails are found.

Taucher2003 avatar Apr 08 '22 18:04 Taucher2003

What would you like to do if that case occurs?

You could process.exit there or something else?

if (results?.warnings?.length > 0 && (results?.errors || []).length === 0) {
  //?
}

fbartho avatar Apr 08 '22 21:04 fbartho

It is something that needs to be handled by danger and not the dangerfile. Using process.exit() in the dangerfile will add a fail "node failed" and ignore the exit code given.

Something that would probably work is creating a file and after danger has finished, I could check if that specific file exists. But that will only work in the CI environment and not when running danger locally

Taucher2003 avatar Apr 08 '22 23:04 Taucher2003

In your wrapping script that executes danger you could write to a file on disk (in my suggested if block) and detect that files existence after danger exits (on success)

if that file exists then infer warnings but no errors existed, and then you can provide whatever status code you want.

fbartho avatar Apr 09 '22 00:04 fbartho

(I only just now saw your edited message, so it sounds like you had already figured it out! Haha)

fbartho avatar Apr 09 '22 00:04 fbartho

When running locally you can execute a local script as well. We wrap danger for our devs too.

fbartho avatar Apr 09 '22 00:04 fbartho