Skip to content

Commit aee2374

Browse files
fix(tool/cmd/migrate): read copyright year from src/ directory (#4841)
extractCopyrightYear now looks for versioned subdirectories under src/ (e.g. src/v1/index.ts) before falling back to src/index.ts so that the copyright year comes from generated sources rather than the hand-maintained top-level index. Fixes #4814 --------- Signed-off-by: Julie Qiu <julie@golang.org> Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent 57af767 commit aee2374

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

tool/cmd/migrate/nodejs.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,34 @@ func ensureNodejsPackage(l *config.Library) *config.NodejsPackage {
350350
// copyrightYearRegex matches "Copyright YYYY Google" in a file header.
351351
var copyrightYearRegex = regexp.MustCompile(`Copyright (\d{4}) Google`)
352352

353-
// extractCopyrightYear reads the copyright year from src/index.ts.
353+
// versionDirRegex matches versioned directory names like v1, v2, v1beta1.
354+
var versionDirRegex = regexp.MustCompile(`^v\d[a-z0-9]*`)
355+
356+
// extractCopyrightYear reads the copyright year from a versioned subdirectory
357+
// of src/ (e.g. src/v1/index.ts), falling back to src/index.ts if no versioned
358+
// directory exists.
354359
func extractCopyrightYear(pkgDir string) string {
355-
data, err := os.ReadFile(filepath.Join(pkgDir, "src", "index.ts"))
360+
srcDir := filepath.Join(pkgDir, "src")
361+
entries, err := os.ReadDir(srcDir)
362+
if err != nil {
363+
return ""
364+
}
365+
var versionDirs []string
366+
for _, e := range entries {
367+
if e.IsDir() && versionDirRegex.MatchString(e.Name()) {
368+
versionDirs = append(versionDirs, e.Name())
369+
}
370+
}
371+
if len(versionDirs) > 0 {
372+
sort.Strings(versionDirs)
373+
data, err := os.ReadFile(filepath.Join(srcDir, versionDirs[0], "index.ts"))
374+
if err == nil {
375+
if m := copyrightYearRegex.FindSubmatch(data); len(m) > 1 {
376+
return string(m[1])
377+
}
378+
}
379+
}
380+
data, err := os.ReadFile(filepath.Join(srcDir, "index.ts"))
356381
if err != nil {
357382
return ""
358383
}

tool/cmd/migrate/nodejs_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestBuildNodejsLibraries(t *testing.T) {
3131
{
3232
Name: "google-cloud-secretmanager",
3333
Version: "6.1.0",
34-
CopyrightYear: "2019",
34+
CopyrightYear: "2020",
3535
APIs: []*config.API{
3636
{Path: "google/cloud/secretmanager/v1"},
3737
},
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2020 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+
export {SecretManagerServiceClient} from './secret_manager_service_client';

0 commit comments

Comments
 (0)