diff options
author | Oswaldo Ferreira <oswaldo@gitlab.com> | 2019-05-31 23:18:27 +0300 |
---|---|---|
committer | Oswaldo Ferreira <oswaldo@gitlab.com> | 2019-06-01 01:16:01 +0300 |
commit | 4246a62118d919e62b94d75eba641ed374c3f241 (patch) | |
tree | 37ef42f76b66b642c88edf3f2a24da34a0f24047 /app/services | |
parent | 96db70a4448fd1e736c10100dccf3a803ec553c0 (diff) |
Add payload to the service response
This introduces payload to the ServiceResponse with
the merge ref HEAD commit data
Diffstat (limited to 'app/services')
-rw-r--r-- | app/services/merge_requests/mergeability_check_service.rb | 34 | ||||
-rw-r--r-- | app/services/service_response.rb | 15 |
2 files changed, 39 insertions, 10 deletions
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 |