Repository: googleapis/api-linter
Pull request #744 (2021-02-02 22:41:51)

feat: Add rules for AIP-162 Rollback HTTP method and body
Part of #721.


Patch:
diff --git a/docs/rules/0162/rollback-http-body.md b/docs/rules/0162/rollback-http-body.md
new file mode 100644
index 0000000..50f2b5c
--- /dev/null
+++ b/docs/rules/0162/rollback-http-body.md
@@ -0,0 +1,66 @@
+---
+rule:
+  aip: 162
+  name: [core, '0162', rollback-http-body]
+  summary: Rollback methods should use `*` as the HTTP body.
+permalink: /162/rollback-http-body
+redirect_from:
+  - /0162/rollback-http-body
+---
+
+# Rollback methods: HTTP body
+
+This rule enforces that all `Rollback` RPCs use `*` as the HTTP `body`, as mandated in
+[AIP-162][].
+
+## Details
+
+This rule looks at any method beginning with `Rollback`, and complains
+if the HTTP `body` field is anything other than `*`.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+rpc RollbackBook(RollbackBookRequest) returns (Book) {
+  option (google.api.http) = {
+    post: "/v1/{name=publishers/*/books/*}:rollback"
+    // body: "*" should be set.
+  };
+}
+```
+
+**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-body=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/*}:rollback"
+  };
+}
+```
+
+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/docs/rules/0162/rollback-http-method.md b/docs/rules/0162/rollback-http-method.md
new file mode 100644
index 0000000..4b43cce
--- /dev/null
+++ b/docs/rules/0162/rollback-http-method.md
@@ -0,0 +1,66 @@
+---
+rule:
+  aip: 162
+  name: [core, '0162', rollback-http-method]
+  summary: Rollback methods must use the POST HTTP verb.
+permalink: /162/rollback-http-method
+redirect_from:
+  - /0162/rollback-http-method
+---
+
+# Rollback methods: POST HTTP verb
+
+This rule enforces that all `Rollback` RPCs use the `POST` HTTP verb, as
+mandated in [AIP-162][].
+
+## Details
+
+This rule looks at any method beginning with `Rollback`, and complains
+if the HTTP verb is anything other than `POST`. It _does_ check additional
+bindings if they are present.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+rpc RollbackBook(RollbackBookRequest) returns (Book) {
+  option (google.api.http) = {
+    get: "/v1/{name=publishers/*/books/*}:rollback"  // Should be `post:`.
+  };
+}
+```
+
+**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-method=disabled
+//     aip.dev/not-precedent: We need to do this because reasons. --)
+rpc RollbackBook(RollbackBookRequest) returns (Book) {
+  option (google.api.http) = {
+    get: "/v1/{name=publishers/*/books/*}:rollback"
+  };
+}
+```
+
+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 0f17c7a..d4a61f7 100644
--- a/rules/aip0162/aip0162.go
+++ b/rules/aip0162/aip0162.go
@@ -35,6 +35,8 @@ func AddRules(r lint.RuleRegistry) error {
 		commitRequestNameField,
 		commitRequestNameReference,
 		commitResponseMessageName,
+		rollbackHTTPBody,
+		rollbackHTTPMethod,
 		rollbackHTTPURISuffix,
 		tagRevisionHTTPBody,
 		tagRevisionHTTPMethod,
diff --git a/rules/aip0162/rollback_http_body.go b/rules/aip0162/rollback_http_body.go
new file mode 100644
index 0000000..643b7be
--- /dev/null
+++ b/rules/aip0162/rollback_http_body.go
@@ -0,0 +1,27 @@
+// 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"
+)
+
+// Rollback methods should have "*" as the HTTP body.
+var rollbackHTTPBody = &lint.MethodRule{
+	Name:       lint.NewRuleName(162, "rollback-http-body"),
+	OnlyIf:     isRollbackMethod,
+	LintMethod: utils.LintWildcardHTTPBody,
+}
diff --git a/rules/aip0162/rollback_http_method.go b/rules/aip0162/rollback_http_method.go
new file mode 100644
index 0000000..3da5f24
--- /dev/null
+++ b/rules/aip0162/rollback_http_method.go
@@ -0,0 +1,27 @@
+// 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"
+)
+
+// Rollback methods should use the HTTP POST method.
+var rollbackHTTPMethod = &lint.MethodRule{
+	Name:       lint.NewRuleName(162, "rollback-http-method"),
+	OnlyIf:     isRollbackMethod,
+	LintMethod: utils.LintHTTPMethod("POST"),
+}


Test patch:
diff --git a/rules/aip0162/rollback_http_body_test.go b/rules/aip0162/rollback_http_body_test.go
new file mode 100644
index 0000000..33d9be7
--- /dev/null
+++ b/rules/aip0162/rollback_http_body_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 TestRollbackHTTPBody(t *testing.T) {
+	tests := []struct {
+		testName   string
+		Body       string
+		MethodName string
+		problems   testutils.Problems
+	}{
+		{"Valid", "*", "RollbackBook", nil},
+		{"Invalid", "", "RollbackBook", testutils.Problems{{Message: "HTTP body"}}},
+		{"Irrelevant", "", "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: "/v1/{name=publishers/*/books/*}:rollback"
+							body: "{{.Body}}"
+						};
+					}
+				}
+				message Book {}
+				message {{.MethodName}}Request {}
+			`, test)
+			method := file.GetServices()[0].GetMethods()[0]
+			problems := rollbackHTTPBody.Lint(file)
+			if diff := test.problems.SetDescriptor(method).Diff(problems); diff != "" {
+				t.Error(diff)
+			}
+		})
+	}
+}
diff --git a/rules/aip0162/rollback_http_method_test.go b/rules/aip0162/rollback_http_method_test.go
new file mode 100644
index 0000000..5115a49
--- /dev/null
+++ b/rules/aip0162/rollback_http_method_test.go
@@ -0,0 +1,58 @@
+// 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 TestRollbackHTTPMethod(t *testing.T) {
+	// Set up testing permutations.
+	tests := []struct {
+		testName   string
+		Method     string
+		MethodName string
+		problems   testutils.Problems
+	}{
+		{"Valid", "post", "RollbackBook", nil},
+		{"Invalid", "delete", "RollbackBook", testutils.Problems{{Message: "HTTP POST"}}},
+		{"Irrelevant", "delete", "AcquireBook", nil},
+	}
+
+	// Run each test.
+	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) = {
+							{{.Method}}: "/v1/{name=publishers/*/books/*}:rollback"
+						};
+					}
+				}
+				message Book {}
+				message {{.MethodName}}Request {}
+			`, test)
+			method := file.GetServices()[0].GetMethods()[0]
+			problems := rollbackHTTPMethod.Lint(file)
+			if diff := test.problems.SetDescriptor(method).Diff(problems); diff != "" {
+				t.Error(diff)
+			}
+		})
+	}
+}