diff --git a/CHANGELOG.md b/CHANGELOG.md index ee55f73dda29dd14784411ccdea452bfe8592475..3d501f6e8cb71a96d94ada55e96023a615022528 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -122,6 +122,7 @@ Please view this file on the master branch, on stable branches it's out of date. - API: all unknown routing will be handled with 404 Not Found - Add docs for request profiling - Make guests unable to view MRs on private projects + - Add `/groups/owned` API endpoint (Borja Aparicio) ## 8.12.7 diff --git a/doc/api/groups.md b/doc/api/groups.md index 95a9c5e69ddd5a1bdf55465f8f3571842bb0b807..dd3ded89140500008abea87895d508d7a6e5fe70 100644 --- a/doc/api/groups.md +++ b/doc/api/groups.md @@ -26,6 +26,14 @@ GET /groups You can search for groups by name or path, see below. +## List owned groups + +Get a list of groups which are owned by the authenticated user. + +``` +GET /groups/owned +``` + ## List a group's projects Get a list of projects in this group. diff --git a/lib/api/groups.rb b/lib/api/groups.rb index 85129d5b701f8fed0e3f63ef015a769a55f293c4..482ee3d3452df9dc89ce4ea968fa7c5454526c3f 100644 --- a/lib/api/groups.rb +++ b/lib/api/groups.rb @@ -26,6 +26,16 @@ class Groups < Grape::API present @groups, with: Entities::Group end + # Get list of owned groups for authenticated user + # + # Example Request: + # GET /groups/owned + get '/owned' do + @groups = current_user.owned_groups + @groups = paginate @groups + present @groups, with: Entities::Group, user: current_user + end + # Create group. Available only for users who can create groups. # # Parameters: diff --git a/spec/requests/api/groups_spec.rb b/spec/requests/api/groups_spec.rb index 850f0d24257ef47307ef24e301e8292717868b11..a77813f9790685e690bb8fd7919248ca1e807618 100644 --- a/spec/requests/api/groups_spec.rb +++ b/spec/requests/api/groups_spec.rb @@ -78,6 +78,25 @@ end end + describe 'GET /groups/owned' do + + context 'when unauthenticated' do + it 'returns authentication error' do + get api('/groups/owned') + expect(response).to have_http_status(401) + end + end + + context 'when authenticated as group owner' do + it 'returns an array of groups the user owns' do + get api('/groups/owned', user2) + expect(response).to have_http_status(200) + expect(json_response).to be_an Array + expect(json_response.first['name']).to eq(group2.name) + end + end + end + describe "GET /groups/:id" do context "when authenticated as user" do it "returns one of user1's groups" do diff --git a/spec/requests/api/projects_spec.rb b/spec/requests/api/projects_spec.rb index 54ba32f6bc457dedb8eebdcb7f4caf94c66d9122..84dbea5ca5b15669cca9fb26b0141df28a71ec53 100644 --- a/spec/requests/api/projects_spec.rb +++ b/spec/requests/api/projects_spec.rb @@ -175,6 +175,29 @@ end end + describe 'GET /projects/owned' do + before do + project4 + end + + context 'when unauthenticated' do + it 'returns authentication error' do + get api('/projects/owned') + expect(response).to have_http_status(401) + end + end + + context 'when authenticated as project owner' do + it 'returns an array of projects the user owns' do + get api('/projects/owned', user4) + expect(response).to have_http_status(200) + expect(json_response).to be_an Array + expect(json_response.first['name']).to eq(project4.name) + expect(json_response.first['owner']['username']).to eq(user4.username) + end + end + end + describe 'GET /projects/visible' do let(:public_project) { create(:project, :public) }