Commit a8c12e95 authored by James Edwards-Jones's avatar James Edwards-Jones Committed by Douwe Maan

CI/CD GitHub import sets up service integration

Sets up service to notify GitHub of pipeline status
parent 3da6d69a
module CiCd
class GithubIntegrationSetupService
attr_reader :project
def initialize(project)
@project = project
end
def execute
github_integration.save
end
private
def github_integration
@github_integration ||= project.build_github_service(github_params)
end
def github_params
GithubParams.new(project).configuration_params
end
class GithubParams
def initialize(project)
@project = project
end
def repository_url
"#{import_uri.scheme}://#{import_uri.host}/#{repo_full_name}"
end
def configuration_params
{
active: true,
repository_url: repository_url,
token: github_access_token
}
end
private
def github_access_token
@project.import_data&.credentials&.dig(:user)
end
def repo_full_name
@project.import_source
end
def import_uri
URI.parse(@project.import_url)
end
end
end
end
...@@ -8,6 +8,7 @@ module CiCd ...@@ -8,6 +8,7 @@ module CiCd
def execute def execute
create_webhook create_webhook
setup_project_integration
end end
private private
...@@ -15,5 +16,9 @@ module CiCd ...@@ -15,5 +16,9 @@ module CiCd
def create_webhook def create_webhook
::CreateGithubWebhookWorker.perform_async(project.id) ::CreateGithubWebhookWorker.perform_async(project.id)
end end
def setup_project_integration
::CiCd::GithubIntegrationSetupService.new(project).execute
end
end end
end end
---
title: GitHub CI/CD import sets up pipeline notification integration
merge_request: 4687
author:
type: added
require 'spec_helper'
describe CiCd::GithubIntegrationSetupService do
let(:repo_full_name) { "MyUser/my-project" }
let(:api_token) { "abcdefghijk123" }
let(:import_url) { "https://#{api_token}@github.com/#{repo_full_name}.git" }
let(:credentials) { { user: api_token } }
let(:project) do
create(:project, import_source: repo_full_name,
import_url: import_url,
import_data_attributes: { credentials: credentials } )
end
subject { described_class.new(project) }
before do
subject.execute
end
describe 'sets up GitHub service integration' do
let(:integration) { project.github_service }
specify 'with API token' do
expect(integration.token).to eq api_token
end
specify 'with repo URL' do
expect(integration.repository_url).to eq 'https://github.com/MyUser/my-project'
end
end
end
...@@ -13,5 +13,13 @@ describe CiCd::GithubSetupService do ...@@ -13,5 +13,13 @@ describe CiCd::GithubSetupService do
subject.execute subject.execute
end end
it 'sets up GithubService project integration' do
allow(subject).to receive(:create_webhook)
subject.execute
expect(project.github_service).to be_active
end
end end
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