Commit 2ed2ef92 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Remove network from CI

parent 187face6
......@@ -4,14 +4,8 @@ module Ci
"app/helpers/ci"
end
include Ci::UserSessionsHelper
rescue_from Ci::Network::UnauthorizedError, with: :invalid_token
before_filter :default_headers
helper_method :gl_project
protect_from_forgery
private
def authenticate_public_page!
......@@ -75,27 +69,6 @@ module Ci
}
end
def check_config
redirect_to oauth2_ci_help_path unless valid_config?
end
def valid_config?
server = GitlabCi.config.gitlab_server
if server.blank? || server.url.blank? || server.app_id.blank? || server.app_secret.blank?
false
else
true
end
rescue Settingslogic::MissingSetting, NoMethodError
false
end
def invalid_token
reset_session
redirect_to ci_root_path
end
def gl_project
::Project.find(@project.gitlab_id)
end
......
......@@ -27,7 +27,7 @@ module Ci
@projects = Ci::Project.where(gitlab_id: @gl_projects.map(&:id)).ordered_by_last_commit_date
@total_count = @gl_projects.size
@gl_projects = @gl_projects.where.not(id: @projects.map(&:gitlab_id))
respond_to do |format|
......@@ -35,8 +35,6 @@ module Ci
pager_json("ci/projects/gitlab", @total_count)
end
end
rescue Ci::Network::UnauthorizedError
raise
rescue
@error = 'Failed to fetch GitLab projects'
end
......@@ -82,8 +80,8 @@ module Ci
end
def destroy
project.gl_project.gitlab_ci_service.update_attributes(active: false)
project.destroy
Ci::Network.new.disable_ci(project.gitlab_id, current_user.authenticate_options)
Ci::EventService.new.remove_project(current_user, project)
......
module Ci
class Network
class UnauthorizedError < StandardError; end
include HTTParty
API_PREFIX = '/api/v3/'
def authenticate(api_opts)
opts = {
query: api_opts
}
endpoint = File.join(url, API_PREFIX, 'user')
response = self.class.get(endpoint, default_opts.merge(opts))
build_response(response)
end
def projects(api_opts, scope = :owned)
# Dont load archived projects
api_opts.merge!(archived: false)
opts = {
query: api_opts
}
query = if scope == :owned
'projects/owned.json'
else
'projects.json'
end
endpoint = File.join(url, API_PREFIX, query)
response = self.class.get(endpoint, default_opts.merge(opts))
build_response(response)
end
def project(api_opts, project_id)
opts = {
query: api_opts
}
query = "projects/#{project_id}.json"
endpoint = File.join(url, API_PREFIX, query)
response = self.class.get(endpoint, default_opts.merge(opts))
build_response(response)
end
def project_hooks(api_opts, project_id)
opts = {
query: api_opts
}
query = "projects/#{project_id}/hooks.json"
endpoint = File.join(url, API_PREFIX, query)
response = self.class.get(endpoint, default_opts.merge(opts))
build_response(response)
end
def enable_ci(project_id, data, api_opts)
opts = {
body: data.to_json,
query: api_opts
}
query = "projects/#{project_id}/services/gitlab-ci.json"
endpoint = File.join(url, API_PREFIX, query)
response = self.class.put(endpoint, default_opts.merge(opts))
case response.code
when 200
true
when 401
raise UnauthorizedError
else
nil
end
end
def disable_ci(project_id, api_opts)
opts = {
query: api_opts
}
query = "projects/#{project_id}/services/gitlab-ci.json"
endpoint = File.join(url, API_PREFIX, query)
response = self.class.delete(endpoint, default_opts.merge(opts))
build_response(response)
end
private
def url
Gitlab.config.gitlab.url
end
def default_opts
{
headers: { "Content-Type" => "application/json" },
}
end
def build_response(response)
case response.code
when 200
response.parsed_response
when 401
raise UnauthorizedError
else
nil
end
end
end
end
......@@ -31,7 +31,7 @@ module Ci
include Ci::ProjectStatus
belongs_to :gl_project, class_name: 'Project', foreign_key: :gitlab_id
belongs_to :gl_project, class_name: '::Project', foreign_key: :gitlab_id
has_many :commits, ->() { order(:committed_at) }, dependent: :destroy, class_name: 'Ci::Commit'
has_many :builds, through: :commits, dependent: :destroy, class_name: 'Ci::Build'
......@@ -92,11 +92,13 @@ module Ci
project
end
# TODO: remove
def from_gitlab(user, scope = :owned, options)
opts = user.authenticate_options
opts.merge! options
projects = Ci::Network.new.projects(opts.compact, scope)
raise 'Implement me of fix'
#projects = Ci::Network.new.projects(opts.compact, scope)
if projects
projects.map { |pr| OpenStruct.new(pr) }
......
......@@ -13,9 +13,9 @@ module Ci
project_url: project_route.gsub(":project_id", @project.id.to_s),
}
unless Ci::Network.new.enable_ci(@project.gitlab_id, data, {private_token: current_user.private_token})
raise ActiveRecord::Rollback
end
gl_project = ::Project.find(@project.gitlab_id)
gl_project.build_missing_services
gl_project.gitlab_ci_service.update_attributes(data.merge(active: true))
end
if forked_project
......
......@@ -20,7 +20,7 @@ module API
end
required_attributes! validators.map(&:attributes).flatten.uniq
attrs = attributes_for_keys service_attributes
attrs = attributes_for_keys service_attributes
if project_service.update_attributes(attrs.merge(active: true))
true
......@@ -41,7 +41,7 @@ module API
attrs = service_attributes.inject({}) do |hash, key|
hash.merge!(key => nil)
end
if project_service.update_attributes(attrs.merge(active: false))
true
else
......
......@@ -18,7 +18,7 @@ module Ci
project = Ci::Project.find(params[:project_id])
unauthorized! unless current_user.can_manage_project?(project.gitlab_id)
web_hook = project.web_hooks.new({ url: params[:web_hook] })
if web_hook.save
......
require 'spec_helper'
describe Network do
let(:network) { Network.new }
describe :enable_ci do
subject { network.enable_ci '', '', '' }
context 'on success' do
before do
response = double
allow(response).to receive(:code) { 200 }
allow(network.class).to receive(:put) { response }
end
it { is_expected.to be_truthy }
end
context 'on failure' do
before do
response = double
allow(response).to receive(:code) { 404 }
allow(network.class).to receive(:put) { response }
end
it { is_expected.to be_nil }
end
end
describe :disable_ci do
let(:response) { double }
subject { network.disable_ci '', '' }
context 'on success' do
let(:parsed_response) { 'parsed' }
before do
allow(response).to receive(:code) { 200 }
allow(response).to receive(:parsed_response) { parsed_response }
allow(network.class).to receive(:delete) { response }
end
it { is_expected.to equal(parsed_response) }
end
context 'on failure' do
before do
allow(response).to receive(:code) { 404 }
allow(network.class).to receive(:delete) { response }
end
it { is_expected.to be_nil }
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