#!/bin/bash # $1 => Slack hook URL # $2 => Channel name # $3 => URL to web repo browser for base project slack_url="$1" channel="$2" browser_url="$3" # Debugging output in /tmp/stash_slack.txt # pwd > /tmp/stash_slack.txt text="" # Each column shown in the payload. comm_desc_col="{\"title\":\"Commit\",\"short\":true,\"value\":\"" author_time_col="{\"title\":\"Author\",\"short\":true,\"value\":\"" first=1 # For every ref updated. while read from_ref to_ref ref_name; do echo ${from_ref} ${to_ref} ${ref_name} >> /tmp/stash_slack.txt args=("log" '--format=%H %h "%an" "%s" "%ar"' "${from_ref}..${to_ref}") commits=$(git "${args[@]}") # echo "${commits}" >> /tmp/stash_slack.txt commit_count=`echo "${commits}" | wc -l` if [ ${first} -eq 1 ]; then first=0 text="[<${browser_url}|${STASH_REPO_NAME}>] ${STASH_USER_NAME} <$STASH_USER_EMAIL> pushed: $commit_count commit" else text="${text}, ${commit_count} commit" fi if [ $commit_count -gt 1 ]; then text="${text}s"; fi text="${text} to ${ref_name#*/}" # Indexes # [0] Full hash # [1] Abbrev hash # [2] Author name # [3] Commit message # [4] Author relative date while read -r line; do eval array=($line) commit_link="<${browser_url}/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 <<< "$commits" done payload="payload={\"channel\":\"${channel}\",\"text\":\"${text}\",\"attachments\":[{\"fallback\":\"Table of commits in this push.\",\"color\":\"good\",\"fields\":[" # Close off all remaining blocks comm_desc_col="${comm_desc_col}\"}," author_time_col="${author_time_col}\"}" payload="${payload}${comm_desc_col}${author_time_col}]}]}" # Deliver! # echo "$payload" >> /tmp/stash_slack.txt curl -X POST --data-urlencode "$payload" ${slack_url} # cat /tmp/stash_slack.txt