Repository: googleapis/api-linter
Pull request #750 (2021-02-05 00:45:30)

feat: Add rules for AIP-162 Rollback request revision_id field and behavior
Part of #721.


Patch:
diff --git a/docs/rules/0162/rollback-request-revision-id-behavior.md b/docs/rules/0162/rollback-request-revision-id-behavior.md
new file mode 100644
index 0000000..f7efa65
--- /dev/null
+++ b/docs/rules/0162/rollback-request-revision-id-behavior.md
@@ -0,0 +1,77 @@
+---
+rule:
+  aip: 162
+  name: [core, '0162', rollback-request-revision-id-behavior]
+  summary: |
+    Rollback requests should annotate the `revision_id` field with `google.api.field_behavior`.
+permalink: /162/rollback-request-revision-id-behavior
+redirect_from:
+  - /0162/rollback-request-revision-id-behavior
+---
+
+# Rollback requests: Revision ID field behavior
+
+This rule enforces that all `Rollback` requests have
+`google.api.field_behavior` set to `REQUIRED` on their `string revision_id` field, as
+mandated in [AIP-162][].
+
+## Details
+
+This rule looks at any message matching `Rollback*Request` and complains if the
+`revision_id` 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 {
+  string name = 1 [
+    (google.api.field_behavior) = REQUIRED,
+    (google.api.resource_reference).type = "library.googleapis.com/Book"
+  ];
+
+  // The `google.api.field_behavior` annotation should be included.
+  string revision_id = 2;
+}
+```
+
+**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 {
+  string name = 1 [
+    (google.api.field_behavior) = REQUIRED,
+    (google.api.resource_reference).type = "library.googleapis.com/Book"
+  ];
+
+  // (-- api-linter: core::0162::rollback-request-revision-id-behavior=disabled
+  //     aip.dev/not-precedent: We need to do this because reasons. --)
+  string revision_id = 2;
+}
+```
+
+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-revision-id-field.md b/docs/rules/0162/rollback-request-revision-id-field.md
new file mode 100644
index 0000000..4e123b5
--- /dev/null
+++ b/docs/rules/0162/rollback-request-revision-id-field.md
@@ -0,0 +1,86 @@
+---
+rule:
+  aip: 162
+  name: [core, '0162', rollback-request-revision-id-field]
+  summary: Rollback RPCs must have a `revision_id` field in the request.
+permalink: /162/rollback-request-revision-id-field
+redirect_from:
+  - /0162/rollback-request-revision-id-field
+---
+
+# Rollback requests: Revision ID field
+
+This rule enforces that all `Rollback` methods have a `string revision_id`
+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 `revision_id` field is missing or it has any type other than `string`.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+// Should include a `string revision_id` field.
+message RollbackBookRequest {
+  string name = 1 [
+    (google.api.field_behavior) = REQUIRED,
+    (google.api.resource_reference).type = "library.googleapis.com/Book"
+  ];
+}
+```
+
+```proto
+// Incorrect.
+message RollbackBookRequest {
+  string name = 1 [
+    (google.api.field_behavior) = REQUIRED,
+    (google.api.resource_reference).type = "library.googleapis.com/Book"
+  ];
+
+  // Field type should be `string`.
+  bytes 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 {
+  string name = 1 [
+    (google.api.field_behavior) = REQUIRED,
+    (google.api.resource_reference).type = "library.googleapis.com/Book"
+  ];
+
+  // (-- api-linter: core::0162::rollback-request-revision-id-field=disabled
+  //     aip.dev/not-precedent: We need to do this because reasons. --)
+  bytes 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 c8d7718..f3da163 100644
--- a/rules/aip0162/aip0162.go
+++ b/rules/aip0162/aip0162.go
@@ -44,6 +44,8 @@ func AddRules(r lint.RuleRegistry) error {
 		rollbackRequestNameBehavior,
 		rollbackRequestNameField,
 		rollbackRequestNameReference,
+		rollbackRequestRevisionIDBehavior,
+		rollbackRequestRevisionIDField,
 		rollbackResponseMessageName,
 		tagRevisionHTTPBody,
 		tagRevisionHTTPMethod,
diff --git a/rules/aip0162/rollback_request_revision_id_behavior.go b/rules/aip0162/rollback_request_revision_id_behavior.go
new file mode 100644
index 0000000..352dfde
--- /dev/null
+++ b/rules/aip0162/rollback_request_revision_id_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 rollbackRequestRevisionIDBehavior = &lint.FieldRule{
+	Name: lint.NewRuleName(162, "rollback-request-revision-id-behavior"),
+	OnlyIf: func(f *desc.FieldDescriptor) bool {
+		return isRollbackRequestMessage(f.GetOwner()) && f.GetName() == "revision_id"
+	},
+	LintField: utils.LintRequiredField,
+}
diff --git a/rules/aip0162/rollback_request_revision_id_field.go b/rules/aip0162/rollback_request_revision_id_field.go
new file mode 100644
index 0000000..3a5ea30
--- /dev/null
+++ b/rules/aip0162/rollback_request_revision_id_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 revision_id field.
+var rollbackRequestRevisionIDField = &lint.MessageRule{
+	Name:        lint.NewRuleName(162, "rollback-request-revision-id-field"),
+	OnlyIf:      isRollbackRequestMessage,
+	LintMessage: utils.LintFieldPresentAndSingularString("revision_id"),
+}


Test patch:
diff --git a/rules/aip0162/rollback_request_revision_id_behavior_test.go b/rules/aip0162/rollback_request_revision_id_behavior_test.go
new file mode 100644
index 0000000..288feb0
--- /dev/null
+++ b/rules/aip0162/rollback_request_revision_id_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 TestRollbackRequestRevisionIDBehavior(t *testing.T) {
+	for _, test := range []struct {
+		name      string
+		RPC       string
+		Field     string
+		FieldOpts string
+		problems  testutils.Problems
+	}{
+		{"Valid", "RollbackBook", "revision_id", " [(google.api.field_behavior) = REQUIRED]", nil},
+		{"Missing", "RollbackBook", "revision_id", "", testutils.Problems{{Message: "(google.api.field_behavior) = REQUIRED"}}},
+		{"IrrelevantMessage", "PurgeBooks", "revision_id", "", 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(rollbackRequestRevisionIDBehavior.Lint(f)); diff != "" {
+				t.Error(diff)
+			}
+		})
+	}
+}
diff --git a/rules/aip0162/rollback_request_revision_id_field_test.go b/rules/aip0162/rollback_request_revision_id_field_test.go
new file mode 100644
index 0000000..5d006f9
--- /dev/null
+++ b/rules/aip0162/rollback_request_revision_id_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 TestRollbackRequestRevisionIDField(t *testing.T) {
+	for _, test := range []struct {
+		name     string
+		RPC      string
+		Field    string
+		problems testutils.Problems
+	}{
+		{"Valid", "RollbackBook", "string revision_id = 1;", nil},
+		{"Missing", "RollbackBook", "", testutils.Problems{{Message: "no `revision_id`"}}},
+		{"InvalidType", "RollbackBook", "int32 revision_id = 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(rollbackRequestRevisionIDField.Lint(f)); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}