Skip to content

Commit

Permalink
Replace adoc refs with url.
Browse files Browse the repository at this point in the history
The adoc ref comes through in the logs which
isn't helpful, so this replaces it with a url.

https://issues.redhat.com/browse/EC-1098
  • Loading branch information
joejstuart committed Feb 7, 2025
1 parent e701bd2 commit f21e168
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
20 changes: 18 additions & 2 deletions internal/opa/rule/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func title(a *ast.AnnotationsRef) string {
}

// xrefRegExp is used to detect asciidoc links in a string.
var xrefRegExp = regexp.MustCompile(`xref:[\w\.\$\#]+\[([\w\s/\.]+)\]`)
var xrefRegExp = regexp.MustCompile(`xref:([^:]+):ROOT:(.+?)\.adoc#([^[]+)\[[^]]+\]`)

func description(a *ast.AnnotationsRef) string {
if a == nil || a.Annotations == nil {
Expand All @@ -53,7 +53,7 @@ func customAnnotationString(a *ast.AnnotationsRef, fieldName string) string {
if value, ok := a.Annotations.Custom[fieldName]; ok {
switch value := value.(type) {
case string:
return value
return replaceXrefReferencesWithURL(value)
case time.Time:
return value.Format(time.RFC3339)
}
Expand All @@ -62,6 +62,22 @@ func customAnnotationString(a *ast.AnnotationsRef, fieldName string) string {
return ""
}

// replace all ascii doc
func replaceXrefReferencesWithURL(input string) string {
return xrefRegExp.ReplaceAllStringFunc(input, func(match string) string {
matches := xrefRegExp.FindStringSubmatch(match)
if len(matches) < 4 {
return match
}

group := matches[1]
filename := matches[2]
anchor := matches[3]

return "https://conforma.dev/docs/" + group + "/" + filename + ".html#" + anchor
})
}

func effectiveOn(a *ast.AnnotationsRef) string {
return customAnnotationString(a, "effective_on")
}
Expand Down
45 changes: 45 additions & 0 deletions internal/opa/rule/rule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,3 +624,48 @@ func TestDependsOn(t *testing.T) {
})
}
}

func TestReplaceXrefReferencesWithURL(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "Single valid reference",
input: "xref:ec-cli:ROOT:configuration.adoc#_data_sources[data sources]",
expected: "https://conforma.dev/docs/ec-cli/configuration.html#_data_sources",
},
{
name: "Multiple valid references",
input: "Check xref:group1:ROOT:guide.adoc#intro[Introduction] and " +
"xref:group2:ROOT:manual.adoc#usage[Usage] for details.",
expected: "Check https://conforma.dev/docs/group1/guide.html#intro and " +
"https://conforma.dev/docs/group2/manual.html#usage for details.",
},
{
name: "No valid reference",
input: "This is a test string with no reference.",
expected: "This is a test string with no reference.",
},
{
name: "Malformed reference",
input: "xref:malformed",
expected: "xref:malformed",
},
{
name: "Reference missing bracketed text",
input: "xref:ec-cli:ROOT:configuration.adoc#_data_sources", // Missing trailing "[...]"
expected: "xref:ec-cli:ROOT:configuration.adoc#_data_sources", // No match → remains unchanged.
},
}

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result := replaceXrefReferencesWithURL(tc.input)
if result != tc.expected {
t.Errorf("Test %q failed:\nexpected: %q\ngot: %q", tc.name, tc.expected, result)
}
})
}
}

0 comments on commit f21e168

Please sign in to comment.