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

Commit 0ba4e0d

Browse files
feat: Add ability to configure BuildTriggers to create Builds that require 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: https://github.com/googleapis/googleapis-gen/commit/22f5dc30b16225014527e354cc2cf0f00ff5c120
1 parent d3c9cdb commit 0ba4e0d

File tree

12 files changed

+680
-8
lines changed

12 files changed

+680
-8
lines changed

google/cloud/devtools/cloudbuild/__init__.py

+8
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@
2121
CloudBuildAsyncClient,
2222
)
2323

24+
from google.cloud.devtools.cloudbuild_v1.types.cloudbuild import ApprovalConfig
25+
from google.cloud.devtools.cloudbuild_v1.types.cloudbuild import ApprovalResult
26+
from google.cloud.devtools.cloudbuild_v1.types.cloudbuild import ApproveBuildRequest
2427
from google.cloud.devtools.cloudbuild_v1.types.cloudbuild import ArtifactResult
2528
from google.cloud.devtools.cloudbuild_v1.types.cloudbuild import Artifacts
2629
from google.cloud.devtools.cloudbuild_v1.types.cloudbuild import Build
30+
from google.cloud.devtools.cloudbuild_v1.types.cloudbuild import BuildApproval
2731
from google.cloud.devtools.cloudbuild_v1.types.cloudbuild import BuildOperationMetadata
2832
from google.cloud.devtools.cloudbuild_v1.types.cloudbuild import BuildOptions
2933
from google.cloud.devtools.cloudbuild_v1.types.cloudbuild import BuildStep
@@ -98,9 +102,13 @@
98102
__all__ = (
99103
"CloudBuildClient",
100104
"CloudBuildAsyncClient",
105+
"ApprovalConfig",
106+
"ApprovalResult",
107+
"ApproveBuildRequest",
101108
"ArtifactResult",
102109
"Artifacts",
103110
"Build",
111+
"BuildApproval",
104112
"BuildOperationMetadata",
105113
"BuildOptions",
106114
"BuildStep",

google/cloud/devtools/cloudbuild_v1/__init__.py

+8
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@
1717
from .services.cloud_build import CloudBuildClient
1818
from .services.cloud_build import CloudBuildAsyncClient
1919

20+
from .types.cloudbuild import ApprovalConfig
21+
from .types.cloudbuild import ApprovalResult
22+
from .types.cloudbuild import ApproveBuildRequest
2023
from .types.cloudbuild import ArtifactResult
2124
from .types.cloudbuild import Artifacts
2225
from .types.cloudbuild import Build
26+
from .types.cloudbuild import BuildApproval
2327
from .types.cloudbuild import BuildOperationMetadata
2428
from .types.cloudbuild import BuildOptions
2529
from .types.cloudbuild import BuildStep
@@ -73,9 +77,13 @@
7377

7478
__all__ = (
7579
"CloudBuildAsyncClient",
80+
"ApprovalConfig",
81+
"ApprovalResult",
82+
"ApproveBuildRequest",
7683
"ArtifactResult",
7784
"Artifacts",
7885
"Build",
86+
"BuildApproval",
7987
"BuildOperationMetadata",
8088
"BuildOptions",
8189
"BuildStep",

google/cloud/devtools/cloudbuild_v1/gapic_metadata.json

+10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
"grpc": {
1111
"libraryClient": "CloudBuildClient",
1212
"rpcs": {
13+
"ApproveBuild": {
14+
"methods": [
15+
"approve_build"
16+
]
17+
},
1318
"CancelBuild": {
1419
"methods": [
1520
"cancel_build"
@@ -100,6 +105,11 @@
100105
"grpc-async": {
101106
"libraryClient": "CloudBuildAsyncClient",
102107
"rpcs": {
108+
"ApproveBuild": {
109+
"methods": [
110+
"approve_build"
111+
]
112+
},
103113
"CancelBuild": {
104114
"methods": [
105115
"cancel_build"

google/cloud/devtools/cloudbuild_v1/services/cloud_build/async_client.py

+116
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,122 @@ async def retry_build(
716716
# Done; return the response.
717717
return response
718718

719+
async def approve_build(
720+
self,
721+
request: cloudbuild.ApproveBuildRequest = None,
722+
*,
723+
name: str = None,
724+
approval_result: cloudbuild.ApprovalResult = None,
725+
retry: retries.Retry = gapic_v1.method.DEFAULT,
726+
timeout: float = None,
727+
metadata: Sequence[Tuple[str, str]] = (),
728+
) -> operation_async.AsyncOperation:
729+
r"""Approves or rejects a pending build.
730+
If approved, the returned LRO will be analogous to the
731+
LRO returned from a CreateBuild call.
732+
733+
If rejected, the returned LRO will be immediately done.
734+
735+
Args:
736+
request (:class:`google.cloud.devtools.cloudbuild_v1.types.ApproveBuildRequest`):
737+
The request object. Request to approve or reject a
738+
pending build.
739+
name (:class:`str`):
740+
Required. Name of the target build. For example:
741+
"projects/{$project_id}/builds/{$build_id}"
742+
743+
This corresponds to the ``name`` field
744+
on the ``request`` instance; if ``request`` is provided, this
745+
should not be set.
746+
approval_result (:class:`google.cloud.devtools.cloudbuild_v1.types.ApprovalResult`):
747+
Approval decision and metadata.
748+
This corresponds to the ``approval_result`` field
749+
on the ``request`` instance; if ``request`` is provided, this
750+
should not be set.
751+
retry (google.api_core.retry.Retry): Designation of what errors, if any,
752+
should be retried.
753+
timeout (float): The timeout for this request.
754+
metadata (Sequence[Tuple[str, str]]): Strings which should be
755+
sent along with the request as metadata.
756+
757+
Returns:
758+
google.api_core.operation_async.AsyncOperation:
759+
An object representing a long-running operation.
760+
761+
The result type for the operation will be
762+
:class:`google.cloud.devtools.cloudbuild_v1.types.Build` A
763+
build resource in the Cloud Build API.
764+
765+
At a high level, a Build describes where to find
766+
source code, how to build it (for example, the
767+
builder image to run on the source), and where to
768+
store the built artifacts.
769+
770+
Fields can include the following variables, which
771+
will be expanded when the build is created:
772+
773+
- $PROJECT_ID: the project ID of the build.
774+
- $PROJECT_NUMBER: the project number of the build.
775+
- $BUILD_ID: the autogenerated ID of the build.
776+
- $REPO_NAME: the source repository name specified
777+
by RepoSource.
778+
- $BRANCH_NAME: the branch name specified by
779+
RepoSource.
780+
- $TAG_NAME: the tag name specified by RepoSource.
781+
- $REVISION_ID or $COMMIT_SHA: the commit SHA
782+
specified by RepoSource or resolved from the
783+
specified branch or tag.
784+
- $SHORT_SHA: first 7 characters of $REVISION_ID or
785+
$COMMIT_SHA.
786+
787+
"""
788+
# Create or coerce a protobuf request object.
789+
# Sanity check: If we got a request object, we should *not* have
790+
# gotten any keyword arguments that map to the request.
791+
has_flattened_params = any([name, approval_result])
792+
if request is not None and has_flattened_params:
793+
raise ValueError(
794+
"If the `request` argument is set, then none of "
795+
"the individual field arguments should be set."
796+
)
797+
798+
request = cloudbuild.ApproveBuildRequest(request)
799+
800+
# If we have keyword arguments corresponding to fields on the
801+
# request, apply these.
802+
if name is not None:
803+
request.name = name
804+
if approval_result is not None:
805+
request.approval_result = approval_result
806+
807+
# Wrap the RPC method; this adds retry and timeout information,
808+
# and friendly error handling.
809+
rpc = gapic_v1.method_async.wrap_method(
810+
self._client._transport.approve_build,
811+
default_timeout=None,
812+
client_info=DEFAULT_CLIENT_INFO,
813+
)
814+
815+
# Certain fields should be provided within the metadata header;
816+
# add these here.
817+
metadata = tuple(metadata) + (
818+
gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)),
819+
)
820+
821+
# Send the request.
822+
response = await rpc(request, retry=retry, timeout=timeout, metadata=metadata,)
823+
824+
# Wrap the response in an operation future.
825+
response = operation_async.from_gapic(
826+
response,
827+
self._client._transport.operations_client,
828+
cloudbuild.Build,
829+
metadata_type=cloudbuild.BuildOperationMetadata,
830+
)
831+
832+
# Done; return the response.
833+
return response
834+
719835
async def create_build_trigger(
720836
self,
721837
request: cloudbuild.CreateBuildTriggerRequest = None,

google/cloud/devtools/cloudbuild_v1/services/cloud_build/client.py

+116
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,122 @@ def retry_build(
984984
# Done; return the response.
985985
return response
986986

987+
def approve_build(
988+
self,
989+
request: cloudbuild.ApproveBuildRequest = None,
990+
*,
991+
name: str = None,
992+
approval_result: cloudbuild.ApprovalResult = None,
993+
retry: retries.Retry = gapic_v1.method.DEFAULT,
994+
timeout: float = None,
995+
metadata: Sequence[Tuple[str, str]] = (),
996+
) -> operation.Operation:
997+
r"""Approves or rejects a pending build.
998+
If approved, the returned LRO will be analogous to the
999+
LRO returned from a CreateBuild call.
1000+
1001+
If rejected, the returned LRO will be immediately done.
1002+
1003+
Args:
1004+
request (google.cloud.devtools.cloudbuild_v1.types.ApproveBuildRequest):
1005+
The request object. Request to approve or reject a
1006+
pending build.
1007+
name (str):
1008+
Required. Name of the target build. For example:
1009+
"projects/{$project_id}/builds/{$build_id}"
1010+
1011+
This corresponds to the ``name`` field
1012+
on the ``request`` instance; if ``request`` is provided, this
1013+
should not be set.
1014+
approval_result (google.cloud.devtools.cloudbuild_v1.types.ApprovalResult):
1015+
Approval decision and metadata.
1016+
This corresponds to the ``approval_result`` field
1017+
on the ``request`` instance; if ``request`` is provided, this
1018+
should not be set.
1019+
retry (google.api_core.retry.Retry): Designation of what errors, if any,
1020+
should be retried.
1021+
timeout (float): The timeout for this request.
1022+
metadata (Sequence[Tuple[str, str]]): Strings which should be
1023+
sent along with the request as metadata.
1024+
1025+
Returns:
1026+
google.api_core.operation.Operation:
1027+
An object representing a long-running operation.
1028+
1029+
The result type for the operation will be
1030+
:class:`google.cloud.devtools.cloudbuild_v1.types.Build` A
1031+
build resource in the Cloud Build API.
1032+
1033+
At a high level, a Build describes where to find
1034+
source code, how to build it (for example, the
1035+
builder image to run on the source), and where to
1036+
store the built artifacts.
1037+
1038+
Fields can include the following variables, which
1039+
will be expanded when the build is created:
1040+
1041+
- $PROJECT_ID: the project ID of the build.
1042+
- $PROJECT_NUMBER: the project number of the build.
1043+
- $BUILD_ID: the autogenerated ID of the build.
1044+
- $REPO_NAME: the source repository name specified
1045+
by RepoSource.
1046+
- $BRANCH_NAME: the branch name specified by
1047+
RepoSource.
1048+
- $TAG_NAME: the tag name specified by RepoSource.
1049+
- $REVISION_ID or $COMMIT_SHA: the commit SHA
1050+
specified by RepoSource or resolved from the
1051+
specified branch or tag.
1052+
- $SHORT_SHA: first 7 characters of $REVISION_ID or
1053+
$COMMIT_SHA.
1054+
1055+
"""
1056+
# Create or coerce a protobuf request object.
1057+
# Sanity check: If we got a request object, we should *not* have
1058+
# gotten any keyword arguments that map to the request.
1059+
has_flattened_params = any([name, approval_result])
1060+
if request is not None and has_flattened_params:
1061+
raise ValueError(
1062+
"If the `request` argument is set, then none of "
1063+
"the individual field arguments should be set."
1064+
)
1065+
1066+
# Minor optimization to avoid making a copy if the user passes
1067+
# in a cloudbuild.ApproveBuildRequest.
1068+
# There's no risk of modifying the input as we've already verified
1069+
# there are no flattened fields.
1070+
if not isinstance(request, cloudbuild.ApproveBuildRequest):
1071+
request = cloudbuild.ApproveBuildRequest(request)
1072+
# If we have keyword arguments corresponding to fields on the
1073+
# request, apply these.
1074+
if name is not None:
1075+
request.name = name
1076+
if approval_result is not None:
1077+
request.approval_result = approval_result
1078+
1079+
# Wrap the RPC method; this adds retry and timeout information,
1080+
# and friendly error handling.
1081+
rpc = self._transport._wrapped_methods[self._transport.approve_build]
1082+
1083+
# Certain fields should be provided within the metadata header;
1084+
# add these here.
1085+
metadata = tuple(metadata) + (
1086+
gapic_v1.routing_header.to_grpc_metadata((("name", request.name),)),
1087+
)
1088+
1089+
# Send the request.
1090+
response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,)
1091+
1092+
# Wrap the response in an operation future.
1093+
response = operation.from_gapic(
1094+
response,
1095+
self._transport.operations_client,
1096+
cloudbuild.Build,
1097+
metadata_type=cloudbuild.BuildOperationMetadata,
1098+
)
1099+
1100+
# Done; return the response.
1101+
return response
1102+
9871103
def create_build_trigger(
9881104
self,
9891105
request: cloudbuild.CreateBuildTriggerRequest = None,

google/cloud/devtools/cloudbuild_v1/services/cloud_build/transports/base.py

+12
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,9 @@ def _prep_wrapped_messages(self, client_info):
196196
self.retry_build: gapic_v1.method.wrap_method(
197197
self.retry_build, default_timeout=600.0, client_info=client_info,
198198
),
199+
self.approve_build: gapic_v1.method.wrap_method(
200+
self.approve_build, default_timeout=None, client_info=client_info,
201+
),
199202
self.create_build_trigger: gapic_v1.method.wrap_method(
200203
self.create_build_trigger,
201204
default_timeout=600.0,
@@ -350,6 +353,15 @@ def retry_build(
350353
]:
351354
raise NotImplementedError()
352355

356+
@property
357+
def approve_build(
358+
self,
359+
) -> Callable[
360+
[cloudbuild.ApproveBuildRequest],
361+
Union[operations_pb2.Operation, Awaitable[operations_pb2.Operation]],
362+
]:
363+
raise NotImplementedError()
364+
353365
@property
354366
def create_build_trigger(
355367
self,

google/cloud/devtools/cloudbuild_v1/services/cloud_build/transports/grpc.py

+30
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,36 @@ def retry_build(
417417
)
418418
return self._stubs["retry_build"]
419419

420+
@property
421+
def approve_build(
422+
self,
423+
) -> Callable[[cloudbuild.ApproveBuildRequest], operations_pb2.Operation]:
424+
r"""Return a callable for the approve build method over gRPC.
425+
426+
Approves or rejects a pending build.
427+
If approved, the returned LRO will be analogous to the
428+
LRO returned from a CreateBuild call.
429+
430+
If rejected, the returned LRO will be immediately done.
431+
432+
Returns:
433+
Callable[[~.ApproveBuildRequest],
434+
~.Operation]:
435+
A function that, when called, will call the underlying RPC
436+
on the server.
437+
"""
438+
# Generate a "stub function" on-the-fly which will actually make
439+
# the request.
440+
# gRPC handles serialization and deserialization, so we just need
441+
# to pass in the functions for each.
442+
if "approve_build" not in self._stubs:
443+
self._stubs["approve_build"] = self.grpc_channel.unary_unary(
444+
"/google.devtools.cloudbuild.v1.CloudBuild/ApproveBuild",
445+
request_serializer=cloudbuild.ApproveBuildRequest.serialize,
446+
response_deserializer=operations_pb2.Operation.FromString,
447+
)
448+
return self._stubs["approve_build"]
449+
420450
@property
421451
def create_build_trigger(
422452
self,

0 commit comments

Comments
 (0)