Repository: googleapis/api-linter
Pull request #729 (2021-01-20 16:38:57)

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


Patch:
diff --git a/docs/rules/0162/commit-http-body.md b/docs/rules/0162/commit-http-body.md
new file mode 100644
index 0000000..db53b7d
--- /dev/null
+++ b/docs/rules/0162/commit-http-body.md
@@ -0,0 +1,66 @@
+---
+rule:
+  aip: 162
+  name: [core, '0162', commit-http-body]
+  summary: Commit methods should use `*` as the HTTP body.
+permalink: /162/commit-http-body
+redirect_from:
+  - /0162/commit-http-body
+---
+
+# Commit methods: HTTP body
+
+This rule enforces that all `Commit` RPCs use `*` as the HTTP `body`, as mandated in
+[AIP-162][].
+
+## Details
+
+This rule looks at any method beginning with `Commit`, and complains
+if the HTTP `body` field is anything other than `*`.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+rpc CommitBook(CommitBookRequest) returns (Book) {
+  option (google.api.http) = {
+    post: "/v1/{name=publishers/*/books/*}:commit"
+    // body: "*" should be set.
+  };
+}
+```
+
+**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-http-body=disabled
+//     aip.dev/not-precedent: We need to do this because reasons. --)
+rpc CommitBook(CommitBookRequest) returns (Book) {
+  option (google.api.http) = {
+    post: "/v1/{name=publishers/*/books/*}:commit"
+  };
+}
+```
+
+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/commit-http-method.md b/docs/rules/0162/commit-http-method.md
new file mode 100644
index 0000000..78632ac
--- /dev/null
+++ b/docs/rules/0162/commit-http-method.md
@@ -0,0 +1,66 @@
+---
+rule:
+  aip: 162
+  name: [core, '0162', commit-http-method]
+  summary: Commit methods must use the POST HTTP verb.
+permalink: /162/commit-http-method
+redirect_from:
+  - /0162/commit-http-method
+---
+
+# Commit methods: POST HTTP verb
+
+This rule enforces that all `Commit` RPCs use the `POST` HTTP verb, as
+mandated in [AIP-162][].
+
+## Details
+
+This rule looks at any method beginning with `Commit`, 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 CommitBook(CommitBookRequest) returns (Book) {
+  option (google.api.http) = {
+    get: "/v1/{name=publishers/*/books/*}:commit"  // Should be `post:`.
+  };
+}
+```
+
+**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-http-method=disabled
+//     aip.dev/not-precedent: We need to do this because reasons. --)
+rpc CommitBook(CommitBookRequest) returns (Book) {
+  option (google.api.http) = {
+    get: "/v1/{name=publishers/*/books/*}:commit"
+  };
+}
+```
+
+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 45c6fe3..29fb8e1 100644
--- a/rules/aip0162/aip0162.go
+++ b/rules/aip0162/aip0162.go
@@ -27,6 +27,8 @@ import (
 func AddRules(r lint.RuleRegistry) error {
 	return r.Register(
 		162,
+		commitHTTPBody,
+		commitHTTPMethod,
 		commitHTTPURISuffix,
 		tagRevisionHTTPBody,
 		tagRevisionHTTPMethod,
diff --git a/rules/aip0162/commit_http_body.go b/rules/aip0162/commit_http_body.go
new file mode 100644
index 0000000..0d16827
--- /dev/null
+++ b/rules/aip0162/commit_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"
+)
+
+// Commit methods should have "*" as the HTTP body.
+var commitHTTPBody = &lint.MethodRule{
+	Name:       lint.NewRuleName(162, "commit-http-body"),
+	OnlyIf:     isCommitMethod,
+	LintMethod: utils.LintWildcardHTTPBody,
+}
diff --git a/rules/aip0162/commit_http_method.go b/rules/aip0162/commit_http_method.go
new file mode 100644
index 0000000..0abb7b5
--- /dev/null
+++ b/rules/aip0162/commit_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"
+)
+
+// Commit methods should use the HTTP POST method.
+var commitHTTPMethod = &lint.MethodRule{
+	Name:       lint.NewRuleName(162, "commit-http-method"),
+	OnlyIf:     isCommitMethod,
+	LintMethod: utils.LintHTTPMethod("POST"),
+}


Test patch:
diff --git a/rules/aip0162/commit_http_body_test.go b/rules/aip0162/commit_http_body_test.go
new file mode 100644
index 0000000..eeb4837
--- /dev/null
+++ b/rules/aip0162/commit_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 TestCommitHTTPBody(t *testing.T) {
+	tests := []struct {
+		testName   string
+		Body       string
+		MethodName string
+		problems   testutils.Problems
+	}{
+		{"Valid", "*", "CommitBook", nil},
+		{"Invalid", "", "CommitBook", 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/*}:commit"
+							body: "{{.Body}}"
+						};
+					}
+				}
+				message Book {}
+				message {{.MethodName}}Request {}
+			`, test)
+			method := file.GetServices()[0].GetMethods()[0]
+			problems := commitHTTPBody.Lint(file)
+			if diff := test.problems.SetDescriptor(method).Diff(problems); diff != "" {
+				t.Error(diff)
+			}
+		})
+	}
+}
diff --git a/rules/aip0162/commit_http_method_test.go b/rules/aip0162/commit_http_method_test.go
new file mode 100644
index 0000000..e702b69
--- /dev/null
+++ b/rules/aip0162/commit_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 TestCommitHTTPMethod(t *testing.T) {
+	// Set up testing permutations.
+	tests := []struct {
+		testName   string
+		Method     string
+		MethodName string
+		problems   testutils.Problems
+	}{
+		{"Valid", "post", "CommitBook", nil},
+		{"Invalid", "delete", "CommitBook", 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/*}:commit"
+						};
+					}
+				}
+				message Book {}
+				message {{.MethodName}}Request {}
+			`, test)
+			method := file.GetServices()[0].GetMethods()[0]
+			problems := commitHTTPMethod.Lint(file)
+			if diff := test.problems.SetDescriptor(method).Diff(problems); diff != "" {
+				t.Error(diff)
+			}
+		})
+	}
+}