Repository: googleapis/api-linter
Pull request #730 (2021-01-20 21:09:53)

feat: Add rule for AIP-162 Commit request message name
Part of #721.


Patch:
diff --git a/docs/rules/0162/commit-request-message-name.md b/docs/rules/0162/commit-request-message-name.md
new file mode 100644
index 0000000..00d8a18
--- /dev/null
+++ b/docs/rules/0162/commit-request-message-name.md
@@ -0,0 +1,69 @@
+---
+rule:
+  aip: 162
+  name: [core, '0162', commit-request-message-name]
+  summary: Commit methods must have standardized request message names.
+permalink: /162/commit-request-message-name
+redirect_from:
+  - /0162/commit-request-message-name
+---
+
+# Commit methods: Request message
+
+This rule enforces that all `Commit` RPCs have a request message name of
+`Commit*Request`, as mandated in [AIP-162][].
+
+## Details
+
+This rule looks at any method beginning with `Commit`, 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 `CommitBookRequest`.
+rpc CommitBook(SaveBookRequest) returns (Book) {
+  option (google.api.http) = {
+    post: "/v1/{name=publishers/*/books/*}:commit"
+    body: "*"
+  };
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+rpc CommitBook(CommitBookRequest) returns (Book) {
+  option (google.api.http) = {
+    post: "/v1/{name=publishers/*/books/*}:commit"
+    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::commit-request-message-name=disabled
+//     aip.dev/not-precedent: We need to do this because reasons. --)
+rpc CommitBook(SaveBookRequest) returns (Book) {
+  option (google.api.http) = {
+    post: "/v1/{name=publishers/*/books/*}:commit"
+    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 29fb8e1..9ad9d0b 100644
--- a/rules/aip0162/aip0162.go
+++ b/rules/aip0162/aip0162.go
@@ -30,6 +30,7 @@ func AddRules(r lint.RuleRegistry) error {
 		commitHTTPBody,
 		commitHTTPMethod,
 		commitHTTPURISuffix,
+		commitRequestMessageName,
 		tagRevisionHTTPBody,
 		tagRevisionHTTPMethod,
 		tagRevisionHTTPURISuffix,
diff --git a/rules/aip0162/commit_request_message_name.go b/rules/aip0162/commit_request_message_name.go
new file mode 100644
index 0000000..b56c89b
--- /dev/null
+++ b/rules/aip0162/commit_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"
+)
+
+// Commit messages should have a properly named request message.
+var commitRequestMessageName = &lint.MethodRule{
+	Name:       lint.NewRuleName(162, "commit-request-message-name"),
+	OnlyIf:     isCommitMethod,
+	LintMethod: utils.LintMethodHasMatchingRequestName,
+}


Test patch:
diff --git a/rules/aip0162/commit_request_message_name_test.go b/rules/aip0162/commit_request_message_name_test.go
new file mode 100644
index 0000000..bd9e1a4
--- /dev/null
+++ b/rules/aip0162/commit_request_message_name_test.go
@@ -0,0 +1,50 @@
+// 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 TestCommitRequestMessageName(t *testing.T) {
+	tests := []struct {
+		testName       string
+		MethodName     string
+		ReqMessageName string
+		problems       testutils.Problems
+	}{
+		{"Valid", "CommitBook", "CommitBookRequest", nil},
+		{"Invalid", "CommitBook", "SaveBookRequest", testutils.Problems{{Suggestion: "CommitBookRequest"}}},
+		{"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(commitRequestMessageName.Lint(f)); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}