summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorOswaldo Ferreira <oswaldo@gitlab.com>2019-05-31 17:18:27 -0300
committerOswaldo Ferreira <oswaldo@gitlab.com>2019-05-31 19:16:01 -0300
commit4246a62118d919e62b94d75eba641ed374c3f241 (patch)
tree37ef42f76b66b642c88edf3f2a24da34a0f24047 /app
parent96db70a4448fd1e736c10100dccf3a803ec553c0 (diff)
downloadgitlab-ce-4246a62118d919e62b94d75eba641ed374c3f241.tar.gz
Add payload to the service response
This introduces payload to the ServiceResponse with the merge ref HEAD commit data
Diffstat (limited to 'app')
-rw-r--r--app/models/merge_request.rb4
-rw-r--r--app/services/merge_requests/mergeability_check_service.rb34
-rw-r--r--app/services/service_response.rb15
3 files changed, 39 insertions, 14 deletions
diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb
index 38b9b886e5f..cf63a7e79bd 100644
--- a/app/models/merge_request.rb
+++ b/app/models/merge_request.rb
@@ -1080,10 +1080,6 @@ class MergeRequest < ApplicationRecord
# Returns the current merge-ref HEAD commit.
#
- # Consider calling mergeability_check method _before_ this if you need
- # the latest possible version of it (it's already automatically updated
- # along the merge_status).
- #
def merge_ref_head
project.repository.commit(merge_ref_path)
end
diff --git a/app/services/merge_requests/mergeability_check_service.rb b/app/services/merge_requests/mergeability_check_service.rb
index d277d38127c..ef833774e65 100644
--- a/app/services/merge_requests/mergeability_check_service.rb
+++ b/app/services/merge_requests/mergeability_check_service.rb
@@ -2,6 +2,8 @@
module MergeRequests
class MergeabilityCheckService < ::BaseService
+ include Gitlab::Utils::StrongMemoize
+
delegate :project, to: :@merge_request
delegate :repository, to: :project
@@ -16,8 +18,8 @@ module MergeRequests
# and the merge-ref is synced. Success in case of being/becoming mergeable,
# error otherwise.
def execute
- return ServiceResponse.error('Invalid argument') unless merge_request
- return ServiceResponse.error('Unsupported operation') if Gitlab::Database.read_only?
+ return ServiceResponse.error(message: 'Invalid argument') unless merge_request
+ return ServiceResponse.error(message: 'Unsupported operation') if Gitlab::Database.read_only?
update_merge_status
@@ -25,13 +27,39 @@ module MergeRequests
return ServiceResponse.error(message: 'Merge request is not mergeable')
end
- ServiceResponse.success
+ unless payload.fetch(:merge_ref_head)
+ return ServiceResponse.error(message: 'Merge ref was not found')
+ end
+
+ ServiceResponse.success(payload: payload)
end
private
attr_reader :merge_request
+ def payload
+ strong_memoize(:payload) do
+ {
+ merge_ref_head: merge_ref_head_payload
+ }
+ end
+ end
+
+ def merge_ref_head_payload
+ commit = merge_request.merge_ref_head
+
+ return unless commit
+
+ target_id, source_id = commit.parent_ids
+
+ {
+ commit_id: commit.id,
+ source_id: source_id,
+ target_id: target_id
+ }
+ end
+
def update_merge_status
return unless merge_request.recheck_merge_status?
diff --git a/app/services/service_response.rb b/app/services/service_response.rb
index 1de30e68d87..f3437ba16de 100644
--- a/app/services/service_response.rb
+++ b/app/services/service_response.rb
@@ -1,19 +1,20 @@
# frozen_string_literal: true
class ServiceResponse
- def self.success(message: nil)
- new(status: :success, message: message)
+ def self.success(message: nil, payload: {})
+ new(status: :success, message: message, payload: payload)
end
- def self.error(message:, http_status: nil)
- new(status: :error, message: message, http_status: http_status)
+ def self.error(message:, payload: {}, http_status: nil)
+ new(status: :error, message: message, payload: payload, http_status: http_status)
end
- attr_reader :status, :message, :http_status
+ attr_reader :status, :message, :http_status, :payload
- def initialize(status:, message: nil, http_status: nil)
+ def initialize(status:, message: nil, payload: {}, http_status: nil)
self.status = status
self.message = message
+ self.payload = payload
self.http_status = http_status
end
@@ -27,5 +28,5 @@ class ServiceResponse
private
- attr_writer :status, :message, :http_status
+ attr_writer :status, :message, :http_status, :payload
end