summaryrefslogtreecommitdiff
path: root/app/services/create_commit_service.rb
blob: 0671a70847cd3c27bba8e4382b39cb1dc1e89e6b (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
class CreateCommitService
  def execute(project, params)
    before_sha = params[:before]
    sha = params[:checkout_sha] || params[:after]
    origin_ref = params[:ref]

    unless origin_ref && sha.present?
      return false
    end

    ref = origin_ref.gsub(/\Arefs\/(tags|heads)\//, '')

    # Skip branch removal
    if sha == Git::BLANK_SHA
      return false
    end

    if origin_ref.start_with?('refs/tags/') && !project.create_commit_for_tag?(ref)
      return false
    end

    if project.skip_ref?(ref)
      return false
    end

    commit = project.commits.find_by_sha_and_ref(sha, ref)

    # Create commit if not exists yet
    unless commit
      data = {
        ref: ref,
        sha: sha,
        before_sha: before_sha,
        push_data: {
          before: before_sha,
          after: sha,
          ref: ref,
          user_name: params[:user_name],
          user_email: params[:user_email],
          repository: params[:repository],
          commits: params[:commits],
          total_commits_count: params[:total_commits_count]
        }
      }

      commit = project.commits.create(data)
    end

    if origin_ref.start_with?('refs/tags/')
      commit.create_builds_for_tag(ref)
    else
      commit.create_builds
    end

    if commit.builds.empty?
      commit.create_deploy_builds(ref)
    end

    commit
  end
end