-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_gerrit_repo.sh
executable file
·164 lines (139 loc) · 5.1 KB
/
setup_gerrit_repo.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
163
164
#!/bin/bash
set -eu
# Creates new gerrit repo with webhook and checks for jenkins integration
# NOTE: Http generated password must be provided to script from gerrit portal
if ! command -v jq &> /dev/null; then
echo "jq not found, please install the command and rerun the script."
exit 1
fi
source .env
gerrit_username=${GERRIT_USERNAME:-admin}
gerrit_user_password=${GERRIT_USER_HTTP_PASSWORD}
gerrit_user_email=${GERRIT_USER_EMAIL:-"[email protected]"}
gerrit_url=${GERRIT_CANONICAL_WEB_URL:-http://localhost:8080}
gerrit_domain="${gerrit_url#*//}"
gerrit_project_name=${GERRIT_PROJECT_NAME:-gerrit-jenkins-test}
gerrit_template_repo_name=${GERRIT_TEMPLATE_REPO_NAME:-'Students-Template-Projects'}
jenkins_username=${JENKINS_USERNAME:-jenkins}
jenkins_password=${JENKINS_PASSWORD:-jenkins}
usage() {
echo "Usage: setup_gerrit_repo.sh -p http-password-from-gerrit"
echo "Admin HTTP password can also be stored in .env under GERRIT_USER_HTTP_PASSWORD variable"
exit 1
}
if [[ -z "$gerrit_user_password" ]]; then
while getopts ":p:" opt; do
case ${opt} in
p )
gerrit_user_password=$OPTARG
;;
* )
usage
;;
esac
done
shift $((OPTIND-1))
if [[ -z "${gerrit_user_password}" ]]; then
usage
fi
fi
# Encode gerrit password
encoded_gerrit_password=$(python3 -c "from urllib.parse import quote; print(quote('''$gerrit_user_password''', safe=''))")
# Http url with gerrit user credentials
gerrit_authorized_url=http://"${gerrit_username}:${encoded_gerrit_password}@${gerrit_domain}/a"
# Install checks plugin
plugin_id="checks"
plugin_source='"https://gerrit-ci.gerritforge.com/view/Plugins-stable-3.2/job/plugin-checks-bazel-stable-3.2/lastSuccessfulBuild/artifact/bazel-bin/plugins/checks/checks.jar"'
curl --header "Content-Type: application/json" \
--request PUT \
--fail \
--silent \
--show-error \
--output /dev/null \
--data '{"url":'"${plugin_source}"'}' \
"${gerrit_authorized_url}/plugins/${plugin_id}.jar"
# Grant permissions for checks-administrateCheckers -> Administrators, Non-interactive users (used by admin to create checks and by jenkins to update status on gui)
permission_request_file='./gerrit/update_permission_rules_request.json'
curl --header "Content-Type: application/json" \
--request POST \
--fail \
--silent \
--show-error \
--output /dev/null \
--data @"${permission_request_file}" \
"${gerrit_authorized_url}/projects/All-Projects/access"
# Create new students repo template
./setup_students_template.sh "$gerrit_authorized_url" "$gerrit_username" "$gerrit_user_email" "$gerrit_template_repo_name"
# Create Jenkins user in gerrit
curl --header "Content-Type: application/json" \
--request PUT \
--fail \
--silent \
--show-error \
--output /dev/null \
--data '{"name":"JenkinsCI", "email": "[email protected]", "groups":["Non-Interactive Users"], "http_password":"'"${jenkins_password}"'"}' \
"${gerrit_authorized_url}/accounts/${jenkins_username}"
# Create new sample gerrit repository
request_data=$(cat <<-END
{
"name": "$gerrit_project_name",
"description": "Sample project for Jenkins<->gerrit integration",
"permissions_only": false,
"parent": "$gerrit_template_repo_name",
"create_empty_commit": true,
"owners": ["Administrators"]
}
END
)
curl --header "Content-Type: application/json" \
--request PUT \
--fail \
--silent \
--show-error \
--output /dev/null \
--data "$request_data" \
"${gerrit_authorized_url}/projects/${gerrit_project_name}"
# Create new check for Jenkins job in ${gerrit_project_name} gerrit repo
request_data=$(cat <<-END
{
"uuid": "Jenkins:Test",
"name": "Jenkins Test",
"description": "Test code on Jenkins Job",
"repository": "$gerrit_project_name",
"query": "",
"blocking": []
}
END
)
curl --header "Content-Type: application/json" \
--request POST \
--fail \
--silent \
--show-error \
--output /dev/null \
--data "$request_data" \
"${gerrit_authorized_url}/plugins/checks/checkers/"
# Clone new repo to host system with commit-msg hook
rm -rf "$gerrit_project_name"
git clone "${gerrit_authorized_url}/${gerrit_project_name}" && cd "${gerrit_project_name}"
mkdir -p .git/hooks
commit_msg_hook=$(git rev-parse --git-dir)/hooks/commit-msg
curl -sSLo "${commit_msg_hook}" "${gerrit_authorized_url}"/tools/hooks/commit-msg
chmod +x "${commit_msg_hook}"
# Save gerrit user credentials in local git config
git config --local user.name "${gerrit_username}"
git config --local user.email "${gerrit_user_email}"
# Add webhook for Jenkins integration in cloned repo
git fetch origin refs/meta/config
git checkout FETCH_HEAD
cp ../gerrit/webhooks.config .
git add webhooks.config
git commit -m "Add jenkins webhook"
git push origin HEAD:refs/meta/config
cd ..
# Post build configuration of Jenkins Jobs
./setup_jenkins_gerrit_jobs.sh
# Setup example lab branches
./setup_lab_branches.sh
# Create sample change in gerrit repo for demo
./create_gerrit_change.sh