BigW Consortium Gitlab

push.rb 2.8 KB
Newer Older
1
module Gitlab
2
  module DataBuilder
3
    module Push
4
      extend self
5

6 7 8 9 10 11 12 13
      # Produce a hash of post-receive data
      #
      # data = {
      #   before: String,
      #   after: String,
      #   ref: String,
      #   user_id: String,
      #   user_name: String,
14
      #   user_email: String
15 16 17 18 19 20 21 22
      #   project_id: String,
      #   repository: {
      #     name: String,
      #     url: String,
      #     description: String,
      #     homepage: String,
      #   },
      #   commits: Array,
23
      #   total_commits_count: Fixnum
24 25
      # }
      #
26
      def build(project, user, oldrev, newrev, ref, commits = [], message = nil)
27 28
        commits = Array(commits)

29 30
        # Total commits count
        commits_count = commits.size
31

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

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

41
        type = Gitlab::Git.tag_ref?(ref) ? 'tag_push' : 'push'
42

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

65
        data
66 67
      end

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

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

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

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