Read about our upcoming Code of Conduct on this issue

git_push_service.rb 6.24 KB
Newer Older
1
2
class GitPushService < BaseService
  attr_accessor :push_data, :push_commits
3
  include Gitlab::Access
4

Yorick Peterse's avatar
Yorick Peterse committed
5
6
7
  # The N most recent commits to process in a single push payload.
  PROCESS_COMMIT_LIMIT = 100

8
  # This method will be called after each git update
9
  # and only if the provided user and project are present in GitLab.
10
11
12
  #
  # All callbacks for post receive action should be placed here.
  #
13
14
  # Next, this method:
  #  1. Creates the push event
15
16
  #  2. Updates merge requests
  #  3. Recognizes cross-references from commit messages
ashleys's avatar
ashleys committed
17
  #  4. Executes the project's webhooks
18
  #  5. Executes the project's services
19
  #  6. Checks if the project's main language has changed
20
  #
21
  def execute
22
    @project.repository.after_create if @project.empty_repo?
23
    @project.repository.after_push_commit(branch_name)
24

25
    if push_remove_branch?
26
      @project.repository.after_remove_branch
27
      @push_commits = []
28
    elsif push_to_new_branch?
29
      @project.repository.after_create_branch
30

31
      # Re-find the pushed commits.
32
      if default_branch?
33
        # Initial push to the default branch. Take the full history of that branch as "newly pushed".
34
        process_default_branch
35
36
37
      else
        # Use the pushed commits that aren't reachable by the default branch
        # as a heuristic. This may include more commits than are actually pushed, but
James Lopez's avatar
typo    
James Lopez committed
38
        # that shouldn't matter because we check for existing cross-references later.
39
        @push_commits = @project.repository.commits_between(@project.default_branch, params[:newrev])
40
41

        # don't process commits for the initial push to the default branch
42
        process_commit_messages
43
      end
44
    elsif push_to_existing_branch?
45
      # Collect data for this git push
46
      @push_commits = @project.repository.commits_between(params[:oldrev], params[:newrev])
47

48
      process_commit_messages
49
50
51

      # Update the bare repositories info/attributes file using the contents of the default branches
      # .gitattributes file
52
      update_gitattributes if default_branch?
53
    end
54

55
    execute_related_hooks
Jacob Vosmaer's avatar
Jacob Vosmaer committed
56
    perform_housekeeping
57

58
    update_remote_mirrors
59
    update_caches
Alexis Reigel's avatar
Alexis Reigel committed
60
61

    update_signatures
62
  end
63

64
65
66
67
  def update_gitattributes
    @project.repository.copy_gitattributes(params[:ref])
  end

68
  def update_caches
69
    if default_branch?
70
71
72
73
74
75
      if push_to_new_branch?
        # If this is the initial push into the default branch, the file type caches
        # will already be reset as a result of `Project#change_head`.
        types = []
      else
        paths = Set.new
76

77
78
79
80
        @push_commits.last(PROCESS_COMMIT_LIMIT).each do |commit|
          commit.raw_deltas.each do |diff|
            paths << diff.new_path
          end
81
82
        end

83
84
        types = Gitlab::FileDetector.types_in_paths(paths.to_a)
      end
85
86
87
88
    else
      types = []
    end

Markus Koller's avatar
Markus Koller committed
89
    ProjectCacheWorker.perform_async(@project.id, types, [:commit_count, :repository_size])
90
91
  end

Alexis Reigel's avatar
Alexis Reigel committed
92
  def update_signatures
93
94
95
96
97
98
99
100
101
102
103
104
105
    commit_shas = @push_commits.last(PROCESS_COMMIT_LIMIT).map(&:sha)

    return if commit_shas.empty?

    shas_with_cached_signatures = GpgSignature.where(commit_sha: commit_shas).pluck(:commit_sha)
    commit_shas -= shas_with_cached_signatures

    return if commit_shas.empty?

    commit_shas = Gitlab::Git::Commit.shas_with_signatures(project.repository, commit_shas)

    commit_shas.each do |sha|
      CreateGpgSignatureWorker.perform_async(sha, project.id)
Alexis Reigel's avatar
Alexis Reigel committed
106
107
108
    end
  end

Yorick Peterse's avatar
Yorick Peterse committed
109
110
  # Schedules processing of commit messages.
  def process_commit_messages
111
    default = default_branch?
Yorick Peterse's avatar
Yorick Peterse committed
112

113
    @push_commits.last(PROCESS_COMMIT_LIMIT).each do |commit|
114
      if commit.matches_cross_reference_regex?
115
116
        ProcessCommitWorker
          .perform_async(project.id, current_user.id, commit.to_hash, default)
117
      end
Yorick Peterse's avatar
Yorick Peterse committed
118
119
120
    end
  end

121
122
  protected

123
124
125
126
127
128
129
  def update_remote_mirrors
    return unless @project.has_remote_mirror?

    @project.mark_stuck_remote_mirrors_as_failed!
    @project.update_remote_mirrors
  end

130
131
132
133
134
135
  def execute_related_hooks
    # Update merge requests that may be affected by this push. A new branch
    # could cause the last commit of a merge request to change.
    #
    UpdateMergeRequestsWorker
      .perform_async(@project.id, current_user.id, params[:oldrev], params[:newrev], params[:ref])
Douwe Maan's avatar
Douwe Maan committed
136

137
    EventCreateService.new.push(@project, current_user, build_push_data)
Kamil Trzcinski's avatar
Kamil Trzcinski committed
138
    Ci::CreatePipelineService.new(@project, current_user, build_push_data).execute(:push)
139

Kamil Trzcinski's avatar
Kamil Trzcinski committed
140
    SystemHookPushWorker.perform_async(build_push_data.dup, :push_hooks)
141
142
    @project.execute_hooks(build_push_data.dup, :push_hooks)
    @project.execute_services(build_push_data.dup, :push_hooks)
143
144
145
146
147
148

    if push_remove_branch?
      AfterBranchDeleteService
        .new(project, current_user)
        .execute(branch_name)
    end
149
150
  end

Jacob Vosmaer's avatar
Jacob Vosmaer committed
151
152
153
154
  def perform_housekeeping
    housekeeping = Projects::HousekeepingService.new(@project)
    housekeeping.increment!
    housekeeping.execute if housekeeping.needed?
155
  rescue Projects::HousekeepingService::LeaseTaken
Jacob Vosmaer's avatar
Jacob Vosmaer committed
156
157
  end

158
  def process_default_branch
159
160
161
162
    @push_commits_count = project.repository.commit_count_for_ref(params[:ref])

    offset = [@push_commits_count - PROCESS_COMMIT_LIMIT, 0].max
    @push_commits = project.repository.commits(params[:newrev], offset: offset, limit: PROCESS_COMMIT_LIMIT)
163

164
    @project.after_create_default_branch
165
  end
166

167
  def build_push_data
168
169
170
171
172
173
    @push_data ||= Gitlab::DataBuilder::Push.build(
      @project,
      current_user,
      params[:oldrev],
      params[:newrev],
      params[:ref],
174
175
      @push_commits,
      commits_count: @push_commits_count)
176
177
  end

178
  def push_to_existing_branch?
179
    # Return if this is not a push to a branch (e.g. new commits)
180
    Gitlab::Git.branch_ref?(params[:ref]) && !Gitlab::Git.blank_ref?(params[:oldrev])
181
182
  end

183
  def push_to_new_branch?
184
    Gitlab::Git.branch_ref?(params[:ref]) && Gitlab::Git.blank_ref?(params[:oldrev])
185
186
  end

187
  def push_remove_branch?
188
    Gitlab::Git.branch_ref?(params[:ref]) && Gitlab::Git.blank_ref?(params[:newrev])
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
189
190
  end

191
  def push_to_branch?
192
    Gitlab::Git.branch_ref?(params[:ref])
193
194
  end

195
  def default_branch?
196
    Gitlab::Git.branch_ref?(params[:ref]) &&
197
      (Gitlab::Git.ref_name(params[:ref]) == project.default_branch || project.default_branch.nil?)
198
199
  end

200
  def commit_user(commit)
201
    commit.author || current_user
202
  end
203
204

  def branch_name
205
    @branch_name ||= Gitlab::Git.ref_name(params[:ref])
206
  end
207
end