2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2018 The Linux Foundation and others.
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Eclipse Public License v1.0
8 # which accompanies this distribution, and is available at
9 # http://www.eclipse.org/legal/epl-v10.html
10 ##############################################################################
12 TMP_WORKSPACE="${OPTARG:-/tmp/}"
13 parent="${OPTARG:-All-Projects}"
14 ENABLE_REPLICATION=false
17 echo gerrit url="$project"
18 echo new repo name="$repository"
21 if [[ $(ssh -p 29418 "$user"@"$project" gerrit version) ]];
23 echo "connected to Gerrit"
25 echo "cannot connect to Gerrit"
29 # shellcheck disable=SC2029
30 if ! $ENABLE_REPLICATION; then
31 ssh -p 29418 "$user"@"$project" gerrit set-project "$repository" &> /dev/null && \
32 echo "$repository Already exists, cannot create exiting" && exit 1
35 #Exit if ldap group does not exist
36 #lftools must be configured and lfservices_releng member must be added to group.
37 echo "Checking that ldapgroup=$ldapgroup exists"
38 check_ldap="$(lftools lfidapi search-members "$ldapgroup")"
39 if [[ -z "$check_ldap" ]]; then
40 echo "ldap group is empty or does not exist."
44 repodashed="$(echo "$repository" | sed -e 's/\//-/g')"
45 gerrit_name="$(echo "$project" | awk -F"." '{print $2}')"
48 if $ENABLE_REPLICATION; then
49 if git ls-remote git@github.com:"$gerrit_name"/"$repodashed".git HEAD &> /dev/null; then
50 echo "PASS $gerrit_name $repository exists in Github"
52 echo "FAIL $gerrit_name $repository does not exist in Github"
60 if ! [[ -d "$TMP_WORKSPACE" ]]; then
62 mkdir -p "$TMP_WORKSPACE"
64 echo "$TMP_WORKSPACE already exists"
70 echo "Creating repository $repository"
71 # shellcheck disable=SC2029
72 if [[ $parent == "All-Projects" ]]; then
73 ssh -p 29418 "$user"@"$project" "gerrit create-project $repository --empty-commit --parent $parent --owner ldap/$ldapgroup"
75 ssh -p 29418 "$user"@"$project" "gerrit create-project $repository --empty-commit --parent $parent"
82 if ! [[ -d "$repository" ]];
84 git clone ssh://"$user"@"$project":29418/"$repository" "$repository" &> /dev/null
90 create_groups_file () {
91 cd "$TMP_WORKSPACE"/"$repository"
92 git fetch origin refs/meta/config &> /dev/null && git checkout FETCH_HEAD &> /dev/null
94 #get uuid for for GitHub users into groups file
95 ssh -p 29418 "$user"@"$project" gerrit ls-groups --verbose \
96 | grep "GitHub\ Replication"\
97 | awk '{print $3"\t"$1,$2}' > groups.tmp
99 if [[ $parent == "All-Projects" ]]; then
100 printf "global:Registered-Users\tRegistered Users\n\
101 ldap:cn=%s,ou=Groups,dc=freestandards,dc=org\tldap/%s\n" "$ldapgroup" "$ldapgroup"\
104 printf "global:Registered-Users\tRegistered Users\n" >> groups.tmp
112 if diff groups groups.tmp; then
113 echo groups file already configured not pushing
118 git commit -sv -m "Creating groups file" &> /dev/null
120 if git push origin HEAD:refs/meta/config &> /dev/null; then
121 echo "git push for groups file succeeded"
123 echo "git push for groups file failed"
132 cd "$TMP_WORKSPACE"/"$repository"
133 if ! git reset --hard origin/master &> /dev/null; then
134 echo "git reset failed"
138 has_gitreview="$(git ls-files .gitreview)"
139 if [[ -z $has_gitreview ]]; then
145 defaultbranch=master\n" "$project" "$repository" > .gitreview
148 git commit -sv -m "Forcing .gitreview into repo"
150 if git push ssh://"$user"@"$project":29418/"$repository" HEAD:refs/heads/master &> /dev/null; then
151 echo "git push of .gitreview succeeded"
153 echo "git push of .gitreview failed"
154 echo "Admins do not have push on refs/heads"
158 echo "Repo Already has a .gitreview"
163 enable_github_replication () {
165 cd "$TMP_WORKSPACE"/"$repository"
167 git fetch origin refs/meta/config &> /dev/null && git checkout FETCH_HEAD &> /dev/null
169 git config --replace -f project.config 'access.refs/*.read' "group GitHub Replication"
171 echo "project.config:"
175 git add project.config
176 git commit -sv -m "Pushing $repository project.config"
178 if git push origin HEAD:refs/meta/config &> /dev/null; then
179 echo "git push for $repository refs meta config succeeded"
181 echo "git push for $repository refs meta config failed"
185 echo "Starting replication"
186 echo "If this hangs, you need to give the replication group in"
187 echo "github write access to the repository"
188 ssh -p 29418 "$user"@"$project" "replication start --wait $repository"
194 "$0": Creates a repository and sets up the permissions.
198 -s server fqdn eg: gerrit.localhost
199 -o owner eg: ldap group
202 -p parent Default: All-Projects
203 -w workspace to do clones etc. (must not be in a git repo)
205 -e enable replication to github (must = True)
207 example: $(basename "$0") -s gerrit.localhost -o project-gerrit-group-committers -r reponame -u lfid
215 # shellcheck disable=SC2199
216 [[ -z "$@" ]] && usage
218 while getopts "s:o:r:u:p:w:eh" OPTION
221 s ) project="$OPTARG" ;;
222 o ) ldapgroup="$OPTARG" ;;
223 r ) repository="$OPTARG" ;;
224 u ) user="$OPTARG" ;;
225 p ) parent="$OPTARG" ;;
226 w ) TMP_WORKSPACE="$OPTARG" ;;
227 e ) ENABLE_REPLICATION=true ;;
229 \? ) echo "Unknown option: -$OPTARG" >&2; exit 1;;
234 if $ENABLE_REPLICATION; then
237 enable_github_replication
246 echo "Repo Created and Configured"
247 echo gerrit="$project"
248 echo ldapgroup="$ldapgroup"
249 echo repository="$repository"