|
| 1 | +// Copyright 2026 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 | +// http://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 | +import {BaseStrategy, BuildUpdatesOptions} from './base'; |
| 16 | +import {Update} from '../update'; |
| 17 | +import {ChangelogJson} from '../updaters/changelog-json'; |
| 18 | +import {PackageLockJson} from '../updaters/node/package-lock-json'; |
| 19 | +import {SamplesPackageJson} from '../updaters/node/samples-package-json'; |
| 20 | +import {Changelog} from '../updaters/changelog'; |
| 21 | +import {PackageJson} from '../updaters/node/package-json'; |
| 22 | +import {LibrarianYamlUpdater} from '../updaters/node/librarian-yaml'; |
| 23 | +import {GitHubFileContents} from '@google-automations/git-file-utils'; |
| 24 | +import {FileNotFoundError, MissingRequiredFileError} from '../errors'; |
| 25 | +import {filterCommits} from '../util/filter-commits'; |
| 26 | + |
| 27 | +export class NodeLibrarian extends BaseStrategy { |
| 28 | + private pkgJsonContents?: GitHubFileContents; |
| 29 | + |
| 30 | + protected async buildUpdates( |
| 31 | + options: BuildUpdatesOptions |
| 32 | + ): Promise<Update[]> { |
| 33 | + const updates: Update[] = []; |
| 34 | + const version = options.newVersion; |
| 35 | + const versionsMap = options.versionsMap; |
| 36 | + const packageName = (await this.getPackageName()) ?? ''; |
| 37 | + const lockFiles = ['package-lock.json', 'npm-shrinkwrap.json']; |
| 38 | + const librarianFilesSearch = this.github.findFilesByFilenameAndRef( |
| 39 | + 'librarian.yaml', |
| 40 | + this.targetBranch, |
| 41 | + this.path |
| 42 | + ); |
| 43 | + lockFiles.forEach(lockFile => { |
| 44 | + updates.push({ |
| 45 | + path: this.addPath(lockFile), |
| 46 | + createIfMissing: false, |
| 47 | + updater: new PackageLockJson({ |
| 48 | + version, |
| 49 | + versionsMap, |
| 50 | + }), |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + updates.push({ |
| 55 | + path: this.addPath('samples/package.json'), |
| 56 | + createIfMissing: false, |
| 57 | + updater: new SamplesPackageJson({ |
| 58 | + version, |
| 59 | + packageName, |
| 60 | + }), |
| 61 | + }); |
| 62 | + |
| 63 | + !this.skipChangelog && |
| 64 | + updates.push({ |
| 65 | + path: this.addPath(this.changelogPath), |
| 66 | + createIfMissing: true, |
| 67 | + updater: new Changelog({ |
| 68 | + version, |
| 69 | + changelogEntry: options.changelogEntry, |
| 70 | + }), |
| 71 | + }); |
| 72 | + |
| 73 | + updates.push({ |
| 74 | + path: this.addPath('package.json'), |
| 75 | + createIfMissing: false, |
| 76 | + cachedFileContents: this.pkgJsonContents, |
| 77 | + updater: new PackageJson({ |
| 78 | + version, |
| 79 | + }), |
| 80 | + }); |
| 81 | + |
| 82 | + // If a machine readable changelog.json exists update it: |
| 83 | + if (options.commits && packageName && !this.skipChangelog) { |
| 84 | + const commits = filterCommits(options.commits, this.changelogSections); |
| 85 | + updates.push({ |
| 86 | + path: 'changelog.json', |
| 87 | + createIfMissing: false, |
| 88 | + updater: new ChangelogJson({ |
| 89 | + artifactName: packageName, |
| 90 | + version, |
| 91 | + commits, |
| 92 | + language: 'JAVASCRIPT', |
| 93 | + }), |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + // Update librarian.yaml for every matching library. |
| 98 | + const librarianFiles = await librarianFilesSearch; |
| 99 | + librarianFiles.forEach(path => { |
| 100 | + updates.push({ |
| 101 | + path: this.addPath(path), |
| 102 | + createIfMissing: false, |
| 103 | + updater: new LibrarianYamlUpdater({ |
| 104 | + version, |
| 105 | + versionsMap, |
| 106 | + }), |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + return updates; |
| 111 | + } |
| 112 | + |
| 113 | + async getDefaultPackageName(): Promise<string | undefined> { |
| 114 | + const pkgJsonContents = await this.getPkgJsonContents(); |
| 115 | + const pkg = JSON.parse(pkgJsonContents.parsedContent); |
| 116 | + return pkg.name; |
| 117 | + } |
| 118 | + |
| 119 | + protected normalizeComponent(component: string | undefined): string { |
| 120 | + if (!component) { |
| 121 | + return ''; |
| 122 | + } |
| 123 | + return component.match(/^@[\w-]+\//) ? component.split('/')[1] : component; |
| 124 | + } |
| 125 | + |
| 126 | + protected async getPkgJsonContents(): Promise<GitHubFileContents> { |
| 127 | + if (!this.pkgJsonContents) { |
| 128 | + try { |
| 129 | + this.pkgJsonContents = await this.github.getFileContentsOnBranch( |
| 130 | + this.addPath('package.json'), |
| 131 | + this.targetBranch |
| 132 | + ); |
| 133 | + } catch (e) { |
| 134 | + if (e instanceof FileNotFoundError) { |
| 135 | + throw new MissingRequiredFileError( |
| 136 | + this.addPath('package.json'), |
| 137 | + 'node', |
| 138 | + `${this.repository.owner}/${this.repository.repo}` |
| 139 | + ); |
| 140 | + } |
| 141 | + throw e; |
| 142 | + } |
| 143 | + } |
| 144 | + return this.pkgJsonContents; |
| 145 | + } |
| 146 | +} |
0 commit comments