From 4bda7937a3027dcfa04c2dc86737fa995c57ba72 Mon Sep 17 00:00:00 2001 From: James Addison Date: Wed, 28 Aug 2024 13:24:26 +0100 Subject: [PATCH] validator: allow custom issue severity to be specified --- src/as-validator.c | 133 ++++++++++++++++++++++++++++++++---------- tests/test-validate.c | 4 ++ 2 files changed, 107 insertions(+), 30 deletions(-) diff --git a/src/as-validator.c b/src/as-validator.c index 457735c9..81871532 100644 --- a/src/as-validator.c +++ b/src/as-validator.c @@ -165,45 +165,32 @@ as_validator_finalize (GObject *object) G_OBJECT_CLASS (as_validator_parent_class)->finalize (object); } -static void as_validator_add_issue (AsValidator *validator, - xmlNode *node, - const gchar *tag, - const gchar *format, - ...) G_GNUC_PRINTF (4, 5); +static void _as_validator_add_issue (AsValidator *validator, + xmlNode *node, + const gchar *tag_final, + const AsIssueSeverity severity, + const gchar *explanation, + const gchar *format, + ...) G_GNUC_PRINTF (6, 7); /** - * as_validator_add_issue: + * _as_validator_add_issue: **/ static void -as_validator_add_issue (AsValidator *validator, - xmlNode *node, - const gchar *tag, - const gchar *format, - ...) +_as_validator_add_issue (AsValidator *validator, + xmlNode *node, + const gchar *tag_final, + const AsIssueSeverity severity, + const gchar *explanation, + const gchar *format, + ...) { AsValidatorPrivate *priv = GET_PRIVATE (validator); va_list args; g_autofree gchar *buffer = NULL; - g_autofree gchar *tag_final = NULL; - const gchar *explanation; - AsIssueSeverity severity; g_autofree gchar *location = NULL; AsValidatorIssue *issue; gchar *id_str; - const AsValidatorIssueTag *tag_data = g_hash_table_lookup (priv->issue_tags, tag); - - if (tag_data == NULL) { - /* we requested data about an invalid tag */ - g_warning ("Validator invoked invalid issue tag: %s", tag); - severity = AS_ISSUE_SEVERITY_ERROR; - explanation = _("The emitted issue tag is unknown in the tag registry of AppStream. " - "This is a bug in the validator itself, please report this issue in our bugtracker."); - tag_final = g_strdup_printf ("__error__%s", tag); - } else { - tag_final = g_strdup (tag); - severity = tag_data->severity; - explanation = tag_data->explanation; - } if (format != NULL) { va_start (args, format); @@ -242,6 +229,84 @@ as_validator_add_issue (AsValidator *validator, } } +static void as_validator_add_issue (AsValidator *validator, + xmlNode *node, + const gchar *tag, + const gchar *format, + ...) G_GNUC_PRINTF (4, 5); + +/** + * as_validator_add_issue: + **/ +static void +as_validator_add_issue (AsValidator *validator, + xmlNode *node, + const gchar *tag, + const gchar *format, + ...) +{ + AsValidatorPrivate *priv = GET_PRIVATE (validator); + va_list args; + g_autofree gchar *tag_final = NULL; + const gchar *explanation; + AsIssueSeverity severity; + const AsValidatorIssueTag *tag_data = g_hash_table_lookup (priv->issue_tags, tag); + + if (tag_data == NULL) { + /* we requested data about an invalid tag */ + g_warning ("Validator invoked invalid issue tag: %s", tag); + severity = AS_ISSUE_SEVERITY_ERROR; + explanation = _("The emitted issue tag is unknown in the tag registry of AppStream. " + "This is a bug in the validator itself, please report this issue in our bugtracker."); + tag_final = g_strdup_printf ("__error__%s", tag); + } else { + tag_final = g_strdup (tag); + severity = tag_data->severity; + explanation = tag_data->explanation; + } + + _as_validator_add_issue(validator, node, tag_final, severity, explanation, format, args); +} + +static void as_validator_add_issue_at_severity (AsValidator *validator, + xmlNode *node, + const gchar *tag, + const AsIssueSeverity severity, + const gchar *format, + ...) G_GNUC_PRINTF (5, 6); + +/** + * as_validator_add_issue_with_severity: + **/ +static void +as_validator_add_issue_at_severity (AsValidator *validator, + xmlNode *node, + const gchar *tag, + AsIssueSeverity severity, + const gchar *format, + ...) +{ + AsValidatorPrivate *priv = GET_PRIVATE (validator); + va_list args; + g_autofree gchar *tag_final = NULL; + const gchar *explanation; + const AsValidatorIssueTag *tag_data = g_hash_table_lookup (priv->issue_tags, tag); + + if (tag_data == NULL) { + /* we requested data about an invalid tag */ + g_warning ("Validator invoked invalid issue tag: %s", tag); + severity = AS_ISSUE_SEVERITY_ERROR; + explanation = _("The emitted issue tag is unknown in the tag registry of AppStream. " + "This is a bug in the validator itself, please report this issue in our bugtracker."); + tag_final = g_strdup_printf ("__error__%s", tag); + } else { + tag_final = g_strdup (tag); + explanation = tag_data->explanation; + } + + _as_validator_add_issue(validator, node, tag_final, severity, explanation, format, args); +} + /** * as_validator_set_current_fname: * @@ -2449,11 +2514,19 @@ as_validator_check_release (AsValidator *validator, xmlNode *node, AsFormatStyle if (prop != NULL) { as_validator_validate_iso8601_complete_date (validator, node, prop); g_free (prop); - } else if (rel_kind != AS_RELEASE_KIND_SNAPSHOT) { + } else { g_autofree gchar *timestamp = as_xml_get_prop_value (node, "timestamp"); if (timestamp == NULL) { /* Neither timestamp, nor date property exists */ - as_validator_add_issue (validator, node, "release-time-missing", "date"); + if (rel_kind == AS_RELEASE_KIND_SNAPSHOT) { + as_validator_add_issue_at_severity (validator, + node, + "release-time-missing", + AS_ISSUE_SEVERITY_WARNING, + "date"); + } else { + as_validator_add_issue (validator, node, "release-time-missing", "date"); + } } else { /* check if the timestamp is both a number and higher than 3000. The 3000 is used to check that it is not a year */ if (!as_str_verify_integer (timestamp, 3000, G_MAXINT64)) diff --git a/tests/test-validate.c b/tests/test-validate.c index 2b86574f..4925199b 100644 --- a/tests/test-validate.c +++ b/tests/test-validate.c @@ -290,6 +290,10 @@ test_validator_snapshot_release (void) "developer-info-missing", "", -1, AS_ISSUE_SEVERITY_INFO, }, + { + "release-time-missing", "date", + 8, + AS_ISSUE_SEVERITY_WARNING, }, { "invalid-iso8601-date", "18:49:09", 9,