Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Integrate Stash with Slack using webhooks

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

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
# Notify commits to slack

# Set Slack channel, passed as 2nd argument
channel=$2
icon=$4

# Get push information from Git STDIN
read from_ref to_ref ref_name
if [[ $ref_name == *master* ]]
then
  # Generate commit list
  pushed_commits=`git log --format="%H %h \"%an\" \"%s\" \"%ar\"" $from_ref..$to_ref`
  commit_count=`echo "$pushed_commits" | wc -l`

  text="[<$3|${STASH_REPO_NAME}>] ${STASH_USER_NAME} <$STASH_USER_EMAIL> pushed $commit_count commit"
  if [ $commit_count -gt 1 ]; then text="${text}s"; fi
  text="${text} to ${ref_name#*/}"

  payload="payload={\"channel\":\"$channel\",\"text\":\"$text\",\"icon_emoji\":\"$icon\",\"attachments\":[{\"fallback\":\"Table of commits in this push.\",\"color\":\"good\",\"fields\":["

  # Indexes
  # [0] Full hash
  # [1] Abbrev hash
  # [2] Author name
  # [3] Commit message
  # [4] Author relative date
  comm_desc_col="{\"title\":\"Commit\",\"short\":true,\"value\":\""
  author_time_col="{\"title\":\"Author\",\"short\":true,\"value\":\""
  while read -r line; do
      eval array=($line)
      commit_link="<$3/commits/${array[0]}|${array[1]}>"
      comm_desc_col="${comm_desc_col}${commit_link}: ${array[3]}\n"
      author_time_col="${author_time_col}${array[2]}, ${array[4]}\n"
  done <<< "$pushed_commits"

  comm_desc_col="${comm_desc_col}\"},"
  author_time_col="${author_time_col}\"}"

  payload="${payload}${comm_desc_col}${author_time_col}]}]}"
  curl -X POST --data-urlencode "$payload" $1
fi

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 (🚀)
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
You can’t perform that action at this time.