Repository: googleapis/api-linter
Pull request #413 (2019-12-04 23:20:27)

[AIP-215] Proto packages should end in a version.


Patch:
diff --git a/docs/rules/0215/index.md b/docs/rules/0215/index.md
new file mode 100644
index 0000000..c760b45
--- /dev/null
+++ b/docs/rules/0215/index.md
@@ -0,0 +1,10 @@
+---
+aip_listing: 215
+permalink: /215/
+redirect_from:
+  - /0215/
+---
+
+# Common component versions
+
+{% include linter-aip-listing.md aip=215 %}
diff --git a/docs/rules/0215/versioned-packages.md b/docs/rules/0215/versioned-packages.md
new file mode 100644
index 0000000..4bf717a
--- /dev/null
+++ b/docs/rules/0215/versioned-packages.md
@@ -0,0 +1,65 @@
+---
+rule:
+  aip: 215
+  name: [core, '0215', versioned-packages]
+  summary: API-specific protos should be in versioned packages.
+permalink: /215/versioned-packages
+redirect_from:
+  - /0215/versioned-packages
+---
+
+# Versioned packages
+
+This rule enforces that API-specific protos use versioned packages, as mandated
+in [AIP-215][].
+
+## Details
+
+This rule examines the proto package and complains if it does not end something
+that appears to be a version component, such as `v1` or `v1beta`. It also
+permits proto packages ending in `type`.
+
+## Examples
+
+**Incorrect** code for this rule:
+
+```proto
+// Incorrect.
+package foo.bar;
+```
+
+```proto
+// Incorrect.
+package foo.bar.v1.resources;
+```
+
+**Correct** code for this rule:
+
+```proto
+// Correct.
+package foo.bar.v1;
+```
+
+## Known issues
+
+This rule blindly assumes that the proto it is looking at is for an individual
+API (unless it ends in `type`). It will therefore complain about protos where
+the lack of a version is proper (such as `google.longrunning`, for example).
+This rule **should** be disabled in this situation.
+
+## Disabling
+
+If you need to violate this rule, use a leading comment above the enum.
+Remember to also include an [aip.dev/not-precedent][] comment explaining why.
+
+```proto
+// (-- api-linter: core::0215::versioned-packages=disabled
+//     aip.dev/not-precedent: We need to do this because reasons. --)
+package foo.bar;
+```
+
+If you need to violate this rule for an entire file, place the comment at the
+top of the file.
+
+[aip-215]: https://aip.dev/215
+[aip.dev/not-precedent]: https://aip.dev/not-precedent
diff --git a/rules/aip0215/aip0215.go b/rules/aip0215/aip0215.go
new file mode 100644
index 0000000..fa1a01f
--- /dev/null
+++ b/rules/aip0215/aip0215.go
@@ -0,0 +1,28 @@
+// Copyright 2019 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 aip0215 contains rules defined in https://aip.dev/215.
+package aip0215
+
+import (
+	"github.com/googleapis/api-linter/lint"
+)
+
+// AddRules adds all of the AIP-215 rules to the provided registry.
+func AddRules(r lint.RuleRegistry) error {
+	return r.Register(
+		215,
+		versionedPackages,
+	)
+}
diff --git a/rules/aip0215/versioned_packages.go b/rules/aip0215/versioned_packages.go
new file mode 100644
index 0000000..0b26285
--- /dev/null
+++ b/rules/aip0215/versioned_packages.go
@@ -0,0 +1,43 @@
+// Copyright 2019 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 aip0215
+
+import (
+	"regexp"
+	"strings"
+
+	"github.com/googleapis/api-linter/lint"
+	"github.com/googleapis/api-linter/locations"
+	"github.com/jhump/protoreflect/desc"
+)
+
+var versionedPackages = &lint.FileRule{
+	Name: lint.NewRuleName(215, "versioned-packages"),
+	OnlyIf: func(f *desc.FileDescriptor) bool {
+		return f.GetPackage() != ""
+	},
+	LintFile: func(f *desc.FileDescriptor) []lint.Problem {
+		if p := f.GetPackage(); !version.MatchString(p) && !strings.HasSuffix(p, ".type") {
+			return []lint.Problem{{
+				Message:    "API components should be in versioned packages.",
+				Descriptor: f,
+				Location:   locations.FilePackage(f),
+			}}
+		}
+		return nil
+	},
+}
+
+var version = regexp.MustCompile(`\.v[\d]+(p[\d]+)?(alpha|beta|eap|test)?[\d]*$`)
diff --git a/rules/rules.go b/rules/rules.go
index 82ecdbf..ca5437b 100644
--- a/rules/rules.go
+++ b/rules/rules.go
@@ -73,6 +73,7 @@ import (
 	"github.com/googleapis/api-linter/rules/aip0192"
 	"github.com/googleapis/api-linter/rules/aip0203"
 	"github.com/googleapis/api-linter/rules/aip0214"
+	"github.com/googleapis/api-linter/rules/aip0215"
 	"github.com/googleapis/api-linter/rules/aip0216"
 	"github.com/googleapis/api-linter/rules/aip0217"
 	"github.com/googleapis/api-linter/rules/aip0231"
@@ -105,6 +106,7 @@ var aipAddRulesFuncs = []addRulesFuncType{
 	aip0192.AddRules,
 	aip0203.AddRules,
 	aip0214.AddRules,
+	aip0215.AddRules,
 	aip0216.AddRules,
 	aip0217.AddRules,
 	aip0231.AddRules,


Test patch:
diff --git a/rules/aip0215/aip0215_test.go b/rules/aip0215/aip0215_test.go
new file mode 100644
index 0000000..7759b09
--- /dev/null
+++ b/rules/aip0215/aip0215_test.go
@@ -0,0 +1,27 @@
+// Copyright 2019 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 aip0215
+
+import (
+	"testing"
+
+	"github.com/googleapis/api-linter/lint"
+)
+
+func TestAddRules(t *testing.T) {
+	if err := AddRules(lint.NewRuleRegistry()); err != nil {
+		t.Errorf("AddRules got an error: %v", err)
+	}
+}
diff --git a/rules/aip0215/versioned_packages_test.go b/rules/aip0215/versioned_packages_test.go
new file mode 100644
index 0000000..e544835
--- /dev/null
+++ b/rules/aip0215/versioned_packages_test.go
@@ -0,0 +1,57 @@
+// Copyright 2019 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 aip0215
+
+import (
+	"testing"
+
+	"github.com/googleapis/api-linter/rules/internal/testutils"
+)
+
+func TestVersionedPackages(t *testing.T) {
+	for _, test := range []struct {
+		name        string
+		PackageStmt string
+		problems    testutils.Problems
+	}{
+		{"Stable", "package foo.bar.v1;", testutils.Problems{}},
+		{"StableBignum", "package foo.bar.v999;", testutils.Problems{}},
+		{"Alpha", "package foo.bar.v1alpha;", testutils.Problems{}},
+		{"Alpha1", "package foo.bar.v1alpha1;", testutils.Problems{}},
+		{"Alpha12", "package foo.bar.v1alpha12;", testutils.Problems{}},
+		{"Beta", "package foo.bar.v1beta;", testutils.Problems{}},
+		{"Beta1", "package foo.bar.v1beta1;", testutils.Problems{}},
+		{"Beta12", "package foo.bar.v1beta12;", testutils.Problems{}},
+		{"P1Beta1", "package foo.bar.v1p1beta1;", testutils.Problems{}},
+		{"P12Beta1", "package foo.bar.v1p12beta1;", testutils.Problems{}},
+		{"P1Beta12", "package foo.bar.v1p1beta12;", testutils.Problems{}},
+		{"P12Beta12", "package foo.bar.v1p12beta12;", testutils.Problems{}},
+		{"EAP", "package foo.bar.v1eap;", testutils.Problems{}},
+		{"EAP1", "package foo.bar.v1eap1;", testutils.Problems{}},
+		{"Test", "package foo.bar.v1test;", testutils.Problems{}},
+		{"Test1", "package foo.bar.v1test1;", testutils.Problems{}},
+		{"Type", "package foo.bar.type;", testutils.Problems{}},
+		{"InvalidNoVersion", "package foo.bar;", testutils.Problems{{Message: "versioned packages"}}},
+		{"InvalidSubpackage", "package foo.bar.v1.resources;", testutils.Problems{{Message: "versioned packages"}}},
+		{"IgnoredNoPackage", "", testutils.Problems{}},
+	} {
+		t.Run(test.name, func(t *testing.T) {
+			f := testutils.ParseProto3Tmpl(t, "{{.PackageStmt}}", test)
+			if diff := test.problems.SetDescriptor(f).Diff(versionedPackages.Lint(f)); diff != "" {
+				t.Errorf(diff)
+			}
+		})
+	}
+}