Skip to content
This repository has been archived by the owner on Dec 31, 2023. It is now read-only.

Commit

Permalink
feat: Add ability to configure BuildTriggers to create Builds that re…
Browse files Browse the repository at this point in the history
…quire approval before executing and ApproveBuild API to approve or reject pending Builds (#147)

Comitter: @joonlim
PiperOrigin-RevId: 391072303

Source-Link: googleapis/googleapis@7fec729

Source-Link: googleapis/googleapis-gen@22f5dc3
  • Loading branch information
gcf-owl-bot[bot] committed Aug 17, 2021
1 parent d3c9cdb commit 0ba4e0d
Show file tree
Hide file tree
Showing 12 changed files with 680 additions and 8 deletions.
8 changes: 8 additions & 0 deletions google/cloud/devtools/cloudbuild/__init__.py
Expand Up @@ -21,9 +21,13 @@
CloudBuildAsyncClient,
)

from google.cloud.devtools.cloudbuild_v1.types.cloudbuild import ApprovalConfig
from google.cloud.devtools.cloudbuild_v1.types.cloudbuild import ApprovalResult
from google.cloud.devtools.cloudbuild_v1.types.cloudbuild import ApproveBuildRequest
from google.cloud.devtools.cloudbuild_v1.types.cloudbuild import ArtifactResult
from google.cloud.devtools.cloudbuild_v1.types.cloudbuild import Artifacts
from google.cloud.devtools.cloudbuild_v1.types.cloudbuild import Build
from google.cloud.devtools.cloudbuild_v1.types.cloudbuild import BuildApproval
from google.cloud.devtools.cloudbuild_v1.types.cloudbuild import BuildOperationMetadata
from google.cloud.devtools.cloudbuild_v1.types.cloudbuild import BuildOptions
from google.cloud.devtools.cloudbuild_v1.types.cloudbuild import BuildStep
Expand Down Expand Up @@ -98,9 +102,13 @@
__all__ = (
"CloudBuildClient",
"CloudBuildAsyncClient",
"ApprovalConfig",
"ApprovalResult",
"ApproveBuildRequest",
"ArtifactResult",
"Artifacts",
"Build",
"BuildApproval",
"BuildOperationMetadata",
"BuildOptions",
"BuildStep",
Expand Down
8 changes: 8 additions & 0 deletions google/cloud/devtools/cloudbuild_v1/__init__.py
Expand Up @@ -17,9 +17,13 @@
from .services.cloud_build import CloudBuildClient
from .services.cloud_build import CloudBuildAsyncClient

from .types.cloudbuild import ApprovalConfig
from .types.cloudbuild import ApprovalResult
from .types.cloudbuild import ApproveBuildRequest
from .types.cloudbuild import ArtifactResult
from .types.cloudbuild import Artifacts
from .types.cloudbuild import Build
from .types.cloudbuild import BuildApproval
from .types.cloudbuild import BuildOperationMetadata
from .types.cloudbuild import BuildOptions
from .types.cloudbuild import BuildStep
Expand Down Expand Up @@ -73,9 +77,13 @@

__all__ = (
"CloudBuildAsyncClient",
"ApprovalConfig",
"ApprovalResult",
"ApproveBuildRequest",
"ArtifactResult",
"Artifacts",
"Build",
"BuildApproval",
"BuildOperationMetadata",
"BuildOptions",
"BuildStep",
Expand Down
10 changes: 10 additions & 0 deletions google/cloud/devtools/cloudbuild_v1/gapic_metadata.json
Expand Up @@ -10,6 +10,11 @@
"grpc": {
"libraryClient": "CloudBuildClient",
"rpcs": {
"ApproveBuild": {
"methods": [
"approve_build"
]
},
"CancelBuild": {
"methods": [
"cancel_build"
Expand Down Expand Up @@ -100,6 +105,11 @@
"grpc-async": {
"libraryClient": "CloudBuildAsyncClient",
"rpcs": {
"ApproveBuild": {
"methods": [
"approve_build"
]
},
"CancelBuild": {
"methods": [
"cancel_build"
Expand Down
Expand Up @@ -716,6 +716,122 @@ async def retry_build(
# Done; return the response.
return response

async def approve_build(
self,
request: cloudbuild.ApproveBuildRequest = None,
*,
name: str = None,
approval_result: cloudbuild.ApprovalResult = None,
retry: retries.Retry = gapic_v1.method.DEFAULT,
timeout: float = None,
metadata: Sequence[Tuple[str, str]] = (),
) -> operation_async.AsyncOperation:
r"""Approves or rejects a pending build.
If approved, the returned LRO will be analogous to the
LRO returned from a CreateBuild call.
If rejected, the returned LRO will be immediately done.
Args:
request (:class:`google.cloud.devtools.cloudbuild_v1.types.ApproveBuildRequest`):
The request object. Request to approve or reject a
pending build.
name (:class:`str`):
Required. Name of the target build. For example:
"projects/{$project_id}/builds/{$build_id}"
This corresponds to the ``name`` field
on the ``request`` instance; if ``request`` is provided, this
should not be set.
approval_result (:class:`google.cloud.devtools.cloudbuild_v1.types.ApprovalResult`):
Approval decision and metadata.
This corresponds to the ``approval_result`` field
on the ``request`` instance; if ``request`` is provided, this
should not be set.
retry (google.api_core.retry.Retry): Designation of what errors, if any,
should be retried.
timeout (float): The timeout for this request.
metadata (Sequence[Tuple[str, str]]): Strings which should be
sent along with the request as metadata.
Returns:
google.api_core.operation_async.AsyncOperation:
An object representing a long-running operation.
The result type for the operation will be
:class:`google.cloud.devtools.cloudbuild_v1.types.Build` A
build resource in the Cloud Build API.
At a high level, a Build describes where to find
source code, how to build it (for example, the
builder image to run on the source), and where to
store the built artifacts.
Fields can include the following variables, which
will be expanded when the build is created:
- $PROJECT_ID: the project ID of the build.
- $PROJECT_NUMBER: the project number of the build.
- $BUILD_ID: the autogenerated ID of the build.
- $REPO_NAME: the source repository name specified
by RepoSource.
- $BRANCH_NAME: the branch name specified by
RepoSource.
- $TAG_NAME: the tag name specified by RepoSource.
- $REVISION_ID or $COMMIT_SHA: the commit SHA
specified by RepoSource or resolved from the
specified branch or tag.
- $SHORT_SHA: first 7 characters of $REVISION_ID or
$COMMIT_SHA.
"""
# Create or coerce a protobuf request object.
# Sanity check: If we got a request object, we should *not* have
# gotten any keyword arguments that map to the request.
has_flattened_params = any([name, approval_result])
if request is not None and has_flattened_params:
raise ValueError(
"If the `request` argument is set, then none of "
"the individual field arguments should be set."
)

request = cloudbuild.ApproveBuildRequest(request)

# If we have keyword arguments corresponding to fields on the
# request, apply these.
if name is not None:
request.name = name
if approval_result is not None:
request.approval_result = approval_result

# Wrap the RPC method; this adds retry and timeout information,
# and friendly error handling.
rpc = gapic_v1.method_async.wrap_method(
self._client._transport.approve_build,
default_timeout=None,
client_info=DEFAULT_CLIENT_INFO,
)

# Certain fields should be provided within the metadata header;
# add these here.
metadata = tuple(metadata) + (
gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)),
)

# Send the request.
response = await rpc(request, retry=retry, timeout=timeout, metadata=metadata,)

# Wrap the response in an operation future.
response = operation_async.from_gapic(
response,
self._client._transport.operations_client,
cloudbuild.Build,
metadata_type=cloudbuild.BuildOperationMetadata,
)

# Done; return the response.
return response

async def create_build_trigger(
self,
request: cloudbuild.CreateBuildTriggerRequest = None,
Expand Down
116 changes: 116 additions & 0 deletions google/cloud/devtools/cloudbuild_v1/services/cloud_build/client.py
Expand Up @@ -984,6 +984,122 @@ def retry_build(
# Done; return the response.
return response

def approve_build(
self,
request: cloudbuild.ApproveBuildRequest = None,
*,
name: str = None,
approval_result: cloudbuild.ApprovalResult = None,
retry: retries.Retry = gapic_v1.method.DEFAULT,
timeout: float = None,
metadata: Sequence[Tuple[str, str]] = (),
) -> operation.Operation:
r"""Approves or rejects a pending build.
If approved, the returned LRO will be analogous to the
LRO returned from a CreateBuild call.
If rejected, the returned LRO will be immediately done.
Args:
request (google.cloud.devtools.cloudbuild_v1.types.ApproveBuildRequest):
The request object. Request to approve or reject a
pending build.
name (str):
Required. Name of the target build. For example:
"projects/{$project_id}/builds/{$build_id}"
This corresponds to the ``name`` field
on the ``request`` instance; if ``request`` is provided, this
should not be set.
approval_result (google.cloud.devtools.cloudbuild_v1.types.ApprovalResult):
Approval decision and metadata.
This corresponds to the ``approval_result`` field
on the ``request`` instance; if ``request`` is provided, this
should not be set.
retry (google.api_core.retry.Retry): Designation of what errors, if any,
should be retried.
timeout (float): The timeout for this request.
metadata (Sequence[Tuple[str, str]]): Strings which should be
sent along with the request as metadata.
Returns:
google.api_core.operation.Operation:
An object representing a long-running operation.
The result type for the operation will be
:class:`google.cloud.devtools.cloudbuild_v1.types.Build` A
build resource in the Cloud Build API.
At a high level, a Build describes where to find
source code, how to build it (for example, the
builder image to run on the source), and where to
store the built artifacts.
Fields can include the following variables, which
will be expanded when the build is created:
- $PROJECT_ID: the project ID of the build.
- $PROJECT_NUMBER: the project number of the build.
- $BUILD_ID: the autogenerated ID of the build.
- $REPO_NAME: the source repository name specified
by RepoSource.
- $BRANCH_NAME: the branch name specified by
RepoSource.
- $TAG_NAME: the tag name specified by RepoSource.
- $REVISION_ID or $COMMIT_SHA: the commit SHA
specified by RepoSource or resolved from the
specified branch or tag.
- $SHORT_SHA: first 7 characters of $REVISION_ID or
$COMMIT_SHA.
"""
# Create or coerce a protobuf request object.
# Sanity check: If we got a request object, we should *not* have
# gotten any keyword arguments that map to the request.
has_flattened_params = any([name, approval_result])
if request is not None and has_flattened_params:
raise ValueError(
"If the `request` argument is set, then none of "
"the individual field arguments should be set."
)

# Minor optimization to avoid making a copy if the user passes
# in a cloudbuild.ApproveBuildRequest.
# There's no risk of modifying the input as we've already verified
# there are no flattened fields.
if not isinstance(request, cloudbuild.ApproveBuildRequest):
request = cloudbuild.ApproveBuildRequest(request)
# If we have keyword arguments corresponding to fields on the
# request, apply these.
if name is not None:
request.name = name
if approval_result is not None:
request.approval_result = approval_result

# Wrap the RPC method; this adds retry and timeout information,
# and friendly error handling.
rpc = self._transport._wrapped_methods[self._transport.approve_build]

# Certain fields should be provided within the metadata header;
# add these here.
metadata = tuple(metadata) + (
gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)),
)

# Send the request.
response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,)

# Wrap the response in an operation future.
response = operation.from_gapic(
response,
self._transport.operations_client,
cloudbuild.Build,
metadata_type=cloudbuild.BuildOperationMetadata,
)

# Done; return the response.
return response

def create_build_trigger(
self,
request: cloudbuild.CreateBuildTriggerRequest = None,
Expand Down
Expand Up @@ -196,6 +196,9 @@ def _prep_wrapped_messages(self, client_info):
self.retry_build: gapic_v1.method.wrap_method(
self.retry_build, default_timeout=600.0, client_info=client_info,
),
self.approve_build: gapic_v1.method.wrap_method(
self.approve_build, default_timeout=None, client_info=client_info,
),
self.create_build_trigger: gapic_v1.method.wrap_method(
self.create_build_trigger,
default_timeout=600.0,
Expand Down Expand Up @@ -350,6 +353,15 @@ def retry_build(
]:
raise NotImplementedError()

@property
def approve_build(
self,
) -> Callable[
[cloudbuild.ApproveBuildRequest],
Union[operations_pb2.Operation, Awaitable[operations_pb2.Operation]],
]:
raise NotImplementedError()

@property
def create_build_trigger(
self,
Expand Down
Expand Up @@ -417,6 +417,36 @@ def retry_build(
)
return self._stubs["retry_build"]

@property
def approve_build(
self,
) -> Callable[[cloudbuild.ApproveBuildRequest], operations_pb2.Operation]:
r"""Return a callable for the approve build method over gRPC.
Approves or rejects a pending build.
If approved, the returned LRO will be analogous to the
LRO returned from a CreateBuild call.
If rejected, the returned LRO will be immediately done.
Returns:
Callable[[~.ApproveBuildRequest],
~.Operation]:
A function that, when called, will call the underlying RPC
on the server.
"""
# Generate a "stub function" on-the-fly which will actually make
# the request.
# gRPC handles serialization and deserialization, so we just need
# to pass in the functions for each.
if "approve_build" not in self._stubs:
self._stubs["approve_build"] = self.grpc_channel.unary_unary(
"/google.devtools.cloudbuild.v1.CloudBuild/ApproveBuild",
request_serializer=cloudbuild.ApproveBuildRequest.serialize,
response_deserializer=operations_pb2.Operation.FromString,
)
return self._stubs["approve_build"]

@property
def create_build_trigger(
self,
Expand Down

0 comments on commit 0ba4e0d

Please sign in to comment.