CI: Use latest actions and reusable workflows
[releng/lftools.git] / shell / gerrit_create
1 #!/bin/bash -l
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2018 The Linux Foundation and others.
5 #
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 ##############################################################################
11 set -euo pipefail
12 TMP_WORKSPACE="${OPTARG:-/tmp/}"
13 parent="${OPTARG:-All-Projects}"
14 ENABLE_REPLICATION=false
15
16 sanity_checks () {
17   echo gerrit url="$project"
18   echo new repo name="$repository"
19   echo parent="$parent"
20
21   if [[ $(ssh -p 29418 "$user"@"$project" gerrit version) ]];
22   then
23     echo "connected to Gerrit"
24   else
25     echo "cannot connect to Gerrit"
26     exit 1
27   fi
28
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
33   fi
34
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."
41     exit 1
42   fi
43
44   repodashed="$(echo "$repository" | sed -e 's/\//-/g')"
45   gerrit_name="$(echo "$project" | awk -F"." '{print $2}')"
46
47
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"
51     else
52       echo "FAIL $gerrit_name $repository does not exist in Github"
53       exit 1
54     fi
55   fi
56
57 }
58
59 movetoworkspace () {
60   if ! [[ -d "$TMP_WORKSPACE" ]]; then
61     echo "$TMP_WORKSPACE"
62     mkdir -p "$TMP_WORKSPACE"
63   else
64     echo "$TMP_WORKSPACE already exists"
65   fi
66   cd "$TMP_WORKSPACE"
67 }
68
69 create_repo () {
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"
74   else
75     ssh -p 29418 "$user"@"$project" "gerrit create-project $repository --empty-commit --parent $parent"
76   fi
77
78 }
79
80 clone_repo() {
81   movetoworkspace
82   if ! [[ -d "$repository" ]];
83   then
84     git clone ssh://"$user"@"$project":29418/"$repository" "$repository" &> /dev/null
85     cd "$repository"
86   fi
87
88 }
89
90 create_groups_file () {
91   cd "$TMP_WORKSPACE"/"$repository"
92   git fetch origin refs/meta/config &> /dev/null && git checkout FETCH_HEAD &> /dev/null
93
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
98
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"\
102 >> groups.tmp
103   else
104     printf "global:Registered-Users\tRegistered Users\n" >> groups.tmp
105   fi
106
107   echo "groups file:"
108   echo ""
109   cat groups.tmp
110   touch groups
111
112   if diff groups groups.tmp; then
113     echo groups file already configured not pushing
114     rm groups.tmp
115   else
116     mv groups.tmp groups
117     git add groups
118     git commit -sv -m "Creating groups file" &> /dev/null
119
120     if git push origin HEAD:refs/meta/config &> /dev/null; then
121       echo "git push for groups file succeeded"
122     else
123       echo "git push for groups file failed"
124       exit 1
125     fi
126   fi
127
128 }
129
130
131 add_gitreview () {
132   cd "$TMP_WORKSPACE"/"$repository"
133   if ! git reset --hard origin/master &> /dev/null; then
134     echo "git reset failed"
135     exit 1
136   fi
137
138   has_gitreview="$(git ls-files .gitreview)"
139   if [[ -z $has_gitreview ]]; then
140
141     printf "[gerrit]\n\
142 host=%s\n\
143 port=29418\n\
144 project=%s.git\n\
145 defaultbranch=master\n" "$project" "$repository" > .gitreview
146
147     git add .gitreview
148     git commit -sv -m "Forcing .gitreview into repo"
149
150     if git push ssh://"$user"@"$project":29418/"$repository" HEAD:refs/heads/master &> /dev/null; then
151       echo "git push of .gitreview succeeded"
152     else
153       echo "git push of .gitreview failed"
154       echo "Admins do not have push on refs/heads"
155       exit 1
156     fi
157   else
158     echo "Repo Already has a .gitreview"
159   fi
160
161 }
162
163 enable_github_replication () {
164
165   cd "$TMP_WORKSPACE"/"$repository"
166
167   git fetch origin refs/meta/config &> /dev/null && git checkout FETCH_HEAD &> /dev/null
168
169   git config  --replace -f project.config 'access.refs/*.read' "group GitHub Replication"
170
171   echo "project.config:"
172   echo ""
173   cat project.config
174
175   git add project.config
176   git commit -sv -m "Pushing $repository project.config"
177
178   if git push origin HEAD:refs/meta/config &> /dev/null; then
179     echo "git push for $repository refs meta config succeeded"
180   else
181     echo "git push for $repository refs meta config failed"
182     exit 1
183   fi
184
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"
189
190 }
191
192 usage() {
193 cat << EOF
194 "$0": Creates a repository and sets up the permissions.
195
196   usage: $0 [OPTIONS]
197    -h  Show this message
198    -s  server fqdn eg: gerrit.localhost
199    -o  owner eg: ldap group
200    -r  repository name
201    -u  ssh user name
202    -p  parent Default: All-Projects
203    -w  workspace to do clones etc. (must not be in a git repo)
204        Default is /tmp/
205    -e  enable replication to github (must = True)
206
207   example: $(basename "$0")  -s gerrit.localhost -o project-gerrit-group-committers -r reponame -u lfid
208
209 EOF
210
211 exit 1
212
213 }
214
215 # shellcheck disable=SC2199
216 [[ -z "$@" ]] && usage
217
218 while getopts "s:o:r:u:p:w:eh" OPTION
219 do
220         case $OPTION in
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 ;;
228                 h ) usage; exit;;
229                 \? ) echo "Unknown option: -$OPTARG" >&2; exit 1;;
230         esac
231 done
232
233
234 if $ENABLE_REPLICATION; then
235   sanity_checks
236   clone_repo
237   enable_github_replication
238 else
239   sanity_checks
240   create_repo
241   clone_repo
242   create_groups_file
243   add_gitreview
244 fi
245
246 echo "Repo Created and Configured"
247 echo gerrit="$project"
248 echo ldapgroup="$ldapgroup"
249 echo repository="$repository"