1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
module Gitlab
module CycleAnalytics
class Events
def initialize(project:, options:)
@project = project
@fetcher = EventsFetcher.new(project: project, options: options)
end
def issue_events
@fetcher.fetch(stage: :issue).map { |event| parse_event(event) }
end
def plan_events
@fetcher.fetch(stage: :plan).each do |event|
event['total_time'] = distance_of_time_in_words(event['total_time'].to_f)
commit = first_time_reference_commit(event.delete('commits'), event)
event['title'] = commit.title
event['url'] = Gitlab::LightUrlBuilder.build(entity: :commit, project: @project, id: commit.id)
event['sha'] = commit.short_id
event['author_name'] = commit.author.name
event['author_profile_url'] = Gitlab::LightUrlBuilder.build(entity: :user, id: commit.author.username)
event['author_avatar_url'] = Gitlab::LightUrlBuilder.build(entity: :user_avatar, id: commit.author.id)
end
end
def code_events
@fetcher.fetch(stage: :code).map { |event| parse_event(event, entity: :merge_request) }
end
def test_events
@fetcher.fetch(stage: :test).map do |event|
parse_build_event(event)
end
end
def review_events
@fetcher.fetch(stage: :review).map { |event| parse_event(event, entity: :merge_request) }
end
def staging_events
@fetcher.fetch(stage: :staging).map do |event|
parse_build_event(event)
end
end
def production_events
@fetcher.fetch(stage: :production).map { |event| parse_event(event) }
end
private
def parse_event(event, entity: :issue)
event['author'] = User.find(event.delete('author_id'))
AnalyticsGenericSerializer.new(project: @project, entity: entity).represent(event).as_json
end
def parse_build_event(event)
build = ::Ci::Build.find(event['id'])
AnalyticsBuildSerializer.new.represent(build).as_json
end
def first_time_reference_commit(commits, event)
st_commit = YAML.load(commits).detect do |commit|
commit['created_at'] == event['first_mentioned_in_commit_at']
end
Commit.new(Gitlab::Git::Commit.new(st_commit), @project)
end
def interval_in_words(diff)
"#{distance_of_time_in_words(diff.to_f)} ago"
end
end
end
end
|