Commit c4a7c7ec authored by Grzegorz Bizon's avatar Grzegorz Bizon

Merge branch 'move-pull-request-event-service-to-ci' into 'master'

Move part of external pull request code to CI

See merge request gitlab-org/gitlab!28517
parents 6b08bc51 d2c83de2
# frozen_string_literal: true
# This service is responsible for creating a pipeline for a given
# ExternalPullRequest coming from other providers such as GitHub.
module Ci
module ExternalPullRequests
class CreatePipelineService < BaseService
def execute(pull_request)
return unless pull_request.open? && pull_request.actual_branch_head?
create_pipeline_for(pull_request)
end
private
def create_pipeline_for(pull_request)
Ci::CreatePipelineService.new(project, current_user, create_params(pull_request))
.execute(:external_pull_request_event, external_pull_request: pull_request)
end
def create_params(pull_request)
{
ref: pull_request.source_ref,
source_sha: pull_request.source_sha,
target_sha: pull_request.target_sha
}
end
end
end
end
# frozen_string_literal: true
# This service is responsible for creating a pipeline for a given
# ExternalPullRequest coming from other providers such as GitHub.
module ExternalPullRequests
class CreatePipelineService < BaseService
def execute(pull_request)
return unless pull_request.open? && pull_request.actual_branch_head?
create_pipeline_for(pull_request)
end
private
def create_pipeline_for(pull_request)
Ci::CreatePipelineService.new(project, current_user, create_params(pull_request))
.execute(:external_pull_request_event, external_pull_request: pull_request)
end
def create_params(pull_request)
{
ref: pull_request.source_ref,
source_sha: pull_request.source_sha,
target_sha: pull_request.target_sha
}
end
end
end
......@@ -21,7 +21,7 @@ class UpdateExternalPullRequestsWorker # rubocop:disable Scalability/IdempotentW
.by_source_branch(branch)
external_pull_requests.find_each do |pull_request|
ExternalPullRequests::CreatePipelineService.new(project, user)
Ci::ExternalPullRequests::CreatePipelineService.new(project, user)
.execute(pull_request)
end
end
......
# frozen_string_literal: true
module Ci
module ExternalPullRequests
class ProcessGithubEventService < ::BaseService
# All possible statuses available here:
# https://developer.github.com/v3/activity/events/types/#pullrequestevent
GITHUB_ACTIONS_TO_STATUS = {
'opened' => :open,
'reopened' => :open,
'synchronize' => :open,
'closed' => :closed
}.freeze
def execute(webhook_params)
return unless project.github_external_pull_request_pipelines_available?
params = params_from_webhook(webhook_params)
return unless params[:status]
# Save pull request info for later. when mirror update will run
# and the pipeline is created for all newly pushed branches.
# At that point we will be able to reference it back if a pull request
# was created.
::ExternalPullRequest.create_or_update_from_params(params).tap do |pull_request|
if pull_request.errors.empty?
Ci::ExternalPullRequests::CreatePipelineService.new(project, current_user)
.execute(pull_request)
end
end
end
private
def params_from_webhook(params)
{
project_id: project.id,
pull_request_iid: params.dig(:pull_request, :number),
source_branch: params.dig(:pull_request, :head, :ref),
source_sha: params.dig(:pull_request, :head, :sha),
source_repository: params.dig(:pull_request, :head, :repo, :full_name),
target_branch: params.dig(:pull_request, :base, :ref),
target_sha: params.dig(:pull_request, :base, :sha),
target_repository: params.dig(:pull_request, :base, :repo, :full_name),
status: GITHUB_ACTIONS_TO_STATUS[params[:action]]
}
end
end
end
end
# frozen_string_literal: true
class ProcessGithubPullRequestEventService < ::BaseService
# All possible statuses available here:
# https://developer.github.com/v3/activity/events/types/#pullrequestevent
GITHUB_ACTIONS_TO_STATUS = {
'opened' => :open,
'reopened' => :open,
'synchronize' => :open,
'closed' => :closed
}.freeze
def execute(webhook_params)
return unless project.github_external_pull_request_pipelines_available?
params = params_from_webhook(webhook_params)
return unless params[:status]
# Save pull request info for later. when mirror update will run
# and the pipeline is created for all newly pushed branches.
# At that point we will be able to reference it back if a pull request
# was created.
ExternalPullRequest.create_or_update_from_params(params).tap do |pull_request|
if pull_request.errors.empty?
ExternalPullRequests::CreatePipelineService.new(project, current_user)
.execute(pull_request)
end
end
end
private
def params_from_webhook(params)
{
project_id: project.id,
pull_request_iid: params.dig(:pull_request, :number),
source_branch: params.dig(:pull_request, :head, :ref),
source_sha: params.dig(:pull_request, :head, :sha),
source_repository: params.dig(:pull_request, :head, :repo, :full_name),
target_branch: params.dig(:pull_request, :base, :ref),
target_sha: params.dig(:pull_request, :base, :sha),
target_repository: params.dig(:pull_request, :base, :repo, :full_name),
status: GITHUB_ACTIONS_TO_STATUS[params[:action]]
}
end
end
......@@ -49,7 +49,7 @@ module API
end
def process_pull_request
external_pull_request = ProcessGithubPullRequestEventService.new(project, mirror_user).execute(params)
external_pull_request = Ci::ExternalPullRequests::ProcessGithubEventService.new(project, mirror_user).execute(params)
if external_pull_request
render_validation_error!(external_pull_request)
......
......@@ -2,7 +2,7 @@
require 'spec_helper'
describe ProcessGithubPullRequestEventService do
describe Ci::ExternalPullRequests::ProcessGithubEventService do
let_it_be(:project) { create(:project, :repository) }
let_it_be(:user) { create(:user) }
let(:action) { 'opened' }
......
......@@ -2,7 +2,7 @@
require 'spec_helper'
describe ExternalPullRequests::CreatePipelineService do
describe Ci::ExternalPullRequests::CreatePipelineService do
describe '#execute' do
let_it_be(:project) { create(:project, :auto_devops, :repository) }
let_it_be(:user) { create(:user) }
......
......@@ -28,10 +28,10 @@ describe UpdateExternalPullRequestsWorker do
context 'when ref is a branch' do
let(:ref) { 'refs/heads/feature-1' }
let(:create_pipeline_service) { instance_double(ExternalPullRequests::CreatePipelineService) }
let(:create_pipeline_service) { instance_double(Ci::ExternalPullRequests::CreatePipelineService) }
it 'runs CreatePipelineService for each pull request matching the source branch and repository' do
expect(ExternalPullRequests::CreatePipelineService)
expect(Ci::ExternalPullRequests::CreatePipelineService)
.to receive(:new)
.and_return(create_pipeline_service)
.twice
......@@ -45,7 +45,7 @@ describe UpdateExternalPullRequestsWorker do
let(:ref) { 'refs/tags/v1.2.3' }
it 'does nothing' do
expect(ExternalPullRequests::CreatePipelineService).not_to receive(:new)
expect(Ci::ExternalPullRequests::CreatePipelineService).not_to receive(:new)
subject
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