Commit 5795fd10 authored by Max Woolf's avatar Max Woolf

Add compliance_framework list to project API endpoint

For EE instances with compliance_framework feature
enabled, this will add a compliance_frameworks key
to the API output for a project.
parent bfc9a2d8
...@@ -890,6 +890,7 @@ GET /projects/:id ...@@ -890,6 +890,7 @@ GET /projects/:id
"suggestion_commit_message": null, "suggestion_commit_message": null,
"marked_for_deletion_at": "2020-04-03", // Deprecated and will be removed in API v5 in favor of marked_for_deletion_on "marked_for_deletion_at": "2020-04-03", // Deprecated and will be removed in API v5 in favor of marked_for_deletion_on
"marked_for_deletion_on": "2020-04-03", "marked_for_deletion_on": "2020-04-03",
"compliance_frameworks": [ "sox" ],
"statistics": { "statistics": {
"commit_count": 37, "commit_count": 37,
"storage_size": 1038090, "storage_size": 1038090,
......
---
title: Add compliance_frameworks list to project API
merge_request: 34709
author:
type: added
...@@ -6,6 +6,15 @@ module EE ...@@ -6,6 +6,15 @@ module EE
module Project module Project
extend ActiveSupport::Concern extend ActiveSupport::Concern
class_methods do
extend ::Gitlab::Utils::Override
override :preload_relation
def preload_relation(projects_relation, options = {})
super(projects_relation).with_compliance_framework_settings
end
end
prepended do prepended do
expose :approvals_before_merge, if: ->(project, _) { project.feature_available?(:merge_request_approvers) } expose :approvals_before_merge, if: ->(project, _) { project.feature_available?(:merge_request_approvers) }
expose :mirror, if: ->(project, _) { project.feature_available?(:repository_mirrors) } expose :mirror, if: ->(project, _) { project.feature_available?(:repository_mirrors) }
...@@ -22,6 +31,9 @@ module EE ...@@ -22,6 +31,9 @@ module EE
expose :marked_for_deletion_on, if: ->(project, _) { project.feature_available?(:adjourned_deletion_for_projects_and_groups) } do |project, _| expose :marked_for_deletion_on, if: ->(project, _) { project.feature_available?(:adjourned_deletion_for_projects_and_groups) } do |project, _|
project.marked_for_deletion_at project.marked_for_deletion_at
end end
expose :compliance_frameworks do |project, _|
[project.compliance_framework_setting&.framework].compact
end
end end
end end
end end
......
...@@ -4,5 +4,9 @@ FactoryBot.define do ...@@ -4,5 +4,9 @@ FactoryBot.define do
factory :compliance_framework_project_setting, class: 'ComplianceManagement::ComplianceFramework::ProjectSettings' do factory :compliance_framework_project_setting, class: 'ComplianceManagement::ComplianceFramework::ProjectSettings' do
project project
framework { ComplianceManagement::ComplianceFramework::ProjectSettings.frameworks.keys.sample } framework { ComplianceManagement::ComplianceFramework::ProjectSettings.frameworks.keys.sample }
trait :sox do
framework { 'sox' }
end
end end
end end
...@@ -95,5 +95,9 @@ FactoryBot.modify do ...@@ -95,5 +95,9 @@ FactoryBot.modify do
trait :with_compliance_framework do trait :with_compliance_framework do
association :compliance_framework_setting, factory: :compliance_framework_project_setting association :compliance_framework_setting, factory: :compliance_framework_project_setting
end end
trait :with_sox_compliance_framework do
association :compliance_framework_setting, factory: :compliance_framework_project_setting, framework: 'sox'
end
end end
end end
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe ::EE::API::Entities::Project do
let(:project) { create(:project) }
let(:entity) do
::API::Entities::Project.new(project)
end
subject { entity.as_json }
context 'compliance_frameworks' do
context 'when project has a compliance framework' do
let(:project) { create(:project, :with_sox_compliance_framework) }
it 'is an array containing a single compliance framework' do
expect(subject[:compliance_frameworks]).to contain_exactly('sox')
end
end
context 'when project has no compliance framework' do
let(:project) { create(:project) }
it 'is empty array when project has no compliance framework' do
expect(subject[:compliance_frameworks]).to eq([])
end
end
end
end
...@@ -160,6 +160,31 @@ RSpec.describe API::Projects do ...@@ -160,6 +160,31 @@ RSpec.describe API::Projects do
end end
end end
describe 'compliance_frameworks attribute' do
context 'when compliance_framework feature is available' do
context 'when project has a compliance framework' do
before do
project.update!(compliance_framework_setting: create(:compliance_framework_project_setting, :sox))
get api("/projects/#{project.id}", user)
end
it 'exposes framework names as array of strings' do
expect(json_response['compliance_frameworks']).to contain_exactly('sox')
end
end
context 'when project has no compliance framework' do
before do
get api("/projects/#{project.id}", user)
end
it 'returns an empty array' do
expect(json_response['compliance_frameworks']).to eq([])
end
end
end
end
describe 'service desk attributes' do describe 'service desk attributes' do
it 'are exposed when the feature is available' do it 'are exposed when the feature is available' do
stub_licensed_features(service_desk: true) stub_licensed_features(service_desk: true)
......
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