osmith submitted this change.

View Change

Approvals: Jenkins Builder: Verified fixeria: Looks good to me, but someone else must approve laforge: Looks good to me, but someone else must approve osmith: Looks good to me, approved
lint: annotate lines in gerrit

Use robot comments to add line based comments for the lint output.
Add checkpatch_json from coreboot 9cae17d0 to parse the checkpatch
output and convert it into a gerrit parsable format.

Example:
https://gerrit.osmocom.org/c/osmo-bsc-nat/+/29294

Co-Authored-By: Oliver Smith <osmith@sysmocom.de>
Change-Id: I1a48ddb976e0f53bfc0552d0be11e42ba68d9e49
---
A lint/checkpatch/checkpatch_json.py
M lint/lint_diff.sh
2 files changed, 88 insertions(+), 0 deletions(-)

diff --git a/lint/checkpatch/checkpatch_json.py b/lint/checkpatch/checkpatch_json.py
new file mode 100755
index 0000000..81946ca
--- /dev/null
+++ b/lint/checkpatch/checkpatch_json.py
@@ -0,0 +1,60 @@
+#!/usr/bin/env python3
+# SPDX-License-Identifier: GPL-2.0-only
+
+"""
+This utilty generate json output to post comment in gerrit.
+
+INPUT: output of checkpatch.pl.
+OUTPUT: json format output that can be used to post comment in gerrit
+"""
+import os
+import sys
+import json
+
+data = {}
+data['comments'] = []
+list_temp = {}
+
+def update_struct( file_path, msg_output, line_number):
+ if file_path not in list_temp:
+ list_temp[file_path] = []
+ list_temp[file_path].append({
+ "robot_id" : "checkpatch",
+ "robot_run_id" : sys.argv[3],
+ "url" : sys.argv[4],
+ "line" : line_number,
+ "message" : msg_output,}
+ )
+
+def parse_file(input_file):
+ fp = open (input_file, "r")
+ for line in fp:
+ if line.startswith("ERROR:"):
+ msg_output = line.split("ERROR:")[1].strip()
+ elif line.startswith("WARNING:"):
+ msg_output = line.split("WARNING:")[1].strip()
+ elif ": FILE:" in line:
+ temp = line.split("FILE:")
+ file_path = temp[1].split(":")[0]
+ line_number = temp[1].split(":")[1]
+ update_struct( file_path.strip(), msg_output, str(line_number) )
+ else:
+ continue
+ fp.close()
+
+def main():
+ if (len(sys.argv) < 5) or (sys.argv[1] == "-h"):
+ print("HELP:")
+ print(sys.argv[0] + " <input file> <output file in json> <job-id> <job-url>")
+ sys.exit()
+
+ print(sys.argv[1])
+ parse_file(sys.argv[1])
+ data['robot_comments'] = list_temp
+ print(json.dumps(data))
+ out_file = open( sys.argv[2] , "w")
+ json.dump(data, out_file, sort_keys=True, indent=4)
+ out_file.close()
+
+if __name__ == "__main__":
+ main()
diff --git a/lint/lint_diff.sh b/lint/lint_diff.sh
index 0762f6b..7ae186f 100755
--- a/lint/lint_diff.sh
+++ b/lint/lint_diff.sh
@@ -45,5 +45,33 @@
echo "Please fix the linting errors above. More information:"
echo "https://osmocom.org/projects/cellular-infrastructure/wiki/Linting"
echo
+
+ if [ -n "$JENKINS_HOME" ]; then
+ echo "Leaving review comments in gerrit..."
+ set -x
+
+ # Run again, but in the proper format for checkpatch_json.py
+ # and store the output in a file
+ git diff -U0 "$COMMIT" | "$SCRIPT_DIR/checkpatch/checkpatch_osmo.sh" \
+ > ../checkpatch_output || true
+ cd ..
+ # Convert to gerrit review format
+ "$SCRIPT_DIR/checkpatch/checkpatch_json.py" \
+ checkpatch_output \
+ gerrit_report.json \
+ "$BUILD_TAG" \
+ "$BUILD_URL"
+ # Apply as review in gerrit
+ ssh \
+ -p "$GERRIT_PORT" \
+ -l jenkins \
+ "$GERRIT_HOST" \
+ gerrit \
+ review \
+ "$GERRIT_PATCHSET_REVISION" \
+ --json \
+ < gerrit_report.json
+ fi
+
exit 1
fi

To view, visit change 26393. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: osmo-ci
Gerrit-Branch: master
Gerrit-Change-Id: I1a48ddb976e0f53bfc0552d0be11e42ba68d9e49
Gerrit-Change-Number: 26393
Gerrit-PatchSet: 2
Gerrit-Owner: lynxis lazus <lynxis@fe80.eu>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: fixeria <vyanitskiy@sysmocom.de>
Gerrit-Reviewer: laforge <laforge@osmocom.org>
Gerrit-Reviewer: osmith <osmith@sysmocom.de>
Gerrit-Reviewer: pespin <pespin@sysmocom.de>
Gerrit-MessageType: merged