Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions packages/google-cloud-secretmanager/librarian.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,21 @@ function replaceInFile(filePath, pattern, replacement) {
try {
const data = fs.readFileSync(filePath, 'utf8');
const result = data.replace(pattern, replacement);
if (result === data) {
throw new Error(`Pattern ${pattern} was not found or did not result in any changes.`);
}
fs.writeFileSync(filePath, result, 'utf8');
console.log(`Successfully updated: ${filePath}`);
} catch (err) {
console.error(`Error processing file ${filePath}:`, err);
process.exitCode = 1;
}
}

const filePath = 'packages/google-cloud-secretmanager/src/v1/secret_manager_service_client.ts';
const filePaths = [
path.resolve(__dirname, 'src/v1/secret_manager_service_client.ts'),
path.resolve(__dirname, 'src/v1beta2/secret_manager_service_client.ts')
];

const replacement1 = `return Promise.resolve();
}
Expand Down Expand Up @@ -106,7 +113,7 @@ const replacement1 = `return Promise.resolve();
* A fully-qualified path representing SecretVersion resource.
* @returns {string} A string representing the secret.
*/
matchSecretFromSecretVersionName(secretVersionName: string) {
matchSecretFromSecretVersionName(secretVersionName: string) {
return this.pathTemplates.secretVersionPathTemplate.match(secretVersionName)
.secret;
}
Expand Down Expand Up @@ -135,5 +142,9 @@ const replacement2 = `topicPathTemplate: new this._gaxModule.PathTemplate(
),
};`;

replaceInFile(path.resolve(filePath), /return\sPromise\.resolve\(\);\s+}\s+}/g, replacement1);
replaceInFile(path.resolve(filePath), /topicPathTemplate:\s+new\s+this\._gaxModule\.PathTemplate\(\s+'projects\/{project}\/topics\/{topic}'\s+\),\s+};/g, replacement2);
filePaths.forEach(filePath => {
if (fs.existsSync(filePath)) {
replaceInFile(filePath, /return\sPromise\.resolve\(\);\s+}\s+}/g, replacement1);
replaceInFile(filePath, /topicPathTemplate:\s+new\s+this\._gaxModule\.PathTemplate\(\s+'projects\/{project}\/topics\/{topic}',?\s*\),?\s*};/g, replacement2);
}
});
Comment on lines +145 to +150

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The replaceInFile function now throws an error if no replacement is made. Since librarian.js is run as a post-processing script, running it on already-processed files (or running it twice) will cause the script to fail with an error, breaking the build/generation pipeline.

To make the script idempotent and safe to run multiple times, we can check if the file has already been processed (e.g., by checking if it contains secretVersionPath) and skip it gracefully.

Suggested change
filePaths.forEach(filePath => {
if (fs.existsSync(filePath)) {
replaceInFile(filePath, /return\sPromise\.resolve\(\);\s+}\s+}/g, replacement1);
replaceInFile(filePath, /topicPathTemplate:\s+new\s+this\._gaxModule\.PathTemplate\(\s+'projects\/{project}\/topics\/{topic}',?\s*\),?\s*};/g, replacement2);
}
});
filePaths.forEach(filePath => {
if (fs.existsSync(filePath)) {
const content = fs.readFileSync(filePath, 'utf8');
if (content.includes('secretVersionPath')) {
console.log("File " + filePath + " is already processed. Skipping.");
return;
}
replaceInFile(filePath, /return\sPromise\.resolve\(\);\s+}\s+}/g, replacement1);
replaceInFile(filePath, /topicPathTemplate:\s+new\s+this\._gaxModule\.PathTemplate\(\s+'projects\/{project}\/topics\/{topic}',?\s*\),?\s*};/g, replacement2);
}
});

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pearigee What do you think? Our long-term goal is to remove the file. Since this will eventually run in Librarian, we should add error handling in librarian for cases where the file fails rather than making it idempotent.

Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,13 @@ export class SecretManagerServiceClient {
'projects/{project}/secrets/{secret}',
),
topicPathTemplate: new this._gaxModule.PathTemplate(
'projects/{project}/topics/{topic}',
'projects/{project}/topics/{topic}'
),
secretPathTemplate: new this._gaxModule.PathTemplate(
'projects/{project}/secrets/{secret}'
),
secretVersionPathTemplate: new this._gaxModule.PathTemplate(
'projects/{project}/secrets/{secret}/versions/{secret_version}'
),
};

Expand Down Expand Up @@ -3304,7 +3310,7 @@ export class SecretManagerServiceClient {
* A fully-qualified path representing SecretVersion resource.
* @returns {string} A string representing the secret.
*/
matchSecretFromSecretVersionName(secretVersionName: string) {
matchSecretFromSecretVersionName(secretVersionName: string) {
return this.pathTemplates.secretVersionPathTemplate.match(secretVersionName)
.secret;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,13 @@ export class SecretManagerServiceClient {
'projects/{project}/secrets/{secret}',
),
topicPathTemplate: new this._gaxModule.PathTemplate(
'projects/{project}/topics/{topic}',
'projects/{project}/topics/{topic}'
),
secretPathTemplate: new this._gaxModule.PathTemplate(
'projects/{project}/secrets/{secret}'
),
secretVersionPathTemplate: new this._gaxModule.PathTemplate(
'projects/{project}/secrets/{secret}/versions/{secret_version}'
),
};

Expand Down Expand Up @@ -3289,4 +3295,90 @@ export class SecretManagerServiceClient {
}
return Promise.resolve();
}

/**
* Return a fully-qualified project resource name string.
*
* @param {string} project
* @returns {string} Resource name string.
*/
secretPath(project: string, secret: string) {
return this.pathTemplates.secretPathTemplate.render({
project: project,
secret: secret,
});
}

/**
* Parse the project from Secret resource.
*
* @param {string} secretName
* A fully-qualified path representing Secret resource.
* @returns {string} A string representing the project.
*/
matchProjectFromSecretName(secretName: string) {
return this.pathTemplates.secretPathTemplate.match(secretName).project;
}

/**
* Parse the secret from Secret resource.
*
* @param {string} secretName
* A fully-qualified path representing Secret resource.
* @returns {string} A string representing the secret.
*/
matchSecretFromSecretName(secretName: string) {
return this.pathTemplates.secretPathTemplate.match(secretName).secret;
}

/**
* Return a fully-qualified secretVersion resource name string.
*
* @param {string} project
* @param {string} secret
* @param {string} secret_version
* @returns {string} Resource name string.
*/
secretVersionPath(project: string, secret: string, secretVersion: string) {
return this.pathTemplates.secretVersionPathTemplate.render({
project: project,
secret: secret,
secret_version: secretVersion,
});
}

/**
* Parse the project from SecretVersion resource.
*
* @param {string} secretVersionName
* A fully-qualified path representing SecretVersion resource.
* @returns {string} A string representing the project.
*/
matchProjectFromSecretVersionName(secretVersionName: string) {
return this.pathTemplates.secretVersionPathTemplate.match(secretVersionName)
.project;
}
/**
* Parse the secret from SecretVersion resource.
*
* @param {string} secretVersionName
* A fully-qualified path representing SecretVersion resource.
* @returns {string} A string representing the secret.
*/
matchSecretFromSecretVersionName(secretVersionName: string) {
return this.pathTemplates.secretVersionPathTemplate.match(secretVersionName)
.secret;
}

/**
* Parse the secret_version from SecretVersion resource.
*
* @param {string} secretVersionName
* A fully-qualified path representing SecretVersion resource.
* @returns {string} A string representing the secret_version.
*/
matchSecretVersionFromSecretVersionName(secretVersionName: string) {
return this.pathTemplates.secretVersionPathTemplate.match(secretVersionName)
.secret_version;
}
}
Loading