Skip to content

Instantly share code, notes, and snippets.

@bensteinberg
Last active March 1, 2019 16:56
Show Gist options
  • Save bensteinberg/b7d56fe64ac57b0ed9dfc2b9a461891b to your computer and use it in GitHub Desktop.
Save bensteinberg/b7d56fe64ac57b0ed9dfc2b9a461891b to your computer and use it in GitHub Desktop.
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
Copy link
Author

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

@bensteinberg
Copy link
Author

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