Class: PushEventPayloadService

Inherits:
Object
  • Object
show all
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

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

#actionObject



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_countObject



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_idObject



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_titleObject

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")
  message = index ? raw_msg[0..index] : raw_msg

  message.strip.truncate(70)
end

#commit_to_idObject



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

Returns:

  • (Boolean)


97
98
99
# File 'app/services/push_event_payload_service.rb', line 97

def create?
  Gitlab::Git.blank_ref?(revision_before)
end

#executeObject

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

#refObject



81
82
83
# File 'app/services/push_event_payload_service.rb', line 81

def ref
  @push_data.fetch(:ref)
end

#ref_typeObject



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

Returns:

  • (Boolean)


101
102
103
# File 'app/services/push_event_payload_service.rb', line 101

def remove?
  Gitlab::Git.blank_ref?(revision_after)
end

#revision_afterObject



89
90
91
# File 'app/services/push_event_payload_service.rb', line 89

def revision_after
  @push_data.fetch(:after)
end

#revision_beforeObject



85
86
87
# File 'app/services/push_event_payload_service.rb', line 85

def revision_before
  @push_data.fetch(:before)
end

#trimmed_refObject



93
94
95
# File 'app/services/push_event_payload_service.rb', line 93

def trimmed_ref
  Gitlab::Git.ref_name(ref)
end