-
Notifications
You must be signed in to change notification settings - Fork 6.6k
fix(workflows): refactor sample 'workflows_api_quickstart' to comply with the Samples Style Guide and fix tests #13261
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
glasnt
merged 11 commits into
GoogleCloudPlatform:main
from
eapl-gemugami:paradalicea/fix/workflows/refactor-sample/api-quickstart/b-391200147
Apr 4, 2025
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
89c17b1
fix(workflows): refactor sample to comply with the Samples Style Guide
eapl-gemugami 0d89d0b
fix(workflows): adjust function definition for line length
eapl-gemugami 271dc52
fix(workflows): fix grammar in comments
eapl-gemugami 9cc2514
fix(workflows): allow testing in Python 3.9+, and update to latest go…
eapl-gemugami b84736e
test(workflows): move test from main to conftest, and add backoff to …
eapl-gemugami 109e1cd
fix(workflows): fix header in conftest.py
eapl-gemugami 602f807
fix(workflows): add api_core.retry as suggested in https://github.com…
eapl-gemugami b9e44b9
fix(workflows): add exponential backoff to main_test
eapl-gemugami 5c6e954
fix(workflows): add missing period to comment
eapl-gemugami 76677bc
fix(workflows): apply Camie Kim's feedback
eapl-gemugami df0c220
fix(workflows): add two missing spaces before in-line comment
eapl-gemugami File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
# Copyright 2025 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import os | ||
import time | ||
import uuid | ||
|
||
from google.cloud import workflows_v1 | ||
|
||
import pytest | ||
|
||
|
||
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT") | ||
LOCATION = "us-central1" | ||
WORKFLOW_ID = "myFirstWorkflow_" + str(uuid.uuid4()) | ||
|
||
|
||
def workflow_exists(client: workflows_v1.WorkflowsClient) -> bool: | ||
"""Returns True if the workflow exists in this project.""" | ||
try: | ||
workflow_name = client.workflow_path( | ||
PROJECT_ID, LOCATION, WORKFLOW_ID | ||
) | ||
client.get_workflow(request={"name": workflow_name}) | ||
return True | ||
except Exception as e: | ||
print(f"Workflow doesn't exist: {e}") | ||
return False | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def client() -> str: | ||
assert PROJECT_ID, "'GOOGLE_CLOUD_PROJECT' environment variable not set." | ||
workflows_client = workflows_v1.WorkflowsClient() | ||
return workflows_client | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def project_id() -> str: | ||
return PROJECT_ID | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def location() -> str: | ||
return LOCATION | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def workflow_id(client: workflows_v1.WorkflowsClient) -> str: | ||
creating_workflow = False | ||
backoff_delay = 1 # Start wait with delay of 1 second. | ||
|
||
# Create the workflow if it doesn't exist. | ||
while not workflow_exists(client): | ||
if not creating_workflow: | ||
# Create the workflow. | ||
workflow_file = open("myFirstWorkflow.workflows.yaml").read() | ||
|
||
parent = client.common_location_path(PROJECT_ID, LOCATION) | ||
|
||
client.create_workflow( | ||
request={ | ||
"parent": parent, | ||
"workflow_id": WORKFLOW_ID, | ||
"workflow": { | ||
"name": WORKFLOW_ID, | ||
"source_contents": workflow_file | ||
}, | ||
} | ||
) | ||
|
||
creating_workflow = True | ||
|
||
# Wait until the workflow is created. | ||
print("- Waiting for the Workflow to be created...") | ||
time.sleep(backoff_delay) | ||
# Double the delay to provide exponential backoff. | ||
backoff_delay *= 2 | ||
|
||
yield WORKFLOW_ID | ||
|
||
# Delete the workflow. | ||
workflow_full_name = client.workflow_path( | ||
PROJECT_ID, LOCATION, WORKFLOW_ID | ||
) | ||
|
||
client.delete_workflow( | ||
request={ | ||
"name": workflow_full_name, | ||
} | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
pytest==8.2.0 | ||
pytest==8.3.5 | ||
backoff==2.2.1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
google-cloud-workflows==1.15.1 | ||
google-cloud-workflows==1.18.0 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you link to where you're reading this in the style guide?
As you can see in this example, the main method is included within the region tags, so we have an example of how to invoke the function. If the styleguide needs addressing, please raise this out of band.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't fit a full explanation in a short sentence, @iennae we could discuss it internally.
samples-style-guide/#result
Python examples in the Style Guide don't show any return. For example: samples-style-guide/#pattern
Removing returns creates a conflict for samples developers, as returned values help with testing. See a previous review by 'davidcavazos' here.
I've seen a pattern in the repo, which I followed, where the returned object is outside of the sample region tags (so it won't be shown in the documentation).
But if I do that, the function definition shows a type hint of a returned object, when the sample doesn't have any (it should be a None then). What I find that complies with all above is not including the function definition part, as it's in this PR. That is the reason for my decision on the region tags positions.
For instance, my samples in View the access policy of a dataset and view_dataset_access_policy.py#L19
I see 'grayside' has addressed a related matter in Issue #169
if __name__ == "__main__":
Only Java and PHP show something similar in Language-specific practices
Please check a previous review by 'davidcavazos' here and samples-style-guide/#no-cli
Review: "Supporting a CLI entry point means additional maintenance."
Guide: "Do not include CLIs for running your sample. [...] Previous practice shows that CLIs are expensive to maintain and detract from the purpose of the snippet."
In addition to external tools and CLIs such as
glogin
, I understand this part as running the sample from the Terminal by usingpython3 sample.py
as well.I see this has not been addressed as an Issue in the samples-style-guide repo.