Skip to content

Instantly share code, notes, and snippets.

@molaschi
Created June 4, 2014 17:10
Show Gist options
  • Save molaschi/f1a296c6f09d2aa92875 to your computer and use it in GitHub Desktop.
Save molaschi/f1a296c6f09d2aa92875 to your computer and use it in GitHub Desktop.
Integrate Stash with Slack using webhooks

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

@RCheesley
Copy link

May I suggest also explaining that when you copy the URL for the repo, to not include /browse :) Thank you so much, this works perfectly 👍

@phillpafford
Copy link

Could we get a condition to Pull Request and Merged? When the PR come in I get the Slack alert and it has the link to the Jira issue, when a Merge happens I get the same Slask alert but it has all zeros and no message. Example of both messages

-- Pull Request
Push on REPO by ppafford me@balrg.com
[REPO:refs/heads/feature-PROJ-123] 6165ef95a52955f49596483f1f80f8a0f0b44f3d: PROJ-123: Generate some meaningful mock data

-- Merged
Push on REPO by ppafford me@balrg.com
[REPO:refs/heads/feature-PROJ-123] 0000000000000000000000000000000000000000:

@perlsaiyan
Copy link

This only sends the last commit in a push, any way to include all commits from a push that has multiple changes?

@msouth
Copy link

msouth commented Feb 10, 2017

@perlsaiyan I think that is what this gist is doing: https://gist.github.com/linjer/2ee6146d3b320cbe9225

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