Skip to content

Commit 1503540

Browse files
authored
impl(sidekick): find cargo manifest files (#2152)
1 parent 1d68698 commit 1503540

File tree

4 files changed

+103
-1
lines changed

4 files changed

+103
-1
lines changed

internal/sidekick/internal/rust_release/bump_versions.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@ func BumpVersions(config *config.Release) error {
2626
if err != nil {
2727
return err
2828
}
29-
_, err = filesChangedSince(config, lastTag)
29+
files, err := filesChangedSince(config, lastTag)
3030
if err != nil {
3131
return err
3232
}
33+
_ = findCargoManifests(files)
3334
return nil
3435
}

internal/sidekick/internal/rust_release/bump_versions_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ func initRepositoryContents(t *testing.T) {
127127
t.Fatal(err)
128128
}
129129
addCrate(t, path.Join("src", "storage"), "google-cloud-storage")
130+
addCrate(t, path.Join("src", "gax-internal"), "google-cloud-gax-internal")
131+
addCrate(t, path.Join("src", "gax-internal", "echo-server"), "echo-server")
130132
addCrate(t, path.Join("src", "generated", "cloud", "secretmanager", "v1"), "google-cloud-secretmanager-v1")
131133
if err := external.Run("git", "add", "."); err != nil {
132134
t.Fatal(err)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package rustrelease
16+
17+
import (
18+
"maps"
19+
"os"
20+
"path"
21+
"slices"
22+
)
23+
24+
func findCargoManifests(files []string) []string {
25+
isCandidate := func(parent string) bool {
26+
return parent != "/" && parent != "." && parent != ""
27+
}
28+
unique := map[string]bool{}
29+
for _, f := range files {
30+
if d := path.Dir(f); isCandidate(d) {
31+
unique[d] = true
32+
}
33+
}
34+
35+
candidates := slices.Collect(maps.Keys(unique))
36+
manifests := map[string]bool{}
37+
for len(candidates) != 0 {
38+
d := candidates[len(candidates)-1]
39+
candidates = candidates[0 : len(candidates)-1]
40+
manifest := path.Join(d, "Cargo.toml")
41+
if _, ok := manifests[manifest]; ok {
42+
continue
43+
}
44+
if _, err := os.Stat(manifest); err == nil {
45+
manifests[manifest] = true
46+
} else if parent := path.Dir(d); isCandidate(parent) {
47+
candidates = append(candidates, parent)
48+
}
49+
}
50+
51+
list := slices.Collect(maps.Keys(manifests))
52+
slices.Sort(list)
53+
return list
54+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package rustrelease
16+
17+
import (
18+
"testing"
19+
20+
"github.com/google/go-cmp/cmp"
21+
)
22+
23+
func TestFindManifest(t *testing.T) {
24+
setupForVersionBump(t, "find-manifest-0.0.1")
25+
// A hypothetical set of file changes.
26+
input := []string{
27+
"doc/not-in-a-Cargo-file",
28+
"doc/subdir/not-a-Cargo-file-either.md",
29+
"src/storage/src/lib.rs",
30+
"src/storage/src/generated/model.rs",
31+
"src/storage/src/generated/stub.rs",
32+
"src/storage/src/generated/client.rs",
33+
"src/generated/cloud/secretmanager/v1/src/stub/subdir/file.rs",
34+
"src/gax-internal/echo-server/src/lib.rs",
35+
}
36+
got := findCargoManifests(input)
37+
want := []string{
38+
"src/gax-internal/echo-server/Cargo.toml",
39+
"src/generated/cloud/secretmanager/v1/Cargo.toml",
40+
"src/storage/Cargo.toml",
41+
}
42+
if diff := cmp.Diff(want, got); diff != "" {
43+
t.Errorf("generated files changed mismatch (-want +got):\n%s", diff)
44+
}
45+
}

0 commit comments

Comments
 (0)