summaryrefslogtreecommitdiff
path: root/lib/gitlab/push_data_builder.rb
blob: fa068d507634a1351985bab5e2877dfd8484e1e3 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
module Gitlab
  class PushDataBuilder
    class << self
      # Produce a hash of post-receive data
      #
      # data = {
      #   before: String,
      #   after: String,
      #   ref: String,
      #   user_id: String,
      #   user_name: String,
      #   user_email: String
      #   project_id: String,
      #   repository: {
      #     name: String,
      #     url: String,
      #     description: String,
      #     homepage: String,
      #   },
      #   commits: Array,
      #   total_commits_count: Fixnum,
      #   added: ["CHANGELOG"],
      #   modified: [],
      #   removed: ["tmp/file.txt"]
      # }
      #
      def build(project, user, oldrev, newrev, ref, commits = [], message = nil)
        # Total commits count
        commits_count = commits.size

        # Get latest 20 commits ASC
        commits_limited = commits.last(20)

        # For performance purposes maximum 20 latest commits
        # will be passed as post receive hook data.
        commit_attrs = commits_limited.map(&:hook_attrs)

        type = Gitlab::Git.tag_ref?(ref) ? "tag_push" : "push"

        repo_changes = repo_changes(project, newrev, oldrev)
        # Hash to be passed as post_receive_data
        data = {
          object_kind: type,
          before: oldrev,
          after: newrev,
          ref: ref,
          checkout_sha: checkout_sha(project.repository, newrev, ref),
          message: message,
          user_id: user.id,
          user_name: user.name,
          user_email: user.email,
          project_id: project.id,
          repository: {
            name: project.name,
            url: project.url_to_repo,
            description: project.description,
            homepage: project.web_url,
            git_http_url: project.http_url_to_repo,
            git_ssh_url: project.ssh_url_to_repo,
            visibility_level: project.visibility_level
          },
          commits: commit_attrs,
          total_commits_count: commits_count,
          added: repo_changes[:added],
          modified: repo_changes[:modified],
          removed: repo_changes[:removed]
        }

        data
      end

      # This method provide a sample data generated with
      # existing project and commits to test web hooks
      def build_sample(project, user)
        commits = project.repository.commits(project.default_branch, nil, 3)
        ref = "#{Gitlab::Git::BRANCH_REF_PREFIX}#{project.default_branch}"
        build(project, user, commits.last.id, commits.first.id, ref, commits)
      end

      def checkout_sha(repository, newrev, ref)
        # Checkout sha is nil when we remove branch or tag
        return if Gitlab::Git.blank_ref?(newrev)

        # Find sha for tag, except when it was deleted.
        if Gitlab::Git.tag_ref?(ref)
          tag_name = Gitlab::Git.ref_name(ref)
          tag = repository.find_tag(tag_name)

          if tag
            commit = repository.commit(tag.target)
            commit.try(:sha)
          end
        else
          newrev
        end
      end

      def repo_changes(project, newrev, oldrev)
        changes = { added: [], modified: [], removed: [] }
        compare_result = CompareService.new.
          execute(project, newrev, project, oldrev)

        if compare_result
          compare_result.diffs.each do |diff|
            case true
            when diff.deleted_file
              changes[:removed] << diff.old_path
            when diff.renamed_file, diff.new_file
              changes[:added] << diff.new_path
            else
              changes[:modified] << diff.new_path
            end
          end
        end

        changes
      end
    end
  end
end