Skip to content

Instantly share code, notes, and snippets.

@phillpafford
Forked from molaschi/stash_slack_integration.md
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phillpafford/50464e29e3d81869fd28 to your computer and use it in GitHub Desktop.
Save phillpafford/50464e29e3d81869fd28 to your computer and use it in GitHub Desktop.

This is a short article on how we integrate stash and slack in openmind

First of all i assume you have:

  • a working stash installation
  • a repository you to notify slack on pushes
  • stash user with administration priviledges
  • full access to the server (linux) where stash is installed on
  • a team configured on slack
  • slack user with administration priviledges

Prepare your stash server

Access your stash server and create a script

vim /home/appuser/stash_slack.sh

Paste the following:

#!/bin/bash

echo "Notify commits to slack"

channel=$2
icon=$4
payload="payload={\"channel\":\"$channel\",\"username\":\"webhookbot\",\"text\":\"Push on ${STASH_REPO_NAME} by ${STASH_USER_NAME} <$STASH_USER_EMAIL>\",\"icon_emoji\":\"$icon\",\"attachments\":["
while read from_ref to_ref ref_name; do
  message=`git log --format=%B -n 1 ${to_ref}`
  title="[$STASH_REPO_NAME:$ref_name] <$3/commits/$to_ref|$to_ref>: $message"
  payload="$payload{\"fallback\":\"$title\",\"pretext\":\"$title\"},"
done

payload="${payload%?}]}"
curl -X POST --data-urlencode "$payload" $1

and save.

Make it executable

chmod +x /home/appuser/stash_slack.sh

Configure Slack

  1. Access to slack: https://YOUR_TEAM.slack.com/services/new (replace YOUR_TEAM with your team name)
  2. Select "Incoming WebHooks"
  3. Choose a channel from drop-down and click "Add incoming WebHook"
  4. You have a new webhook configured! Copy the URL under "Your Unique Webhook URL"

Configure Stash

  1. Access to stash web interface
  2. Go to your repository
  3. Click on Settings
  4. Click on Hooks
  5. Click on "Add Hook" button and then on "Search"
  6. Search for "External Hooks" and click "Install"
  7. Once installed, go back to Hooks page in your repository
  8. Click on "Enable" on the "External Post Receive Hook" row (not pre-receive)
  9. Put the full path to the stash_slack.sh executable script in "Executable" field: /home/appuser/stash_slack.sh
  10. Fill "Positional Parameters" with:
    • 1st line: Web hook url you copied at last point in previous paragraph
    • 2nd line: Channel name (#general)
    • 3rd line: full path to your web interface repository page (without trailing slash)
    • 4th line: emoji for your notification (:rocket:)
https://openmindonline.slack.com/services/hooks/incoming-webhook?token=6PPy9nEngOuRhl79XbSPrRCK
#general
https://your_stash_server/projects/ACME/repos/foo
:rocket:

Everything should work! Try to push something!

HTH

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment