-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjenkins.sh
executable file
·71 lines (63 loc) · 2.28 KB
/
jenkins.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
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
jenkins_base_url="${jenkins_base_url:-}"
jenkins_auth="${jenkins_auth:-}"
if [[ "$jenkins_base_url" == "" ]] || [[ "$jenkins_auth" == "" ]]; then
echo "ERROR jenkins_base_url or jenkins_auth is not set" >&2
exit 1
fi
function extractDescription() {
perl -0777 -ne 'print if s/^(# .+?)\n##.*/\1/s' "$1" \
| perl -0777 -pe 's/^# .+\n+//' \
| perl -0777 -pe 's/([^\n])\n([A-Za-z])/\1 \2/g'
}
function processBadges() {
perl -pe 's/^\[!\[([^]]+)\]\(([^)]+)\)/[![\1](\2?style=flat-square)/g' \
| perl -pe 's/^\[!\[([^]]+)\]\(([^?)]+)\?([^?]*)\?([^)]*)\)/[![\1](\2?\3&\4)/g' \
| perl -0777 -pe 's/^(\[!.+)\n/\1 /mg' \
| perl -0777 -pe 's/(\w) *\n(\w)/\1 \2/mg' \
| perl -pe 's/^(\[!.+) $/\1\n/mg'
}
function encodeJobConfigXmlValue() {
perl -pe 's/</</g' \
| perl -pe 's/>/>/g' \
| perl -pe 's/&/&/g' \
| perl -pe "s/'/'/g" \
| perl -0777 -pe "s/\n/\\\n/g"
}
function updateJobConfigDescription() {
local description
description=$(echo "$2" | encodeJobConfigXmlValue | sed -E 's#/#\\/#g')
perl -0777 -i -pe "s/<description>.*?<\/description>/<description>${description//\\\\\\/}<\/description>/s" "$1"
}
function encodeURI() {
perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "$1"
}
for f in $(find . -mindepth 2 -maxdepth 2 -type f -name README.md); do
project=$(basename "$(dirname "$f")")
description=$(extractDescription "$f" | processBadges)
if [[ "$description" == "" ]]; then
echo "skip $project"
continue
fi
config_url="$jenkins_base_url/job/$project/config.xml"
config_file="/tmp/$project-config.xml"
set +e
curl -sSL --fail --show-error -u "$jenkins_auth" "$config_url" >"$config_file"
set -e
if [[ -s "$config_file" ]]; then
echo -n "update $project .. "
updateJobConfigDescription "$config_file" "$description"
# curl -sSL -u "$jenkins_auth" "$config_url" \
# -H 'Content-Type: application/xml' \
# --data-binary "@$config_file"
curl -sSL --fail --show-error -u "$jenkins_auth" "$jenkins_base_url/job/$project/submitDescription" \
-H 'Content-Type: application/x-www-form-urlencoded' \
--data-raw "description=$(encodeURI "$description")&Submit=Save" >/dev/null
echo "ok"
else
echo "skip $project"
fi
rm "$config_file"
done