Skip to content

fix(functions): update and fix functions_billing_stop sample #4085

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

eapl-gemugami
Copy link
Contributor

@eapl-gemugami eapl-gemugami commented May 8, 2025

Description

Fixes b/403855924

This sample for a Cloud Run Function used in disable-billing-with-notifications#create-cloud-run-function is updated and validated to current Node 20 requirements.

As the original index.js file has several samples in the same file, this one will me moved to new folder, to apply the One sample per file principle.

Checklist

  • I have followed guidelines from CONTRIBUTING.MD and Samples Style Guide
  • Tests pass: npm test (see Testing)
  • Lint pass: npm run lint (see Style)
  • This pull request is from a branch created directly off of GoogleCloudPlatform/nodejs-docs-samples. Not a fork.
  • This sample adds a new sample directory, and I updated the CODEOWNERS file with the codeowners for this sample <- It creates a subdirectory, I don't think this is required.
  • This sample adds a new sample directory, and I created GitHub Actions workflow for this sample <- Same as above
  • Please merge this PR for me once it is approved

@product-auto-label product-auto-label bot added api: cloudfunctions Issues related to the Cloud Run functions API. samples Issues that are directly related to samples. asset: pattern DEE Asset tagging - Pattern. labels May 8, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Hello @eapl-gemugami, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

Summary of Changes

This pull request addresses issue b/403855924 by updating and validating the functions_billing_stop sample for a Cloud Run Function to meet current Node 20 requirements. The sample is used in the disable-billing-with-notifications#create-cloud-run-function documentation.

Highlights

  • Node.js 20 Compatibility: The sample has been updated to ensure compatibility with Node.js 20 runtime environment.
  • .gcloudignore update: A .gcloudignore file was added to prevent uploading node_modules to Google Cloud Platform.
  • Code Validation: The code has been validated to ensure it functions correctly with the updated dependencies and Node.js version.

Changelog

Click here to see the changelog
  • functions/billing/stop_billing/.gcloudignore
    • Added a .gcloudignore file to prevent uploading node_modules and other unnecessary files to Google Cloud Platform. This improves deployment speed and reduces the size of the deployed function.
  • functions/billing/stop_billing/index.js
    • Updated the copyright year to 2025.
    • The code was reviewed and updated to ensure compatibility with Node.js 20 and the latest versions of the Google Cloud Functions Framework and Billing client libraries.
    • No functional changes were made to the core logic of the function.
  • functions/billing/stop_billing/package.json
    • Updated the engines.node field to '>=20.0.0' to specify the required Node.js version.
    • Updated the versions of @google-cloud/billing, @google-cloud/functions-framework, and gcp-metadata to their latest versions.
    • Updated the versions of dev dependencies such as c8, gaxios, mocha, promise-retry, proxyquire, sinon, and wait-port to their latest versions.
  • functions/billing/stop_billing/test/index.test.js
    • No changes were made to this file.
  • functions/billing/stop_billing/test/periodic.test.js
    • No changes were made to this file.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.


Node twenty's call,
Billing stops before the fall,
A budget's defense.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request updates and fixes the functions_billing_stop sample for a Cloud Run Function, ensuring it meets current Node 20 requirements. The changes include updates to dependencies, the .gcloudignore file, and the main function logic. The tests have also been updated to reflect these changes. Overall, the changes seem well-structured and address the identified issue.

Summary of Findings

  • Missing test case: The test case for disabling billing is currently marked as skipped (xit). It should be enabled and validated to ensure the function works as expected.
  • Copyright year: The copyright year in index.js is set to 2025. Please confirm that this is the correct year.
  • Simulated billing disable: The billing disable function is currently simulating the disable. The actual billing disable code should be enabled.

Merge Readiness

The pull request is almost ready for merging. However, the test case for disabling billing is currently skipped and the billing disable function is simulating the disable. These issues should be addressed before merging. I am unable to directly approve this pull request, and recommend that others review and approve this code before merging.

@eapl-gemugami eapl-gemugami added the snippet-bot:force-run Force snippet-bot runs its logic label May 9, 2025
@snippet-bot snippet-bot bot removed the snippet-bot:force-run Force snippet-bot runs its logic label May 9, 2025
Copy link

snippet-bot bot commented May 9, 2025

Here is the summary of changes.

You are about to add 1 region tag.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@iennae
Copy link
Contributor

iennae commented May 15, 2025

@davidcavazos if you could also take a look at this one; thanks!

@eapl-gemugami
Copy link
Contributor Author

@davidcavazos for this sample I'm planning to add a Unit Test similar to that on Python stop_billing_test.py

The current one is just a copy from the test in the parent folder.

@eapl-gemugami
Copy link
Contributor Author

@davidcavazos any comments on this approach of making an Unit Test based on the Python sample? Thanks!

eapl-gemugami and others added 5 commits July 22, 2025 11:58
- Implement a PoC unit test for the CloudEvent received
- Refactor index.js in stop-billing.js to validate in local environment and Cloud Run
- Add test for notification within budget
- Validate unit tests
- Apply linting
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request adds a new, standalone Cloud Function sample for stopping billing based on budget notifications. The code is well-structured and includes tests. I've identified a few critical issues related to error handling and asynchronous operations that could lead to function crashes. My review includes suggestions to make the function more robust by handling potential runtime errors and correctly managing async calls. I also have a minor suggestion for the package.json file to align with npm specifications.

@eapl-gemugami eapl-gemugami changed the title fix(functions): WIP update and fix functions_billing_stop sample fix(functions): update and fix functions_billing_stop sample Jul 24, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The code changes introduce the functions_billing_stop sample into its own dedicated directory and updates it to support Node.js 20. There are a couple of critical correctness issues related to asynchronous operations that need to be addressed, as well as a misconfiguration in the ESLint setup.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request modernizes the functions_billing_stop sample by moving it to its own directory, updating it to use CloudEvents for Node.js 20, and adding a simulation flag. The feedback includes suggestions to align with logging best practices and update the linter configuration.

@eapl-gemugami eapl-gemugami marked this pull request as ready for review July 25, 2025 14:47
@eapl-gemugami eapl-gemugami requested review from a team as code owners July 25, 2025 14:47
glasnt
glasnt previously requested changes Jul 27, 2025
Copy link
Contributor

@glasnt glasnt left a comment

Choose a reason for hiding this comment

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

It's unclear if this is the companion to https://github.com/GoogleCloudPlatform/python-docs-samples/tree/main/functions/billing_stop_on_notification; if so, there may be more changes required (same folder name functionality suggestions). Some other changes requested.

Comment on lines 25 to 28
// TODO(developer): As stopping billing is a destructive action
// for your project, change the following constant to `false`
// after you validate with a test budget.
const simulateDeactivation = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not something the Python sample does (unless there's a pending change there to match).

Also, I would not use the term "simulateDeactivation". Instead I'd use something more forceful like "dry-run". That is a term more associated to actions that don't invoke the processing required.

You should also remove any output later that appends "(simulated)" and make it more obvious that it's a dry-run of the function. I can imagine support requests given the seriousness of this sample, and want to make sure it's as obvious as possible what's going on.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please watch my updated sample here: stop_billing.py#L70-L73

I agree with the term dry-run. We could update the Node sample first, and replicate the change on Python afterwards.

About "make it more obvious that it's a dry-run of the function", do you have some reference? I've seen the dry-run for Let's Encrypt, Apache, etc, but I don't know of a case for GCP on a Client Library.

I've found for example: https://cloud.google.com/resource-manager/docs/organization-policy/dry-run-policy

Copy link
Contributor Author

@eapl-gemugami eapl-gemugami Jul 28, 2025

Choose a reason for hiding this comment

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

I couldn't find an example in the Node repo.

Based on Let's encrypt output, I'm proposing:

console.log('** DRY RUN: simulating billing deactivation');
console.log('Billing disabled.');

Please let me know if there's a more appropriate syntax or wording for our samples.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think you can change your test, with comment documentation, to check for the dryrun message rather than the live message. You don't want users to see the output of "billing disabled", even with a dryrun message preceeding it.

I'd recommend something like:

  // TODO(developer): As stopping billing is a destructive action
  // for your project, this code defaults to not performing the change, unless 
  // the following variable has been manually changed. 
  const dryRun = true;

...

  const _disableBillingForProject = async (projectName, dryRun) => {
  if (dryRun) {
    console.log('** This script would disable billing here, but "dryRun" has been set to True. Change "dryRun" to alter this behaviour');
    return;
  } else { // ⚠️  Important to add here

  // rest of code here
};

Then update the test to test for the dryrun message, with a comment saying something like "We would never test that this function actually runs, as it breaks CI. So instead, test that the dryrun worked"

@eapl-gemugami
Copy link
Contributor Author

@glasnt thanks a lot for your review! I'll work on those comments I have given a thumbs up to, and I have a few questions in the replies.

@eapl-gemugami eapl-gemugami requested a review from glasnt July 28, 2025 19:51
Copy link
Contributor

@glasnt glasnt left a comment

Choose a reason for hiding this comment

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

I think this PR is now okay, but I would appreciate a second reviewer/approver to confirm

const _disableBillingForProject = async projectName => {
if (dryRun) {
console.log(
'** This script would disable billing here, but "dryRun" has been set to true.' +
Copy link
Contributor

@iennae iennae Aug 1, 2025

Choose a reason for hiding this comment

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

nit: ** INFO: Script running in info-only mode because "dryRun" is true. To disable billing, set "dryRun" to false.

@iennae iennae dismissed glasnt’s stale review August 1, 2025 17:07

glasnt provided a LGTM

@iennae iennae merged commit d198266 into main Aug 1, 2025
18 checks passed
@iennae iennae deleted the paradalicea/fix/functions/sample-billing-stop/b-403855924 branch August 1, 2025 17:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: cloudfunctions Issues related to the Cloud Run functions API. asset: pattern DEE Asset tagging - Pattern. samples Issues that are directly related to samples.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants