Skip to content
Source

Cool Story About danger-kotlin

I'm having infrastructure weeks. Rewriting CI pipelines, setting up automations, enjoying it. Now the turn came to danger-kotlin, I was adding several new rules.

For those who don't know, it's such a utility that looks at a merge request, runs some checks, and posts a comment back to this merge request via the GitLab/GitHub API with the results of these checks.

For example, we have request size checks there. If it has more than N lines, the CI pipeline immediately fails with words like "split it into several, nobody will review this". And on all my runs where I corrected the Dangerfile config - everything worked well.

But in some requests, randomly, this job on CI would hang forever and fall by timeout. I never got around to figuring it out. And finally got to it. This is a hilarious story. All this time I thought that the kotlin-script just wasn't starting because the whole technology is a bit raw. But no.

We have GitLab, and it doesn't provide MR statistics via API. Git does, because it can always look at the diff and count the number of pluses/minuses. So we got Git.additions in the danger config, a property built into danger-kotlin.

As it turned out - the script started consistently, but when getting this property, everything hung. Watch closely.

Git.additions under the hood calls git diff. git diff itself under the hood shows results using less. In turn, the essence of less is that on a small diff it prints the result to the console and returns control to it, and on a large one it opens paging mode, from which you need to exit through :q like in vim. This is where it can't exit. Joker's trap.

How it was solved: instead of the built-in property, I wrote my own, which instead of git diff does git diff --shortstat and parses this one line like 3 files changed, 12 insertions(+), 5 deletions(-). Or you can run the git function in code with the --no-pager argument. But I don't need the whole diff, I only need the numbers. Why this isn't implemented this way initially is a mystery. Seems like it's time to make a pull request.