@@ -50,6 +50,8 @@ type Executor interface {
5050 Start (ctx context.Context )
5151 Wait (ctx context.Context ) ([]* batches.ChangesetSpec , error )
5252
53+ CheckCache (ctx context.Context , task * Task ) (specs []* batches.ChangesetSpec , found bool , err error )
54+
5355 // LockedTaskStatuses calls the given function with the current state of
5456 // the task statuses. Before calling the function, the statuses are locked
5557 // to provide a consistent view of all statuses, but that also means the
@@ -289,6 +291,43 @@ func (x *executor) Wait(ctx context.Context) ([]*batches.ChangesetSpec, error) {
289291 return x .specs , nil
290292}
291293
294+ func (x * executor ) CheckCache (ctx context.Context , task * Task ) (specs []* batches.ChangesetSpec , found bool , err error ) {
295+ // Check if the task is cached.
296+ cacheKey := task .cacheKey ()
297+ if x .clearCache {
298+ if err = x .cache .Clear (ctx , cacheKey ); err != nil {
299+ return specs , false , errors .Wrapf (err , "clearing cache for %q" , task .Repository .Name )
300+ }
301+
302+ return specs , false , nil
303+ }
304+
305+ var result executionResult
306+ result , found , err = x .cache .Get (ctx , cacheKey )
307+ if err != nil {
308+ return specs , false , errors .Wrapf (err , "checking cache for %q" , task .Repository .Name )
309+ }
310+
311+ if ! found {
312+ return specs , false , nil
313+ }
314+
315+ // If the cached result resulted in an empty diff, we don't need to
316+ // add it to the list of specs that are displayed to the user and
317+ // send to the server. Instead, we can just report that the task is
318+ // complete and move on.
319+ if result .Diff == "" {
320+ return specs , true , nil
321+ }
322+
323+ specs , err = createChangesetSpecs (task , result , x .features )
324+ if err != nil {
325+ return specs , false , err
326+ }
327+
328+ return specs , true , nil
329+ }
330+
292331func (x * executor ) do (ctx context.Context , task * Task ) (err error ) {
293332 // Ensure that the status is updated when we're done.
294333 defer func () {
@@ -349,7 +388,7 @@ func (x *executor) do(ctx context.Context, task *Task) (err error) {
349388 })
350389
351390 // Add the spec to the executor's list of completed specs.
352- if err := x .addCompletedSpecs (task . Repository , specs ); err != nil {
391+ if err := x .addCompletedSpecs (specs ); err != nil {
353392 return err
354393 }
355394
@@ -429,7 +468,7 @@ func (x *executor) do(ctx context.Context, task *Task) (err error) {
429468 status .ChangesetSpecs = specs
430469 })
431470
432- if err := x .addCompletedSpecs (task . Repository , specs ); err != nil {
471+ if err := x .addCompletedSpecs (specs ); err != nil {
433472 return err
434473 }
435474
@@ -446,7 +485,7 @@ func (x *executor) updateTaskStatus(task *Task, update func(status *TaskStatus))
446485 }
447486}
448487
449- func (x * executor ) addCompletedSpecs (repository * graphql. Repository , specs []* batches.ChangesetSpec ) error {
488+ func (x * executor ) addCompletedSpecs (specs []* batches.ChangesetSpec ) error {
450489 x .specsMu .Lock ()
451490 defer x .specsMu .Unlock ()
452491
0 commit comments