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