Repository: googleapis/api-linter
Pull request #740 (2021-02-02 14:09:19)

feat: Add rule for AIP-162 Rollback HTTP URI suffix
Part of #721.


Patch:
diff --git a/docs/rules/0162/rollback-http-uri-suffix.md b/docs/rules/0162/rollback-http-uri-suffix.md
new file mode 100644
index 0000000..79ba7a8
--- /dev/null
+++ b/docs/rules/0162/rollback-http-uri-suffix.md
@@ -0,0 +1,67 @@
+---
+rule:
+  aip: 162
+  name: [core, '0162', rollback-http-uri-suffix]
+  summary: Rollback methods must have the correct URI suffix
+permalink: /162/rollback-http-uri-suffix
+redirect_from:
+  - /0162/rollback-http-uri-suffix
+---
+
+# Rollback methods: URI suffix
+
+This rule enforces that `Rollback` methods include the `:rollback` suffix
+in the REST URI, as mandated in [AIP-162][].
+
+## Details
+
+This rule looks at any method beginning with `Rollback`, and
+complains if the HTTP URI does not end with `:rollback`.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+rpc RollbackBook(RollbackBookRequest) returns (Book) {
+  option (google.api.http) = {
+    post: "/v1/{name=publishers/*/books/*}:undo"  // Should end with `:rollback`
+    body: "*"
+  };
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+rpc RollbackBook(RollbackBookRequest) returns (Book) {
+  option (google.api.http) = {
+    post: "/v1/{name=publishers/*/books/*}:rollback"
+    body: "*"
+  };
+}
+```
+
+## Disabling
+
+If you need to violate this rule, use a leading comment above the method.
+Remember to also include an [aip.dev/not-precedent][] comment explaining why.
+
+```proto
+// (-- api-linter: core::0162::rollback-http-uri-suffix=disabled
+//     aip.dev/not-precedent: We need to do this because reasons. --)
+rpc RollbackBook(RollbackBookRequest) returns (Book) {
+  option (google.api.http) = {
+    post: "/v1/{name=publishers/*/books/*}:undo"
+    body: "*"
+  };
+}
+```
+
+If you need to violate this rule for an entire file, place the comment at the
+top of the file.
+
+[aip-162]: https://aip.dev/162
+[aip.dev/not-precedent]: https://aip.dev/not-precedent
diff --git a/rules/aip0162/aip0162.go b/rules/aip0162/aip0162.go
index c6a3442..88bcfab 100644
--- a/rules/aip0162/aip0162.go
+++ b/rules/aip0162/aip0162.go
@@ -33,6 +33,7 @@ func AddRules(r lint.RuleRegistry) error {
 		commitRequestNameField,
 		commitRequestMessageName,
 		commitResponseMessageName,
+		rollbackHTTPURISuffix,
 		tagRevisionHTTPBody,
 		tagRevisionHTTPMethod,
 		tagRevisionHTTPURISuffix,
@@ -73,3 +74,11 @@ func isCommitMethod(m *desc.MethodDescriptor) bool {
 func isCommitRequestMessage(m *desc.MessageDescriptor) bool {
 	return commitReqMessageRegexp.MatchString(m.GetName())
 }
+
+var rollbackMethodRegexp = regexp.MustCompile(`^Rollback(?:[A-Za-z0-9]+)$`)
+var rollbackURINameRegexp = regexp.MustCompile(`:rollback$`)
+
+// Returns true if this is an AIP-162 Rollback method, false otherwise.
+func isRollbackMethod(m *desc.MethodDescriptor) bool {
+	return rollbackMethodRegexp.MatchString(m.GetName())
+}
diff --git a/rules/aip0162/rollback_http_uri_suffix.go b/rules/aip0162/rollback_http_uri_suffix.go
new file mode 100644
index 0000000..81a77eb
--- /dev/null
+++ b/rules/aip0162/rollback_http_uri_suffix.go
@@ -0,0 +1,39 @@
+// Copyright 2021 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 aip0162
+
+import (
+	"github.com/googleapis/api-linter/lint"
+	"github.com/googleapis/api-linter/rules/internal/utils"
+	"github.com/jhump/protoreflect/desc"
+)
+
+// Rollback methods should have a proper HTTP pattern.
+var rollbackHTTPURISuffix = &lint.MethodRule{
+	Name:   lint.NewRuleName(162, "rollback-http-uri-suffix"),
+	OnlyIf: isRollbackMethod,
+	LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
+		for _, httpRule := range utils.GetHTTPRules(m) {
+			if !rollbackURINameRegexp.MatchString(httpRule.URI) {
+				return []lint.Problem{{
+					Message:    `Rollback URI should end with ":rollback".`,
+					Descriptor: m,
+				}}
+			}
+		}
+
+		return nil
+	},
+}


Test patch:
diff --git a/rules/aip0162/rollback_http_uri_suffix_test.go b/rules/aip0162/rollback_http_uri_suffix_test.go
new file mode 100644
index 0000000..d3660fe
--- /dev/null
+++ b/rules/aip0162/rollback_http_uri_suffix_test.go
@@ -0,0 +1,57 @@
+// Copyright 2021 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 aip0162
+
+import (
+	"testing"
+
+	"github.com/googleapis/api-linter/rules/internal/testutils"
+)
+
+func TestRollbackHTTPURISuffix(t *testing.T) {
+	tests := []struct {
+		testName   string
+		URI        string
+		MethodName string
+		problems   testutils.Problems
+	}{
+		{"Valid", "/v1/{name=publishers/*/books/*}:rollback", "RollbackBook", nil},
+		{"InvalidSuffix", "/v1/{name=publishers/*/books/*}:undo", "RollbackBook", testutils.Problems{{Message: ":rollback"}}},
+		{"Irrelevant", "/v1/{name=publishers/*/books/*}:save", "AcquireBook", nil},
+	}
+
+	for _, test := range tests {
+		t.Run(test.testName, func(t *testing.T) {
+			file := testutils.ParseProto3Tmpl(t, `
+				import "google/api/annotations.proto";
+				service Library {
+					rpc {{.MethodName}}({{.MethodName}}Request) returns (Book) {
+						option (google.api.http) = {
+							post: "{{.URI}}"
+							body: "*"
+						};
+					}
+				}
+				message Book {}
+				message {{.MethodName}}Request {}
+				`, test)
+
+			problems := rollbackHTTPURISuffix.Lint(file)
+			if diff := test.problems.SetDescriptor(file.GetServices()[0].GetMethods()[0]).Diff(problems); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}