BigW Consortium Gitlab

push.rb 2.87 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_username: String,
15
      #   user_email: String
16 17 18 19 20 21 22 23
      #   project_id: String,
      #   repository: {
      #     name: String,
      #     url: String,
      #     description: String,
      #     homepage: String,
      #   },
      #   commits: Array,
24
      #   total_commits_count: Fixnum
25 26
      # }
      #
27
      def build(project, user, oldrev, newrev, ref, commits = [], message = nil)
28 29
        commits = Array(commits)

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

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

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

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

44
        # Hash to be passed as post_receive_data
45
        {
46
          object_kind: type,
47
          event_name: type,
48 49 50 51
          before: oldrev,
          after: newrev,
          ref: ref,
          checkout_sha: checkout_sha(project.repository, newrev, ref),
52
          message: message,
53 54
          user_id: user.id,
          user_name: user.name,
55
          user_username: user.username,
56
          user_email: user.email,
57
          user_avatar: user.avatar_url,
58
          project_id: project.id,
59
          project: project.hook_attrs,
60
          commits: commit_attrs,
61 62 63 64
          total_commits_count: commits_count,
          # DEPRECATED
          repository: project.hook_attrs.slice(:name, :url, :description, :homepage,
                                               :git_http_url, :git_ssh_url, :visibility_level)
65
        }
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
        ref = "#{Gitlab::Git::BRANCH_REF_PREFIX}#{project.default_branch}"
72 73 74
        commits = project.repository.commits(project.default_branch.to_s, limit: 3) rescue []

        build(project, user, commits.last&.id, commits.first&.id, ref, commits)
75
      end
76

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

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

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