Repository: googleapis/api-linter
Pull request #733 (2021-01-21 14:02:02)

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


Patch:
diff --git a/docs/rules/0162/commit-response-message-name.md b/docs/rules/0162/commit-response-message-name.md
new file mode 100644
index 0000000..61d6b2e
--- /dev/null
+++ b/docs/rules/0162/commit-response-message-name.md
@@ -0,0 +1,69 @@
+---
+rule:
+  aip: 162
+  name: [core, '0162', commit-response-message-name]
+  summary: Commit methods must return a long-running operation.
+permalink: /162/commit-response-message-name
+redirect_from:
+  - /0162/commit-response-message-name
+---
+
+# Commit methods: Response message
+
+This rule enforces that all `Commit` RPCs have a response message of the
+resource, as mandated in [AIP-162][].
+
+## Details
+
+This rule looks at any method beginning with `Commit`, and complains
+if the name of the corresponding output message does not match the name of the
+RPC with the prefix `Commit` removed.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+// Should return `Book`.
+rpc CommitBook(CommitBookRequest) returns (CommitBookResponse) {
+  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-response-message-name=disabled
+//     aip.dev/not-precedent: We need to do this because reasons. --)
+rpc CommitBook(CommitBookRequest) returns (CommitBookResponse) {
+  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 ac49a14..c6a3442 100644
--- a/rules/aip0162/aip0162.go
+++ b/rules/aip0162/aip0162.go
@@ -32,6 +32,7 @@ func AddRules(r lint.RuleRegistry) error {
 		commitHTTPURISuffix,
 		commitRequestNameField,
 		commitRequestMessageName,
+		commitResponseMessageName,
 		tagRevisionHTTPBody,
 		tagRevisionHTTPMethod,
 		tagRevisionHTTPURISuffix,
@@ -59,7 +60,7 @@ func isTagRevisionRequestMessage(m *desc.MessageDescriptor) bool {
 	return tagRevisionReqMessageRegexp.MatchString(m.GetName())
 }
 
-var commitMethodRegexp = regexp.MustCompile(`^Commit(?:[A-Za-z0-9]+)$`)
+var commitMethodRegexp = regexp.MustCompile(`^Commit([A-Za-z0-9]+)$`)
 var commitReqMessageRegexp = regexp.MustCompile(`^Commit(?:[A-Za-z0-9]+)Request$`)
 var commitURINameRegexp = regexp.MustCompile(`:commit$`)
 
diff --git a/rules/aip0162/commit_response_message_name.go b/rules/aip0162/commit_response_message_name.go
new file mode 100644
index 0000000..9987692
--- /dev/null
+++ b/rules/aip0162/commit_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 commitResponseMessageName = &lint.MethodRule{
+	Name:   lint.NewRuleName(162, "commit-response-message-name"),
+	OnlyIf: isCommitMethod,
+	LintMethod: func(m *desc.MethodDescriptor) []lint.Problem {
+		// Rule check: Establish that for methods such as `CommitBook`, the response
+		// message is `Book`.
+		want := commitMethodRegexp.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(
+					"Commit RPCs should have response message type %q, not %q.",
+					want,
+					got,
+				),
+				Suggestion: want,
+				Descriptor: m,
+				Location:   locations.MethodResponseType(m),
+			}}
+		}
+		return nil
+	},
+}
diff --git a/rules/aip0162/tag_revision_response_message_name.go b/rules/aip0162/tag_revision_response_message_name.go
index b3a930e..9c01214 100644
--- a/rules/aip0162/tag_revision_response_message_name.go
+++ b/rules/aip0162/tag_revision_response_message_name.go
@@ -32,7 +32,6 @@ var tagRevisionResponseMessageName = &lint.MethodRule{
 		got := m.GetOutputType().GetName()
 
 		// Return a problem if we did not get the expected return name.
-		// would be confusing.
 		if got != want {
 			return []lint.Problem{{
 				Message: fmt.Sprintf(


Test patch:
diff --git a/rules/aip0162/commit_response_message_name_test.go b/rules/aip0162/commit_response_message_name_test.go
new file mode 100644
index 0000000..2ec5044
--- /dev/null
+++ b/rules/aip0162/commit_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 TestCommitResponseMessageName(t *testing.T) {
+	for _, test := range []struct {
+		name         string
+		Method       string
+		ResponseType string
+		problems     testutils.Problems
+	}{
+		{"Valid", "CommitBook", "Book", nil},
+		{"Invalid", "CommitBook", "CommitBookResponse", 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}}(CommitBookRequest) returns ({{.ResponseType}});
+				}
+				message CommitBookRequest {}
+				message {{.ResponseType}} {}
+			`, test)
+			m := f.GetServices()[0].GetMethods()[0]
+			if diff := test.problems.SetDescriptor(m).Diff(commitResponseMessageName.Lint(f)); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}