BigW Consortium Gitlab

push_data_builder.rb 2.78 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
          event_name: type,
45 46 47 48
          before: oldrev,
          after: newrev,
          ref: ref,
          checkout_sha: checkout_sha(project.repository, newrev, ref),
49
          message: message,
50 51
          user_id: user.id,
          user_name: user.name,
52
          user_email: user.email,
53
          user_avatar: user.avatar_url,
54
          project_id: project.id,
55
          project: project.hook_attrs,
56
          commits: commit_attrs,
57 58 59 60
          total_commits_count: commits_count,
          # DEPRECATED
          repository: project.hook_attrs.slice(:name, :url, :description, :homepage,
                                               :git_http_url, :git_ssh_url, :visibility_level)
61
        }
62

63
        data
64 65
      end

66
      # This method provide a sample data generated with
ashleys committed
67
      # existing project and commits to test webhooks
68
      def build_sample(project, user)
69
        commits = project.repository.commits(project.default_branch, limit: 3)
70 71
        ref = "#{Gitlab::Git::BRANCH_REF_PREFIX}#{project.default_branch}"
        build(project, user, commits.last.id, commits.first.id, ref, commits)
72
      end
73

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

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

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