Class: PushEventPayloadService
- Inherits:
-
Object
- Object
- PushEventPayloadService
- Defined in:
- app/services/push_event_payload_service.rb
Overview
Service class for creating push event payloads as stored in the “push_event_payloads” table.
Example:
data = Gitlab::DataBuilder::Push.build(...)
event = Event.create(...)
PushEventPayloadService.new(event, data).execute
Instance Method Summary collapse
- #action ⇒ Object
- #commit_count ⇒ Object
- #commit_from_id ⇒ Object
-
#commit_title ⇒ Object
Returns the commit title to use.
- #commit_to_id ⇒ Object
- #create? ⇒ Boolean
-
#execute ⇒ Object
Creates and returns a new PushEventPayload row.
-
#initialize(event, push_data) ⇒ PushEventPayloadService
constructor
event - The event this push payload belongs to.
- #ref ⇒ Object
- #ref_type ⇒ Object
- #remove? ⇒ Boolean
- #revision_after ⇒ Object
- #revision_before ⇒ Object
- #trimmed_ref ⇒ Object
Constructor Details
#initialize(event, push_data) ⇒ PushEventPayloadService
event - The event this push payload belongs to. push_data - A Hash produced by ‘Gitlab::DataBuilder::Push.build` to use for
building the push payload.
16 17 18 19 |
# File 'app/services/push_event_payload_service.rb', line 16 def initialize(event, push_data) @event = event @push_data = push_data end |
Instance Method Details
#action ⇒ Object
105 106 107 108 109 110 111 112 113 |
# File 'app/services/push_event_payload_service.rb', line 105 def action if create? :created elsif remove? :removed else :pushed end end |
#commit_count ⇒ Object
77 78 79 |
# File 'app/services/push_event_payload_service.rb', line 77 def commit_count @push_data.fetch(:total_commits_count) end |
#commit_from_id ⇒ Object
61 62 63 64 65 66 67 |
# File 'app/services/push_event_payload_service.rb', line 61 def commit_from_id if create? nil else revision_before end end |
#commit_title ⇒ Object
Returns the commit title to use.
The commit title is limited to the first line and a maximum of 70 characters.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'app/services/push_event_payload_service.rb', line 46 def commit_title commit = @push_data.fetch(:commits).last return unless commit && commit[:message] raw_msg = commit[:message] # Find where the first line ends, without turning the entire message into an # Array of lines (this is a waste of memory for large commit messages). index = raw_msg.index("\n") = index ? raw_msg[0..index] : raw_msg .strip.truncate(70) end |
#commit_to_id ⇒ Object
69 70 71 72 73 74 75 |
# File 'app/services/push_event_payload_service.rb', line 69 def commit_to_id if remove? nil else revision_after end end |
#create? ⇒ Boolean
97 98 99 |
# File 'app/services/push_event_payload_service.rb', line 97 def create? Gitlab::Git.blank_ref?(revision_before) end |
#execute ⇒ Object
Creates and returns a new PushEventPayload row.
This method will raise upon encountering validation errors.
Returns an instance of PushEventPayload.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'app/services/push_event_payload_service.rb', line 26 def execute @event.build_push_event_payload( commit_count: commit_count, action: action, ref_type: ref_type, commit_from: commit_from_id, commit_to: commit_to_id, ref: trimmed_ref, commit_title: commit_title, event_id: @event.id ) @event.push_event_payload.save! @event.push_event_payload end |
#ref ⇒ Object
81 82 83 |
# File 'app/services/push_event_payload_service.rb', line 81 def ref @push_data.fetch(:ref) end |
#ref_type ⇒ Object
115 116 117 118 119 120 121 |
# File 'app/services/push_event_payload_service.rb', line 115 def ref_type if Gitlab::Git.tag_ref?(ref) :tag else :branch end end |
#remove? ⇒ Boolean
101 102 103 |
# File 'app/services/push_event_payload_service.rb', line 101 def remove? Gitlab::Git.blank_ref?(revision_after) end |
#revision_after ⇒ Object
89 90 91 |
# File 'app/services/push_event_payload_service.rb', line 89 def revision_after @push_data.fetch(:after) end |
#revision_before ⇒ Object
85 86 87 |
# File 'app/services/push_event_payload_service.rb', line 85 def revision_before @push_data.fetch(:before) end |