summaryrefslogtreecommitdiff
path: root/lib/gitlab/cycle_analytics/events.rb
blob: 21e295ceeb61625404a5160a12731ffcc6319b77 (plain)
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
78
79
80
81
82
83
module Gitlab
  module CycleAnalytics
    class Events
      include ActionView::Helpers::DateHelper

      def initialize(project:, options:)
        @project = project
        @fetcher = EventsFetcher.new(project: project, options: options)
      end

      def issue_events
        @fetcher.fetch(stage: :issue).each { |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).each { |event| parse_event(event, entity: :merge_request) }
      end

      def test_events
        @fetcher.fetch(stage: :test).each do |event|
          parse_build_event(event)
        end
      end

      def review_events
        @fetcher.fetch(stage: :review).each { |event| parse_event(event) }
      end

      def staging_events
        @fetcher.fetch(stage: :staging).each do |event|
          parse_build_event(event)
        end
      end

      def production_events
        @fetcher.fetch(stage: :production).each { |event| parse_event(event) }
      end

      private

      def parse_event(event, entity: :issue)
        event['url'] = Gitlab::LightUrlBuilder.build(entity: entity, project: @project, id: event['iid'].to_s)
        event['total_time'] = distance_of_time_in_words(event['total_time'].to_f)
        event['created_at'] = interval_in_words(event['created_at'])
        event['author_profile_url'] = Gitlab::LightUrlBuilder.build(entity: :user, id: event['author_username'])
        event['author_avatar_url'] = Gitlab::LightUrlBuilder.build(entity: :user_avatar, id: event['author_id'])

        event.except!('author_id', 'author_username')
      end

      def parse_build_event(event)
        build = ::Ci::Build.find(event['id'])

        #event['author_name'] = build.author.try(:name)
      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