2019-02-05 12:19:43 -05:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
if [ -z "$CI_MERGE_REQUEST_TARGET_BRANCH_NAME" ]; then
|
2020-11-04 14:48:54 -05:00
|
|
|
echo This is not a merge request, skipping
|
|
|
|
exit 0
|
2019-02-05 12:19:43 -05:00
|
|
|
fi
|
|
|
|
|
|
|
|
git fetch $CI_MERGE_REQUEST_PROJECT_URL.git $CI_MERGE_REQUEST_TARGET_BRANCH_NAME
|
|
|
|
|
|
|
|
branch_point=$(git merge-base HEAD FETCH_HEAD)
|
|
|
|
|
|
|
|
commits=$(git log --format='format:%H' $branch_point..$CI_COMMIT_SHA)
|
|
|
|
|
|
|
|
if [ -z "$commits" ]; then
|
|
|
|
echo Commit range empty
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2020-11-04 12:44:01 -05:00
|
|
|
function commit_message_has_mr_url() {
|
2019-02-05 12:19:43 -05:00
|
|
|
commit=$1
|
|
|
|
commit_message=$(git show -s --format='format:%b' $commit)
|
2020-11-04 12:44:01 -05:00
|
|
|
echo "$commit_message" | grep -qe "^$CI_MERGE_REQUEST_PROJECT_URL\/\(-\/\)\?merge_requests\/$CI_MERGE_REQUEST_IID$"
|
2019-02-05 12:19:43 -05:00
|
|
|
return $?
|
|
|
|
}
|
|
|
|
|
2020-11-04 12:35:04 -05:00
|
|
|
JUNIT_REPORT_TESTS_FILE=$(mktemp)
|
|
|
|
|
|
|
|
function append_failed_test_case() {
|
|
|
|
test_name="$1"
|
|
|
|
commit="$2"
|
|
|
|
test_message="$3"
|
|
|
|
commit_short=${commit:0:8}
|
|
|
|
|
|
|
|
echo "<testcase name=\"$test_name: $commit_short\"><failure message=\"$commit_short: $test_message\"/></testcase>" >> $JUNIT_REPORT_TESTS_FILE
|
2020-12-03 17:41:34 -05:00
|
|
|
echo >&2 "Commit check failed: $commit_short: $test_message"
|
2020-11-04 12:35:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function append_passed_test_case() {
|
|
|
|
test_name="$1"
|
|
|
|
commit="$2"
|
|
|
|
commit_short=${commit:0:8}
|
|
|
|
|
|
|
|
echo "<testcase name=\"$test_name: $commit_short\"></testcase>" >> $JUNIT_REPORT_TESTS_FILE
|
|
|
|
}
|
|
|
|
|
|
|
|
function generate_junit_report() {
|
|
|
|
junit_report_file="$1"
|
|
|
|
num_tests=$(cat "$JUNIT_REPORT_TESTS_FILE" | wc -l)
|
2020-11-20 10:37:01 -05:00
|
|
|
num_failures=$(grep '<failure' "$JUNIT_REPORT_TESTS_FILE" | wc -l )
|
2020-11-04 12:35:04 -05:00
|
|
|
|
|
|
|
echo Generating JUnit report \"$(pwd)/$junit_report_file\" with $num_tests tests and $num_failures failures.
|
|
|
|
|
|
|
|
cat > $junit_report_file << __EOF__
|
|
|
|
<?xml version="1.0" encoding="utf-8"?>
|
|
|
|
<testsuites tests="$num_tests" errors="0" failures="$num_failures">
|
|
|
|
<testsuite name="commit-review" tests="$num_tests" errors="0" failures="$num_failures" skipped="0">
|
|
|
|
$(< $JUNIT_REPORT_TESTS_FILE)
|
|
|
|
</testsuite>
|
|
|
|
</testsuites>
|
|
|
|
__EOF__
|
|
|
|
}
|
|
|
|
|
|
|
|
function check_commit_message_subject() {
|
2019-04-01 07:10:10 -04:00
|
|
|
commit=$1
|
|
|
|
commit_message_subject=$(git show -s --format='format:%s' $commit)
|
|
|
|
|
|
|
|
if echo "$commit_message_subject" | grep -qe "\(^meta-\|^Meta\)"; then
|
2020-11-04 12:35:04 -05:00
|
|
|
append_failed_test_case meta-prefix $commit \
|
|
|
|
"Commit message subject should not be prefixed with 'meta-' or 'Meta'"
|
|
|
|
else
|
|
|
|
append_passed_test_case meta-prefix $commit
|
2019-04-01 07:10:10 -04:00
|
|
|
fi
|
|
|
|
|
2020-11-04 11:19:56 -05:00
|
|
|
if echo "$commit_message_subject" | grep -qe "\(^clutter-\|^Clutter\)"; then
|
2020-11-04 12:35:04 -05:00
|
|
|
append_failed_test_case clutter-prefix $commit \
|
|
|
|
"Commit message subject should not be prefixed with 'clutter-' or 'Clutter', use 'clutter/' instead"
|
|
|
|
else
|
|
|
|
append_passed_test_case clutter-prefix $commit
|
2020-11-04 11:19:56 -05:00
|
|
|
fi
|
|
|
|
|
|
|
|
if echo "$commit_message_subject" | grep -qe "\(^cogl-\|^Cogl\)"; then
|
2020-11-04 12:35:04 -05:00
|
|
|
append_failed_test_case cogl-prefix $commit \
|
|
|
|
"Commit message subject should not be prefixed with 'cogl-' or 'Cogl', use 'cogl/' instead"
|
|
|
|
else
|
|
|
|
append_passed_test_case cogl-prefix $commit
|
2020-11-04 11:19:56 -05:00
|
|
|
fi
|
|
|
|
|
2020-11-04 11:51:04 -05:00
|
|
|
if echo "$commit_message_subject" | sed -e 's/^[^:]\+: //' | grep -qe '^[[:lower:]]'; then
|
2020-11-04 12:35:04 -05:00
|
|
|
append_failed_test_case capitalization $commit \
|
|
|
|
"Commit message subject should be properly Capitalized. E.g. 'window: Marginalize extradicity'"
|
|
|
|
else
|
|
|
|
append_passed_test_case capitalization $commit
|
2020-11-04 11:51:04 -05:00
|
|
|
fi
|
|
|
|
|
2019-04-02 04:47:06 -04:00
|
|
|
if echo "$commit_message_subject" | grep -qe "\.[ch]:"; then
|
2020-11-04 12:35:04 -05:00
|
|
|
append_failed_test_case not-file-suffix $commit \
|
|
|
|
"Commit message subject prefix should not include .c, .h, etc."
|
|
|
|
else
|
|
|
|
append_passed_test_case not-file-suffix $commit
|
2019-04-02 04:47:06 -04:00
|
|
|
fi
|
|
|
|
|
2019-04-01 07:10:10 -04:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2019-10-21 07:19:16 -04:00
|
|
|
RET=0
|
2019-02-05 12:19:43 -05:00
|
|
|
for commit in $commits; do
|
2019-04-01 07:10:10 -04:00
|
|
|
|
2020-11-04 12:44:01 -05:00
|
|
|
if commit_message_has_mr_url $commit; then
|
2020-11-04 12:35:04 -05:00
|
|
|
append_failed_test_case superfluous_url $commit \
|
|
|
|
"Commit message must not contain a link to its own merge request"
|
|
|
|
else
|
|
|
|
append_passed_test_case superfluous_url $commit
|
2019-04-01 07:10:10 -04:00
|
|
|
fi
|
|
|
|
|
2020-11-04 12:35:04 -05:00
|
|
|
check_commit_message_subject $commit
|
2019-02-05 12:19:43 -05:00
|
|
|
done
|
2019-10-21 07:19:16 -04:00
|
|
|
|
2020-11-04 12:35:04 -05:00
|
|
|
generate_junit_report commit-message-junit-report.xml
|
|
|
|
|
|
|
|
! grep -q '<failure' commit-message-junit-report.xml
|
|
|
|
exit $?
|