In our current project we have been converting Groovy code to Java. To remind the team about remaining Groovy code lines count, I have set up a bash script using Unix magic and Slack incoming webhooks. Here it goes:

$ cat groovyLineCount.sh

#!/bin/sh
cd /path/to/repo   # assuming the repo is cloned to this dir
git checkout master && git pull
counts=$(find . -name *.groovy -exec wc -l {} \; | awk -F '|' ' {sum += $1} END {print sum}')
curl -X POST --data-urlencode 'payload={"channel": "#engineering", "text": "Remaining groovy code lines: '$counts'", "icon_emoji": "💩"}' https://hooks.slack.com/incoming-webhook-url

Slack provides a quick setup for incoming webhooks to notify a channel (which can be found here). The script simply executes following steps:

  • Go to repository directory and pull latest changes
  • Find all files with .groovy extension
  • Print line counts of Groovy files with wc -l (prints a line for each file, first line count and then file name)
  • Parse outputs of wc -l with awk, take sum of the first column, line counts, and print the sum in the end.
  • Make a POST request to Slack incoming webhook endpoint with proper message and emoji to post a message in #engineering channel.

Here is how it looks like:

Then, I have added cron jobs in a server to execute this script multiple times a day. You can add new cron jobs with the command crontab -e. Here is my configured cron jobs:

$ crontab -l
0 6 * * 1-5 sh /home/ec2-user/groovyLineCount.sh
0 9 * * 1-5 sh /home/ec2-user/groovyLineCount.sh
0 12 * * 1-5 sh /home/ec2-user/groovyLineCount.sh
0 15 * * 1-5 sh /home/ec2-user/groovyLineCount.sh

Each line follows this pattern:

[min] [hour] [day of month] [month] [day of week] [script or command]

This configuration ensures that the script runs at 6.00, 9.00, 12.00 and 15.00 every weekday (1-5 means Monday to Friday). Not on weekend, not at midnight; just when it’s required.

We have quite a lot of Groovy code, so reminding the existence of the crap in the project is a good motivation to get rid of it.