3 # Copyright (c) 2008 Stephen Haberman
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.
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.
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.
21 . $(dirname $0)/functions
23 while read oldrev newrev refname ; do
29 short_refname=${refname##refs/heads/}
32 echo >&2 "*** Unknown type of update to $refname"
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
43 if [ -z "$USER_EMAIL" ] ; then
44 USER_EMAIL=$(git log -1 --pretty=format:'%ce' $newrev)
47 branch_config=$(wget -O - $hudson_url/job/${short_refname}/config.xml 2>/dev/null)
48 if [ $? -ne 0 ] ; then
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}"
56 # Replace stable with our branch
57 branch_config="${stable_config/<branch>stable</<branch>$short_refname<}"
59 # Add email to recipients list
60 if [ "${branch_config/$USER_EMAIL/}" == "$branch_config" ] ; then
61 branch_config="${branch_config/<recipients>/<recipients>$USER_EMAIL }"
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}"
71 # Add email to recipients list
72 if [ "${branch_config/$USER_EMAIL/}" == "$branch_config" ] ; then
73 branch_config="${branch_config/<recipients>/<recipients>$USER_EMAIL }"
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}"