Giter Site home page Giter Site logo

git-slack-hook's Introduction

Git post-receive hook for Slack

This is a bash script that posts a message into your Slack channel when changes are pushed.

Hook this script into post-receive for your git repositories.

How to Install

Note: some git repositories may be "bare". You'll know if your repo is bare or not by checking for a .git folder where your repo lives.

Download git-slack-hook onto the server which hosts your git repo.

For bare repos, copy/rename it as /path/to/your/repo/hooks/post-receive.

For normal/non-bare repos, copy/rename it as /path/to/your/repo/.git/hooks/post-receive.

Finally, chmod +x post-receive to allow the script to be executed.

Configuration

Add an Incoming WebHooks integration in your Slack by going to:

https://my.slack.com/services/new/incoming-webhook

For a normal/non-bare repo, configure the webhook URL

git config hooks.slack.webhook-url 'https://hooks.slack.com/services/...'

For a bare repo:

git config -f /path/to/your/repo/config hooks.slack.webhook-url 'https://hooks.slack.com/services/...'

Optional

Specify a channel to post in Slack instead of the default:

git config hooks.slack.channel '#general'

    '#channelname' - post to channel
    '@username' - direct message to user
    'groupname' - post to group

Specify a username to post as. If not specified, the default name incoming-webhook will be used:

git config hooks.slack.username 'git'

Specify an icon to display in Slack instead of the default:

git config hooks.slack.icon-url 'https://example.com/icon.png'

Specify an emoji icon to display in Slack instead of the default:

git config hooks.slack.icon-emoji ':twisted_rightwards_arrows:'

Specify a repository nice name that will be shown in messages:

git config hooks.slack.repo-nice-name 'My Awesome Repository'

Specify whether you want to show only the last commit (or all) when pushing multiple commits:

git config hooks.slack.show-only-last-commit true

Specify whether you want to show the body of the commit message as well as the title:

git config hooks.slack.show-full-commit true

Specify if you want to send only certain branches:

git config hooks.slack.branch-regexp regexp

Linking to Changesets

When the following parameters are set, revision hashes will be turned into links to a web view of your repository.

git config hooks.slack.repos-root '/path/to/repos'
git config hooks.slack.changeset-url-pattern 'http://yourserver/%repo_path%/changeset/%rev_hash%'

For example, if your repository is in /usr/local/repos/myrepo, set repos_root to /usr/local/repos/ and set changeset_url_pattern to http://yourserver/%repo_path%/changeset/%rev_hash% or whatever.

Links can also be created that summarize a list of commits:

git config hooks.slack.compare-url-pattern 'http://yourserver/%repo_path%/compare/%old_rev_hash%..%new_rev_hash%'

git-slack-hook's People

Contributors

adamdehaven avatar alessandroorru avatar chriseldredge avatar cuschk avatar dobesv avatar elimisteve avatar jjweston avatar jovandeginste avatar ksuther avatar legecha avatar lennartvdd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

git-slack-hook's Issues

Git Noob - Help setting up?

Hi, I'm pretty new to git. Can you do a quick run through on how to setup a post-receive hook to use this? I have never setup a hook script before.

Curl wasn't working for me

I cloned your script (excellent script by the way... super helpful!) but it didnt work for me out of the box.

My bash scripting isnt that great, but I did a load of debugging and I came to the conclusion that everything is working up until the curl command.

I replaced:

curl -s
-d "payload=$payload"
"$webhook_url"
>/dev/null

with

curl -X POST
--data-urlencode "payload=$payload"
$webhook_url
>/dev/null

and now it works fine.

Just in case anyone else has a similar problem.

Changesets

Can anyone explain for me the changesets and how they work? Maybe provide an example?

Change shebang to env

#!/bin/bash is not default interpreter. Better way is to change it to #!/bin/env bash

Working with a Locally Hosted Git

Would this work with a git repo that is hosted locally? I have this working on a server that I own but I recently started a new project that I'm working on only locally, any chance this could work that way ?

Documentation typo for repos-root parameter

In the 'Linking to changesets' section of the README.md file, the parameter for the repo's root is listed with an underscore, but it really should be a dash. This is true in the command code block, and in the example description below it.

I think git config hooks.slack.repos_root '/path/to/repos' should be git config hooks.slack.repos-root '/path/to/repos' and the mention of 'repos_root' later in the description should also be changed.

REQUEST: Honor newlines in commit message

Currently new lines are seemingly not converted over to \n. This would be very helpful for end user consumption (especially managers) when the notification hits Slack. Thanks.

Which Hook?

I would suggest you document which hook should trigger this script!

post-receive or post-update or something else?

Unusable in FreeBSD

GNU sed is not compatible with BSD sed. Didn't check all expressions, but first problem line is
sed ':a;N;$!ba;s/\n/\\n/g'.

Double-quotes in commit message

It appears that double-quotes in the commit message are not escaped, which means commit messages with double-quotes aren't properly formatted and are not sent to slack.

Commit messages garbled in notifications; sed-related

I've recently set up git-slack-hook on a machine running Debian squeeze. My config looks like this:

[hooks "slack"]
        webhook-url = https://hooks.slack.com/services/...
        channel = "#notify"
        username = Git
        icon-emoji = :twisted_rightwards_arrows:
        repo-nice-name = testrepo
        show-only-last-commit = false

Messages from the hook appear garbled, with all commit messages but the last one smashed together:

GitBOT [12:35]
3 new commits ​*pushed*​ to ​*master*​ in testrepo

fog;;;;;test: Fix typo in README fog;;;;;test: Modify README fog
----------------
Add script in Python

On my Mac (running OS X 10.10.3), the same configuration works correctly:

GitBOT [12:35]
3 new commits ​*pushed*​ to ​*master*​ in testrepo

fog
----------------
test: Fix typo in README

fog
----------------
test: Modify README

fog
----------------
Add script in Python

Closed investigation reveals that a certain call to sed works differently on my Debian machine and on my Mac.

On the Mac (as intended):

mac:testrepo targi$ git log --pretty=format:"%cN;;;;;%s" 27fac..73e2 | sed ':a;N;$!ba;s/\n/\\n/g'
fog;;;;;test: Fix typo in README
fog;;;;;test: Modify README
fog;;;;;Add script in Python

On the Debian machine (problem):

targi@debian:~/tmp/testrepo.git$ git log --pretty=format:"%cN;;;;;%s" 27fac..73e2 | sed ':a;N;$!ba;s/\n/\\n/g'
fog;;;;;test: Fix typo in README\nfog;;;;;test: Modify README\nfog;;;;;Add script in Python

On the Debian machine, I have GNU sed version 4.2.1.

As I don't need full commit messages in the notification, I replaced the call to sed with a no-op:

-sed ':a;N;$!ba;s/\n/\\n/g'
+sed ':a;N;$!ba;s/\n/\n/g'

This is obviously a work-around (breaks full commit messages). For reference, it's available at targi/git-slack-hook@2148553.

Post failed when '&' include

Post failed when '&' is included in commit message.
For example, "Erase user.name & user.email" for commit message.

& should be escaped to '%26'.

repo-root is not optional

If it's not set, readlink on the remote host throws an error:

Zähle Objekte: 5, Fertig.
Delta compression using up to 4 threads.
Komprimiere Objekte: 100% (5/5), Fertig.
Schreibe Objekte: 100% (5/5), 440 bytes | 0 bytes/s, Fertig.
Total 5 (delta 4), reused 0 (delta 0)
remote: readlink: fehlender Operand
remote: „readlink --help“ liefert weitere Informationen.
To user@host:/home/user/repo.git
   bdcbbab..aedc28e  master -> master

(sorry for the German text output, but I guess the error is clear anyways)

message garbled with multiple commits in push

my config contains ; in url:

[hooks "slack"]
        webhook-url = https://hooks.slack.com/services/
        channel = "#git"
        repos-root = /srv/git/
        changeset-url-pattern = "https://git.local/cgi-bin/gitweb.cgi?p=%repo_path%;a=commitdiff;h=%rev_hash%"
        show-full-commit = true
        show-only-last-commit = false
        username = git

this looks ok if one commit is pushed,
but shows Author Name;;;;;<https://git.local> when multiple commits are pushed.

i.e all but last commit is ok with push with multiple commits

probably related to #15 being not tested with multiple commits

How to disable optional hook configs

Thanks for this helpful script. Just wish to disable the following:

echo " git config hooks.slack.repos-root '/path/to/repos' #optional"

echo " git config hooks.slack.changeset-url-pattern 'http://yourserver/%repo_path%/changeset/%rev_hash%' #optional"

echo " git config hooks.slack.compare-url-pattern 'http://yourserver/%repo_path%/changeset/%old_rev_hash%..%new_rev_hash%' #optional"

echo " git config hooks.slack.branch-regexp 'regexp' #optional"

But I don't know what to do, should I just get rid of these lines in the code or put false as an argument?

Specify in documentation that Nice Names must be URL-encoded

I am using git to push webapp changes to a simple HTTP staging server, so I wanted the nice-name in Slack to conveniently link to the new live changes. I tried setting hooks.slack.repo-nice-name to the URL of my staging server but that broke the app (it wouldn't send anything on post-receive).

But! I just discovered that URL-encoding the nice-name works perfectly, and Slack (or some other step in the API chain) conveniently URL-decodes it as well.

For example:
git config hooks.slack.repo-nice-name 'http://127.0.0.1/path/to/page.html' does not work.
git config hooks.slack.repo-nice-name 'http%3A%2F%2F127.0.0.1%2Fpath%2Fto%2Fpage.html' works perfectly.

Multiple line commits

Cant seam to get the script to post multi line commits into slack!

Is it supposed to support this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.