Repository: googleapis/api-linter
Pull request #736 (2021-01-28 22:38:20)

feat: Add rules for AIP-162 Commit request name behavior and resource reference
Part of #721.

apasel422: Is there anything left before this can be merged?

Patch:
diff --git a/docs/rules/0162/commit-request-name-behavior.md b/docs/rules/0162/commit-request-name-behavior.md
new file mode 100644
index 0000000..b440150
--- /dev/null
+++ b/docs/rules/0162/commit-request-name-behavior.md
@@ -0,0 +1,69 @@
+---
+rule:
+  aip: 162
+  name: [core, '0162', commit-request-name-behavior]
+  summary: |
+    Commit requests should annotate the `name` field with `google.api.field_behavior`.
+permalink: /162/commit-request-name-behavior
+redirect_from:
+  - /0162/commit-request-name-behavior
+---
+
+# Commit requests: Name field behavior
+
+This rule enforces that all `Commit` requests have
+`google.api.field_behavior` set to `REQUIRED` on their `string name` field, as
+mandated in [AIP-162][].
+
+## Details
+
+This rule looks at any message matching `Commit*Request` and complains if the
+`name` field does not have a `google.api.field_behavior` annotation with a
+value of `REQUIRED`.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+message CommitBookRequest {
+  // The `google.api.field_behavior` annotation should also be included.
+  string name = 1 [
+    (google.api.resource_reference).type = "library.googleapis.com/Book"
+  ];
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+message CommitBookRequest {
+  string name = 1 [
+    (google.api.field_behavior) = REQUIRED,
+    (google.api.resource_reference).type = "library.googleapis.com/Book"
+  ];
+}
+```
+
+## Disabling
+
+If you need to violate this rule, use a leading comment above the field.
+Remember to also include an [aip.dev/not-precedent][] comment explaining why.
+
+```proto
+message CommitBookRequest {
+  // (-- api-linter: core::0162::commit-request-name-behavior=disabled
+  //     aip.dev/not-precedent: We need to do this because reasons. --)
+  string name = 1 [
+    (google.api.resource_reference).type = "library.googleapis.com/Book"
+  ];
+}
+```
+
+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-request-name-reference.md b/docs/rules/0162/commit-request-name-reference.md
new file mode 100644
index 0000000..4cd7b34
--- /dev/null
+++ b/docs/rules/0162/commit-request-name-reference.md
@@ -0,0 +1,64 @@
+---
+rule:
+  aip: 162
+  name: [core, '0162', commit-request-name-reference]
+  summary: |
+    Commit requests should annotate the `name` field with `google.api.resource_reference`.
+permalink: /162/commit-request-name-reference
+redirect_from:
+  - /0162/commit-request-name-reference
+---
+
+# Commit requests: Name resource reference
+
+This rule enforces that all `Commit` requests have
+`google.api.resource_reference` on their `string name` field, as mandated in
+[AIP-162][].
+
+## Details
+
+This rule looks at the `name` field of any message matching `Commit*Request`
+and complains if it does not have a `google.api.resource_reference` annotation.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+message CommitBookRequest {
+  // The `google.api.resource_reference` annotation should also be included.
+  string name = 1 [(google.api.field_behavior) = REQUIRED];
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+message CommitBookRequest {
+  string name = 1 [
+    (google.api.field_behavior) = REQUIRED,
+    (google.api.resource_reference).type = "library.googleapis.com/Book"
+  ];
+}
+```
+
+## Disabling
+
+If you need to violate this rule, use a leading comment above the field.
+Remember to also include an [aip.dev/not-precedent][] comment explaining why.
+
+```proto
+message CommitBookRequest {
+  // (-- api-linter: core::0162::commit-request-name-reference=disabled
+  //     aip.dev/not-precedent: We need to do this because reasons. --)
+  string name = 1 [(google.api.field_behavior) = REQUIRED];
+}
+```
+
+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 88bcfab..0f17c7a 100644
--- a/rules/aip0162/aip0162.go
+++ b/rules/aip0162/aip0162.go
@@ -30,8 +30,10 @@ func AddRules(r lint.RuleRegistry) error {
 		commitHTTPBody,
 		commitHTTPMethod,
 		commitHTTPURISuffix,
-		commitRequestNameField,
 		commitRequestMessageName,
+		commitRequestNameBehavior,
+		commitRequestNameField,
+		commitRequestNameReference,
 		commitResponseMessageName,
 		rollbackHTTPURISuffix,
 		tagRevisionHTTPBody,
diff --git a/rules/aip0162/commit_request_name_behavior.go b/rules/aip0162/commit_request_name_behavior.go
new file mode 100644
index 0000000..6a73199
--- /dev/null
+++ b/rules/aip0162/commit_request_name_behavior.go
@@ -0,0 +1,29 @@
+// 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"
+	"github.com/jhump/protoreflect/desc"
+)
+
+var commitRequestNameBehavior = &lint.FieldRule{
+	Name: lint.NewRuleName(162, "commit-request-name-behavior"),
+	OnlyIf: func(f *desc.FieldDescriptor) bool {
+		return isCommitRequestMessage(f.GetOwner()) && f.GetName() == "name"
+	},
+	LintField: utils.LintRequiredField,
+}
diff --git a/rules/aip0162/commit_request_name_reference.go b/rules/aip0162/commit_request_name_reference.go
new file mode 100644
index 0000000..23d887a
--- /dev/null
+++ b/rules/aip0162/commit_request_name_reference.go
@@ -0,0 +1,29 @@
+// 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"
+	"github.com/jhump/protoreflect/desc"
+)
+
+var commitRequestNameReference = &lint.FieldRule{
+	Name: lint.NewRuleName(162, "commit-request-name-reference"),
+	OnlyIf: func(f *desc.FieldDescriptor) bool {
+		return isCommitRequestMessage(f.GetOwner()) && f.GetName() == "name"
+	},
+	LintField: utils.LintFieldResourceReference,
+}


Test patch:
diff --git a/rules/aip0162/commit_request_name_behavior_test.go b/rules/aip0162/commit_request_name_behavior_test.go
new file mode 100644
index 0000000..e7643e5
--- /dev/null
+++ b/rules/aip0162/commit_request_name_behavior_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 TestCommitRequestNameBehavior(t *testing.T) {
+	for _, test := range []struct {
+		name      string
+		RPC       string
+		Field     string
+		FieldOpts string
+		problems  testutils.Problems
+	}{
+		{"Valid", "CommitBook", "name", " [(google.api.field_behavior) = REQUIRED]", nil},
+		{"Missing", "CommitBook", "name", "", testutils.Problems{{Message: "(google.api.field_behavior) = REQUIRED"}}},
+		{"IrrelevantMessage", "PurgeBooks", "name", "", nil},
+		{"IrrelevantField", "CommitBook", "something_else", "", nil},
+	} {
+		t.Run(test.name, func(t *testing.T) {
+			f := testutils.ParseProto3Tmpl(t, `
+				import "google/api/field_behavior.proto";
+				message {{.RPC}}Request {
+					string {{.Field}} = 1{{.FieldOpts}};
+				}
+			`, test)
+			field := f.GetMessageTypes()[0].GetFields()[0]
+			if diff := test.problems.SetDescriptor(field).Diff(commitRequestNameBehavior.Lint(f)); diff != "" {
+				t.Error(diff)
+			}
+		})
+	}
+}
diff --git a/rules/aip0162/commit_request_name_reference_test.go b/rules/aip0162/commit_request_name_reference_test.go
new file mode 100644
index 0000000..61ef059
--- /dev/null
+++ b/rules/aip0162/commit_request_name_reference_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 TestCommitRequestNameReference(t *testing.T) {
+	for _, test := range []struct {
+		name      string
+		RPC       string
+		Field     string
+		FieldOpts string
+		problems  testutils.Problems
+	}{
+		{"Valid", "CommitBook", "name", ` [(google.api.resource_reference) = { type: "foo" }]`, nil},
+		{"Missing", "CommitBook", "name", "", testutils.Problems{{Message: "google.api.resource_reference"}}},
+		{"IrrelevantMessage", "PurgeBooks", "name", "", nil},
+		{"IrrelevantField", "CommitBook", "something_else", "", nil},
+	} {
+		t.Run(test.name, func(t *testing.T) {
+			f := testutils.ParseProto3Tmpl(t, `
+				import "google/api/resource.proto";
+				message {{.RPC}}Request {
+					repeated string {{.Field}} = 1{{.FieldOpts}};
+				}
+			`, test)
+			field := f.GetMessageTypes()[0].GetFields()[0]
+			if diff := test.problems.SetDescriptor(field).Diff(commitRequestNameReference.Lint(f)); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}