Skip to content

feat(workflows): Add client library sample to get a workflow execution - #14803

Open
iennae wants to merge 1 commit into
refactor/workflows-cloud-client-packagefrom
feat/workflows-get-execution
Open

iennae wants to merge 1 commit into
refactor/workflows-cloud-client-packagefrom
feat/workflows-get-execution

Conversation

@iennae

@iennae iennae commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

Description

Callers that trigger workflows asynchronously or inspect past runs need to fetch execution state and output by execution ID.

Adds get_execution.py (workflows_get_execution) using executions_v1.ExecutionsClient.get_execution, with unit and gated live integration tests.

Note: Stacked on #14797 (refactor/workflows-cloud-client-package) so this PR shows only this sample's diff.

Checklist

Testing

  • I have tested this change on a live environment and verified it works as intended. Unit and live integration tests pass on Python 3.13.

Compliance & Style


Post-Approval Actions

  • Please merge this PR for me once it is approved

@iennae
iennae requested review from a team as code owners November 14, 2026 00:21
@snippet-bot

snippet-bot Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

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

@product-auto-label product-auto-label Bot added api: workflows Issues related to the Workflows API. samples Issues that are directly related to samples. labels Nov 14, 2026

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

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.

Code Review

This pull request introduces a new sample script get_execution.py and its corresponding tests to retrieve Google Cloud Workflows execution details. The feedback suggests using the client library's built-in execution_path helper method instead of manual string formatting, mocking this method in the unit tests, and conditionally printing the Result and Error fields to prevent confusing output when they are empty.

Comment on lines +34 to +37
name = (
f"projects/{project_id}/locations/{location_id}/workflows/{workflow_id}"
f"/executions/{execution_id}"
)

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

Use the client library's built-in execution_path helper method to construct the resource name instead of manual string formatting. This is more robust, idiomatic, and prevents potential formatting errors.

    name = client.execution_path(
        project=project_id,
        location=location_id,
        workflow=workflow_id,
        execution=execution_id,
    )

Comment on lines +54 to +58
print(f"Resource ID: {execution.name}")
# The result is set when the execution succeeds; the error when it fails.
print(f"Error: {execution.error.payload}")
print(f"Result: {execution.result}")
print(f"State: {execution.state.name}")

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

Conditionally print the Result and Error fields only when they are populated. Printing empty/unset fields (e.g., printing Error: for a successful execution or Result: for a failed execution) can be confusing to users running the sample.

Suggested change
print(f"Resource ID: {execution.name}")
# The result is set when the execution succeeds; the error when it fails.
print(f"Error: {execution.error.payload}")
print(f"Result: {execution.result}")
print(f"State: {execution.state.name}")
print(f"Resource ID: {execution.name}")
print(f"State: {execution.state.name}")
if execution.result:
print(f"Result: {execution.result}")
if execution.error and execution.error.payload:
print(f"Error: {execution.error.payload}")

Comment on lines +39 to +44
client = mock_client_class.return_value
client.get_execution.return_value = executions_v1.Execution(
name=EXECUTION_NAME,
state=executions_v1.Execution.State.SUCCEEDED,
result='"done"',
)

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

Mock the execution_path method on the client to return the expected execution name. This ensures the unit test remains correct and passes when using the client's built-in path helper.

Suggested change
client = mock_client_class.return_value
client.get_execution.return_value = executions_v1.Execution(
name=EXECUTION_NAME,
state=executions_v1.Execution.State.SUCCEEDED,
result='"done"',
)
client = mock_client_class.return_value
client.execution_path.return_value = EXECUTION_NAME
client.get_execution.return_value = executions_v1.Execution(
name=EXECUTION_NAME,
state=executions_v1.Execution.State.SUCCEEDED,
result='"done"',
)

Polling or auditing an execution created asynchronously requires fetching its state, result, and error payload by execution ID. Adding `workflows_get_execution` covers `executions_v1.ExecutionsClient.get_execution` with `NotFound` handling.
@iennae
iennae force-pushed the feat/workflows-get-execution branch from 2857542 to 80308e3 Compare November 14, 2026 00:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: workflows Issues related to the Workflows API. samples Issues that are directly related to samples.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant