Merge branch 'fix-external-ci-services' into 'master'

Allow to access statuses for external CI services

Closes #30714, #29369, and #15220

See merge request !11176
This commit is contained in:
Grzegorz Bizon 2017-06-14 07:35:27 +00:00
commit da66c90b0f
24 changed files with 175 additions and 43 deletions

View file

@ -80,10 +80,6 @@ class Projects::ApplicationController < ApplicationController
cookies.permanent[:diff_view] = params.delete(:view) if params[:view].present?
end
def builds_enabled
return render_404 unless @project.feature_available?(:builds, current_user)
end
def require_pages_enabled!
not_found unless Gitlab.config.pages.enabled
end

View file

@ -5,7 +5,6 @@ class Projects::GraphsController < Projects::ApplicationController
before_action :require_non_empty_project
before_action :assign_ref_vars
before_action :authorize_download_code!
before_action :builds_enabled, only: :ci
def show
respond_to do |format|

View file

@ -4,7 +4,6 @@ class Projects::PipelinesController < Projects::ApplicationController
before_action :authorize_read_pipeline!
before_action :authorize_create_pipeline!, only: [:new, :create]
before_action :authorize_update_pipeline!, only: [:retry, :cancel]
before_action :builds_enabled, only: :charts
wrap_parameters Ci::Pipeline

View file

@ -218,6 +218,10 @@ module ProjectsHelper
nav_tabs << :container_registry
end
if project.builds_enabled? && can?(current_user, :read_pipeline, project)
nav_tabs << :pipelines
end
tab_ability_map.each do |tab, ability|
if can?(current_user, ability, project)
nav_tabs << tab
@ -231,7 +235,6 @@ module ProjectsHelper
{
environments: :read_environment,
milestones: :read_milestone,
pipelines: :read_pipeline,
snippets: :read_project_snippet,
settings: :admin_project,
builds: :read_build,

View file

@ -11,6 +11,7 @@ class GenericCommitStatus < CommitStatus
def set_default_values
self.context ||= 'default'
self.stage ||= 'external'
self.stage_idx ||= 1000000
end
def tags

View file

@ -203,7 +203,7 @@ class ProjectPolicy < BasePolicy
unless project.feature_available?(:builds, user) && repository_enabled
cannot!(*named_abilities(:build))
cannot!(*named_abilities(:pipeline))
cannot!(*named_abilities(:pipeline) - [:read_pipeline])
cannot!(*named_abilities(:pipeline_schedule))
cannot!(*named_abilities(:environment))
cannot!(*named_abilities(:deployment))

View file

@ -1,4 +1,4 @@
class BuildDetailsEntity < BuildEntity
class BuildDetailsEntity < JobEntity
expose :coverage, :erased_at, :duration
expose :tag_list, as: :tags
expose :user, using: UserEntity

View file

@ -1,5 +1,5 @@
class BuildSerializer < BaseSerializer
entity BuildEntity
entity JobEntity
def represent_status(resource)
data = represent(resource, { only: [:status] })

View file

@ -24,6 +24,6 @@ class DeploymentEntity < Grape::Entity
expose :user, using: UserEntity
expose :commit, using: CommitEntity
expose :deployable, using: BuildEntity
expose :manual_actions, using: BuildEntity
expose :deployable, using: JobEntity
expose :manual_actions, using: JobEntity
end

View file

@ -1,11 +1,11 @@
class BuildEntity < Grape::Entity
class JobEntity < Grape::Entity
include RequestAwareEntity
expose :id
expose :name
expose :build_path do |build|
path_to(:namespace_project_job, build)
build.target_url || path_to(:namespace_project_job, build)
end
expose :retry_path, if: -> (*) { retryable? } do |build|

View file

@ -4,7 +4,7 @@ class JobGroupEntity < Grape::Entity
expose :name
expose :size
expose :detailed_status, as: :status, with: StatusEntity
expose :jobs, with: BuildEntity
expose :jobs, with: JobEntity
private

View file

@ -101,12 +101,12 @@ class GitPushService < BaseService
UpdateMergeRequestsWorker
.perform_async(@project.id, current_user.id, params[:oldrev], params[:newrev], params[:ref])
SystemHookPushWorker.perform_async(build_push_data.dup, :push_hooks)
EventCreateService.new.push(@project, current_user, build_push_data)
Ci::CreatePipelineService.new(@project, current_user, build_push_data).execute(:push)
SystemHookPushWorker.perform_async(build_push_data.dup, :push_hooks)
@project.execute_hooks(build_push_data.dup, :push_hooks)
@project.execute_services(build_push_data.dup, :push_hooks)
Ci::CreatePipelineService.new(@project, current_user, build_push_data).execute(:push)
if push_remove_branch?
AfterBranchDeleteService

View file

@ -8,10 +8,12 @@ class GitTagPushService < BaseService
@push_data = build_push_data
EventCreateService.new.push(project, current_user, @push_data)
Ci::CreatePipelineService.new(project, current_user, @push_data).execute(:push)
SystemHooksService.new.execute_hooks(build_system_push_data.dup, :tag_push_hooks)
project.execute_hooks(@push_data.dup, :tag_push_hooks)
project.execute_services(@push_data.dup, :tag_push_hooks)
Ci::CreatePipelineService.new(project, current_user, @push_data).execute(:push)
ProjectCacheWorker.perform_async(project.id, [], [:commit_count, :repository_size])
true

View file

@ -0,0 +1,4 @@
---
title: Fix support for external CI services
merge_request: 11176
author:

View file

@ -3,6 +3,10 @@ module Gitlab
module Status
module External
module Common
def label
subject.description
end
def has_details?
subject.target_url.present? &&
can?(user, :read_commit_status, subject)

View file

@ -5,9 +5,12 @@ describe Projects::PipelinesController do
let(:user) { create(:user) }
let(:project) { create(:empty_project, :public) }
let(:feature) { ProjectFeature::DISABLED }
before do
project.add_developer(user)
project.project_feature.update(
builds_access_level: feature)
sign_in(user)
end
@ -153,16 +156,26 @@ describe Projects::PipelinesController do
format: :json
end
it 'retries a pipeline without returning any content' do
expect(response).to have_http_status(:no_content)
expect(build.reload).to be_retried
context 'when builds are enabled' do
let(:feature) { ProjectFeature::ENABLED }
it 'retries a pipeline without returning any content' do
expect(response).to have_http_status(:no_content)
expect(build.reload).to be_retried
end
end
context 'when builds are disabled' do
it 'fails to retry pipeline' do
expect(response).to have_http_status(:not_found)
end
end
end
describe 'POST cancel.json' do
let!(:pipeline) { create(:ci_pipeline, project: project) }
let!(:build) { create(:ci_build, :running, pipeline: pipeline) }
before do
post :cancel, namespace_id: project.namespace,
project_id: project,
@ -170,9 +183,19 @@ describe Projects::PipelinesController do
format: :json
end
it 'cancels a pipeline without returning any content' do
expect(response).to have_http_status(:no_content)
expect(pipeline.reload).to be_canceled
context 'when builds are enabled' do
let(:feature) { ProjectFeature::ENABLED }
it 'cancels a pipeline without returning any content' do
expect(response).to have_http_status(:no_content)
expect(pipeline.reload).to be_canceled
end
end
context 'when builds are disabled' do
it 'fails to retry pipeline' do
expect(response).to have_http_status(:not_found)
end
end
end
end

View file

@ -68,9 +68,12 @@ describe 'Edit Project Settings', feature: true do
end
describe 'project features visibility pages' do
let(:pipeline) { create(:ci_empty_pipeline, project: project) }
let(:job) { create(:ci_build, pipeline: pipeline) }
let(:tools) do
{
builds: namespace_project_pipelines_path(project.namespace, project),
builds: namespace_project_job_path(project.namespace, project, job),
issues: namespace_project_issues_path(project.namespace, project),
wiki: namespace_project_wiki_path(project.namespace, project, :home),
snippets: namespace_project_snippets_path(project.namespace, project),

View file

@ -300,4 +300,37 @@ describe ProjectsHelper do
expect(helper.send(:visibility_select_options, project, Gitlab::VisibilityLevel::PRIVATE)).to include('Private')
end
end
describe '#get_project_nav_tabs' do
let(:project) { create(:empty_project) }
let(:user) { create(:user) }
before do
allow(helper).to receive(:can?) { true }
end
subject do
helper.send(:get_project_nav_tabs, project, user)
end
context 'when builds feature is enabled' do
before do
allow(project).to receive(:builds_enabled?).and_return(true)
end
it "does include pipelines tab" do
is_expected.to include(:pipelines)
end
end
context 'when builds feature is disabled' do
before do
allow(project).to receive(:builds_enabled?).and_return(false)
end
it "do not include pipelines tab" do
is_expected.not_to include(:pipelines)
end
end
end
end

View file

@ -4,9 +4,10 @@ describe Gitlab::Ci::Status::External::Common do
let(:user) { create(:user) }
let(:project) { external_status.project }
let(:external_target_url) { 'http://example.gitlab.com/status' }
let(:external_description) { 'my description' }
let(:external_status) do
create(:generic_commit_status, target_url: external_target_url)
create(:generic_commit_status, target_url: external_target_url, description: external_description)
end
subject do
@ -15,6 +16,12 @@ describe Gitlab::Ci::Status::External::Common do
.extend(described_class)
end
describe '#label' do
it 'returns description' do
expect(subject.label).to eq external_description
end
end
describe '#has_action?' do
it { is_expected.not_to have_action }
end

View file

@ -139,6 +139,18 @@ describe ProjectPolicy, models: true do
is_expected.not_to include(:read_build, :read_pipeline)
end
end
context 'when builds are disabled' do
before do
project.project_feature.update(
builds_access_level: ProjectFeature::DISABLED)
end
it do
is_expected.not_to include(:read_build)
is_expected.to include(:read_pipeline)
end
end
end
context 'reporter' do

View file

@ -3,8 +3,8 @@ require 'spec_helper'
describe BuildDetailsEntity do
set(:user) { create(:admin) }
it 'inherits from BuildEntity' do
expect(described_class).to be < BuildEntity
it 'inherits from JobEntity' do
expect(described_class).to be < JobEntity
end
describe '#as_json' do

View file

@ -1,9 +1,9 @@
require 'spec_helper'
describe BuildEntity do
describe JobEntity do
let(:user) { create(:user) }
let(:build) { create(:ci_build) }
let(:project) { build.project }
let(:job) { create(:ci_build) }
let(:project) { job.project }
let(:request) { double('request') }
before do
@ -12,12 +12,12 @@ describe BuildEntity do
end
let(:entity) do
described_class.new(build, request: request)
described_class.new(job, request: request)
end
subject { entity.as_json }
it 'contains paths to build page action' do
it 'contains paths to job page action' do
expect(subject).to include(:build_path)
end
@ -27,7 +27,7 @@ describe BuildEntity do
end
it 'contains whether it is playable' do
expect(subject[:playable]).to eq build.playable?
expect(subject[:playable]).to eq job.playable?
end
it 'contains timestamps' do
@ -39,9 +39,9 @@ describe BuildEntity do
expect(subject[:status]).to include :icon, :favicon, :text, :label
end
context 'when build is retryable' do
context 'when job is retryable' do
before do
build.update(status: :failed)
job.update(status: :failed)
end
it 'contains cancel path' do
@ -49,9 +49,9 @@ describe BuildEntity do
end
end
context 'when build is cancelable' do
context 'when job is cancelable' do
before do
build.update(status: :running)
job.update(status: :running)
end
it 'contains cancel path' do
@ -59,7 +59,7 @@ describe BuildEntity do
end
end
context 'when build is a regular build' do
context 'when job is a regular job' do
it 'does not contain path to play action' do
expect(subject).not_to include(:play_path)
end
@ -69,8 +69,8 @@ describe BuildEntity do
end
end
context 'when build is a manual action' do
let(:build) { create(:ci_build, :manual) }
context 'when job is a manual action' do
let(:job) { create(:ci_build, :manual) }
context 'when user is allowed to trigger action' do
before do
@ -99,4 +99,25 @@ describe BuildEntity do
end
end
end
context 'when job is generic commit status' do
let(:job) { create(:generic_commit_status, target_url: 'http://google.com') }
it 'contains paths to target action' do
expect(subject).to include(:build_path)
end
it 'does not contain paths to other action paths' do
expect(subject).not_to include(:retry_path, :cancel_path, :play_path)
end
it 'contains timestamps' do
expect(subject).to include(:created_at, :updated_at)
end
it 'contains details' do
expect(subject).to include :status
expect(subject[:status]).to include :icon, :favicon, :text, :label
end
end
end

View file

@ -91,6 +91,20 @@ describe PipelineDetailsEntity do
end
end
context 'when pipeline has commit statuses' do
let(:pipeline) { create(:ci_empty_pipeline) }
before do
create(:generic_commit_status, pipeline: pipeline)
end
it 'contains stages' do
expect(subject).to include(:details)
expect(subject[:details]).to include(:stages)
expect(subject[:details][:stages].first).to include(name: 'external')
end
end
context 'when pipeline has YAML errors' do
let(:pipeline) do
create(:ci_pipeline, config: { rspec: { invalid: :value } })

View file

@ -54,6 +54,17 @@ describe StageEntity do
it 'exposes the group key' do
expect(subject).to include :groups
end
context 'and contains commit status' do
before do
create(:generic_commit_status, pipeline: pipeline, stage: 'test')
end
it 'contains commit status' do
groups = subject[:groups].map { |group| group[:name] }
expect(groups).to include('generic')
end
end
end
end
end