summaryrefslogtreecommitdiff
path: root/app/services/git/base_hooks_service.rb
blob: 9d371e234eeb85c6159a54e58a1a1e390c397e4c (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
84
85
86
87
88
89
90
91
92
93
94
95
96
# frozen_string_literal: true

module Git
  class BaseHooksService < ::BaseService
    include Gitlab::Utils::StrongMemoize

    # The N most recent commits to process in a single push payload.
    PROCESS_COMMIT_LIMIT = 100

    def execute
      project.repository.after_create if project.empty_repo?

      create_events
      create_pipelines
      execute_project_hooks

      # Not a hook, but it needs access to the list of changed commits
      enqueue_invalidate_cache

      push_data
    end

    private

    def hook_name
      raise NotImplementedError, "Please implement #{self.class}##{__method__}"
    end

    def commits
      raise NotImplementedError, "Please implement #{self.class}##{__method__}"
    end

    def limited_commits
      commits.last(PROCESS_COMMIT_LIMIT)
    end

    def commits_count
      commits.count
    end

    def event_message
      nil
    end

    def invalidated_file_types
      []
    end

    def create_events
      EventCreateService.new.push(project, current_user, push_data)
    end

    def create_pipelines
      return unless params.fetch(:create_pipelines, true)

      Ci::CreatePipelineService
        .new(project, current_user, push_data)
        .execute(:push, pipeline_options)
    end

    def execute_project_hooks
      project.execute_hooks(push_data, hook_name)
      project.execute_services(push_data, hook_name)
    end

    def enqueue_invalidate_cache
      ProjectCacheWorker.perform_async(
        project.id,
        invalidated_file_types,
        [:commit_count, :repository_size]
      )
    end

    def push_data
      @push_data ||= Gitlab::DataBuilder::Push.build(
        project: project,
        user: current_user,
        oldrev: params[:oldrev],
        newrev: params[:newrev],
        ref: params[:ref],
        commits: limited_commits,
        message: event_message,
        commits_count: commits_count,
        push_options: params[:push_options] || {}
      )

      # Dependent code may modify the push data, so return a duplicate each time
      @push_data.dup
    end

    # to be overridden in EE
    def pipeline_options
      {}
    end
  end
end