Skip to content

Commit afd0d2b

Browse files
authored
Merge pull request #16266 from argotorg/move-coding-style-check-to-gh-actions
Move coding-style check to GitHub actions
2 parents f72a1e2 + ca7d54c commit afd0d2b

4 files changed

Lines changed: 112 additions & 102 deletions

File tree

.circleci/config.yml

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,25 +1002,6 @@ jobs:
10021002
command: test/docsCodeStyle.sh
10031003
- matrix_notify_failure_unless_pr
10041004

1005-
chk_coding_style:
1006-
<<: *base_cimg_small
1007-
steps:
1008-
- checkout
1009-
- run:
1010-
name: Install shellcheck
1011-
command: |
1012-
sudo apt -q update
1013-
sudo apt install -y shellcheck
1014-
- run:
1015-
name: Check for C++ coding style
1016-
command: scripts/check_style.sh
1017-
- run:
1018-
name: checking shell scripts
1019-
command: scripts/chk_shellscripts/chk_shellscripts.sh
1020-
- run:
1021-
name: Check for broken symlinks
1022-
command: scripts/check_symlinks.sh
1023-
- matrix_notify_failure_unless_pr
10241005

10251006
chk_errorcodes:
10261007
<<: *base_ubuntu2404_small
@@ -2094,7 +2075,6 @@ workflows:
20942075
jobs:
20952076
# basic checks
20962077
- chk_spelling: *requires_nothing
2097-
- chk_coding_style: *requires_nothing
20982078
# DISABLED FOR 0.6.0 - chk_docs_examples: *requires_nothing
20992079
- chk_buglist: *requires_nothing
21002080
- chk_proofs: *requires_nothing

.github/workflows/code-style.yml

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
name: Code Style Check
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, reopened]
6+
7+
permissions:
8+
pull-requests: write
9+
contents: read
10+
11+
jobs:
12+
chk_coding_style:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 #v5.0.0
16+
17+
- name: Install dependencies
18+
run: |
19+
sudo apt --quiet update
20+
sudo apt install --yes shellcheck git openssl
21+
22+
- name: Check for C++ coding style
23+
id: style-check
24+
run: |
25+
output=$(scripts/check_style.sh 2>&1) || {
26+
delimiter="$(openssl rand -hex 8)"
27+
{
28+
echo "output<<${delimiter}"
29+
echo "$output"
30+
echo "${delimiter}"
31+
} >> "$GITHUB_OUTPUT"
32+
exit 1
33+
}
34+
35+
- name: Comment PR on failure
36+
if: ${{ failure() && steps.style-check.outcome == 'failure' }}
37+
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd #v8.0.0
38+
env:
39+
STYLE_OUTPUT: ${{ steps.style-check.outputs.output }}
40+
with:
41+
script: |
42+
const output = process.env.STYLE_OUTPUT;
43+
const headSha = context.payload.pull_request.head.sha;
44+
const body = `There was an error when running \`Code Style Check\` for commit \`${headSha}\`:
45+
\`\`\`
46+
${output}
47+
\`\`\`
48+
Please check that your changes are working as intended.`;
49+
50+
await github.rest.issues.createComment({
51+
issue_number: context.issue.number,
52+
owner: context.repo.owner,
53+
repo: context.repo.repo,
54+
body: body
55+
});
56+
57+
for (const line of output.split('\n')) {
58+
const match = line.match(/^\[([^\]]+)\]\s*([^:]+\.(cpp|h)):(\d+):/);
59+
if (!match) continue;
60+
const [, errorDescription, filePath, , lineNumber] = match;
61+
try {
62+
await github.rest.pulls.createReviewComment({
63+
owner: context.repo.owner,
64+
repo: context.repo.repo,
65+
pull_number: context.issue.number,
66+
commit_id: headSha,
67+
path: filePath,
68+
line: parseInt(lineNumber),
69+
side: 'RIGHT',
70+
body: `**Coding style error:** ${errorDescription}`
71+
});
72+
} catch (error) {
73+
console.log(`Could not add inline comment for ${filePath}:${lineNumber}: ${error.message}`);
74+
}
75+
}
76+
77+
- name: checking shell scripts
78+
run: scripts/chk_shellscripts/chk_shellscripts.sh
79+
80+
- name: Check for broken symlinks
81+
run: scripts/check_symlinks.sh

scripts/check_style.sh

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -25,60 +25,64 @@ EXCLUDE_FILES_JOINED=${EXCLUDE_FILES_JOINED%??}
2525
REPO_ROOT="$(dirname "$0")"/..
2626
cd "$REPO_ROOT" || exit 1
2727

28+
function preparedGrep
29+
{
30+
git grep -nIE "$1" -- '*.h' '*.cpp' ':!test/' | grep -v "${EXCLUDE_FILES_JOINED}"
31+
return $?
32+
}
33+
34+
function addPrefix
35+
{
36+
sed "s/^/[$1] /"
37+
}
38+
2839
WHITESPACE=$(git grep -n -I -E "^.*[[:space:]]+$" |
2940
grep -v "test/libsolidity/ASTJSON\|test/compilationTests/zeppelin/LICENSE\|${EXCLUDE_FILES_JOINED}" || true
3041
)
3142

3243
if [[ "$WHITESPACE" != "" ]]
3344
then
3445
echo "Error: Trailing whitespace found:" | tee -a "$ERROR_LOG"
35-
echo "$WHITESPACE" | tee -a "$ERROR_LOG"
36-
scripts/ci/post_style_errors_on_github.sh "$ERROR_LOG"
37-
exit 1
46+
echo "$WHITESPACE" | addPrefix "Trailing whitespace" | tee -a "$ERROR_LOG"
3847
fi
3948

40-
function preparedGrep
41-
{
42-
git grep -nIE "$1" -- '*.h' '*.cpp' | grep -v "${EXCLUDE_FILES_JOINED}"
43-
return $?
44-
}
45-
4649
FORMATERROR=$(
4750
(
48-
preparedGrep "#include \"" | grep -E -v -e "license.h" -e "BuildInfo.h" # Use include with <> characters
49-
preparedGrep "\<(if|for|while|switch)\(" # no space after "if", "for", "while" or "switch"
50-
preparedGrep "\<for\>[[:space:]]*\([^=]*\>[[:space:]]:[[:space:]].*\)" # no space before range based for-loop
51-
preparedGrep "\<if\>[[:space:]]*\(.*\)[[:space:]]*\{[[:space:]]*$" # "{\n" on same line as "if"
52-
preparedGrep "namespace .*\{"
53-
preparedGrep "[,\(<][[:space:]]*const " # const on left side of type
54-
preparedGrep "^[[:space:]]*(static)?[[:space:]]*const " # const on left side of type (beginning of line)
55-
preparedGrep "^ [^*]|[^*] | [^*]" # uses spaces for indentation or mixes spaces and tabs
56-
preparedGrep "[a-zA-Z0-9_][[:space:]]*[&][a-zA-Z_]" | grep -E -v "return [&]" # right-aligned reference ampersand (needs to exclude return)
57-
# right-aligned reference pointer star (needs to exclude return and comments)
58-
preparedGrep "[a-zA-Z0-9_][[:space:]]*[*][a-zA-Z_]" | grep -E -v -e "return [*]" -e ":[0-9]+:[[:space:]]*\*[[:space:]]" -e "//"
59-
# unqualified move()/forward() checks, i.e. make sure that std::move() and std::forward() are used instead of move() and forward()
60-
preparedGrep "move\(.+\)" | grep -v "std::move" | grep -E "[^a-z]move"
61-
preparedGrep "forward\(.+\)" | grep -v "std::forward" | grep -E "[^a-z]forward"
62-
) | grep -E -v -e "^[a-zA-Z./]*:[0-9]*:[[:space:]]*/[/*]" -e "^test/" || true
51+
preparedGrep "#include \"" | grep -E -v -e "license.h" -e "BuildInfo.h" | addPrefix "Use angle brackets for includes"
52+
preparedGrep "\<(if|for|while|switch)\(" | addPrefix "Missing space after keyword"
53+
preparedGrep "\<for\>[[:space:]]*\([^=]*\>[[:space:]]:[[:space:]].*\)" | addPrefix "Missing space before range-based for colon"
54+
preparedGrep "\<if\>[[:space:]]*\(.*\)[[:space:]]*\{[[:space:]]*$" | addPrefix "Opening brace on same line as if"
55+
preparedGrep "namespace .*\{" | addPrefix "Missing space before opening brace in namespace"
56+
preparedGrep "[,\(<][[:space:]]*const " | addPrefix "const should be on the right side of the type"
57+
preparedGrep "^[[:space:]]*(static)?[[:space:]]*const " | addPrefix "const should be on the right side of the type"
58+
preparedGrep "^ [^*]|[^*] | [^*]" | addPrefix "Use tabs for indentation"
59+
preparedGrep "[a-zA-Z0-9_][[:space:]]*[&][a-zA-Z_]" | grep -E -v "return [&]" | addPrefix "Reference ampersand should be left-aligned"
60+
preparedGrep "[a-zA-Z0-9_][[:space:]]*[*][a-zA-Z_]" | grep -E -v -e "return [*]" -e ":[0-9]+:[[:space:]]*\*[[:space:]]" -e "//" | addPrefix "Pointer star should be left-aligned"
61+
preparedGrep "move\(.+\)" | grep -v "std::move" | grep -E "[^a-z]move" | addPrefix "Use std::move"
62+
preparedGrep "forward\(.+\)" | grep -v "std::forward" | grep -E "[^a-z]forward" | addPrefix "Use std::forward"
63+
) | grep -E -v -e "^\[[^]]*\] [a-zA-Z./]*:[0-9]*:[[:space:]]*/[/*]" || true
6364
)
6465

6566
# Special error handling for `using namespace std;` exclusion, since said statement can be present in the test directory
6667
# and its subdirectories, but is excluded in the above ruleset. In order to have consistent codestyle with regards to
6768
# std namespace usage, test directory must also be covered.
6869
FORMATSTDERROR=$(
6970
(
70-
git grep -nIE "using namespace std;" -- '*.h' '*.cpp'
71+
git grep -nIE "using namespace std;" -- '*.h' '*.cpp' | addPrefix "Do not use 'using namespace std'"
7172
) || true
7273
)
7374

7475
# Merge errors into single string
75-
FORMATEDERRORS="$FORMATERROR$FORMATSTDERROR"
76+
FORMATEDERRORS=$(printf '%s\n' "$FORMATERROR" "$FORMATSTDERROR" | grep -v '^$' || true)
7677

7778
if [[ "$FORMATEDERRORS" != "" ]]
7879
then
7980
echo "Coding style error:" | tee -a "$ERROR_LOG"
8081
echo "$FORMATEDERRORS" | tee -a "$ERROR_LOG"
81-
scripts/ci/post_style_errors_on_github.sh "$ERROR_LOG"
82+
fi
83+
84+
if [[ -s "$ERROR_LOG" ]]
85+
then
8286
exit 1
8387
fi
8488
)

scripts/ci/post_style_errors_on_github.sh

Lines changed: 0 additions & 55 deletions
This file was deleted.

0 commit comments

Comments
 (0)