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

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

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

29
      # Re-find the pushed commits.
30
      if is_default_branch?
31
        # Initial push to the default branch. Take the full history of that branch as "newly pushed".
32
        process_default_branch
33 34 35
      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
36
        # that shouldn't matter because we check for existing cross-references later.
37
        @push_commits = @project.repository.commits_between(@project.default_branch, params[:newrev])
38 39

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

      # Update the bare repositories info/attributes file using the contents of the default branches
      # .gitattributes file
      update_gitattributes if is_default_branch?
50
    end
51

52 53
    # Update merge requests that may be affected by this push. A new branch
    # could cause the last commit of a merge request to change.
54
    update_merge_requests
55

56 57 58 59
    # Checks if the main language has changed in the project and if so
    # it updates it accordingly
    update_main_language

60
    perform_housekeeping
61
  end
62

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

67
  def update_main_language
68 69 70 71
    # Performance can be bad so for now only check main_language once
    # See https://gitlab.com/gitlab-org/gitlab-ce/issues/14937
    return if @project.main_language.present?

72 73 74
    return unless is_default_branch?
    return unless push_to_new_branch? || push_to_existing_branch?

75
    current_language = @project.repository.main_language
76
    @project.update_attributes(main_language: current_language)
77
    true
78 79
  end

80 81
  protected

82
  def update_merge_requests
83
    @project.update_merge_requests(params[:oldrev], params[:newrev], params[:ref], current_user)
84

85
    EventCreateService.new.push(@project, current_user, build_push_data)
86
    SystemHooksService.new.execute_hooks(build_push_data_system_hook.dup, :push_hooks)
87 88
    @project.execute_hooks(build_push_data.dup, :push_hooks)
    @project.execute_services(build_push_data.dup, :push_hooks)
89
    CreateCommitBuildsService.new.execute(@project, current_user, build_push_data)
90
    ProjectCacheWorker.perform_async(@project.id)
91 92
  end

93 94 95 96
  def perform_housekeeping
    housekeeping = Projects::HousekeepingService.new(@project)
    housekeeping.increment!
    housekeeping.execute if housekeeping.needed?
97
  rescue Projects::HousekeepingService::LeaseTaken
98 99
  end

100
  def process_default_branch
101
    @push_commits = project.repository.commits(params[:newrev])
102 103 104 105 106

    # Ensure HEAD points to the default branch in case it is not master
    project.change_head(branch_name)

    # Set protection on the default branch if configured
107
    if current_application_settings.default_branch_protection != PROTECTION_NONE
108 109 110 111
      developers_can_push = current_application_settings.default_branch_protection == PROTECTION_DEV_CAN_PUSH ? true : false
      @project.protected_branches.create({ name: @project.default_branch, developers_can_push: developers_can_push })
    end
  end
112

113 114
  # Extract any GFM references from the pushed commit messages. If the configured issue-closing regex is matched,
  # close the referenced Issue. Create cross-reference Notes corresponding to any other referenced Mentionables.
115 116
  def process_commit_messages
    is_default_branch = is_default_branch?
117

118 119
    authors = Hash.new do |hash, commit|
      email = commit.author_email
120
      next hash[email] if hash.has_key?(email)
121

122 123
      hash[email] = commit_user(commit)
    end
124

125
    @push_commits.each do |commit|
126 127 128
      # Keep track of the issues that will be actually closed because they are on a default branch.
      # Hence, when creating cross-reference notes, the not-closed issues (on non-default branches)
      # will also have cross-reference.
129 130 131 132 133 134
      closed_issues = []

      if is_default_branch
        # Close issues if these commits were pushed to the project's default branch and the commit message matches the
        # closing regex. Exclude any mentioned Issues from cross-referencing even if the commits are being pushed to
        # a different branch.
135
        closed_issues = commit.closes_issues(current_user)
136
        closed_issues.each do |issue|
137
          if can?(current_user, :update_issue, issue)
138
            Issues::CloseService.new(project, authors[commit], {}).execute(issue, commit: commit)
139
          end
140
        end
141 142
      end

143
      commit.create_cross_references!(authors[commit], closed_issues)
144 145 146
    end
  end

147 148
  def build_push_data
    @push_data ||= Gitlab::PushDataBuilder.
149
      build(@project, current_user, params[:oldrev], params[:newrev], params[:ref], push_commits)
150 151
  end

152 153
  def build_push_data_system_hook
    @push_data_system ||= Gitlab::PushDataBuilder.
154
      build(@project, current_user, params[:oldrev], params[:newrev], params[:ref], [])
155 156
  end

157
  def push_to_existing_branch?
158
    # Return if this is not a push to a branch (e.g. new commits)
159
    Gitlab::Git.branch_ref?(params[:ref]) && !Gitlab::Git.blank_ref?(params[:oldrev])
160 161
  end

162
  def push_to_new_branch?
163
    Gitlab::Git.branch_ref?(params[:ref]) && Gitlab::Git.blank_ref?(params[:oldrev])
164 165
  end

166
  def push_remove_branch?
167
    Gitlab::Git.branch_ref?(params[:ref]) && Gitlab::Git.blank_ref?(params[:newrev])
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
168 169
  end

170
  def push_to_branch?
171
    Gitlab::Git.branch_ref?(params[:ref])
172 173
  end

174
  def is_default_branch?
175
    Gitlab::Git.branch_ref?(params[:ref]) &&
176
      (Gitlab::Git.ref_name(params[:ref]) == project.default_branch || project.default_branch.nil?)
177 178
  end

179
  def commit_user(commit)
180
    commit.author || current_user
181
  end
182 183

  def branch_name
184
    @branch_name ||= Gitlab::Git.ref_name(params[:ref])
185
  end
186
end