danger-js
danger-js copied to clipboard
[Feature Request]: Optional exit code if *warnings* are found
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.
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.
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".
oh right... sorry my bad
@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.
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.
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) {
//?
}
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
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.
(I only just now saw your edited message, so it sounds like you had already figured it out! Haha)
When running locally you can execute a local script as well. We wrap danger for our devs too.