Skip to content

Commit

Permalink
validator: allow custom issue severity to be specified
Browse files Browse the repository at this point in the history
  • Loading branch information
jayaddison committed Aug 28, 2024
1 parent 1ad7c73 commit 95eec14
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 30 deletions.
133 changes: 103 additions & 30 deletions src/as-validator.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
const AsValidatorIssueTag *tag_data = g_hash_table_lookup (priv->issue_tags, tag);
g_autofree gchar *tag_final = NULL;
const gchar *explanation;
AsIssueSeverity severity;

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;
const AsValidatorIssueTag *tag_data = g_hash_table_lookup (priv->issue_tags, tag);
g_autofree gchar *tag_final = NULL;
const gchar *explanation;

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:
*
Expand Down Expand Up @@ -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))
Expand Down
4 changes: 4 additions & 0 deletions tests/test-validate.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 95eec14

Please sign in to comment.