Repository: googleapis/api-linter
Pull request #752 (2021-02-08 23:32:44)

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


Patch:
diff --git a/docs/rules/0162/delete-revision-http-body.md b/docs/rules/0162/delete-revision-http-body.md
new file mode 100644
index 0000000..fa7cd6d
--- /dev/null
+++ b/docs/rules/0162/delete-revision-http-body.md
@@ -0,0 +1,66 @@
+---
+rule:
+  aip: 162
+  name: [core, '0162', delete-revision-http-body]
+  summary: Delete Revision methods should not have an HTTP body.
+permalink: /162/delete-revision-http-body
+redirect_from:
+  - /0162/delete-revision-http-body
+---
+
+# Delete Revision methods: HTTP body
+
+This rule enforces that all Delete Revision RPCs have no HTTP `body`, as mandated in
+[AIP-162][].
+
+## Details
+
+This rule looks at any method matching `Delete*Revision`, and complains
+if it has an HTTP `body`.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+rpc DeleteBookRevision(DeleteBookRevisionRequest) returns (google.protobuf.Empty) {
+  option (google.api.http) = {
+    delete: "/v1/{name=publishers/*/books/*}:deleteRevision"
+    body: "*" // This should be removed.
+  };
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+rpc DeleteBookRevision(DeleteBookRevisionRequest) returns (google.protobuf.Empty) {
+  option (google.api.http) = {
+    delete: "/v1/{name=publishers/*/books/*}:deleteRevision"
+  };
+}
+```
+
+## 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::delete-revision-http-body=disabled
+//     aip.dev/not-precedent: We need to do this because reasons. --)
+rpc DeleteBookRevision(DeleteBookRevisionRequest) returns (google.protobuf.Empty) {
+  option (google.api.http) = {
+    delete: "/v1/{name=publishers/*/books/*}:deleteRevision"
+    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/docs/rules/0162/delete-revision-http-method.md b/docs/rules/0162/delete-revision-http-method.md
new file mode 100644
index 0000000..fbe40a1
--- /dev/null
+++ b/docs/rules/0162/delete-revision-http-method.md
@@ -0,0 +1,65 @@
+---
+rule:
+  aip: 162
+  name: [core, '0162', delete-revision-http-method]
+  summary: Delete Revision methods must use the DELETE HTTP verb.
+permalink: /162/delete-revision-http-method
+redirect_from:
+  - /0162/delete-revision-http-method
+---
+
+# Delete Revision methods: POST HTTP verb
+
+This rule enforces that all Delete Revision RPCs use the `DELETE` HTTP verb, as
+mandated in [AIP-162][].
+
+## Details
+
+This rule looks at any method matching `Delete*Revision`, and complains
+if the HTTP verb is anything other than `DELETE`. It _does_ check additional
+bindings if they are present.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+rpc DeleteBookRevision(DeleteBookRevisionRequest) returns (google.protobuf.Empty) {
+  option (google.api.http) = {
+    post: "/v1/{name=publishers/*/books/*}:deleteRevision"  // Should be `delete:`.
+  };
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+rpc DeleteBookRevision(DeleteBookRevisionRequest) returns (google.protobuf.Empty) {
+  option (google.api.http) = {
+    delete: "/v1/{name=publishers/*/books/*}:deleteRevision"
+  };
+}
+```
+
+## 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::delete-revision-http-method=disabled
+//     aip.dev/not-precedent: We need to do this because reasons. --)
+rpc DeleteBookRevision(DeleteBookRevisionRequest) returns (google.protobuf.Empty) {
+  option (google.api.http) = {
+    post: "/v1/{name=publishers/*/books/*}:deleteRevision"
+  };
+}
+```
+
+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 3541a0d..c8d7718 100644
--- a/rules/aip0162/aip0162.go
+++ b/rules/aip0162/aip0162.go
@@ -35,6 +35,8 @@ func AddRules(r lint.RuleRegistry) error {
 		commitRequestNameField,
 		commitRequestNameReference,
 		commitResponseMessageName,
+		deleteRevisionHTTPBody,
+		deleteRevisionHTTPMethod,
 		rollbackHTTPBody,
 		rollbackHTTPMethod,
 		rollbackHTTPURISuffix,
@@ -97,3 +99,10 @@ func isRollbackMethod(m *desc.MethodDescriptor) bool {
 func isRollbackRequestMessage(m *desc.MessageDescriptor) bool {
 	return rollbackReqMessageRegexp.MatchString(m.GetName())
 }
+
+var deleteRevisionMethodRegexp = regexp.MustCompile(`^Delete(?:[A-Za-z0-9]+)Revision$`)
+
+// Returns true if this is an AIP-162 Delete Revision method, false otherwise.
+func isDeleteRevisionMethod(m *desc.MethodDescriptor) bool {
+	return deleteRevisionMethodRegexp.MatchString(m.GetName())
+}
diff --git a/rules/aip0162/delete_revision_http_body.go b/rules/aip0162/delete_revision_http_body.go
new file mode 100644
index 0000000..522d572
--- /dev/null
+++ b/rules/aip0162/delete_revision_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"
+)
+
+// Delete Revision methods should have no HTTP body.
+var deleteRevisionHTTPBody = &lint.MethodRule{
+	Name:       lint.NewRuleName(162, "delete-revision-http-body"),
+	OnlyIf:     isDeleteRevisionMethod,
+	LintMethod: utils.LintNoHTTPBody,
+}
diff --git a/rules/aip0162/delete_revision_http_method.go b/rules/aip0162/delete_revision_http_method.go
new file mode 100644
index 0000000..8c9f5d8
--- /dev/null
+++ b/rules/aip0162/delete_revision_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"
+)
+
+// Delete Revision methods should use the HTTP DELETE method.
+var deleteRevisionHTTPMethod = &lint.MethodRule{
+	Name:       lint.NewRuleName(162, "delete-revision-http-method"),
+	OnlyIf:     isDeleteRevisionMethod,
+	LintMethod: utils.LintHTTPMethod("DELETE"),
+}


Test patch:
diff --git a/rules/aip0162/delete_revision_http_body_test.go b/rules/aip0162/delete_revision_http_body_test.go
new file mode 100644
index 0000000..ff5e9dd
--- /dev/null
+++ b/rules/aip0162/delete_revision_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 TestDeleteRevisionHTTPBody(t *testing.T) {
+	tests := []struct {
+		testName   string
+		Body       string
+		MethodName string
+		problems   testutils.Problems
+	}{
+		{"Valid", "", "DeleteBookRevision", nil},
+		{"Invalid", "*", "DeleteBookRevision", 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";
+				import "google/protobuf/empty.proto";
+				service Library {
+					rpc {{.MethodName}}({{.MethodName}}Request) returns (google.protobuf.Empty) {
+						option (google.api.http) = {
+							delete: "/v1/{name=publishers/*/books/*}:deleteRevision"
+							body: "{{.Body}}"
+						};
+					}
+				}
+				message {{.MethodName}}Request {}
+			`, test)
+			method := file.GetServices()[0].GetMethods()[0]
+			problems := deleteRevisionHTTPBody.Lint(file)
+			if diff := test.problems.SetDescriptor(method).Diff(problems); diff != "" {
+				t.Error(diff)
+			}
+		})
+	}
+}
diff --git a/rules/aip0162/delete_revision_http_method_test.go b/rules/aip0162/delete_revision_http_method_test.go
new file mode 100644
index 0000000..6848feb
--- /dev/null
+++ b/rules/aip0162/delete_revision_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 TestDeleteRevisionHTTPMethod(t *testing.T) {
+	// Set up testing permutations.
+	tests := []struct {
+		testName   string
+		Method     string
+		MethodName string
+		problems   testutils.Problems
+	}{
+		{"Valid", "delete", "DeleteBookRevision", nil},
+		{"Invalid", "post", "DeleteBookRevision", testutils.Problems{{Message: "HTTP DELETE"}}},
+		{"Irrelevant", "post", "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";
+				import "google/protobuf/empty.proto";
+				service Library {
+					rpc {{.MethodName}}({{.MethodName}}Request) returns (google.protobuf.Empty) {
+						option (google.api.http) = {
+							{{.Method}}: "/v1/{name=publishers/*/books/*}:deleteRevision"
+						};
+					}
+				}
+				message {{.MethodName}}Request {}
+			`, test)
+			method := file.GetServices()[0].GetMethods()[0]
+			problems := deleteRevisionHTTPMethod.Lint(file)
+			if diff := test.problems.SetDescriptor(method).Diff(problems); diff != "" {
+				t.Error(diff)
+			}
+		})
+	}
+}