Add rule code to GitLab description (#19896)

## Summary

Fixes #19881. While I was here, I also made a couple of related tweaks
to the output format. First, we don't need to strip the `SyntaxError: `
prefix anymore since that's not added directly to the diagnostic message
after #19644. Second, we can use `secondary_code_or_id` to fall back on
the lint ID for syntax errors, which changes the `check_name` from
`syntax-error` to `invalid-syntax`. And then the main change requested
in the issue, prepending the `check_name` to the description.

## Test Plan

Existing tests and a new screenshot from GitLab:

<img width="362" height="113" alt="image"
src="https://github.com/user-attachments/assets/97654ad4-a639-4489-8c90-8661c7355097"
/>
This commit is contained in:
Brent Westbrook 2025-08-13 11:19:26 -04:00 committed by GitHub
parent d59282ebb5
commit 11d2cb6d56
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 16 additions and 22 deletions

View file

@ -19,7 +19,7 @@ exit_code: 1
[
{
"check_name": "F401",
"description": "`os` imported but unused",
"description": "F401: `os` imported but unused",
"fingerprint": "4dbad37161e65c72",
"location": {
"path": "input.py",
@ -38,7 +38,7 @@ exit_code: 1
},
{
"check_name": "F821",
"description": "Undefined name `y`",
"description": "F821: Undefined name `y`",
"fingerprint": "7af59862a085230",
"location": {
"path": "input.py",
@ -56,8 +56,8 @@ exit_code: 1
"severity": "major"
},
{
"check_name": "syntax-error",
"description": "Cannot use `match` statement on Python 3.9 (syntax was added in Python 3.10)",
"check_name": "invalid-syntax",
"description": "invalid-syntax: Cannot use `match` statement on Python 3.9 (syntax was added in Python 3.10)",
"fingerprint": "e558cec859bb66e8",
"location": {
"path": "input.py",

View file

@ -88,20 +88,14 @@ impl Serialize for SerializedMessages<'_> {
}
fingerprints.insert(message_fingerprint);
let (description, check_name) = if let Some(code) = diagnostic.secondary_code() {
(diagnostic.body().to_string(), code.as_str())
} else {
let description = diagnostic.body();
let description_without_prefix = description
.strip_prefix("SyntaxError: ")
.unwrap_or(description);
(description_without_prefix.to_string(), "syntax-error")
};
let description = diagnostic.body();
let check_name = diagnostic.secondary_code_or_id();
let value = json!({
"check_name": check_name,
"description": description,
// GitLab doesn't display the separate `check_name` field in a Code Quality report,
// so prepend it to the description too.
"description": format!("{check_name}: {description}"),
"severity": "major",
"fingerprint": format!("{:x}", message_fingerprint),
"location": {

View file

@ -5,7 +5,7 @@ expression: redact_fingerprint(&content)
[
{
"check_name": "F401",
"description": "`os` imported but unused",
"description": "F401: `os` imported but unused",
"fingerprint": "<redacted>",
"location": {
"path": "fib.py",
@ -24,7 +24,7 @@ expression: redact_fingerprint(&content)
},
{
"check_name": "F841",
"description": "Local variable `x` is assigned to but never used",
"description": "F841: Local variable `x` is assigned to but never used",
"fingerprint": "<redacted>",
"location": {
"path": "fib.py",
@ -43,7 +43,7 @@ expression: redact_fingerprint(&content)
},
{
"check_name": "F821",
"description": "Undefined name `a`",
"description": "F821: Undefined name `a`",
"fingerprint": "<redacted>",
"location": {
"path": "undef.py",

View file

@ -4,8 +4,8 @@ expression: redact_fingerprint(&content)
---
[
{
"check_name": "syntax-error",
"description": "Expected one or more symbol names after import",
"check_name": "invalid-syntax",
"description": "invalid-syntax: Expected one or more symbol names after import",
"fingerprint": "<redacted>",
"location": {
"path": "syntax_errors.py",
@ -23,8 +23,8 @@ expression: redact_fingerprint(&content)
"severity": "major"
},
{
"check_name": "syntax-error",
"description": "Expected ')', found newline",
"check_name": "invalid-syntax",
"description": "invalid-syntax: Expected ')', found newline",
"fingerprint": "<redacted>",
"location": {
"path": "syntax_errors.py",