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
        # Total commits count
        commits_count = commits.size
27

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

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

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

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

64
        data
65 66
      end

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

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

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

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