Skip to content
代码片段 群组 项目
未验证 提交 24d01935 编辑于 作者: Bojan Marjanovic's avatar Bojan Marjanovic 提交者: GitLab
浏览文件

Add basic testing functionality for group level

Changelog: added
EE: true
上级 5fd16665
No related branches found
No related tags found
无相关合并请求
......@@ -46,7 +46,11 @@ def update
end
def test
render json: {}, status: :ok
if integration.testable?
render json: integration_test_response, status: :ok
else
render json: {}, status: :not_found
end
end
def reset
......@@ -80,4 +84,25 @@ def serialize_as_json
.as_json(only: integration.json_fields)
.merge(errors: integration.errors.as_json)
end
def integration_test_response
result = if integration.project_level?
::Integrations::Test::ProjectService.new(integration, current_user, params[:event]).execute
elsif integration.group_level?
::Integrations::Test::GroupService.new(integration, current_user, params[:event]).execute
else
{}
end
unless result[:success]
return {
error: true,
message: s_('Integrations|Connection failed. Check your integration settings.'),
service_response: result[:result].to_s,
test_failed: true
}
end
result[:data].presence || {}
end
end
......@@ -408,6 +408,10 @@ def avatar_url
ActionController::Base.helpers.image_path('illustrations/third-party-logos/integrations-logos/jira.svg')
end
def testable?
group_level? || project_level?
end
private
def jira_issue_match_regex
......
# frozen_string_literal: true
module Integrations
module GroupTestData
NoDataError = Class.new(ArgumentError)
private
def push_events_data
Gitlab::DataBuilder::Push.sample_data
end
end
end
# frozen_string_literal: true
module Integrations
module Test
class GroupService < Integrations::Test::BaseService
include Integrations::GroupTestData
include Gitlab::Utils::StrongMemoize
def group
integration.group
end
strong_memoize_attr :group
private
def data
case event || integration.default_test_event
when 'push', 'tag_push'
push_events_data
end
end
strong_memoize_attr :data
end
end
end
......@@ -39,7 +39,7 @@ def configured_to_create_issues_from_vulnerabilities?
def test(_)
super.then do |result|
next result unless result[:success]
next result unless project.jira_vulnerabilities_integration_enabled?
next result unless jira_vulnerabilities_integration_enabled?
result.merge(data: { issuetypes: issue_types })
end
......@@ -103,12 +103,9 @@ def jira_project
#
# @return [Array] the array of objects with JIRA Issuetype ID, Name and Description
def issue_types
return [] if jira_project.blank?
issuetypes = jira_project.blank? ? client.Issuetype.all : jira_project.issuetypes
jira_project
.issuetypes
.reject(&:subtask)
.map do |issue_type|
issuetypes.reject(&:subtask).map do |issue_type|
{
id: issue_type.id,
name: issue_type.name,
......
......@@ -2,7 +2,7 @@
require 'spec_helper'
RSpec.describe Integrations::Jira do
RSpec.describe Integrations::Jira, feature_category: :integrations do
let(:jira_integration) { build(:jira_integration, **options) }
let(:headers) { { 'Content-Type' => 'application/json' } }
......@@ -105,7 +105,7 @@
context 'when vulnerabilities integration is not enabled' do
before do
allow(jira_integration.project).to receive(:jira_vulnerabilities_integration_enabled?).and_return(false)
allow(jira_integration).to receive(:jira_vulnerabilities_integration_enabled?).and_return(false)
end
it { is_expected.to eq(success: true, result: { jira: true }) }
......@@ -113,7 +113,7 @@
context 'when vulnerabilities integration is enabled' do
before do
allow(jira_integration.project).to receive(:jira_vulnerabilities_integration_enabled?).and_return(true)
allow(jira_integration).to receive(:jira_vulnerabilities_integration_enabled?).and_return(true)
end
context 'when deployment type is cloud' do
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Integrations::Test::GroupService, feature_category: :integrations do
include AfterNextHelpers
describe '#execute' do
let_it_be(:group) { create(:group) }
let_it_be(:integration) { create(:integrations_slack, :group, group: group) }
let_it_be(:user) { create(:user) }
let(:event) { nil }
let(:sample_data) { { data: 'sample' } }
let(:success_result) { { success: true, result: {} } }
subject(:test_service) { described_class.new(integration, user, event).execute }
before_all do
group.add_owner(user)
end
context 'without event specified' do
it 'tests the integration with default data' do
allow(Gitlab::DataBuilder::Push).to receive(:sample_data).and_return(sample_data)
expect(integration).to receive(:test).with(sample_data).and_return(success_result)
expect(test_service).to eq(success_result)
end
end
context 'with event specified' do
context 'if event is not supported by integration' do
let_it_be(:integration) { create(:jira_integration, :group, group: group) }
let(:event) { 'push' }
it 'returns error message' do
expect(test_service).to include({ status: :error, message: 'Testing not available for this event' })
end
end
context 'for `push` event' do
let(:event) { 'push' }
it 'executes integration' do
allow(Gitlab::DataBuilder::Push).to receive(:sample_data).and_return(sample_data)
expect(integration).to receive(:test).with(sample_data).and_return(success_result)
expect(test_service).to eq(success_result)
end
end
context 'for `tag_push` event' do
let(:event) { 'tag_push' }
it 'executes integration' do
allow(Gitlab::DataBuilder::Push).to receive(:sample_data).and_return(sample_data)
expect(integration).to receive(:test).with(sample_data).and_return(success_result)
expect(test_service).to eq(success_result)
end
end
end
end
end
......@@ -77,5 +77,34 @@
end
it_behaves_like 'unknown integration'
context 'with untestable integration' do
before do
allow_next_found_instance_of(integration.class) do |integration|
allow(integration).to receive(:testable?).and_return(false)
end
put :test, params: routing_params
end
it 'returns 404 Not Found' do
expect(response).to have_gitlab_http_status(:not_found)
end
end
context 'with testable integration' do
before do
allow_next_found_instance_of(integration.class) do |integration|
allow(integration).to receive(:testable?).and_return(true)
allow(integration).to receive(:test).and_return({ success: true, data: [] })
end
put :test, params: routing_params
end
it 'returns 200' do
expect(response).to have_gitlab_http_status(:ok)
end
end
end
end
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册