Repository: googleapis/api-linter
Pull request #749 (2021-02-04 20:26:32)

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


Patch:
diff --git a/docs/rules/0162/rollback-request-name-behavior.md b/docs/rules/0162/rollback-request-name-behavior.md
new file mode 100644
index 0000000..b5dcf9c
--- /dev/null
+++ b/docs/rules/0162/rollback-request-name-behavior.md
@@ -0,0 +1,75 @@
+---
+rule:
+  aip: 162
+  name: [core, '0162', rollback-request-name-behavior]
+  summary: |
+    Rollback requests should annotate the `name` field with `google.api.field_behavior`.
+permalink: /162/rollback-request-name-behavior
+redirect_from:
+  - /0162/rollback-request-name-behavior
+---
+
+# Rollback requests: Name field behavior
+
+This rule enforces that all `Rollback` 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 `Rollback*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 RollbackBookRequest {
+  // The `google.api.field_behavior` annotation should also be included.
+  string name = 1 [
+    (google.api.resource_reference).type = "library.googleapis.com/Book"
+  ];
+
+  string revision_id = 2 [(google.api.field_behavior) = REQUIRED];
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+message RollbackBookRequest {
+  string name = 1 [
+    (google.api.field_behavior) = REQUIRED,
+    (google.api.resource_reference).type = "library.googleapis.com/Book"
+  ];
+
+  string revision_id = 2 [(google.api.field_behavior) = REQUIRED];
+}
+```
+
+## 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 RollbackBookRequest {
+  // (-- api-linter: core::0162::rollback-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"
+  ];
+
+  string revision_id = 2 [(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/docs/rules/0162/rollback-request-name-field.md b/docs/rules/0162/rollback-request-name-field.md
new file mode 100644
index 0000000..c68a9b3
--- /dev/null
+++ b/docs/rules/0162/rollback-request-name-field.md
@@ -0,0 +1,83 @@
+---
+rule:
+  aip: 162
+  name: [core, '0162', rollback-request-name-field]
+  summary: Rollback RPCs must have a `name` field in the request.
+permalink: /162/rollback-request-name-field
+redirect_from:
+  - /0162/rollback-request-name-field
+---
+
+# Rollback requests: Name field
+
+This rule enforces that all `Rollback` methods have a `string name`
+field in the request message, as mandated in [AIP-162][].
+
+## Details
+
+This rule looks at any message matching `Rollback*Request` and complains if
+either the `name` field is missing or it has any type other than `string`.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+// Should include a `string name` field.
+message RollbackBookRequest {
+  string revision_id = 2 [(google.api.field_behavior) = REQUIRED];
+}
+```
+
+```proto
+// Incorrect.
+message RollbackBookRequest {
+  // Field type should be `string`.
+  bytes name = 1 [
+    (google.api.field_behavior) = REQUIRED,
+    (google.api.resource_reference).type = "library.googleapis.com/Book"
+  ];
+
+  string revision_id = 2 [(google.api.field_behavior) = REQUIRED];
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+message RollbackBookRequest {
+  string name = 1 [
+    (google.api.field_behavior) = REQUIRED,
+    (google.api.resource_reference).type = "library.googleapis.com/Book"
+  ];
+
+  string revision_id = 2 [(google.api.field_behavior) = REQUIRED];
+}
+```
+
+## Disabling
+
+If you need to violate this rule, use a leading comment above the message (if
+the `name` field is missing) or above the field (if it is the wrong type).
+Remember to also include an [aip.dev/not-precedent][] comment explaining why.
+
+```proto
+message RollbackBookRequest {
+  // (-- api-linter: core::0162::rollback-request-name-field=disabled
+  //     aip.dev/not-precedent: We need to do this because reasons. --)
+  bytes name = 1 [
+    (google.api.field_behavior) = REQUIRED,
+    (google.api.resource_reference).type = "library.googleapis.com/Book"
+  ];
+
+  string revision_id = 2 [(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/docs/rules/0162/rollback-request-name-reference.md b/docs/rules/0162/rollback-request-name-reference.md
new file mode 100644
index 0000000..8224b86
--- /dev/null
+++ b/docs/rules/0162/rollback-request-name-reference.md
@@ -0,0 +1,70 @@
+---
+rule:
+  aip: 162
+  name: [core, '0162', rollback-request-name-reference]
+  summary: |
+    Rollback requests should annotate the `name` field with `google.api.resource_reference`.
+permalink: /162/rollback-request-name-reference
+redirect_from:
+  - /0162/rollback-request-name-reference
+---
+
+# Rollback requests: Name resource reference
+
+This rule enforces that all `Rollback` 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 `Rollback*Request`
+and complains if it does not have a `google.api.resource_reference` annotation.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+message RollbackBookRequest {
+  // The `google.api.resource_reference` annotation should also be included.
+  string name = 1 [(google.api.field_behavior) = REQUIRED];
+
+  string revision_id = 2 [(google.api.field_behavior) = REQUIRED];
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+message RollbackBookRequest {
+  string name = 1 [
+    (google.api.field_behavior) = REQUIRED,
+    (google.api.resource_reference).type = "library.googleapis.com/Book"
+  ];
+
+  string revision_id = 2 [(google.api.field_behavior) = REQUIRED];
+}
+```
+
+## 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 RollbackBookRequest {
+  // (-- api-linter: core::0162::rollback-request-name-reference=disabled
+  //     aip.dev/not-precedent: We need to do this because reasons. --)
+  string name = 1 [(google.api.field_behavior) = REQUIRED];
+
+  string revision_id = 2 [(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 d0acfbe..3541a0d 100644
--- a/rules/aip0162/aip0162.go
+++ b/rules/aip0162/aip0162.go
@@ -39,6 +39,9 @@ func AddRules(r lint.RuleRegistry) error {
 		rollbackHTTPMethod,
 		rollbackHTTPURISuffix,
 		rollbackRequestMessageName,
+		rollbackRequestNameBehavior,
+		rollbackRequestNameField,
+		rollbackRequestNameReference,
 		rollbackResponseMessageName,
 		tagRevisionHTTPBody,
 		tagRevisionHTTPMethod,
@@ -82,9 +85,15 @@ func isCommitRequestMessage(m *desc.MessageDescriptor) bool {
 }
 
 var rollbackMethodRegexp = regexp.MustCompile(`^Rollback([A-Za-z0-9]+)$`)
+var rollbackReqMessageRegexp = regexp.MustCompile(`^Rollback(?:[A-Za-z0-9]+)Request$`)
 var rollbackURINameRegexp = regexp.MustCompile(`:rollback$`)
 
 // Returns true if this is an AIP-162 Rollback method, false otherwise.
 func isRollbackMethod(m *desc.MethodDescriptor) bool {
 	return rollbackMethodRegexp.MatchString(m.GetName())
 }
+
+// Returns true if this is an AIP-162 Rollback request message, false otherwise.
+func isRollbackRequestMessage(m *desc.MessageDescriptor) bool {
+	return rollbackReqMessageRegexp.MatchString(m.GetName())
+}
diff --git a/rules/aip0162/rollback_request_name_behavior.go b/rules/aip0162/rollback_request_name_behavior.go
new file mode 100644
index 0000000..a3a722e
--- /dev/null
+++ b/rules/aip0162/rollback_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 rollbackRequestNameBehavior = &lint.FieldRule{
+	Name: lint.NewRuleName(162, "rollback-request-name-behavior"),
+	OnlyIf: func(f *desc.FieldDescriptor) bool {
+		return isRollbackRequestMessage(f.GetOwner()) && f.GetName() == "name"
+	},
+	LintField: utils.LintRequiredField,
+}
diff --git a/rules/aip0162/rollback_request_name_field.go b/rules/aip0162/rollback_request_name_field.go
new file mode 100644
index 0000000..23364f7
--- /dev/null
+++ b/rules/aip0162/rollback_request_name_field.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"
+)
+
+// The Rollback request message should have a name field.
+var rollbackRequestNameField = &lint.MessageRule{
+	Name:        lint.NewRuleName(162, "rollback-request-name-field"),
+	OnlyIf:      isRollbackRequestMessage,
+	LintMessage: utils.LintFieldPresentAndSingularString("name"),
+}
diff --git a/rules/aip0162/rollback_request_name_reference.go b/rules/aip0162/rollback_request_name_reference.go
new file mode 100644
index 0000000..b885356
--- /dev/null
+++ b/rules/aip0162/rollback_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 rollbackRequestNameReference = &lint.FieldRule{
+	Name: lint.NewRuleName(162, "rollback-request-name-reference"),
+	OnlyIf: func(f *desc.FieldDescriptor) bool {
+		return isRollbackRequestMessage(f.GetOwner()) && f.GetName() == "name"
+	},
+	LintField: utils.LintFieldResourceReference,
+}


Test patch:
diff --git a/rules/aip0162/rollback_request_name_behavior_test.go b/rules/aip0162/rollback_request_name_behavior_test.go
new file mode 100644
index 0000000..21e957f
--- /dev/null
+++ b/rules/aip0162/rollback_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 TestRollbackRequestNameBehavior(t *testing.T) {
+	for _, test := range []struct {
+		name      string
+		RPC       string
+		Field     string
+		FieldOpts string
+		problems  testutils.Problems
+	}{
+		{"Valid", "RollbackBook", "name", " [(google.api.field_behavior) = REQUIRED]", nil},
+		{"Missing", "RollbackBook", "name", "", testutils.Problems{{Message: "(google.api.field_behavior) = REQUIRED"}}},
+		{"IrrelevantMessage", "PurgeBooks", "name", "", nil},
+		{"IrrelevantField", "RollbackBook", "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(rollbackRequestNameBehavior.Lint(f)); diff != "" {
+				t.Error(diff)
+			}
+		})
+	}
+}
diff --git a/rules/aip0162/rollback_request_name_field_test.go b/rules/aip0162/rollback_request_name_field_test.go
new file mode 100644
index 0000000..6013aa4
--- /dev/null
+++ b/rules/aip0162/rollback_request_name_field_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"
+	"github.com/jhump/protoreflect/desc"
+)
+
+func TestRollbackRequestNameField(t *testing.T) {
+	for _, test := range []struct {
+		name     string
+		RPC      string
+		Field    string
+		problems testutils.Problems
+	}{
+		{"Valid", "RollbackBook", "string name = 1;", nil},
+		{"Missing", "RollbackBook", "", testutils.Problems{{Message: "no `name`"}}},
+		{"InvalidType", "RollbackBook", "int32 name = 1;", testutils.Problems{{Suggestion: "string"}}},
+		{"IrrelevantRPCName", "EnumerateBooks", "", nil},
+	} {
+		t.Run(test.name, func(t *testing.T) {
+			f := testutils.ParseProto3Tmpl(t, `
+				message {{.RPC}}Request {
+					{{.Field}}
+				}
+			`, test)
+			var d desc.Descriptor = f.GetMessageTypes()[0]
+			if test.name == "InvalidType" {
+				d = f.GetMessageTypes()[0].GetFields()[0]
+			}
+			if diff := test.problems.SetDescriptor(d).Diff(rollbackRequestNameField.Lint(f)); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}
diff --git a/rules/aip0162/rollback_request_name_reference_test.go b/rules/aip0162/rollback_request_name_reference_test.go
new file mode 100644
index 0000000..44f9c95
--- /dev/null
+++ b/rules/aip0162/rollback_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 TestRollbackRequestNameReference(t *testing.T) {
+	for _, test := range []struct {
+		name      string
+		RPC       string
+		Field     string
+		FieldOpts string
+		problems  testutils.Problems
+	}{
+		{"Valid", "RollbackBook", "name", ` [(google.api.resource_reference) = { type: "foo" }]`, nil},
+		{"Missing", "RollbackBook", "name", "", testutils.Problems{{Message: "google.api.resource_reference"}}},
+		{"IrrelevantMessage", "PurgeBooks", "name", "", nil},
+		{"IrrelevantField", "RollbackBook", "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(rollbackRequestNameReference.Lint(f)); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}