Repository: googleapis/api-linter
Pull request #747 (2021-02-04 16:58:44)

feat: Add rules for AIP-162 Rollback request and response message name
Part of #721.


Patch:
diff --git a/docs/rules/0162/rollback-request-message-name.md b/docs/rules/0162/rollback-request-message-name.md
new file mode 100644
index 0000000..f2987cd
--- /dev/null
+++ b/docs/rules/0162/rollback-request-message-name.md
@@ -0,0 +1,69 @@
+---
+rule:
+  aip: 162
+  name: [core, '0162', rollback-request-message-name]
+  summary: Rollback methods must have standardized request message names.
+permalink: /162/rollback-request-message-name
+redirect_from:
+  - /0162/rollback-request-message-name
+---
+
+# Rollback methods: Request message
+
+This rule enforces that all `Rollback` RPCs have a request message name of
+`Rollback*Request`, as mandated in [AIP-162][].
+
+## Details
+
+This rule looks at any method beginning with `Rollback`, and complains
+if the name of the corresponding input message does not match the name of the
+RPC with the suffix `Request` appended.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+// Should be `RollbackBookRequest`.
+rpc RollbackBook(UndoBookRequest) returns (Book) {
+  option (google.api.http) = {
+    post: "/v1/{name=publishers/*/books/*}: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-request-message-name=disabled
+//     aip.dev/not-precedent: We need to do this because reasons. --)
+rpc RollbackBook(UndoBookRequest) returns (Book) {
+  option (google.api.http) = {
+    post: "/v1/{name=publishers/*/books/*}:rollback"
+    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/rollback-response-message-name.md b/docs/rules/0162/rollback-response-message-name.md
new file mode 100644
index 0000000..1d96398
--- /dev/null
+++ b/docs/rules/0162/rollback-response-message-name.md
@@ -0,0 +1,69 @@
+---
+rule:
+  aip: 162
+  name: [core, '0162', rollback-response-message-name]
+  summary: Rollback methods must return a long-running operation.
+permalink: /162/rollback-response-message-name
+redirect_from:
+  - /0162/rollback-response-message-name
+---
+
+# Rollback methods: Response message
+
+This rule enforces that all `Rollback` RPCs have a response message of the
+resource, as mandated in [AIP-162][].
+
+## Details
+
+This rule looks at any method beginning with `Rollback`, and complains
+if the name of the corresponding output message does not match the name of the
+RPC with the prefix `Rollback` removed.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+// Should return `Book`.
+rpc RollbackBook(RollbackBookRequest) returns (RollbackBookResponse) {
+  option (google.api.http) = {
+    post: "/v1/{name=publishers/*/books/*}: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-response-message-name=disabled
+//     aip.dev/not-precedent: We need to do this because reasons. --)
+rpc RollbackBook(RollbackBookRequest) returns (RollbackBookResponse) {
+  option (google.api.http) = {
+    post: "/v1/{name=publishers/*/books/*}:rollback"
+    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 d4a61f7..d0acfbe 100644
--- a/rules/aip0162/aip0162.go
+++ b/rules/aip0162/aip0162.go
@@ -38,6 +38,8 @@ func AddRules(r lint.RuleRegistry) error {
 		rollbackHTTPBody,
 		rollbackHTTPMethod,
 		rollbackHTTPURISuffix,
+		rollbackRequestMessageName,
+		rollbackResponseMessageName,
 		tagRevisionHTTPBody,
 		tagRevisionHTTPMethod,
 		tagRevisionHTTPURISuffix,
@@ -79,7 +81,7 @@ func isCommitRequestMessage(m *desc.MessageDescriptor) bool {
 	return commitReqMessageRegexp.MatchString(m.GetName())
 }
 
-var rollbackMethodRegexp = regexp.MustCompile(`^Rollback(?:[A-Za-z0-9]+)$`)
+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.
diff --git a/rules/aip0162/rollback_request_message_name.go b/rules/aip0162/rollback_request_message_name.go
new file mode 100644
index 0000000..2de5b45
--- /dev/null
+++ b/rules/aip0162/rollback_request_message_name.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 messages should have a properly named request message.
+var rollbackRequestMessageName = &lint.MethodRule{
+	Name:       lint.NewRuleName(162, "rollback-request-message-name"),
+	OnlyIf:     isRollbackMethod,
+	LintMethod: utils.LintMethodHasMatchingRequestName,
+}
diff --git a/rules/aip0162/rollback_response_message_name.go b/rules/aip0162/rollback_response_message_name.go
new file mode 100644
index 0000000..5fe5eff
--- /dev/null
+++ b/rules/aip0162/rollback_response_message_name.go
@@ -0,0 +1,49 @@
+// 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 (
+	"fmt"
+
+	"github.com/googleapis/api-linter/lint"
+	"github.com/googleapis/api-linter/locations"
+	"github.com/jhump/protoreflect/desc"
+)
+
+var rollbackResponseMessageName = &lint.MethodRule{
+	Name:   lint.NewRuleName(162, "rollback-response-message-name"),
+	OnlyIf: isRollbackMethod,
+	LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
+		// Rule check: Establish that for methods such as `RollbackBook`, the response
+		// message is `Book`.
+		want := rollbackMethodRegexp.FindStringSubmatch(m.GetName())[1]
+		got := m.GetOutputType().GetName()
+
+		// Return a problem if we did not get the expected return name.
+		if got != want {
+			return []lint.Problem{{
+				Message: fmt.Sprintf(
+					"Rollback RPCs should have response message type %q, not %q.",
+					want,
+					got,
+				),
+				Suggestion: want,
+				Descriptor: m,
+				Location:   locations.MethodResponseType(m),
+			}}
+		}
+		return nil
+	},
+}


Test patch:
diff --git a/rules/aip0162/rollback_request_message_name_test.go b/rules/aip0162/rollback_request_message_name_test.go
new file mode 100644
index 0000000..ac85986
--- /dev/null
+++ b/rules/aip0162/rollback_request_message_name_test.go
@@ -0,0 +1,51 @@
+// 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 TestRollbackRequestMessageName(t *testing.T) {
+	// Set up the testing permutations.
+	tests := []struct {
+		testName       string
+		MethodName     string
+		ReqMessageName string
+		problems       testutils.Problems
+	}{
+		{"Valid", "RollbackBook", "RollbackBookRequest", nil},
+		{"Invalid", "RollbackBook", "UndoBookRequest", testutils.Problems{{Suggestion: "RollbackBookRequest"}}},
+		{"Irrelevant", "AcquireBook", "GetBookRequest", nil},
+	}
+
+	for _, test := range tests {
+		t.Run(test.testName, func(t *testing.T) {
+			f := testutils.ParseProto3Tmpl(t, `
+				service Library {
+					rpc {{.MethodName}}({{.ReqMessageName}}) returns (Book) {}
+				}
+				message Book {}
+				message {{.ReqMessageName}} {}
+			`, test)
+			m := f.GetServices()[0].GetMethods()[0]
+			if diff := test.problems.SetDescriptor(m).Diff(rollbackRequestMessageName.Lint(f)); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}
diff --git a/rules/aip0162/rollback_response_message_name_test.go b/rules/aip0162/rollback_response_message_name_test.go
new file mode 100644
index 0000000..56d92e0
--- /dev/null
+++ b/rules/aip0162/rollback_response_message_name_test.go
@@ -0,0 +1,49 @@
+// 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 TestRollbackResponseMessageName(t *testing.T) {
+	for _, test := range []struct {
+		name         string
+		Method       string
+		ResponseType string
+		problems     testutils.Problems
+	}{
+		{"Valid", "RollbackBook", "Book", nil},
+		{"Invalid", "RollbackBook", "RollbackBookResponse", testutils.Problems{{Suggestion: "Book"}}},
+		{"Irrelevant", "AcquireBook", "PurgeBooksResponse", nil},
+	} {
+		t.Run(test.name, func(t *testing.T) {
+			f := testutils.ParseProto3Tmpl(t, `
+				import "google/longrunning/operations.proto";
+				service Library {
+					rpc {{.Method}}(RollbackBookRequest) returns ({{.ResponseType}});
+				}
+				message RollbackBookRequest {}
+				message {{.ResponseType}} {}
+			`, test)
+			m := f.GetServices()[0].GetMethods()[0]
+			if diff := test.problems.SetDescriptor(m).Diff(rollbackResponseMessageName.Lint(f)); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}