Commit 94278340 authored by Sean McGivern's avatar Sean McGivern

Merge branch '24784-system-notes-meta-data' into 'master'

CE -> EE port: System notes meta data

See merge request !1526
parents 98b66256 727412ab
......@@ -38,6 +38,7 @@ class Note < ActiveRecord::Base
has_many :todos, dependent: :destroy
has_many :events, as: :target, dependent: :destroy
has_one :system_note_metadata
delegate :gfm_reference, :local_reference, to: :noteable
delegate :name, to: :project, prefix: true
......@@ -72,7 +73,9 @@ class Note < ActiveRecord::Base
scope :fresh, ->{ order(created_at: :asc, id: :asc) }
scope :inc_author_project, ->{ includes(:project, :author) }
scope :inc_author, ->{ includes(:author) }
scope :inc_relations_for_view, ->{ includes(:project, :author, :updated_by, :resolved_by, :award_emoji) }
scope :inc_relations_for_view, -> do
includes(:project, :author, :updated_by, :resolved_by, :award_emoji, :system_note_metadata)
end
scope :diff_notes, ->{ where(type: %w(LegacyDiffNote DiffNote)) }
scope :non_diff_notes, ->{ where(type: ['Note', nil]) }
......
class SystemNoteMetadata < ActiveRecord::Base
ICON_TYPES = %w[
commit merge confidentiality status label assignee cross_reference
title time_tracking branch milestone discussion task moved approvals
].freeze
validates :note, presence: true
validates :action, inclusion: ICON_TYPES, allow_nil: true
belongs_to :note
end
class NoteSummary
attr_reader :note
attr_reader :metadata
def initialize(noteable, project, author, body, action: nil, commit_count: nil)
@note = { noteable: noteable, project: project, author: author, note: body }
@metadata = { action: action, commit_count: commit_count }.compact
set_commit_params if note[:noteable].is_a?(Commit)
end
def metadata?
metadata.present?
end
def set_commit_params
note.merge!(noteable_type: 'Commit', commit_id: note[:noteable].id)
note[:noteable] = nil
end
end
This diff is collapsed.
---
title: Add metadata to system notes
merge_request: 1526
author:
......@@ -10,6 +10,6 @@
# end
#
ActiveSupport::Inflector.inflections do |inflect|
inflect.uncountable %w(award_emoji project_statistics project_registry file_registry)
inflect.uncountable %w(award_emoji project_statistics project_registry file_registry system_note_metadata)
inflect.acronym 'EE'
end
class CreateSystemNoteMetadata < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
create_table :system_note_metadata do |t|
t.references :note, null: false
t.integer :commit_count
t.string :action
t.timestamps null: false
end
add_concurrent_foreign_key :system_note_metadata, :notes, column: :note_id
end
def down
drop_table :system_note_metadata
end
end
......@@ -1272,6 +1272,14 @@ ActiveRecord::Schema.define(version: 20170317203554) do
add_index "subscriptions", ["subscribable_id", "subscribable_type", "user_id", "project_id"], name: "index_subscriptions_on_subscribable_and_user_id_and_project_id", unique: true, using: :btree
create_table "system_note_metadata", force: :cascade do |t|
t.integer "note_id", null: false
t.integer "commit_count"
t.string "action"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "taggings", force: :cascade do |t|
t.integer "tag_id"
t.integer "taggable_id"
......@@ -1518,6 +1526,7 @@ ActiveRecord::Schema.define(version: 20170317203554) do
add_foreign_key "protected_branch_push_access_levels", "users"
add_foreign_key "remote_mirrors", "projects"
add_foreign_key "subscriptions", "projects", on_delete: :cascade
add_foreign_key "system_note_metadata", "notes", name: "fk_d83a918cb1", on_delete: :cascade
add_foreign_key "timelogs", "issues", name: "fk_timelogs_issues_issue_id", on_delete: :cascade
add_foreign_key "timelogs", "merge_requests", name: "fk_timelogs_merge_requests_merge_request_id", on_delete: :cascade
add_foreign_key "trending_projects", "projects", on_delete: :cascade
......
FactoryGirl.define do
factory :system_note_metadata do
note
action 'merge'
end
end
......@@ -29,6 +29,7 @@ notes:
- resolved_by
- todos
- events
- system_note_metadata
label_links:
- target
- label
......
require 'spec_helper'
describe SystemNoteMetadata, models: true do
describe 'associations' do
it { is_expected.to belong_to(:note) }
end
describe 'validation' do
it { is_expected.to validate_presence_of(:note) }
context 'when action type is invalid' do
subject do
build(:system_note_metadata, note: build(:note), action: 'invalid_type' )
end
it { is_expected.to be_invalid }
end
context 'when action type is valid' do
subject do
build(:system_note_metadata, note: build(:note), action: 'merge' )
end
it { is_expected.to be_valid }
end
end
end
require 'spec_helper'
describe NoteSummary, services: true do
let(:project) { build(:empty_project) }
let(:noteable) { build(:issue) }
let(:user) { build(:user) }
def create_note_summary
described_class.new(noteable, project, user, 'note', action: 'icon', commit_count: 5)
end
describe '#metadata?' do
it 'returns true when metadata present' do
expect(create_note_summary.metadata?).to be_truthy
end
it 'returns false when metadata not present' do
expect(described_class.new(noteable, project, user, 'note').metadata?).to be_falsey
end
end
describe '#note' do
it 'returns note hash' do
expect(create_note_summary.note).to eq(noteable: noteable, project: project, author: user, note: 'note')
end
context 'when noteable is a commit' do
let(:noteable) { build(:commit) }
it 'returns note hash specific to commit' do
expect(create_note_summary.note).to eq(
noteable: nil, project: project, author: user, note: 'note',
noteable_type: 'Commit', commit_id: noteable.id
)
end
end
end
describe '#metadata' do
it 'returns metadata hash' do
expect(create_note_summary.metadata).to eq(action: 'icon', commit_count: 5)
end
end
end
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment