From 6f3cfbc10208407c3927a5e3b691e68b168dd604 Mon Sep 17 00:00:00 2001 From: Zaiming Shi Date: Thu, 4 Nov 2021 14:39:27 +0100 Subject: [PATCH 1/4] chore: add a script to find files without new line at EOF --- scripts/check-nl-at-eof.sh | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100755 scripts/check-nl-at-eof.sh diff --git a/scripts/check-nl-at-eof.sh b/scripts/check-nl-at-eof.sh new file mode 100755 index 000000000..5d93d04ac --- /dev/null +++ b/scripts/check-nl-at-eof.sh @@ -0,0 +1,26 @@ +#!/bin/bash + +set -euo pipefail + +cd -P -- "$(dirname -- "$0")/.." + +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 + fi +} + +while read -r file; do + nl_at_eof "$file" +done < <(git ls-files) From 56e2a9741f3413a3c0606c0574ad2094291b8a8d Mon Sep 17 00:00:00 2001 From: Zaiming Shi Date: Thu, 4 Nov 2021 14:40:14 +0100 Subject: [PATCH 2/4] style: ensure newline at EOF for all files --- CONTRIBUTING.md | 2 +- apps/emqx/test/emqx_broker_helper_SUITE.erl | 6 +++--- apps/emqx/test/emqx_pqueue_SUITE.erl | 2 +- apps/emqx/test/emqx_sup_SUITE.erl | 2 +- apps/emqx_authn/test/emqx_authn_SUITE.erl | 2 +- apps/emqx_connector/src/emqx_connector_redis.erl | 2 +- apps/emqx_limiter/etc/emqx_limiter.conf | 2 +- apps/emqx_prometheus/grafana_template/EMQ.json | 2 +- apps/emqx_prometheus/grafana_template/EMQ_Dashboard.json | 2 +- apps/emqx_prometheus/grafana_template/ErlangVM.json | 2 +- apps/emqx_resource/examples/log_tracer.conf | 2 +- apps/emqx_resource/src/emqx_resource_sup.erl | 2 +- apps/emqx_resource/src/emqx_resource_uitils.erl | 2 +- apps/emqx_retainer/.gitignore | 2 +- apps/emqx_rule_engine/src/emqx_rule_maps.erl | 2 +- deploy/charts/emqx/templates/ingress.yaml | 2 +- deploy/charts/emqx/templates/rbac.yaml | 6 +++--- 17 files changed, 21 insertions(+), 21 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 5e73359e8..118e9a046 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -79,4 +79,4 @@ Just as in the **subject**, use the imperative, present tense: "change" not "cha The footer should contain any information about **Breaking Changes** and is also the place to reference GitHub issues that this commit **Closes**. -**Breaking Changes** should start with the word `BREAKING CHANGE:` with a space or two newlines. The rest of the commit message is then used for this. \ No newline at end of file +**Breaking Changes** should start with the word `BREAKING CHANGE:` with a space or two newlines. The rest of the commit message is then used for this. diff --git a/apps/emqx/test/emqx_broker_helper_SUITE.erl b/apps/emqx/test/emqx_broker_helper_SUITE.erl index 59b3847b0..29aca6e3b 100644 --- a/apps/emqx/test/emqx_broker_helper_SUITE.erl +++ b/apps/emqx/test/emqx_broker_helper_SUITE.erl @@ -41,7 +41,7 @@ t_lookup_subpid(_) -> emqx_broker_helper:register_sub(self(), <<"clientid">>), ct:sleep(10), ?assertEqual(self(), emqx_broker_helper:lookup_subpid(<<"clientid">>)). - + t_register_sub(_) -> ok = emqx_broker_helper:register_sub(self(), <<"clientid">>), ct:sleep(10), @@ -62,7 +62,7 @@ t_shard_seq(_) -> t_shards_num(_) -> ?assertEqual(emqx_vm:schedulers() * 32, emqx_broker_helper:shards_num()). - + t_get_sub_shard(_) -> ?assertEqual(0, emqx_broker_helper:get_sub_shard(self(), <<"topic">>)). @@ -72,4 +72,4 @@ t_terminate(_) -> t_uncovered_func(_) -> gen_server:call(emqx_broker_helper, test), gen_server:cast(emqx_broker_helper, test), - emqx_broker_helper ! test. \ No newline at end of file + emqx_broker_helper ! test. diff --git a/apps/emqx/test/emqx_pqueue_SUITE.erl b/apps/emqx/test/emqx_pqueue_SUITE.erl index a51c21473..76075381d 100644 --- a/apps/emqx/test/emqx_pqueue_SUITE.erl +++ b/apps/emqx/test/emqx_pqueue_SUITE.erl @@ -174,4 +174,4 @@ t_filter(_) -> t_highest(_) -> empty = ?PQ:highest(?PQ:new()), 0 = ?PQ:highest(?PQ:from_list([{0, a}, {0, b}])), - 2 = ?PQ:highest(?PQ:from_list([{0, a}, {0, b}, {1, c}, {2, d}, {2, e}])). \ No newline at end of file + 2 = ?PQ:highest(?PQ:from_list([{0, a}, {0, b}, {1, c}, {2, d}, {2, e}])). diff --git a/apps/emqx/test/emqx_sup_SUITE.erl b/apps/emqx/test/emqx_sup_SUITE.erl index 185e1d752..2f5600e38 100644 --- a/apps/emqx/test/emqx_sup_SUITE.erl +++ b/apps/emqx/test/emqx_sup_SUITE.erl @@ -36,4 +36,4 @@ t_child(_) -> ?assertMatch({error, not_found}, emqx_sup:stop_child(undef)), ?assertMatch({error, _}, emqx_sup:start_child(emqx_broker_sup, supervisor)), ?assertEqual(ok, emqx_sup:stop_child(emqx_broker_sup)), - ?assertMatch({ok, _}, emqx_sup:start_child(emqx_broker_sup, supervisor)). \ No newline at end of file + ?assertMatch({ok, _}, emqx_sup:start_child(emqx_broker_sup, supervisor)). diff --git a/apps/emqx_authn/test/emqx_authn_SUITE.erl b/apps/emqx_authn/test/emqx_authn_SUITE.erl index 1ee419bb0..d3704679f 100644 --- a/apps/emqx_authn/test/emqx_authn_SUITE.erl +++ b/apps/emqx_authn/test/emqx_authn_SUITE.erl @@ -19,4 +19,4 @@ -compile(export_all). -compile(nowarn_export_all). -all() -> emqx_common_test_helpers:all(?MODULE). \ No newline at end of file +all() -> emqx_common_test_helpers:all(?MODULE). diff --git a/apps/emqx_connector/src/emqx_connector_redis.erl b/apps/emqx_connector/src/emqx_connector_redis.erl index aed06e724..ff6e4d82d 100644 --- a/apps/emqx_connector/src/emqx_connector_redis.erl +++ b/apps/emqx_connector/src/emqx_connector_redis.erl @@ -182,4 +182,4 @@ to_server(Server) -> case string:tokens(Server, ":") of [Host, Port] -> {ok, {Host, list_to_integer(Port)}}; _ -> {error, Server} - end. \ No newline at end of file + end. diff --git a/apps/emqx_limiter/etc/emqx_limiter.conf b/apps/emqx_limiter/etc/emqx_limiter.conf index 44bbb1740..7298931e3 100644 --- a/apps/emqx_limiter/etc/emqx_limiter.conf +++ b/apps/emqx_limiter/etc/emqx_limiter.conf @@ -47,4 +47,4 @@ emqx_limiter { per_client = "100/10s,10" } } -} \ No newline at end of file +} diff --git a/apps/emqx_prometheus/grafana_template/EMQ.json b/apps/emqx_prometheus/grafana_template/EMQ.json index 137e3a5a4..54d0a9d47 100644 --- a/apps/emqx_prometheus/grafana_template/EMQ.json +++ b/apps/emqx_prometheus/grafana_template/EMQ.json @@ -2099,4 +2099,4 @@ "title": "EMQ", "uid": "tjRlQw6Zk", "version": 29 -} \ No newline at end of file +} diff --git a/apps/emqx_prometheus/grafana_template/EMQ_Dashboard.json b/apps/emqx_prometheus/grafana_template/EMQ_Dashboard.json index 0b0e2036b..0ee3c6741 100644 --- a/apps/emqx_prometheus/grafana_template/EMQ_Dashboard.json +++ b/apps/emqx_prometheus/grafana_template/EMQ_Dashboard.json @@ -630,4 +630,4 @@ "title": "EMQ Dashboard", "uid": "5sreUw6Wz", "version": 11 -} \ No newline at end of file +} diff --git a/apps/emqx_prometheus/grafana_template/ErlangVM.json b/apps/emqx_prometheus/grafana_template/ErlangVM.json index 556d815b0..23088123c 100644 --- a/apps/emqx_prometheus/grafana_template/ErlangVM.json +++ b/apps/emqx_prometheus/grafana_template/ErlangVM.json @@ -1471,4 +1471,4 @@ "title": "ErlangVM", "uid": "stprQQ6Zk", "version": 13 -} \ No newline at end of file +} diff --git a/apps/emqx_resource/examples/log_tracer.conf b/apps/emqx_resource/examples/log_tracer.conf index 7b438ec1f..49a57a13f 100644 --- a/apps/emqx_resource/examples/log_tracer.conf +++ b/apps/emqx_resource/examples/log_tracer.conf @@ -8,4 +8,4 @@ "bulk": "10KB" "chars_limit": 1024 } -} \ No newline at end of file +} diff --git a/apps/emqx_resource/src/emqx_resource_sup.erl b/apps/emqx_resource/src/emqx_resource_sup.erl index 22984b940..69d1acd20 100644 --- a/apps/emqx_resource/src/emqx_resource_sup.erl +++ b/apps/emqx_resource/src/emqx_resource_sup.erl @@ -55,4 +55,4 @@ ensure_pool_worker(Pool, Name, Slot) -> try gproc_pool:add_worker(Pool, Name, Slot) catch error:exists -> ok - end. \ No newline at end of file + end. diff --git a/apps/emqx_resource/src/emqx_resource_uitils.erl b/apps/emqx_resource/src/emqx_resource_uitils.erl index ab3f6dd1e..9f5f11a01 100644 --- a/apps/emqx_resource/src/emqx_resource_uitils.erl +++ b/apps/emqx_resource/src/emqx_resource_uitils.erl @@ -13,4 +13,4 @@ %% See the License for the specific language governing permissions and %% limitations under the License. %%-------------------------------------------------------------------- --module(emqx_resource_uitils). \ No newline at end of file +-module(emqx_resource_uitils). diff --git a/apps/emqx_retainer/.gitignore b/apps/emqx_retainer/.gitignore index d51b4e87f..a0c149280 100644 --- a/apps/emqx_retainer/.gitignore +++ b/apps/emqx_retainer/.gitignore @@ -23,4 +23,4 @@ logs/ rebar.lock test/ct.cover.spec etc/emqx_retainer.conf.rendered -.rebar3/ \ No newline at end of file +.rebar3/ diff --git a/apps/emqx_rule_engine/src/emqx_rule_maps.erl b/apps/emqx_rule_engine/src/emqx_rule_maps.erl index 4bb104f7f..c3f8fd3cf 100644 --- a/apps/emqx_rule_engine/src/emqx_rule_maps.erl +++ b/apps/emqx_rule_engine/src/emqx_rule_maps.erl @@ -206,4 +206,4 @@ unsafe_atom_key_map(BinKeyMap) when is_map(BinKeyMap) -> end, #{}, BinKeyMap); unsafe_atom_key_map(ListV) when is_list(ListV) -> [unsafe_atom_key_map(V) || V <- ListV]; -unsafe_atom_key_map(Val) -> Val. \ No newline at end of file +unsafe_atom_key_map(Val) -> Val. diff --git a/deploy/charts/emqx/templates/ingress.yaml b/deploy/charts/emqx/templates/ingress.yaml index 7fc66a86a..926023f61 100644 --- a/deploy/charts/emqx/templates/ingress.yaml +++ b/deploy/charts/emqx/templates/ingress.yaml @@ -95,4 +95,4 @@ spec: {{- toYaml .Values.ingress.mgmt.tls | nindent 4 }} {{- end }} --- -{{- end }} \ No newline at end of file +{{- end }} diff --git a/deploy/charts/emqx/templates/rbac.yaml b/deploy/charts/emqx/templates/rbac.yaml index 87cd18178..d2a0f35f4 100644 --- a/deploy/charts/emqx/templates/rbac.yaml +++ b/deploy/charts/emqx/templates/rbac.yaml @@ -17,8 +17,8 @@ rules: - apiGroups: - "" resources: - - endpoints - verbs: + - endpoints + verbs: - get - watch - list @@ -39,4 +39,4 @@ subjects: roleRef: kind: Role name: {{ include "emqx.fullname" . }} - apiGroup: rbac.authorization.k8s.io \ No newline at end of file + apiGroup: rbac.authorization.k8s.io From dc58e4a4418940ebd3c0217a84304ac9dc2a264b Mon Sep 17 00:00:00 2001 From: Zaiming Shi Date: Thu, 4 Nov 2021 14:47:05 +0100 Subject: [PATCH 3/4] ci: check code style run elvis and nl-at-eof check --- .../{elvis_lint.yaml => code-style-check.yaml} | 8 ++++++-- scripts/check-nl-at-eof.sh | 12 +++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) rename .github/workflows/{elvis_lint.yaml => code-style-check.yaml} (74%) diff --git a/.github/workflows/elvis_lint.yaml b/.github/workflows/code-style-check.yaml similarity index 74% rename from .github/workflows/elvis_lint.yaml rename to .github/workflows/code-style-check.yaml index 1fdbeba87..93e363245 100644 --- a/.github/workflows/elvis_lint.yaml +++ b/.github/workflows/code-style-check.yaml @@ -1,4 +1,4 @@ -name: Elvis Linter +name: Code style check on: [pull_request] @@ -12,5 +12,9 @@ jobs: run: | echo "https://ci%40emqx.io:${{ secrets.CI_GIT_TOKEN }}@github.com" > $HOME/.git-credentials git config --global credential.helper store - - run: | + - name: Run elvis check + run: | ./scripts/elvis-check.sh $GITHUB_BASE_REF + - name: Check line-break at EOF + - run: | + ./scripts/check-nl-at-eof.sh diff --git a/scripts/check-nl-at-eof.sh b/scripts/check-nl-at-eof.sh index 5d93d04ac..f4f1ef04f 100755 --- a/scripts/check-nl-at-eof.sh +++ b/scripts/check-nl-at-eof.sh @@ -6,7 +6,7 @@ cd -P -- "$(dirname -- "$0")/.." nl_at_eof() { local file="$1" - if ! [ -f $file ]; then + if ! [ -f "$file" ]; then return fi case "$file" in @@ -17,10 +17,16 @@ nl_at_eof() { local lastbyte lastbyte="$(tail -c 1 "$file" 2>&1)" if [ "$lastbyte" != '' ]; then - echo $file + echo "$file" + return 1 fi } +n=0 while read -r file; do - nl_at_eof "$file" + if ! nl_at_eof "$file"; then + n=$(( n + 1 )) + fi done < <(git ls-files) + +exit $n From d1abb3081890169e391e78f3bcba0499964fdbcc Mon Sep 17 00:00:00 2001 From: Zaiming Shi Date: Thu, 4 Nov 2021 15:22:33 +0100 Subject: [PATCH 4/4] ci: fix elvis check --- .github/workflows/code-style-check.yaml | 9 ++++++++- apps/emqx/test/emqx_pqueue_SUITE.erl | 3 ++- apps/emqx_connector/src/emqx_connector_redis.erl | 3 ++- apps/emqx_rule_engine/src/emqx_rule_maps.erl | 16 ++++++++-------- scripts/elvis-check.sh | 12 ++++-------- 5 files changed, 24 insertions(+), 19 deletions(-) diff --git a/.github/workflows/code-style-check.yaml b/.github/workflows/code-style-check.yaml index 93e363245..581dc4316 100644 --- a/.github/workflows/code-style-check.yaml +++ b/.github/workflows/code-style-check.yaml @@ -7,6 +7,8 @@ jobs: runs-on: ubuntu-20.04 steps: - uses: actions/checkout@v2 + with: + fetch-depth: 1000 - name: Set git token if: endsWith(github.repository, 'enterprise') run: | @@ -14,7 +16,12 @@ jobs: git config --global credential.helper store - name: Run elvis check run: | - ./scripts/elvis-check.sh $GITHUB_BASE_REF + 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 - name: Check line-break at EOF - run: | ./scripts/check-nl-at-eof.sh diff --git a/apps/emqx/test/emqx_pqueue_SUITE.erl b/apps/emqx/test/emqx_pqueue_SUITE.erl index 76075381d..797adb9ab 100644 --- a/apps/emqx/test/emqx_pqueue_SUITE.erl +++ b/apps/emqx/test/emqx_pqueue_SUITE.erl @@ -112,7 +112,8 @@ t_out(_) -> t_out_2(_) -> {empty, {pqueue, [{-1, {queue, [a], [], 1}}]}} = ?PQ:out(0, ?PQ:from_list([{1, a}])), {{value, a}, {queue, [], [], 0}} = ?PQ:out(1, ?PQ:from_list([{1, a}])), - {{value, a}, {pqueue, [{-1, {queue, [], [b], 1}}]}} = ?PQ:out(1, ?PQ:from_list([{1, a}, {1, b}])), + {{value, a}, {pqueue, [{-1, {queue, [], [b], 1}}]}} = + ?PQ:out(1, ?PQ:from_list([{1, a}, {1, b}])), {{value, a}, {queue, [b], [], 1}} = ?PQ:out(1, ?PQ:from_list([{1, a}, {0, b}])). t_out_p(_) -> diff --git a/apps/emqx_connector/src/emqx_connector_redis.erl b/apps/emqx_connector/src/emqx_connector_redis.erl index ff6e4d82d..670add693 100644 --- a/apps/emqx_connector/src/emqx_connector_redis.erl +++ b/apps/emqx_connector/src/emqx_connector_redis.erl @@ -100,7 +100,8 @@ on_start(InstId, #{redis_type := Type, Options = case maps:get(enable, SSL) of true -> [{ssl, true}, - {ssl_options, emqx_plugin_libs_ssl:save_files_return_opts(SSL, "connectors", InstId)} + {ssl_options, + emqx_plugin_libs_ssl:save_files_return_opts(SSL, "connectors", InstId)} ]; false -> [{ssl, false}] end ++ [{sentinel, maps:get(sentinel, Config, undefined)}], diff --git a/apps/emqx_rule_engine/src/emqx_rule_maps.erl b/apps/emqx_rule_engine/src/emqx_rule_maps.erl index c3f8fd3cf..fe4595c03 100644 --- a/apps/emqx_rule_engine/src/emqx_rule_maps.erl +++ b/apps/emqx_rule_engine/src/emqx_rule_maps.erl @@ -33,7 +33,7 @@ nested_get({var, Key}, Data, Default) -> nested_get({path, Path}, Data, Default) when is_list(Path) -> do_nested_get(Path, Data, Data, Default). -do_nested_get([Key|More], Data, OrgData, Default) -> +do_nested_get([Key | More], Data, OrgData, Default) -> case general_map_get(Key, Data, OrgData, undefined) of undefined -> Default; Val -> do_nested_get(More, Val, OrgData, Default) @@ -51,7 +51,7 @@ nested_put({var, Key}, Val, Map) -> nested_put({path, Path}, Val, Map) when is_list(Path) -> do_nested_put(Path, Val, Map, Map). -do_nested_put([Key|More], Val, Map, OrgData) -> +do_nested_put([Key | More], Val, Map, OrgData) -> SubMap = general_map_get(Key, Map, OrgData, undefined), general_map_put(Key, do_nested_put(More, Val, SubMap, OrgData), Map, OrgData); do_nested_put([], Val, _Map, _OrgData) -> @@ -131,13 +131,13 @@ setnth(tail, List, Val) when is_list(List) -> List ++ [Val]; setnth(tail, _List, Val) -> [Val]; setnth(I, List, _Val) when not is_integer(I) -> List; setnth(0, List, _Val) -> List; -setnth(I, List, _Val) when is_integer(I), I > 0 -> - do_setnth(I, List, _Val); -setnth(I, List, _Val) when is_integer(I), I < 0 -> - lists:reverse(do_setnth(-I, lists:reverse(List), _Val)). +setnth(I, List, Val) when is_integer(I), I > 0 -> + do_setnth(I, List, Val); +setnth(I, List, Val) when is_integer(I), I < 0 -> + lists:reverse(do_setnth(-I, lists:reverse(List), Val)). -do_setnth(1, [_|Rest], Val) -> [Val|Rest]; -do_setnth(I, [E|Rest], Val) -> [E|setnth(I-1, Rest, Val)]; +do_setnth(1, [_ | Rest], Val) -> [Val | Rest]; +do_setnth(I, [E | Rest], Val) -> [E | setnth(I-1, Rest, Val)]; do_setnth(_, [], _Val) -> []. getnth(0, _) -> diff --git a/scripts/elvis-check.sh b/scripts/elvis-check.sh index 5fe482865..ebcea98bc 100755 --- a/scripts/elvis-check.sh +++ b/scripts/elvis-check.sh @@ -5,16 +5,16 @@ set -euo pipefail -ELVIS_VERSION='1.0.0-emqx-2' +elvis_version='1.0.0-emqx-2' base="${1:-}" +repo="${2:-emqx}" +REPO="${GITHUB_REPOSITORY:-${repo}}" if [ "${base}" = "" ]; then echo "Usage $0 " exit 1 fi -elvis_version="${2:-$ELVIS_VERSION}" - echo "elvis -v: $elvis_version" echo "git diff base: $base" @@ -27,11 +27,7 @@ if [[ "$base" =~ [0-9a-f]{8,40} ]]; then # base is a commit sha1 compare_base="$base" else - if [[ $CI == true ]];then - 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 + remote="$(git remote -v | grep -E "github\.com(:|/)emqx/$REPO((\.git)|(\s))" | grep fetch | awk '{print $1}')" git fetch "$remote" "$base" compare_base="$remote/$base" fi