Repository: googleapis/api-linter
Pull request #530 (2020-04-06 19:59:42)

[AIP-192] Prohibit relative links.

lukesneeringer: @mingzhi Ping :)

Patch:
diff --git a/docs/rules/0192/absolute-links.md b/docs/rules/0192/absolute-links.md
new file mode 100644
index 0000000..8a8ce01
--- /dev/null
+++ b/docs/rules/0192/absolute-links.md
@@ -0,0 +1,60 @@
+---
+rule:
+  aip: 192
+  name: [core, '0192', absolute-links]
+  summary: Comments must not use raw HTML
+permalink: /192/absolute-links
+redirect_from:
+  - /0192/absolute-links
+---
+
+# Absolute links
+
+This rule attempts to enforce that every descriptor in every proto file uses
+absolute links, as mandated in [AIP-192][].
+
+## Details
+
+This rule looks at each descriptor in each proto file (exempting oneofs and the
+file itself) and tries to find Markdown links using the `[link](uri)` syntax,
+and complains if the URI does not have `://` in it.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+// A representation of [a book](/wiki/Book).
+message Book {
+  string name = 1;
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+// A representation of [a book](https://en.wikipedia.org/wiki/Book).
+message Book {
+  string name = 1;
+}
+```
+
+## Disabling
+
+If you need to violate this rule, use a leading comment above the descriptor
+(and revel in the irony). Remember to also include an [aip.dev/not-precedent][]
+comment explaining why.
+
+```proto
+// (-- api-linter: core::0192::absolute-links=disabled
+//     aip.dev/not-precedent: We need to do this because reasons. --)
+// A representation of [a book](/wiki/Book).
+message Book {
+  string name = 1;
+}
+```
+
+[aip-192]: https://aip.dev/192
+[aip.dev/not-precedent]: https://aip.dev/not-precedent
diff --git a/rules/aip0192/absolute_links.go b/rules/aip0192/absolute_links.go
new file mode 100644
index 0000000..b28a9e9
--- /dev/null
+++ b/rules/aip0192/absolute_links.go
@@ -0,0 +1,53 @@
+// Copyright 2020 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package aip0192
+
+import (
+	"regexp"
+	"strings"
+
+	"github.com/googleapis/api-linter/lint"
+	"github.com/jhump/protoreflect/desc"
+)
+
+// absoluteLinks ensures that a descriptor has only absolute links.
+var absoluteLinks = &lint.DescriptorRule{
+	Name: lint.NewRuleName(192, "absolute-links"),
+	LintDescriptor: func(d desc.Descriptor) []lint.Problem {
+		comment := strings.Join(
+			separateInternalComments(d.GetSourceInfo().GetLeadingComments()).External,
+			"\n",
+		)
+
+		// Absolute links can appear in either free text or Markdown links,
+		// so we need to spot both, but we also do not want a ton of false
+		// positives.
+		//
+		// It seems infeasible to check free text, but we can check Markdown links.
+		for _, match := range mdLink.FindAllStringSubmatch(comment, -1) {
+			uri := match[1]
+			if !strings.Contains(uri, "://") {
+				return []lint.Problem{{
+					Message:    "Links in comments should be absolute and begin with https://",
+					Descriptor: d,
+				}}
+			}
+		}
+
+		return nil
+	},
+}
+
+var mdLink = regexp.MustCompile(`\[.*?\]\(([\w\d:/.%_-]+)\)`)
diff --git a/rules/aip0192/aip0192.go b/rules/aip0192/aip0192.go
index a240719..eba3696 100644
--- a/rules/aip0192/aip0192.go
+++ b/rules/aip0192/aip0192.go
@@ -25,6 +25,7 @@ import (
 func AddRules(r lint.RuleRegistry) error {
 	return r.Register(
 		192,
+		absoluteLinks,
 		hasComments,
 		noHTML,
 		onlyLeadingComments,


Test patch:
diff --git a/rules/aip0192/absolute_links_test.go b/rules/aip0192/absolute_links_test.go
new file mode 100644
index 0000000..141780b
--- /dev/null
+++ b/rules/aip0192/absolute_links_test.go
@@ -0,0 +1,45 @@
+// Copyright 2020 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package aip0192
+
+import (
+	"testing"
+
+	"github.com/googleapis/api-linter/rules/internal/testutils"
+)
+
+func TestAbsoluteLinks(t *testing.T) {
+	for _, test := range []struct {
+		name     string
+		URI      string
+		problems testutils.Problems
+	}{
+		{"Valid", "https://google.com/", testutils.Problems{}},
+		{"NoProtocol", "google.com", testutils.Problems{{Message: "https://"}}},
+		{"Relative", "/foo/bar/baz.html", testutils.Problems{{Message: "https://"}}},
+	} {
+		t.Run(test.name, func(t *testing.T) {
+			f := testutils.ParseProto3Tmpl(t, `
+			  // This is [a link]({{.URI}}).
+				message Foo {}
+			`, test)
+			m := f.GetMessageTypes()[0]
+			problems := absoluteLinks.Lint(f)
+			if diff := test.problems.SetDescriptor(m).Diff(problems); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}
diff --git a/rules/aip0192/no_html_test.go b/rules/aip0192/no_html_test.go
index 7ccaf41..70c6c2b 100644
--- a/rules/aip0192/no_html_test.go
+++ b/rules/aip0192/no_html_test.go
@@ -22,7 +22,7 @@ import (
 )
 
 func TestNoHTML(t *testing.T) {
-	rawHtmlProblem := testutils.Problems{{Message: "must not include raw HTML"}}
+	rawHTMLProblem := testutils.Problems{{Message: "must not include raw HTML"}}
 	noProblems := testutils.Problems{}
 
 	for _, test := range []struct {
@@ -35,9 +35,9 @@ func TestNoHTML(t *testing.T) {
 		{"ValidMoreMath", "x < 10 > y", noProblems},
 		{"ValidAngleBracketPlaceholders", "Format: http://<server>/<path>", noProblems},
 		{"ValidComplexTag", `<img src="https://foo.bar/mickey" />`, noProblems},
-		{"InvalidBold", "It is <b>great!</b>", rawHtmlProblem},
-		{"InvalidCode", "This is <code>code font</code>.", rawHtmlProblem},
-		{"InvalidBreak", "This spans<br />two lines.", rawHtmlProblem},
+		{"InvalidBold", "It is <b>great!</b>", rawHTMLProblem},
+		{"InvalidCode", "This is <code>code font</code>.", rawHTMLProblem},
+		{"InvalidBreak", "This spans<br />two lines.", rawHTMLProblem},
 		{"IntentionalFalseNegativeInnerSpace", "Something < b > bold < /b >", noProblems},
 	} {
 		t.Run(test.name, func(t *testing.T) {