build: fix elvis check and ensure newline at EOF

This commit is contained in:
Zaiming Shi 2021-11-04 18:05:30 +01:00
parent 4c4993fa25
commit 37edb03866
2 changed files with 43 additions and 11 deletions

View File

@ -1,4 +1,4 @@
name: Elvis Linter name: Code style check
on: [pull_request] on: [pull_request]
@ -7,10 +7,18 @@ jobs:
runs-on: ubuntu-20.04 runs-on: ubuntu-20.04
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
with:
fetch-depth: 1000
- name: Set git token - name: Set git token
if: endsWith(github.repository, 'enterprise') if: endsWith(github.repository, 'enterprise')
run: | run: |
echo "https://ci%40emqx.io:${{ secrets.CI_GIT_TOKEN }}@github.com" > $HOME/.git-credentials echo "https://ci%40emqx.io:${{ secrets.CI_GIT_TOKEN }}@github.com" > $HOME/.git-credentials
git config --global credential.helper store git config --global credential.helper store
- run: | - name: Run elvis check
./scripts/elvis-check.sh $GITHUB_BASE_REF run: |
set -e
if [ -f EMQX_ENTERPRISE ]; then
./scripts/elvis-check.sh $GITHUB_BASE_REF emqx-enterprise
else
./scripts/elvis-check.sh $GITHUB_BASE_REF emqx
fi

View File

@ -5,16 +5,16 @@
set -euo pipefail set -euo pipefail
ELVIS_VERSION='1.0.0-emqx-2' elvis_version='1.0.0-emqx-2'
base="${1:-}" base="${1:-}"
repo="${2:-emqx/emqx}"
REPO="${GITHUB_REPOSITORY:-${repo}}"
if [ "${base}" = "" ]; then if [ "${base}" = "" ]; then
echo "Usage $0 <git-compare-base-ref>" echo "Usage $0 <git-compare-base-ref>"
exit 1 exit 1
fi fi
elvis_version="${2:-$ELVIS_VERSION}"
echo "elvis -v: $elvis_version" echo "elvis -v: $elvis_version"
echo "git diff base: $base" echo "git diff base: $base"
@ -27,11 +27,7 @@ if [[ "$base" =~ [0-9a-f]{8,40} ]]; then
# base is a commit sha1 # base is a commit sha1
compare_base="$base" compare_base="$base"
else else
if [[ $CI == true ]];then remote="$(git remote -v | grep -E "github\.com(:|/)$REPO((\.git)|(\s))" | grep fetch | awk '{print $1}')"
remote="$(git remote -v | grep -E "github\.com(.|/)$GITHUB_REPOSITORY" | grep fetch | awk '{print $1}')"
else
remote="$(git remote -v | grep -E 'github\.com(.|/)emqx' | grep fetch | awk '{print $1}')"
fi
git fetch "$remote" "$base" git fetch "$remote" "$base"
compare_base="$remote/$base" compare_base="$remote/$base"
fi fi
@ -58,3 +54,31 @@ if [ $bad_file_count -gt 0 ]; then
echo "elvis: $bad_file_count errors" echo "elvis: $bad_file_count errors"
exit 1 exit 1
fi fi
### now check new-line at EOF for changed files
nl_at_eof() {
local file="$1"
if ! [ -f "$file" ]; then
return
fi
case "$file" in
*.png|*rebar3)
return
;;
esac
local lastbyte
lastbyte="$(tail -c 1 "$file" 2>&1)"
if [ "$lastbyte" != '' ]; then
echo "$file"
return 1
fi
}
for file in $(git_diff); do
if ! nl_at_eof "$file"; then
bad_file_count=$(( bad_file_count + 1 ))
fi
done
exit $bad_file_count