This repository was archived by the owner on Mar 25, 2024. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathgit-post-receive-hook
More file actions
executable file
·94 lines (76 loc) · 3.15 KB
/
Copy pathgit-post-receive-hook
File metadata and controls
executable file
·94 lines (76 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python
#
# Simple post-receive git hook to install on your git server
# Make sure you have dogapi install; usually fixed by doing
# sudo easy_install dogapi
#
# To install:
# 1. copy this file to your repository, in .git/hooks/
# 2. rename the file from git-post-receive-hook to post-receive
# 3. make sure ls -l .git/hooks/ contains "post-receive"
# 4. make that file executable (chmod 755 .git/hooks/post-receive)
# 5. set up your datadog keys with git:
# git config datadog.api abcdefghi123456
# git config datadog.application jklmnop7890123
import logging
from logging.handlers import SysLogHandler
logger = logging.getLogger("datadogit")
logger.addHandler(SysLogHandler())
try:
import os
import sys
import re
from hashlib import sha1
from socket import gethostname
from dogapi import dog_http_api as dog
dog.api_key = os.popen("git config datadog.api").read().strip()
dog.application_key = os.popen("git config datadog.application").read().strip()
# Check if bare repository
isbare = os.popen("git config core.bare").read().strip()
logger.debug("Notifying Datadog with %s/%s" % (dog.api_key, dog.application_key))
# Extract oldrev, newrev, refs
args = sys.stdin.read().split()
assert args is not None and len(args) == 3, args
# Extract the log entry for the commits
p = os.popen("git log --graph --format=format:'%%h %%ce %%s' %s..%s" % (args[0], args[1]))
event_data = p.read()
commit_count = event_data.count("\n")
# Events are limited to 4KB in size
if len(event_data) > 4000: event_data = event_data[:3980] + "..."
# Surround event_data with $$$ to trigger fixed-width font treatment
event_data = "$$$\n" + event_data + "\n$$$"
event_title = ""
# Used to aggregate events together, best left alone.
event_object = sha1(gethostname() + ":" + os.getcwd() + ":" + args[2]).hexdigest()
# branch name
branch_name = args[2].split("/")[-1]
# repo name
repo_name = "repository"
try:
if isbare == "true":
# Get the basename of CWD, but strip '/hooks' from the end if we happen to be in there.
repo_name = os.path.basename(re.sub('/?hooks/?$', '', os.getcwd()))
# Could also be done as so:
# p = os.popen("basename $(echo $(pwd) | sed 's/hooks$//')")
# repo_name = p.read()
else:
# go 2 dirs up and use pwd as the repo name, not great but should work
repo_name = os.path.basename(os.path.realpath(os.path.join(__file__, "..", "..", "..")))
except:
# fail
pass
# Post to Datadog
commit = "Commit"
if commit_count > 1:
commit = "Commits"
event_title = "%s on %s (%s)" % (commit, repo_name, branch_name)
dog.event(event_title, event_data,
event_type="commit",
source_type_name="git",
event_object=event_object)
logger.debug("Datadog has been notified")
except ImportError:
logger.warn("Make sure the dogapi module is installed; easy_install dogapi should do the trick")
except:
# If something happens, survive
logger.exception("Error while notifying Datadog")