Skip to content

Commit b1f8baf

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

File tree

5 files changed

+111
-64
lines changed

5 files changed

+111
-64
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/check_uuids.sh
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 scripts/check_uuids.sh

+21-3
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,24 @@
22

33
# This script checks duplicated or empty uuids and exit with 1 if found
44

5+
ID_TOKEN="@id:"
6+
57
# finds occurrences of empty @id: testimony tags
6-
grep -E -i -r -n "@id:(.+[[:blank:]]|$)" tests/foreman/
8+
grep -E -i -r -n "${ID_TOKEN}(.+[[:blank:]]|$)" tests/foreman/ --include=*.py
79
EMPTY=$?
810

911
if [ $EMPTY = 0 ]; then
10-
echo "Empty @id found in testimony tags"
12+
echo "Empty id found in testimony tags"
1113
exit 1
14+
else
15+
echo "No empty id found"
1216
fi
1317

1418
# Finds occurrences of @id: in testimony tags then
1519
# sort the output and filters only the duplicated
1620
# then looks for existence of "@id:" in final output
1721
# NOTE: can't print the line number -n here because of uniq -d
18-
grep -r -i "@id:" tests/foreman/ | sort | uniq -d | grep "@id:"
22+
grep -r -i $ID_TOKEN tests/foreman/ --include=*.py | sort | uniq -d | grep $ID_TOKEN
1923
DUPLICATE=$?
2024

2125
# grep exits with status code 0 if text is found
@@ -24,4 +28,18 @@ DUPLICATE=$?
2428
if [ $DUPLICATE = 0 ]; then
2529
echo "Duplicate @id found in testimony tags"
2630
exit 1
31+
else
32+
echo "No duplicate @id found"
33+
fi
34+
35+
36+
# finds missing empty space after @id:
37+
grep -E -i -r -n "${ID_TOKEN}[^ ]" tests/foreman/ --include=*.py
38+
MISSING_SPACES=$?
39+
40+
if [ $MISSING_SPACES = 0 ]; then
41+
echo "Found id: tags missing space after : ..."
42+
exit 1
43+
else
44+
echo "No id missing spaces after : was found"
2745
fi

scripts/fix_uuids.sh

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
# finds occurrences of empty @id: testimony tags
10+
EMPTY_IDS=$(grep -E -i -r -n "${ID_TOKEN}(.+[[:blank:]]|$)" tests/foreman/ --include=*.py)
11+
12+
if [ -n "$EMPTY_IDS" ]; then
13+
echo "Generating new UUIDS for empty id tags..."
14+
else
15+
echo "No empty id was found"
16+
fi
17+
18+
# iterate if any empty @id found
19+
for output_line in $EMPTY_IDS
20+
do
21+
if (echo "$output_line" | grep "tests/foreman"); then
22+
OLDIFS=$IFS
23+
# splits the grep output to get filename and occurrence line number
24+
IFS=':' read -r filename line <<< $output_line
25+
# generate uuid and place in specific line number
26+
NEW_ID=$(python -c 'import uuid; print(uuid.uuid4())')
27+
sed -r -i~ "${line}s/${ID_TOKEN}(.+[[:blank:]]|$)/${ID_TOKEN} ${NEW_ID}/g" $filename
28+
IFS=$OLDIFS
29+
fi
30+
done
31+
32+
# This script finds duplicated @id and replaces with new uuids
33+
34+
DUP_EXISTS=$(grep -r -i $ID_TOKEN tests/foreman/ --include=*.py | sort | uniq -d | grep $ID_TOKEN)
35+
36+
if [ -n "$DUP_EXISTS" ]; then
37+
echo "Generating new UUIDS for duplicated id tags..."
38+
else
39+
echo "No duplicated id was found"
40+
fi
41+
42+
grep -r -i $ID_TOKEN tests/foreman/ --include=*.py | sort | uniq -d | grep $ID_TOKEN | while read -r line ; do
43+
OLDIFS=$IFS
44+
IFS=':' read -r dup_file dup_id <<< $line
45+
echo "filename: $dup_file"
46+
echo "Id to replace: $dup_id"
47+
NEW_ID=$(python -c 'import uuid; print(uuid.uuid4())')
48+
echo "Replacing with the new id: $NEW_ID"
49+
LAST_LINE=$(grep -i -n "$dup_id" $dup_file | tail -1)
50+
51+
IFS=':' read -r linenumber linecontent <<< $LAST_LINE
52+
echo $linenumber
53+
trimmed_linecontent=$(echo $linecontent)
54+
sed -i~ "${linenumber}s/${trimmed_linecontent}/${ID_TOKEN} ${NEW_ID}/g" $dup_file
55+
echo "----------------------------------------------------------------"
56+
IFS=$OLDIFS
57+
done
58+
59+
# This script finds @id: missing spaces after :
60+
61+
62+
MISSING_SPACES=$(grep -E -i -r -n "${ID_TOKEN}[^ ]" tests/foreman/ --include=*.py)
63+
64+
if [ -n "$MISSING_SPACES" ]; then
65+
echo "Fixing id: tags missing spaces..."
66+
else
67+
echo "No id missing spaces was found"
68+
fi
69+
70+
grep -E -i -r -n "${ID_TOKEN}[^ ]" tests/foreman/ --include=*.py | while read -r line ; do
71+
OLDIFS=$IFS
72+
IFS=':' read -r missing_file linenumber linecontent <<< $line
73+
IFS=':' read -r tag uuid <<< $linecontent
74+
trimmed_linecontent=$(echo $linecontent)
75+
sed -i~ "${linenumber}s/${trimmed_linecontent}/${tag}: ${uuid}/g" $missing_file
76+
IFS=$OLDIFS
77+
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)