Commit e48ba809 authored by Aakriti Gupta's avatar Aakriti Gupta

Add 2 endpoints to return issue and MR count

- These return counts for issues/MRs visible to
the current user, for the given group
parent ad9e7c76
---
title: Add API endpoints for Group Activity Analytics
merge_request: 25606
author:
type: added
# frozen_string_literal: true
module API
module Analytics
class GroupActivityAnalytics < Grape::API
DESCRIPTION_DETAIL =
'This feature is gated by the `:group_activity_analytics`'\
' feature flag, introduced in GitLab 12.9.'
before do
authenticate!
not_found! unless Feature.enabled?(:group_activity_analytics)
end
helpers do
def group
@group ||= find_group!(params[:group_path])
end
def calculator
@calculator ||=
::Analytics::GroupActivityCalculator.new(group, current_user)
end
end
resource :analytics do
resource :group_activity do
desc 'Get count of recently created issues for group' do
detail DESCRIPTION_DETAIL
success EE::API::Entities::Analytics::GroupActivity::IssuesCount
end
params do
requires :group_path, type: String, desc: 'Group Path'
end
get 'issues_count' do
authorize! :read_group_activity_analytics, group
present(
calculator,
with: EE::API::Entities::Analytics::GroupActivity::IssuesCount
)
end
desc 'Get count of recently created merge requests for group' do
detail DESCRIPTION_DETAIL
success EE::API::Entities::Analytics::GroupActivity::MergeRequestsCount
end
params do
requires :group_path, type: String, desc: 'Group Path'
end
get 'merge_requests_count' do
authorize! :read_group_activity_analytics, group
present(
calculator,
with: EE::API::Entities::Analytics::GroupActivity::MergeRequestsCount
)
end
end
end
end
end
end
......@@ -52,6 +52,7 @@ module EE
mount ::API::Dependencies
mount ::API::VisualReviewDiscussions
mount ::API::Analytics::CodeReviewAnalytics
mount ::API::Analytics::GroupActivityAnalytics
mount ::API::ProtectedEnvironments
version 'v3', using: :path do
......
......@@ -512,6 +512,16 @@ module EE
# rubocop: enable CodeReuse/ActiveRecord
end
end
module GroupActivity
class IssuesCount < Grape::Entity
expose :issues_count
end
class MergeRequestsCount < Grape::Entity
expose :merge_requests_count
end
end
end
module UserDetailsWithAdmin
......
# frozen_string_literal: true
require 'spec_helper'
describe EE::API::Entities::Analytics::GroupActivity do
let(:count) { 10 }
shared_examples 'exposes count' do |count_type, entity|
let(:data) { { count_type.to_sym => count } }
subject(:entity_representation) { entity.new(data).as_json }
it "exposes #{count_type}" do
expect(entity_representation).to include({ count_type.to_sym => count })
end
end
context 'issues count' do
it_behaves_like 'exposes count', 'issues_count', EE::API::Entities::Analytics::GroupActivity::IssuesCount
end
context 'merge requests count' do
it_behaves_like 'exposes count', 'merge_requests_count', EE::API::Entities::Analytics::GroupActivity::MergeRequestsCount
end
end
# frozen_string_literal: true
require 'spec_helper'
describe API::Analytics::GroupActivityAnalytics do
let_it_be(:group) { create(:group, :private) }
let_it_be(:reporter) do
create(:user).tap { |u| group.add_reporter(u) }
end
let_it_be(:anonymous_user) { create(:user) }
shared_examples 'GET group_activity' do |activity|
let(:feature_enabled) { true }
let(:params) { { group_path: group.full_path } }
let(:current_user) { reporter }
let(:request) do
get api("/analytics/group_activity/#{activity}_count", current_user), params: params
end
before do
stub_licensed_features(group_activity_analytics: feature_enabled)
request
end
it 'is successful' do
expect(response).to have_gitlab_http_status(:ok)
end
it 'is returns a count' do
expect(response.parsed_body).to eq({ "#{activity}_count" => 0 })
end
context 'when feature is not available in plan' do
let(:feature_enabled) { false }
it 'is returns `forbidden`' do
expect(response).to have_gitlab_http_status(:forbidden)
end
end
context 'when `group_path` is not specified' do
let(:params) { }
it 'is returns `bad_request`' do
expect(response).to have_gitlab_http_status(:bad_request)
end
end
context 'when user has no authorization to view a private group' do
let(:current_user) { anonymous_user }
it 'is returns `not_found`' do
expect(response).to have_gitlab_http_status(:not_found)
end
end
end
context 'GET /group_activity/issues_count' do
it_behaves_like 'GET group_activity', 'issues'
end
context 'GET /group_activity/merge_requests_count' do
it_behaves_like 'GET group_activity', 'merge_requests'
end
end
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