Commit aabd693e authored by Grzegorz Bizon's avatar Grzegorz Bizon

Merge branch 'notes_api_system_filter' into 'master'

Add system/comment filter to notes api

See merge request gitlab-org/gitlab!21159
parents 2b5b97bf 36c4e1db
---
title: 25968-activity-filter-to-notes-api
merge_request: 21159
author: jhenkens
type: added
...@@ -24,6 +24,8 @@ module API ...@@ -24,6 +24,8 @@ module API
desc: 'Return notes ordered by `created_at` or `updated_at` fields.' desc: 'Return notes ordered by `created_at` or `updated_at` fields.'
optional :sort, type: String, values: %w[asc desc], default: 'desc', optional :sort, type: String, values: %w[asc desc], default: 'desc',
desc: 'Return notes sorted in `asc` or `desc` order.' desc: 'Return notes sorted in `asc` or `desc` order.'
optional :activity_filter, type: String, values: UserPreference::NOTES_FILTERS.stringify_keys.keys, default: 'all_notes',
desc: 'The type of notables which are returned.'
use :pagination use :pagination
end end
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
...@@ -35,7 +37,8 @@ module API ...@@ -35,7 +37,8 @@ module API
# at the DB query level (which we cannot in that case), the current # at the DB query level (which we cannot in that case), the current
# page can have less elements than :per_page even if # page can have less elements than :per_page even if
# there's more than one page. # there's more than one page.
raw_notes = noteable.notes.with_metadata.reorder(order_options_with_tie_breaker) notes_filter = UserPreference::NOTES_FILTERS[params[:activity_filter].to_sym]
raw_notes = noteable.notes.with_metadata.with_notes_filter(notes_filter).reorder(order_options_with_tie_breaker)
# paginate() only works with a relation. This could lead to a # paginate() only works with a relation. This could lead to a
# mismatch between the pagination headers info and the actual notes # mismatch between the pagination headers info and the actual notes
......
...@@ -5,6 +5,12 @@ require 'spec_helper' ...@@ -5,6 +5,12 @@ require 'spec_helper'
describe UserPreference do describe UserPreference do
let(:user_preference) { create(:user_preference) } let(:user_preference) { create(:user_preference) }
describe 'notes filters global keys' do
it 'contains expected values' do
expect(UserPreference::NOTES_FILTERS.keys).to match_array([:all_notes, :only_comments, :only_activity])
end
end
describe '#set_notes_filter' do describe '#set_notes_filter' do
let(:issuable) { build_stubbed(:issue) } let(:issuable) { build_stubbed(:issue) }
......
...@@ -101,6 +101,75 @@ describe API::Notes do ...@@ -101,6 +101,75 @@ describe API::Notes do
expect(json_response.first['body']).to eq(cross_reference_note.note) expect(json_response.first['body']).to eq(cross_reference_note.note)
end end
end end
context "activity filters" do
let!(:user_reference_note) do
create :note,
noteable: ext_issue, project: ext_proj,
note: "Hello there general!",
system: false
end
let(:test_url) {"/projects/#{ext_proj.id}/issues/#{ext_issue.iid}/notes"}
shared_examples 'a notes request' do
it 'is a note array response' do
expect(response).to have_gitlab_http_status(200)
expect(response).to include_pagination_headers
expect(json_response).to be_an Array
end
end
context "when not provided" do
let(:count) { 2 }
before do
get api(test_url, private_user)
end
it_behaves_like 'a notes request'
it 'returns all the notes' do
expect(json_response.count).to eq(count)
end
end
context "when all_notes provided" do
let(:count) { 2 }
before do
get api(test_url + "?activity_filter=all_notes", private_user)
end
it_behaves_like 'a notes request'
it 'returns all the notes' do
expect(json_response.count).to eq(count)
end
end
context "when provided" do
using RSpec::Parameterized::TableSyntax
where(:filter, :count, :system_notable) do
"only_comments" | 1 | false
"only_activity" | 1 | true
end
with_them do
before do
get api(test_url + "?activity_filter=#{filter}", private_user)
end
it_behaves_like 'a notes request'
it "properly filters the returned notables" do
expect(json_response.count).to eq(count)
expect(json_response.first["system"]).to be system_notable
end
end
end
end
end end
describe "GET /projects/:id/noteable/:noteable_id/notes/:note_id" do describe "GET /projects/:id/noteable/:noteable_id/notes/:note_id" do
......
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