diff --git a/app/models/ci/pipeline.rb b/app/models/ci/pipeline.rb
index 72b09c12e013c169aac041a252bc4d933c45fc27..8d43f3051eefcb1b421abda9382b007e917f93f3 100644
--- a/app/models/ci/pipeline.rb
+++ b/app/models/ci/pipeline.rb
@@ -234,9 +234,7 @@ module Ci
     end
 
     def mark_as_processable_after_stage(stage_idx)
-      builds.skipped
-        .where('stage_idx > ?', stage_idx)
-        .find_each(&:process)
+      builds.skipped.after_stage(stage_idx).find_each(&:process)
     end
 
     def latest?
diff --git a/app/models/commit_status.rb b/app/models/commit_status.rb
index 9547c57b2ae3374dd2b5f040cf07a074af09a193..99a6326309d0217056af2e45e2687ff4b05b1d61 100644
--- a/app/models/commit_status.rb
+++ b/app/models/commit_status.rb
@@ -23,9 +23,6 @@ class CommitStatus < ActiveRecord::Base
     where(id: max_id.group(:name, :commit_id))
   end
 
-  scope :retried, -> { where.not(id: latest) }
-  scope :ordered, -> { order(:name) }
-
   scope :failed_but_allowed, -> do
     where(allow_failure: true, status: [:failed, :canceled])
   end
@@ -36,8 +33,11 @@ class CommitStatus < ActiveRecord::Base
       false, all_state_names - [:failed, :canceled])
   end
 
+  scope :retried, -> { where.not(id: latest) }
+  scope :ordered, -> { order(:name) }
   scope :latest_ordered, -> { latest.ordered.includes(project: :namespace) }
   scope :retried_ordered, -> { retried.ordered.includes(project: :namespace) }
+  scope :after_stage, -> (index) { where('stage_idx > ?', index) }
 
   state_machine :status do
     event :enqueue do
diff --git a/app/services/ci/retry_pipeline_service.rb b/app/services/ci/retry_pipeline_service.rb
index fd180b2c624e20ed9ee1e610aed186b3effd81fd..b062c50e82dcf6bec7f69649dda960a2f4bd283c 100644
--- a/app/services/ci/retry_pipeline_service.rb
+++ b/app/services/ci/retry_pipeline_service.rb
@@ -11,13 +11,18 @@ module Ci
       # Reprocess builds in subsequent stages
       #
       pipeline.builds
-        .where('stage_idx > ?', resume_stage.index)
+        .after_stage(resume_stage.index)
         .failed_or_canceled.find_each do |build|
           Ci::RetryBuildService
             .new(project, current_user)
             .reprocess(build)
         end
 
+      ##
+      # Mark skipped builds as processable again
+      #
+      pipeline.mark_as_processable_after_stage(resume_stage.index)
+
       ##
       # Retry builds in the first unsuccessful stage
       #
@@ -26,11 +31,6 @@ module Ci
           .new(project, current_user)
           .retry(build)
       end
-
-      ##
-      # Mark skipped builds as processable again
-      #
-      pipeline.mark_as_processable_after_stage(resume_stage.index)
     end
 
     private