Commit 774548be authored by Rémy Coutable's avatar Rémy Coutable

Merge branch 'fix-events-api' into 'master'

Fix events order in users/:id/events endpoint

## What does this MR do?

Order of events in contributions API is currently being lost, though docs are saying:

> Get the contribution events for the specified user, sorted **from newest to oldest**.

Order becomes different after `.merge(ProjectsFinder.new.execute(current_user))` call, so I moved ordering below this line.

This MR also removes extra `.page(params[:page])` call in the method chain, since [`paginate(events)` already does it](https://gitlab.com/airat/gitlab-ce/blob/master/lib/api/helpers.rb#L112).

See merge request !7039
parents 6fc54496 05084a45
...@@ -333,11 +333,11 @@ module API ...@@ -333,11 +333,11 @@ module API
user = User.find_by(id: declared(params).id) user = User.find_by(id: declared(params).id)
not_found!('User') unless user not_found!('User') unless user
events = user.recent_events. events = user.events.
merge(ProjectsFinder.new.execute(current_user)). merge(ProjectsFinder.new.execute(current_user)).
references(:project). references(:project).
with_associations. with_associations.
page(params[:page]) recent
present paginate(events), with: Entities::Event present paginate(events), with: Entities::Event
end end
......
...@@ -958,6 +958,29 @@ describe API::API, api: true do ...@@ -958,6 +958,29 @@ describe API::API, api: true do
expect(joined_event['author']['name']).to eq(user.name) expect(joined_event['author']['name']).to eq(user.name)
end end
end end
context 'when there are multiple events from different projects' do
let(:second_note) { create(:note_on_issue, project: create(:empty_project)) }
let(:third_note) { create(:note_on_issue, project: project) }
before do
second_note.project.add_user(user, :developer)
[second_note, third_note].each do |note|
EventCreateService.new.leave_note(note, user)
end
end
it 'returns events in the correct order (from newest to oldest)' do
get api("/users/#{user.id}/events", user)
comment_events = json_response.select { |e| e['action_name'] == 'commented on' }
expect(comment_events[0]['target_id']).to eq(third_note.id)
expect(comment_events[1]['target_id']).to eq(second_note.id)
expect(comment_events[2]['target_id']).to eq(note.id)
end
end
end end
it 'returns a 404 error if not found' do it 'returns a 404 error if not found' 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