Drop dashes for post-receive-commitnumbers and post-receive-gitconfig.
[git-central.git] / server / post-receive-hudson
1 #!/bin/sh
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
18 #
19
20 . $(dirname $0)/functions
21
22 while read oldrev newrev refname ; do
23         case "$refname" in
24                 refs/tags/*)
25                         exit 0
26                         ;;
27                 refs/heads/*)
28                         short_refname=${refname##refs/heads/}
29                         ;;
30                 *)
31                         echo >&2 "*** Unknown type of update to $refname"
32                         exit 1
33                         ;;
34         esac
35
36         ignored=" $(git config hooks.post-receive-hudson.ignored) "
37         hudson_url=$(git config hooks.post-receive-hudson.url)
38         if [[ $ignored =~ " $short_refname " ]] ; then
39                 exit 0
40         fi
41
42         branch_config=$(wget -O - $hudson_url/job/${short_refname}/config.xml 2>/dev/null)
43         if [ $? -ne 0 ] ; then
44                 # Create the job
45                 stable_config=$(wget -O - $hudson_url/job/stable/config.xml 2>/dev/null)
46                 if [ $? -ne 0 ] ; then
47                         display_error_message "Could not get existing Hudson config for ${short_refname}"
48                         exit 0
49                 fi
50
51                 # Replace stable with our branch
52                 branch_config="${stable_config/<branch>stable</<branch>$short_refname<}"
53
54                 # Add email to recipients list
55                 if [ "${branch_config/$USER_EMAIL/}" == "$branch_config" ] ; then
56                         branch_config="${branch_config/<recipients>/<recipients>$USER_EMAIL }"
57                 fi
58
59                 # Make the new job
60                 wget --header "Content-Type: text/xml" --post-data="$branch_config" -O - "$hudson_url/createItem?name=${short_refname}" >/dev/null 2>/dev/null
61                 if [ $? -ne 0 ] ; then
62                         display_error_message "Could not create new Hudson job for ${short_refname}"
63                         exit 0
64                 fi
65         else
66                 # Add email to recipients list
67                 if [ "${branch_config/$USER_EMAIL/}" == "$branch_config" ] ; then
68                         branch_config="${branch_config/<recipients>/<recipients>$USER_EMAIL }"
69
70                         # Update the config
71                         wget --header "Content-Type: text/xml" --post-data="$branch_config" -O - "$hudson_url/job/${short_refname}/config.xml" >/dev/null 2>/dev/null
72                         if [ $? -ne 0 ] ; then
73                                 display_error_message "Could not add $USER_EMAIL to Hudson job ${short_refname}"
74                                 exit 0
75                         fi
76                 fi
77         fi
78 done
79
80 exit 0
81