Commit 0a0428d6 authored by Alex Kalderimis's avatar Alex Kalderimis

Wiki page event support

Add support for wiki page events

* Update visibility test
* Update action name for wiki page events
* Update event specs to use wiki metadata models
* Add specific factories

This adds a `Policy` for WikiPage::Meta, which is required for
interoperation with `Event#visible_to`. The policy is very simple
- just delegating to the project.
parent 3416ee42
......@@ -6,6 +6,7 @@ class Event < ApplicationRecord
include Presentable
include DeleteWithLimit
include CreatedAtFilterable
include Gitlab::Utils::StrongMemoize
default_scope { reorder(nil) }
......@@ -42,7 +43,8 @@ class Event < ApplicationRecord
note: Note,
project: Project,
snippet: Snippet,
user: User
user: User,
wiki: WikiPage::Meta
).freeze
RESET_PROJECT_ACTIVITY_INTERVAL = 1.hour
......@@ -79,6 +81,7 @@ class Event < ApplicationRecord
scope :recent, -> { reorder(id: :desc) }
scope :code_push, -> { where(action: PUSHED) }
scope :merged, -> { where(action: MERGED) }
scope :for_wiki_page, -> { where(target_type: 'WikiPage::Meta') }
scope :with_associations, -> do
# We're using preload for "push_event_payload" as otherwise the association
......@@ -197,6 +200,14 @@ class Event < ApplicationRecord
created_action? && !target && target_type.nil?
end
def created_wiki_page?
wiki_page? && action == CREATED
end
def updated_wiki_page?
wiki_page? && action == UPDATED
end
def created_target?
created_action? && target
end
......@@ -217,6 +228,10 @@ class Event < ApplicationRecord
target_type == "MergeRequest"
end
def wiki_page?
target_type == "WikiPage::Meta"
end
def milestone
target if milestone?
end
......@@ -229,6 +244,14 @@ class Event < ApplicationRecord
target if merge_request?
end
def wiki_page
strong_memoize(:wiki_page) do
next unless wiki_page?
ProjectWiki.new(project, author).find_page(target.canonical_slug)
end
end
def note
target if note?
end
......@@ -250,6 +273,10 @@ class Event < ApplicationRecord
'destroyed'
elsif commented_action?
"commented on"
elsif created_wiki_page?
'created'
elsif updated_wiki_page?
'updated'
elsif created_project_action?
created_project_action_name
else
......@@ -362,6 +389,8 @@ class Event < ApplicationRecord
:read_snippet
elsif milestone?
:read_milestone
elsif wiki_page?
:read_wiki
end
end
end
......
# frozen_string_literal: true
class WikiPage::MetaPolicy < BasePolicy
delegate { @subject.project }
end
......@@ -22,6 +22,16 @@ FactoryBot.define do
action { Event::CLOSED }
target factory: :closed_issue
end
factory :wiki_page_event do
action { Event::CREATED }
transient do
wiki_page { create(:wiki_page, wiki: create(:project_wiki, project: project)) }
end
target { wiki_page.meta }
end
end
factory :push_event, class: 'PushEvent' do
......
......@@ -99,6 +99,38 @@ describe Event do
end
end
describe '#target_title' do
let_it_be(:project) { create(:project) }
let(:author) { project.owner }
let(:target) { nil }
let(:event) do
described_class.new(project: project,
target: target,
author_id: author.id)
end
context 'for an issue' do
let(:issue) { create(:issue, project: project) }
let(:target) { issue }
it 'delegates to issue title' do
expect(event.target_title).to eq(issue.title)
end
end
context 'for a wiki page' do
let(:wiki) { create(:project_wiki, project: project) }
let(:wiki_page) { create(:wiki_page, wiki: wiki) }
let(:event) { create(:wiki_page_event, project: project, wiki_page: wiki_page) }
it 'delegates to issue title' do
expect(event.target_title).to eq(wiki_page.title)
end
end
end
describe '#membership_changed?' do
context "created" do
subject { build(:event, :created).membership_changed? }
......@@ -165,7 +197,11 @@ describe Event do
let(:note_on_project_snippet) { create(:note_on_project_snippet, author: author, noteable: project_snippet, project: project) }
let(:note_on_personal_snippet) { create(:note_on_personal_snippet, author: author, noteable: personal_snippet, project: nil) }
let(:milestone_on_project) { create(:milestone, project: project) }
let(:event) { described_class.new(project: project, target: target, author_id: author.id) }
let(:event) do
described_class.new(project: project,
target: target,
author_id: author.id)
end
before do
project.add_developer(member)
......@@ -332,6 +368,19 @@ describe Event do
expect(event.visible_to_user?(admin)).to be_truthy
end
end
context 'wiki-page event', :aggregate_failures do
let(:project) { create(:project) }
let(:event) { create(:wiki_page_event, project: project) }
it do
expect(event.visible_to_user?(nil)).to be_falsy
expect(event.visible_to_user?(non_member)).to be_falsy
expect(event.visible_to_user?(member)).to be_truthy
expect(event.visible_to_user?(guest)).to be_truthy
expect(event.visible_to_user?(admin)).to be_truthy
end
end
end
context 'project snippet note event' do
......@@ -415,6 +464,55 @@ describe Event do
end
end
describe '.for_wiki_page' do
let_it_be(:events) do
[
create(:closed_issue_event),
create(:wiki_page_event),
create(:closed_issue_event),
create(:event, :created),
create(:wiki_page_event)
]
end
it 'only contains the wiki page events' do
wiki_events = events.select(&:wiki_page?)
expect(described_class.for_wiki_page).to match_array(wiki_events)
end
end
describe '#wiki_page and #wiki_page?' do
let_it_be(:project) { create(:project, :repository) }
let_it_be(:title) { FFaker::Lorem.sentence }
context 'for a wiki page event' do
let(:wiki) { create(:project_wiki, project: project) }
let(:wiki_page) do
wiki.create_page(title, FFaker::Lorem.sentence)
page_title, page_dir = wiki.page_title_and_dir(title)
page = wiki.wiki.page(title: page_title, dir: page_dir, version: nil)
WikiPage.new(wiki, page)
end
subject(:event) { create(:wiki_page_event, project: project, wiki_page: wiki_page) }
it { is_expected.to have_attributes(wiki_page?: be_truthy, wiki_page: wiki_page) }
end
[:issue, :user, :merge_request, :snippet, :milestone, nil].each do |kind|
context "for a #{kind} event" do
it 'is nil' do
target = create(kind) if kind
event = create(:event, project: project, target: target)
expect(event).to have_attributes(wiki_page: be_nil, wiki_page?: be_falsy)
end
end
end
end
describe '.limit_recent' do
let!(:event1) { create(:closed_issue_event) }
let!(:event2) { create(:closed_issue_event) }
......
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