post-receive-delivery: add a deliver command
[git-central.git] / server / post-receive-hudson
1 #!/bin/bash
2 #
3 # Copyright (c) 2008 Stephen Haberman
4 #
5 # This hook creates new jobs for each branch in the Hudson continuous
6 # integration tool. Besides creating the job if needed, the user who pushed is
7 # added to the job's email list if they were not already there.
8 #
9 # Config
10 # ------
11 # hooks.post-receive-hudson.url
12 #   The url to hudson, e.g. http://internalbox/hudson
13 # hooks.post-receive-hudson.ignored
14 #   Whitespace separated list of branches to not make jobs for.
15 # USER_EMAIL
16 #   Environment variable that should be set by your repository-specific
17 #   post-receive hook. E.g. export USER_EMAIL=${USER}@example.com. If
18 #   unset, defaults to the email by of the pushed commit.
19 #
20
21 . $(dirname $0)/functions
22
23 while read oldrev newrev refname ; do
24         case "$refname" in
25                 refs/tags/*)
26                         exit 0
27                         ;;
28                 refs/heads/*)
29                         short_refname=${refname##refs/heads/}
30                         ;;
31                 *)
32                         echo >&2 "*** Unknown type of update to $refname"
33                         exit 1
34                         ;;
35         esac
36
37         ignored=" $(git config hooks.post-receive-hudson.ignored) "
38         hudson_url=$(git config hooks.post-receive-hudson.url)
39         if [[ $ignored =~ " $short_refname " ]] ; then
40                 exit 0
41         fi
42
43         if [ -z "$USER_EMAIL" ] ; then
44                 USER_EMAIL=$(git log -1 --pretty=format:'%ce' $newrev)
45         fi
46
47         branch_config=$(wget -O - $hudson_url/job/${short_refname}/config.xml 2>/dev/null)
48         if [ $? -ne 0 ] ; then
49                 # Create the job
50                 stable_config=$(wget -O - $hudson_url/job/stable/config.xml 2>/dev/null)
51                 if [ $? -ne 0 ] ; then
52                         display_error_message "Could not get existing Hudson config for ${short_refname}"
53                         exit 0
54                 fi
55
56                 # Replace stable with our branch
57                 branch_config="${stable_config/<branch>stable</<branch>$short_refname<}"
58
59                 # Add email to recipients list
60                 if [ "${branch_config/$USER_EMAIL/}" == "$branch_config" ] ; then
61                         branch_config="${branch_config/<recipients>/<recipients>$USER_EMAIL }"
62                 fi
63
64                 # Make the new job
65                 wget --header "Content-Type: text/xml" --post-data="$branch_config" -O - "$hudson_url/createItem?name=${short_refname}" >/dev/null 2>/dev/null
66                 if [ $? -ne 0 ] ; then
67                         display_error_message "Could not create new Hudson job for ${short_refname}"
68                         exit 0
69                 fi
70         else
71                 # Add email to recipients list
72                 if [ "${branch_config/$USER_EMAIL/}" == "$branch_config" ] ; then
73                         branch_config="${branch_config/<recipients>/<recipients>$USER_EMAIL }"
74
75                         # Update the config
76                         wget --header "Content-Type: text/xml" --post-data="$branch_config" -O - "$hudson_url/job/${short_refname}/config.xml" >/dev/null 2>/dev/null
77                         if [ $? -ne 0 ] ; then
78                                 display_error_message "Could not add $USER_EMAIL to Hudson job ${short_refname}"
79                                 exit 0
80                         fi
81                 fi
82         fi
83 done
84
85 exit 0
86