Repository: googleapis/api-linter
Pull request #767 (2021-02-23 20:57:51)

feat: Add rule for AIP-152 Jobs resource reference suffix
Part of #698.

apasel422: > @apasel422 Can you clear your "changes requested" flag (unless you want more changes)?

Done.

Patch:
diff --git a/docs/rules/0152/request-resource-suffix.md b/docs/rules/0152/request-resource-suffix.md
new file mode 100644
index 0000000..bea032d
--- /dev/null
+++ b/docs/rules/0152/request-resource-suffix.md
@@ -0,0 +1,72 @@
+---
+rule:
+  aip: 152
+  name: [core, '0152', request-resource-suffix]
+  summary: |
+    Run requests should identify a resource type which ends in "Job".
+permalink: /152/request-resource-suffix
+redirect_from:
+  - /0152/request-resource-suffix
+---
+
+# Run requests: Resource reference
+
+This rule enforces that all `Run` methods specify a `type` that ends in "Job" 
+in the `google.api.resource_reference` annotation of their `string name` field, 
+as mandated in [AIP-152][].
+
+## Details
+
+This rule looks at the `name` field of any message matching `Run*JobRequest` 
+and complains if the `type` in its `google.api.resource_reference` annotation
+does not end in "Job".
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+message RunWriteBookJobRequest {
+  // The `type` of the `google.api.resource_reference` annotation should end in 
+  // "Job".
+  string name = 1 [
+    (google.api.field_behavior) = REQUIRED,
+    (google.api.resource_reference).type = "library.googleapis.com/Book"
+  ];
+}
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+message RunWriteBookJobRequest {
+  string name = 1 [
+    (google.api.field_behavior) = REQUIRED,
+    (google.api.resource_reference).type = "library.googleapis.com/WriteBookJob"
+  ];
+}
+```
+
+## 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 RunWriteBookJobRequest {
+  // (-- api-linter: core::0152::request-resource-suffix=disabled
+  //     aip.dev/not-precedent: We need to do this because reasons. --)
+  string name = 1 [
+    (google.api.field_behavior) = REQUIRED,
+    (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-152]: https://aip.dev/152
+[aip.dev/not-precedent]: https://aip.dev/not-precedent
diff --git a/rules/aip0152/aip0152.go b/rules/aip0152/aip0152.go
index 6e7fe92..e0c2e97 100644
--- a/rules/aip0152/aip0152.go
+++ b/rules/aip0152/aip0152.go
@@ -34,6 +34,7 @@ func AddRules(r lint.RuleRegistry) error {
 		requestNameBehavior,
 		requestNameField,
 		requestNameReference,
+		requestResourceSuffix,
 		responseMessageName,
 	)
 }
diff --git a/rules/aip0152/request_resource_suffix.go b/rules/aip0152/request_resource_suffix.go
new file mode 100644
index 0000000..bb92d86
--- /dev/null
+++ b/rules/aip0152/request_resource_suffix.go
@@ -0,0 +1,50 @@
+// 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 aip0152
+
+import (
+	"fmt"
+	"strings"
+
+	"github.com/googleapis/api-linter/lint"
+	"github.com/googleapis/api-linter/locations"
+	"github.com/googleapis/api-linter/rules/internal/utils"
+	"github.com/jhump/protoreflect/desc"
+)
+
+// The name of the resource must end with the word "Job".
+var requestResourceSuffix = &lint.FieldRule{
+	Name: lint.NewRuleName(152, "request-resource-suffix"),
+	OnlyIf: func(f *desc.FieldDescriptor) bool {
+		return isRunRequestMessage(f.GetOwner()) && f.GetName() == "name"
+	},
+	LintField: func(f *desc.FieldDescriptor) []lint.Problem {
+		// Rule check: Establish that the `resource_reference` annotation's
+		// type ends in "Job".
+		ref := utils.GetResourceReference(f)
+		if ref != nil && !strings.HasSuffix(ref.GetType(), "Job") {
+			suggestion := strings.TrimPrefix(f.GetOwner().GetName(), "Run")
+			suggestion = strings.TrimSuffix(suggestion, "Request")
+
+			return []lint.Problem{{
+				Message:    fmt.Sprintf("The `type` of the `google.api.resource_reference` annotation should end in %q.", "Job"),
+				Descriptor: f,
+				Location:   locations.FieldResourceReference(f),
+				Suggestion: suggestion,
+			}}
+		}
+		return nil
+	},
+}


Test patch:
diff --git a/rules/aip0152/request_resource_suffix_test.go b/rules/aip0152/request_resource_suffix_test.go
new file mode 100644
index 0000000..4f2a8ec
--- /dev/null
+++ b/rules/aip0152/request_resource_suffix_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 aip0152
+
+import (
+	"testing"
+
+	"github.com/googleapis/api-linter/rules/internal/testutils"
+)
+
+func TestRequestResourceSuffix(t *testing.T) {
+	for _, test := range []struct {
+		name      string
+		RPC       string
+		Field     string
+		FieldOpts string
+		problems  testutils.Problems
+	}{
+		{"ValidPresent", "RunWriteBookJob", "name", ` [(google.api.resource_reference) = { type: "WriteBookJob" }]`, nil},
+		{"ValidMissing", "RunWriteBookJob", "name", "", nil},
+		{"IncorrectSuffix", "RunWriteBookJob", "name", ` [(google.api.resource_reference) = { type: "Book" }]`,
+			testutils.Problems{{Message: "google.api.resource_reference", Suggestion: "WriteBookJob"}}},
+		{"IrrelevantMessage", "PurgeBooks", "name", "", nil},
+		{"IrrelevantField", "RunWriteBookJob", "something_else", "", nil},
+	} {
+		t.Run(test.name, func(t *testing.T) {
+			f := testutils.ParseProto3Tmpl(t, `
+				import "google/api/resource.proto";
+				message {{.RPC}}Request {
+					string {{.Field}} = 1{{.FieldOpts}};
+				}
+			`, test)
+			field := f.GetMessageTypes()[0].GetFields()[0]
+			if diff := test.problems.SetDescriptor(field).Diff(requestResourceSuffix.Lint(f)); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}