BigW Consortium Gitlab

push_data_builder.rb 2.75 KB
Newer Older
1 2
module Gitlab
  class PushDataBuilder
3 4 5 6 7 8 9 10 11
    class << self
      # Produce a hash of post-receive data
      #
      # data = {
      #   before: String,
      #   after: String,
      #   ref: String,
      #   user_id: String,
      #   user_name: String,
12
      #   user_email: String
13 14 15 16 17 18 19 20
      #   project_id: String,
      #   repository: {
      #     name: String,
      #     url: String,
      #     description: String,
      #     homepage: String,
      #   },
      #   commits: Array,
21
      #   total_commits_count: Fixnum
22 23
      # }
      #
24
      def build(project, user, oldrev, newrev, ref, commits = [], message = nil)
25 26
        commits = Array(commits)

27 28
        # Total commits count
        commits_count = commits.size
29

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

33 34
        # For performance purposes maximum 20 latest commits
        # will be passed as post receive hook data.
Valery Sizov committed
35
        commit_attrs = commits_limited.map do |commit|
Valery Sizov committed
36
          commit.hook_attrs(with_changed_files: true)
Valery Sizov committed
37
        end
38

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

41 42
        # Hash to be passed as post_receive_data
        data = {
43
          object_kind: type,
44 45 46 47
          before: oldrev,
          after: newrev,
          ref: ref,
          checkout_sha: checkout_sha(project.repository, newrev, ref),
48
          message: message,
49 50
          user_id: user.id,
          user_name: user.name,
51
          user_email: user.email,
52
          user_avatar: user.avatar_url,
53
          project_id: project.id,
54
          project: project.hook_attrs,
55
          commits: commit_attrs,
56 57 58 59
          total_commits_count: commits_count,
          # DEPRECATED
          repository: project.hook_attrs.slice(:name, :url, :description, :homepage,
                                               :git_http_url, :git_ssh_url, :visibility_level)
60
        }
61

62
        data
63 64
      end

65 66 67 68
      # 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)
69 70
        ref = "#{Gitlab::Git::BRANCH_REF_PREFIX}#{project.default_branch}"
        build(project, user, commits.last.id, commits.first.id, ref, commits)
71
      end
72

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

77
        # Find sha for tag, except when it was deleted.
78
        if Gitlab::Git.tag_ref?(ref)
79
          tag_name = Gitlab::Git.ref_name(ref)
80
          tag = repository.find_tag(tag_name)
81 82 83 84 85

          if tag
            commit = repository.commit(tag.target)
            commit.try(:sha)
          end
86 87 88 89
        else
          newrev
        end
      end
90 91 92
    end
  end
end