Chore: Upgrade Jenkins-job-builder to 6.3.0
[releng/global-jjb.git] / shell / gerrit-branch-lock.sh
1 #!/bin/bash
2 # SPDX-License-Identifier: EPL-1.0
3 ##############################################################################
4 # Copyright (c) 2017 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 echo "---> gerrit-branch-lock.sh"
12 # Generates a patch to lock|unlock a branch in Gerrit
13 #
14 # Assumes that the project repository was cloned via ssh and thus uses ssh to
15 # install the git commit hook.
16
17 # Ensure we fail the job if any steps fail.
18 set -eu -o pipefail
19
20 git fetch origin refs/meta/config:config
21 git checkout config
22
23 install_gerrit_hook() {
24     ssh_url=$(git remote show origin | grep Fetch | grep 'ssh://' \
25         | awk -F'/' '{print $3}' | awk -F':' '{print $1}')
26     ssh_port=$(git remote show origin | grep Fetch | grep 'ssh://' \
27         | awk -F'/' '{print $3}' | awk -F':' '{print $2}')
28
29     if [ -z "$ssh_url" ]; then
30         echo "ERROR: Gerrit SSH URL not found."
31         exit 1
32     fi
33
34     scp -p -P "$ssh_port" "$ssh_url":hooks/commit-msg .git/hooks/
35     chmod u+x .git/hooks/commit-msg
36 }
37 install_gerrit_hook
38
39 # Groups must be mapped in the groups file before they can be used
40 if ! grep 'Registered Users'; then
41     echo -e "global:Registered-Users\tRegistered Users" >> groups
42     git add groups
43 fi
44
45 mode=$(echo "$GERRIT_EVENT_COMMENT_TEXT" | grep branch | awk '{print $1}')
46 case $mode in
47     lock)
48         echo "Locking branch: $GERRIT_BRANCH"
49         git config -f project.config \
50             "access.refs/heads/${GERRIT_BRANCH}.exclusiveGroupPermissions" \
51             "submit"
52         git config -f project.config \
53             "access.refs/heads/${GERRIT_BRANCH}.submit" \
54             "block group Registered Users"
55         git commit -asm "Lock branch $GERRIT_BRANCH"
56         ;;
57
58     unlock)
59         echo "Unlocking branch: $GERRIT_BRANCH"
60         git config -f project.config --remove-section \
61             "access.refs/heads/${GERRIT_BRANCH}" || true
62         git commit -asm "Unlock branch $GERRIT_BRANCH"
63         ;;
64
65     *)
66         echo "ERROR: Unknown mode selected '$mode'."
67         exit 1
68         ;;
69 esac
70
71 git diff HEAD~1
72 git push origin HEAD:refs/for/refs/meta/config