-
Notifications
You must be signed in to change notification settings - Fork 1
/
description.sh
executable file
·162 lines (145 loc) · 4.15 KB
/
description.sh
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#! /bin/bash
# description.sh
########################################################################
# CHANGE JENKINS BUILD DESCRIPTION
#
USAGE="
description.sh [-a|-p] [-u <JenkinsURL>] -j <JenkinsJob> \\
-b <jenkinsBuild> -d <description> [-c <color>]
where:
-a: Append new description to current description
-p: Prepend new description to current description
Description replaced if neither -p or -a are
specified.
-d: Description for build
-u: Full URL of Jenkins server. If not given, it will be taken
from value embedded in program.
-j: Jenkins 'Job' name (aka 'project).
-b: Build Number
-c: Color of text
-h: Display this help text
"
#
########################################################################
########################################################################
# FUNCTIONS
#
function error {
printf "ERROR: $1\n$USAGE\n" 1>&2
exit 2
}
#
########################################################################
########################################################################
# MAIN PROGRAM
#
jenkins_url="http://buildl02.tcprod.local/jenkins"
prepend_flag=""
append_flag=""
readonly USER="xxxxxxx"
readonly PASSWORD="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
#
# Set options
#
is_extglob_set=$(shopt -q extglob)
[ "$is_extglob_set" ] || shopt -s extglob
while getopts :aphd:u:j:b:c: option
do
case $option in
a) append_flag=1;;
p) prepend_flag=1;;
u) jenkins_url="$OPTARG";;
j) job_name="$OPTARG";;
b) build_number="$OPTARG"
if [[ "$build_number" != +([0-9]) ]]
then
die "Build number must be numeric"
fi
;;
c) color="$OPTARG";;
d) new_description="$OPTARG";;
h) printf "\n$USAGE\n"
exit 0;;
*) error "Invalid Argument";;
esac
done
shift $(( $OPTIND - 1 ))
[ "$is_extglob_set" ] || shopt -u extglob
#
# Verify options
#
[ -z "$new_description" ] && error "Missing parameter '-d': Description"
[ -z "$build_number" ] && error "Missing parameter '-b': Build Number"
[ -z "$job_name" ] && error "Missing parameter '-j': Job Name"
#
# Get old description (if -a or -p flag)
#
if [ -n "$append_flag" -o -n "$prepend_flag" ]
then
old_description=$(curl -s --user "$USER:$PASSWORD" --data "tree=description" \
"$jenkins_url/job/$job_name/$build_number/api/json")
if [ $? -ne 0 ]
then
echo "Could not retrieve old description of build" 1>&2
exit 2
fi
fi
if [ "$old_description" == '{"description":null}' ]
then
old_description=""
else
old_description=${old_description#*:\"} #Remove JSON garbage
old_description=${old_description%\"\}} #Remove JSON garbage
fi
#
# Translate linefeeds
#
old_description=$(sed "s/\\r\\n/\r\n/g" <<<$old_description)
if [ $? -ne 0 ]
then
echo "ERROR: Could not modify the old description with 'sed' command" 1>&2
exit 2
fi
#
# Create the new description
#
if [ -n "$color" ] #Add color to the description
then
new_description="<font color=$color><b>$new_description</b></font>"
fi
if [ "$prepend_flag" -a -n "$old_description" ] #Prepend new description to old description
then
new_description="$(printf "$new_description<br/>\r\n$old_description")"
elif [ "$append_flag" -a -n "$old_description" ] #Append new description to old description
then
new_description="$(printf "$old_description<br/>\r\n$new_description")"
fi
#
# Now set the description
#
new_description=$(perl url-encode.pl -input "$new_description")
output=$( curl -s -u $USER:$PASSWORD --data "description=$new_description" --data "Submit=Submit" \
"$jenkins_url/job/$job_name/$build_number/submitDescription" 2>&1)
if [ $? -ne 0 ]
then
echo "WARNING: Description was not set. Manually change the descripiton of the build" >&2
echo " for Build #$build_number in Jenkins job $job_name" >&2
exit 2
fi
#
# If any "output" is returned by the curl command, it has failed
#
if [ -n "$output" ]
then
error=$(sed 's/<[^>]*>//g' <<<"$output")
printf "There was some sort of error:\n%s\n", "$error" >&2
exit 2
fi
#
# Everything worked out!
#
echo "Description successfully changed on Build #$build_number in Jenkins job $job_name"
#
#
# DONE
########################################################################