Skip to content

Commit 1230c10

Browse files
committed
improved uuid checks on commit hook and make targets
1 parent 7ca6664 commit 1230c10

5 files changed

+120
-88
lines changed

Makefile

+13-16
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ help:
4747
@echo " logs-join to join xdist log files into one"
4848
@echo " logs-clean to delete all xdist log files in the root"
4949
@echo " pyc-clean to delete all temporary artifacts"
50-
@echo " uuid-check to check for duplicated @id: in testimony docstring tags"
51-
@echo " uuid-replace-empty to replace empty @id: with new generated uuid"
52-
@echo " uuid-replace-duplicate to replace duplicated @id: with new generated uuid"
50+
@echo " uuid-check to check for duplicated or empty @id: in testimony docstring tags"
51+
@echo " uuid-fix to fix all duplicated or empty @id: in testimony docstring tags"
5352
@echo " can-i-push? to check if local changes are suitable to push"
5453
@echo " install-commit-hook to install pre-commit hook to check if changes are suitable to push"
54+
@echo " gitflake8 to check flake8 styling only for modified files"
5555

5656
docs:
5757
@cd docs; $(MAKE) html
@@ -132,26 +132,24 @@ logs-join:
132132
logs-clean:
133133
-rm -f robottelo_gw*.log
134134

135-
uuid-check: ## list duplicated uuids
135+
uuid-check: ## list duplicated or empty uuids
136136
$(info "Checking for empty or duplicated @id: in docstrings...")
137-
scripts/check_duplicate_uuids.sh
137+
@scripts/fix_uuids.sh --check
138138

139-
uuid-replace-duplicate: ## list duplicated uuids
140-
scripts/replace_dup_uuids.sh
139+
uuid-fix:
140+
@scripts/fix_uuids.sh
141141

142-
uuid-replace-empty: ## list duplicated uuids
143-
scripts/replace_empty_uuids.sh
144-
145-
flake8:
142+
gitflake8:
146143
$(info "Checking style and syntax errors with flake8 linter...")
147-
@flake8 . --show-source
144+
@flake8 $(shell git diff --name-only) --show-source
148145

149-
can-i-push?: flake8 uuid-check test-docstrings test-robottelo
146+
can-i-push?: gitflake8 uuid-check test-docstrings test-robottelo
150147
$(info "!!! Congratulations your changes are good to fly, make a great PR! ${USER}++ !!!")
151148

152149
install-commit-hook:
153150
$(info "Installing git pre-commit hook...")
154-
echo "make can-i-push?" >> .git/hooks/pre-commit
151+
@grep -q '^make uuid-fix' .git/hooks/pre-commit || echo "make uuid-fix" >> .git/hooks/pre-commit
152+
@grep -q '^make can-i-push?' .git/hooks/pre-commit || echo "make can-i-push?" >> .git/hooks/pre-commit
155153

156154
# Special Targets -------------------------------------------------------------
157155

@@ -161,5 +159,4 @@ install-commit-hook:
161159
test-foreman-tier2 test-foreman-tier3 test-foreman-tier4 \
162160
test-foreman-ui test-foreman-ui-xvfb test-foreman-endtoend \
163161
graph-entities lint logs-join logs-clean pyc-clean \
164-
uuid-check uuid-replace-duplicate uuid-replace-empty \
165-
can-i-push? install-commit-hook
162+
uuid-check uuid-fix can-i-push? install-commit-hook gitflake8

scripts/check_duplicate_uuids.sh

-27
This file was deleted.

scripts/fix_uuids.sh

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#!/usr/bin/env bash
2+
3+
# This script finds empty @id: and replace with new uuids
4+
# Then it finds for duplicates and replaces last occurrence with new uuids
5+
# finally it fixes '@id:xyz' to '@id: xyz' (missing space after :)
6+
7+
ID_TOKEN="@id:"
8+
9+
if [ "$1" = "--check" ]; then
10+
CHECK_ONLY=true
11+
echo "Running in uuid-check only mode..."
12+
else
13+
CHECK_ONLY=false
14+
echo "Running in uuid-fix mode..."
15+
fi
16+
17+
# finds occurrences of empty id: testimony tags
18+
EMPTY_IDS=$(grep -E -i -r -n "${ID_TOKEN}(.+[[:blank:]]|$)" tests/foreman/ --include=*.py)
19+
20+
if [ -n "$EMPTY_IDS" ]; then
21+
if [ $CHECK_ONLY = true ]; then
22+
echo "Empty $ID_TOKEN found in testimony tags"
23+
echo $EMPTY_IDS
24+
exit 1
25+
else
26+
echo "Generating new UUIDS for empty $ID_TOKEN tags..."
27+
fi
28+
else
29+
echo "No empty $ID_TOKEN was found"
30+
fi
31+
32+
# iterate if any empty @id found
33+
for output_line in $EMPTY_IDS
34+
do
35+
if (echo "$output_line" | grep "tests/foreman"); then
36+
OLDIFS=$IFS
37+
# splits the grep output to get filename and occurrence line number
38+
IFS=':' read -r filename line <<< $output_line
39+
# generate uuid and place in specific line number
40+
NEW_ID=$(python -c 'import uuid; print(uuid.uuid4())')
41+
sed -r -i~ "${line}s/${ID_TOKEN}(.+[[:blank:]]|$)/${ID_TOKEN} ${NEW_ID}/g" $filename
42+
IFS=$OLDIFS
43+
fi
44+
done
45+
46+
# This script finds duplicated @id and replaces with new uuids
47+
48+
# Finds occurrences of @id: in testimony tags then
49+
# sort the output and filters only the duplicated
50+
# then looks for existence of "@id:" in final output
51+
# NOTE: can't print the line number -n here because of uniq -d
52+
DUP_EXISTS=$(grep -r -i $ID_TOKEN tests/foreman/ --include=*.py | sort | uniq -d | grep $ID_TOKEN)
53+
54+
if [ -n "$DUP_EXISTS" ]; then
55+
if [ $CHECK_ONLY = true ]; then
56+
echo "Duplicate $ID_TOKEN found in testimony tags"
57+
echo $DUP_EXISTS
58+
exit 1
59+
else
60+
echo "Generating new UUIDS for duplicated $ID_TOKEN tags..."
61+
fi
62+
else
63+
echo "No duplicated $ID_TOKEN was found"
64+
fi
65+
66+
grep -r -i $ID_TOKEN tests/foreman/ --include=*.py | sort | uniq -d | grep $ID_TOKEN | while read -r line ; do
67+
OLDIFS=$IFS
68+
IFS=':' read -r dup_file dup_id <<< $line
69+
echo "filename: $dup_file"
70+
echo "Id to replace: $dup_id"
71+
NEW_ID=$(python -c 'import uuid; print(uuid.uuid4())')
72+
echo "Replacing with the new id: $NEW_ID"
73+
LAST_LINE=$(grep -i -n "$dup_id" $dup_file | tail -1)
74+
75+
IFS=':' read -r linenumber linecontent <<< $LAST_LINE
76+
echo $linenumber
77+
trimmed_linecontent=$(echo $linecontent)
78+
sed -i~ "${linenumber}s/${trimmed_linecontent}/${ID_TOKEN} ${NEW_ID}/g" $dup_file
79+
echo "----------------------------------------------------------------"
80+
IFS=$OLDIFS
81+
done
82+
83+
# This script finds id: missing spaces after :
84+
85+
86+
MISSING_SPACES=$(grep -E -i -r -n "${ID_TOKEN}[^ ]" tests/foreman/ --include=*.py)
87+
88+
if [ -n "$MISSING_SPACES" ]; then
89+
if [ $CHECK_ONLY = true ]; then
90+
echo "Found $ID_TOKEN tags missing space after : ..."
91+
echo $MISSING_SPACES
92+
exit 1
93+
else
94+
echo "Fixing $ID_TOKEN tags missing spaces..."
95+
fi
96+
else
97+
echo "No $ID_TOKEN missing spaces was found"
98+
fi
99+
100+
grep -E -i -r -n "${ID_TOKEN}[^ ]" tests/foreman/ --include=*.py | while read -r line ; do
101+
OLDIFS=$IFS
102+
IFS=':' read -r missing_file linenumber linecontent <<< $line
103+
IFS=':' read -r tag uuid <<< $linecontent
104+
trimmed_linecontent=$(echo $linecontent)
105+
sed -i~ "${linenumber}s/${trimmed_linecontent}/${tag}: ${uuid}/g" $missing_file
106+
IFS=$OLDIFS
107+
done

scripts/replace_dup_uuids.sh

-20
This file was deleted.

scripts/replace_empty_uuids.sh

-25
This file was deleted.

0 commit comments

Comments
 (0)