Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Set up a daily notification in Slack for changes in a remote git repo on Heroku Scheduler
# set up an incoming webhook on Slack (https://api.slack.com/incoming-webhooks), then
heroku create my-notifier
heroku config:set REPO='https://github.com/some/repo.git' -a my-notifier
heroku config:set SINCE='1 day ago' -a my-notifier
heroku config:set WATCHFILES='file1 file2 /full/path/to/file3' -a my-notifier
heroku config:set WEBHOOK='https://hooks.slack.com/services/my/web/hook' -a my-notifier
heroku config:set USERNAME='a user who notifies' -a my-notifier
heroku config:set EMOJI='ghost' -a my-notifier
heroku addons:create scheduler:standard -a my-notifier
# note the ID of the scheduler, then
heroku addons:open scheduler-with-some-id
# and add a new job to run the following command line daily:
# git clone $REPO . -q ; CHANGED=`git log --pretty=format: --name-only --since="$SINCE" | grep -v -e '^$' | sort | uniq` ; MATCHED=`for FILE in $WATCHFILES ; do grep $FILE <<< "$CHANGED" ; done` ; [ -n "$MATCHED" ] && curl -X POST -H 'Content-type: application/json' --data '{"text":"Files changed in <'"$REPO"'|'"`basename ${REPO%.*}`"'> since '"$SINCE"':\n'"$(echo $MATCHED | sed 's/[^ ][^ ]*/`&`\\n/g')"'", "username": "'"$USERNAME"'", "icon_emoji": ":'"$EMOJI"':"}' "$WEBHOOK"
@bensteinberg

This comment has been minimized.

Copy link
Owner Author

bensteinberg commented Mar 1, 2019

There's probably a way to do this with the GitHub API, but this approach should work with non-GitHub remotes.

@bensteinberg

This comment has been minimized.

Copy link
Owner Author

bensteinberg commented Mar 1, 2019

Note that Heroku Scheduler (https://devcenter.heroku.com/articles/scheduler) says that

Scheduler is a free add-on with no guarantee that jobs will execute at their scheduled time, or at all:

In very rare instances, a job may be skipped.
In very rare instances, a job may run twice.

That is, with a daily run and $SINCE set to 1 day ago, it is possible to miss a notification.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.