note.rb 2.2 KB
Newer Older
1 2
# frozen_string_literal: true

3
module Gitlab
4
  module DataBuilder
5
    module Note
6
      extend self
7

8 9 10 11 12 13
      # Produce a hash of post-receive data
      #
      # For all notes:
      #
      # data = {
      #   object_kind: "note",
14
      #   event_type: "confidential_note",
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
      #   user: {
      #     name: String,
      #     username: String,
      #     avatar_url: String
      #   }
      #   project_id: Integer,
      #   repository: {
      #     name: String,
      #     url: String,
      #     description: String,
      #     homepage: String,
      #   }
      #  object_attributes: {
      #    <hook data for note>
      #  }
      #  <note-specific data>: {
      # }
      # note-specific data is a hash with one of the following keys and contains
      # the hook data for that type.
      #  - commit
      #  - issue
      #  - merge_request
      #  - snippet
      #
      def build(note, user)
        project = note.project
        data = build_base_data(project, user, note)

        if note.for_commit?
          data[:commit] = build_data_for_commit(project, user, note)
        elsif note.for_issue?
          data[:issue] = note.noteable.hook_attrs
47
          data[:issue][:labels] = note.noteable.labels.map(&:hook_attrs)
48 49
        elsif note.for_merge_request?
          data[:merge_request] = note.noteable.hook_attrs
50
        elsif note.for_snippet?
51 52 53 54 55 56 57
          data[:snippet] = note.noteable.hook_attrs
        end

        data
      end

      def build_base_data(project, user, note)
58 59
        event_type = note.confidential? ? 'confidential_note' : 'note'

60 61
        base_data = {
          object_kind: "note",
62
          event_type: event_type,
63 64
          user: user.hook_attrs,
          project_id: project.id,
65 66 67 68
          project: project.hook_attrs,
          object_attributes: note.hook_attrs,
          # DEPRECATED
          repository: project.hook_attrs.slice(:name, :url, :description, :homepage)
69 70
        }

71
        base_data[:object_attributes][:url] = Gitlab::UrlBuilder.build(note)
72 73 74 75 76
        base_data
      end

      def build_data_for_commit(project, user, note)
        # commit_id is the SHA hash
77
        commit = project.commit(note.commit_id)
78
        commit.hook_attrs
79 80 81 82
      end
    end
  end
end