@@ -47,6 +47,14 @@ func CreateBuildContextE(t testing.TestingT, ctx context.Context, projectID stri
4747
4848 defer func () { _ = service .Close () }()
4949
50+ return CreateBuildWithClient (ctx , service , projectID , build )
51+ }
52+
53+ // CreateBuildWithClient creates a new build blocking until the operation is complete using the
54+ // supplied *cloudbuild.Client. Prefer this variant in unit tests where the client is backed by a
55+ // mock gRPC server.
56+ // The ctx parameter supports cancellation and timeouts.
57+ func CreateBuildWithClient (ctx context.Context , service * cloudbuild.Client , projectID string , build * cloudbuildpb.Build ) (* cloudbuildpb.Build , error ) {
5058 req := & cloudbuildpb.CreateBuildRequest {
5159 ProjectId : projectID ,
5260 Build : build ,
@@ -100,6 +108,13 @@ func GetBuildContextE(t testing.TestingT, ctx context.Context, projectID string,
100108
101109 defer func () { _ = service .Close () }()
102110
111+ return GetBuildWithClient (ctx , service , projectID , buildID )
112+ }
113+
114+ // GetBuildWithClient gets the given build using the supplied *cloudbuild.Client. Prefer this variant
115+ // in unit tests where the client is backed by a mock gRPC server.
116+ // The ctx parameter supports cancellation and timeouts.
117+ func GetBuildWithClient (ctx context.Context , service * cloudbuild.Client , projectID string , buildID string ) (* cloudbuildpb.Build , error ) {
103118 req := & cloudbuildpb.GetBuildRequest {
104119 ProjectId : projectID ,
105120 Id : buildID ,
@@ -148,6 +163,14 @@ func GetBuildsContextE(t testing.TestingT, ctx context.Context, projectID string
148163
149164 defer func () { _ = service .Close () }()
150165
166+ return GetBuildsWithClient (ctx , service , projectID )
167+ }
168+
169+ // GetBuildsWithClient gets the list of builds for a given project using the supplied
170+ // *cloudbuild.Client. Prefer this variant in unit tests where the client is backed by a mock gRPC
171+ // server.
172+ // The ctx parameter supports cancellation and timeouts.
173+ func GetBuildsWithClient (ctx context.Context , service * cloudbuild.Client , projectID string ) ([]* cloudbuildpb.Build , error ) {
151174 req := & cloudbuildpb.ListBuildsRequest {
152175 ProjectId : projectID ,
153176 }
@@ -205,15 +228,34 @@ func GetBuildsForTriggerContextE(t testing.TestingT, ctx context.Context, projec
205228 return nil , fmt .Errorf ("GetBuildsForTriggerContextE.ListBuilds(%s) got error: %w" , projectID , err )
206229 }
207230
208- filteredBuilds := []* cloudbuildpb.Build {}
231+ return filterBuildsByTrigger (builds , triggerID ), nil
232+ }
233+
234+ // GetBuildsForTriggerWithClient gets a list of builds for a specific cloud build trigger using the
235+ // supplied *cloudbuild.Client. Prefer this variant in unit tests where the client is backed by a
236+ // mock gRPC server.
237+ // The ctx parameter supports cancellation and timeouts.
238+ func GetBuildsForTriggerWithClient (ctx context.Context , service * cloudbuild.Client , projectID string , triggerID string ) ([]* cloudbuildpb.Build , error ) {
239+ builds , err := GetBuildsWithClient (ctx , service , projectID )
240+ if err != nil {
241+ return nil , fmt .Errorf ("GetBuildsForTriggerContextE.ListBuilds(%s) got error: %w" , projectID , err )
242+ }
243+
244+ return filterBuildsByTrigger (builds , triggerID ), nil
245+ }
246+
247+ // filterBuildsByTrigger returns the subset of the given builds that were produced by the trigger
248+ // with the given ID.
249+ func filterBuildsByTrigger (builds []* cloudbuildpb.Build , triggerID string ) []* cloudbuildpb.Build {
250+ filtered := []* cloudbuildpb.Build {}
209251
210252 for _ , build := range builds {
211253 if build .GetBuildTriggerId () == triggerID {
212- filteredBuilds = append (filteredBuilds , build )
254+ filtered = append (filtered , build )
213255 }
214256 }
215257
216- return filteredBuilds , nil
258+ return filtered
217259}
218260
219261// NewCloudBuildService creates a new Cloud Build service, which is used to make Cloud Build API calls.
0 commit comments