CI: Use latest actions and reusable workflows
[releng/lftools.git] / shell / autocorrectinfofile
1 #!/bin/bash
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
12
13 #echo "Diff LDAP against INFO.yaml"
14
15 tmpdir(){
16
17   DIR="/tmp/infofile/$purpose.$repo.$ldapgroup"
18   mkdir -p "$DIR"
19
20 }
21
22 parseoptions() {
23
24   if [[ -z $purpose ]]; then
25     purpose=correct
26   fi
27
28   echo "    gerritclonebase $gerritclonebase"
29   echo "    ldapgroup $ldapgroup"
30   echo "    repo $repo"
31   echo "    purpose $purpose"
32
33   if [[ $purpose =~ "READY_FOR_INFO" ]]; then
34
35     tmpdir "$@"
36
37     if ! [[ -d "$DIR"/"$repo" ]]; then
38       echo "    git clone -q $gerritclonebase$repo $DIR/$repo || exit 1"
39       git clone -q "$gerritclonebase""$repo" "$DIR"/"$repo" || exit 1
40     fi
41     if [ ! -f "$DIR"/"$repo"/INFO.yaml ]; then
42       cp INFO.template.yaml "$DIR"/"$repo"/INFO.yaml || exit 1
43     else
44       echo "INFO file already exists, refusing to overwrite"
45       exit 1
46     fi
47
48   fi
49
50   if [[ $purpose =~ "LINT" ]]; then
51
52     tmpdir "$@"
53
54     if ! [[ -d "$DIR"/"$repo" ]]; then
55       echo "    git clone -q $gerritclonebase$repo $DIR/$repo || exit 1"
56       git clone -q "$gerritclonebase""$repo" "$DIR"/"$repo" || exit 1
57     fi
58     if ! yamllint "$DIR"/"$repo"/INFO.yaml; then
59       echo "ERROR LINT FAILED"
60       exit 1
61     fi
62
63   fi
64
65   #I should only clone the review if their is a discrepancy in commiters
66   if [[ $purpose =~ "IN-REVIEW" ]]; then
67
68     tmpdir "$@"
69
70     if ! [[ -d "$DIR"/"$repo" ]]; then
71       echo "    git clone -q $gerritclonebase$repo $DIR/$repo || exit 1"
72       SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
73       git clone -q "$gerritclonebase""$repo" "$DIR"/"$repo" || exit 1
74       cd "$DIR"/"$repo" || exit
75       git fetch origin "$review" && git checkout --quiet FETCH_HEAD
76       cd "$SCRIPTDIR" || exit
77       yamllint "$DIR"/"$repo"/INFO.yaml
78       if ! yamllint "$DIR"/"$repo"/INFO.yaml; then
79         echo "   ERROR LINT FAILED CANNOT AUTO CORRECT FILE IN REVIEW"
80         exit 1
81       fi
82
83     fi
84   fi
85
86   main "$@"
87 }
88
89 main() {
90
91   if [[ -z $DIR ]]; then
92     tmpdir "$@"
93   fi
94   echo "    tmpdir = $DIR"
95
96   if lftools ldap yaml4info "$ldapgroup" 2>&- > "$DIR"/LDAP.yaml
97     then
98       echo "    LDAP lookup sucssesfull"
99     else
100       echo "    LDAP lookup failed"
101       exit 1
102   fi
103
104   if ! [[ -d "$DIR"/"$repo" ]]; then
105     echo "    git clone -q $gerritclonebase/$repo $DIR/$repo || exit 1"
106     git clone -q "$gerritclonebase"/"$repo" "$DIR"/"$repo" || exit 1
107   fi
108
109   diff="$(diff <(lftools infofile get-committers "$DIR"/LDAP.yaml 2>&-| sort) <(lftools infofile get-committers "$DIR"/"$repo"/INFO.yaml 2>&- | sort))"
110   status="$?"
111
112   diff_array=()
113   onlyinINFO=()
114   onlyinLDAP=()
115
116   while IFS= read -r line; do
117     diff_array+=( "$line" )
118     if [[ $(echo "$line" | grep ">") ]];
119     then
120       onlyinINFO+=( "$(echo "$line" | awk -F"id: " '{ print $2 }')" )
121     fi
122     if [[ $(echo "$line" | grep "<") ]];
123     then
124       onlyinLDAP+=( "$(echo "$line" | awk -F"id: " '{ print $2 }')" )
125     fi
126   done < <(echo "${diff[@]}" )
127
128
129   if ! [ "${#onlyinINFO[@]}" -eq 0 ]; then
130     for missing in "${onlyinINFO[@]}"; do
131       if ! [ -z "$missing" ]; then
132         echo "    DUMMY: sending invite to $missing"
133         lftools infofile get-committers --id "$missing" "$DIR"/"$repo"/INFO.yaml 2>&-
134       fi
135     done
136   fi
137
138   if ! [ "${#onlyinLDAP[@]}" -eq 0 ]; then
139     echo "    These users are listed as commiters in LDAP and not in the INFO.yaml"
140     for missing in "${onlyinLDAP[@]}"; do
141       echo "    lftools infofile sync-committers $DIR/$repo/INFO.yaml $DIR/LDAP.yaml $missing --repo $repo 2>&-"
142       lftools infofile sync-committers "$DIR"/"$repo"/INFO.yaml "$DIR"/LDAP.yaml "$missing" --repo "$repo" 2>&-
143     done
144
145   fi
146
147   echo "    Exit status = $status"
148   exit "$status"
149
150 }
151
152 usage() {
153 cat << EOF
154 Must be called from lftools
155 eg: lftools ldap autocorrectinfofile
156 EOF
157 exit 1
158 }
159
160 if [[ -z "$*" ]]; then usage
161 fi
162
163 gerritclonebase="$1"
164 ldapgroup="$2"
165 repo="$3"
166 purpose="$4"
167 review="$5"
168
169 parseoptions "$@"