Skip to content
Snippets Groups Projects
Commit d8e31e82 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets
Browse files

Merge branch 'refactoring_user_url' into 'master'

Refactoring network related code

https://dev.gitlab.org/gitlab/gitlab-ci/issues/139

See merge request !74
parents 5939b183 db03dc31
No related branches found
No related tags found
No related merge requests found
Showing
with 53 additions and 60 deletions
Loading
Loading
@@ -75,7 +75,7 @@ class ProjectsController < ApplicationController
 
def destroy
project.destroy
Network.new.disable_ci(current_user.url, project.gitlab_id, current_user.private_token)
Network.new.disable_ci(project.gitlab_id, current_user.private_token)
 
EventService.new.remove_project(current_user, project)
 
Loading
Loading
Loading
Loading
@@ -18,7 +18,7 @@ class UserSessionsController < ApplicationController
token = client.auth_code.get_token(params[:code], redirect_uri: callback_user_sessions_url).token
@user_session = UserSession.new
user = @user_session.authenticate(access_token: token, url: GitlabCi.config.gitlab_server.url)
user = @user_session.authenticate(access_token: token)
 
if user && sign_in(user)
redirect_to root_path
Loading
Loading
Loading
Loading
@@ -5,38 +5,35 @@ class Network
 
API_PREFIX = '/api/v3/'
 
def authenticate(url, api_opts)
def authenticate(api_opts)
opts = {
body: api_opts.to_json,
headers: { "Content-Type" => "application/json" },
body: api_opts.to_json
}
 
endpoint = File.join(url, API_PREFIX, 'user')
response = self.class.get(endpoint, opts)
response = self.class.get(endpoint, default_opts.merge(opts))
 
build_response(response)
end
 
def authenticate_by_token(url, api_opts)
def authenticate_by_token(api_opts)
opts = {
query: api_opts,
headers: { "Content-Type" => "application/json" },
query: api_opts
}
 
endpoint = File.join(url, API_PREFIX, 'user.json')
response = self.class.get(endpoint, opts)
response = self.class.get(endpoint, default_opts.merge(opts))
 
build_response(response)
end
 
 
def projects(url, api_opts, scope = :owned)
def projects(api_opts, scope = :owned)
# Dont load archived projects
api_opts.merge!(archived: false)
 
opts = {
query: api_opts,
headers: { "Content-Type" => "application/json" },
query: api_opts
}
 
query = if scope == :owned
Loading
Loading
@@ -46,48 +43,45 @@ class Network
end
 
endpoint = File.join(url, API_PREFIX, query)
response = self.class.get(endpoint, opts)
response = self.class.get(endpoint, default_opts.merge(opts))
 
build_response(response)
end
 
def project(url, api_opts, project_id)
def project(api_opts, project_id)
opts = {
query: api_opts,
headers: { "Content-Type" => "application/json" },
query: api_opts
}
 
query = "projects/#{project_id}.json"
 
endpoint = File.join(url, API_PREFIX, query)
response = self.class.get(endpoint, opts)
response = self.class.get(endpoint, default_opts.merge(opts))
 
build_response(response)
end
 
def project_hooks(url, api_opts, project_id)
def project_hooks(api_opts, project_id)
opts = {
query: api_opts,
headers: { "Content-Type" => "application/json" },
query: api_opts
}
 
query = "projects/#{project_id}/hooks.json"
 
endpoint = File.join(url, API_PREFIX, query)
response = self.class.get(endpoint, opts)
response = self.class.get(endpoint, default_opts.merge(opts))
 
build_response(response)
end
 
def enable_ci(url, project_id, ci_opts, token)
def enable_ci(project_id, api_opts, token)
opts = {
body: ci_opts.to_json,
headers: { "Content-Type" => "application/json" },
body: api_opts.to_json
}
 
query = "projects/#{project_id}/services/gitlab-ci.json?private_token=#{token}"
endpoint = File.join(url, API_PREFIX, query)
response = self.class.put(endpoint, opts)
response = self.class.put(endpoint, default_opts.merge(opts))
 
case response.code
when 200
Loading
Loading
@@ -99,21 +93,27 @@ class Network
end
end
 
def disable_ci(url, project_id, token)
opts = {
headers: { "Content-Type" => "application/json" },
}
def disable_ci(project_id, token)
query = "projects/#{project_id}/services/gitlab-ci.json?private_token=#{token}"
 
endpoint = File.join(url, API_PREFIX, query)
response = self.class.delete(endpoint, opts)
response = self.class.delete(endpoint, default_opts)
 
build_response(response)
end
 
private
 
def url
GitlabCi.config.gitlab_server.url
end
def default_opts
{
headers: { "Content-Type" => "application/json" },
}
end
def build_response(response)
case response.code
when 200
Loading
Loading
Loading
Loading
@@ -94,7 +94,7 @@ ls -la
opts = { private_token: user.private_token }
opts.merge! options
 
projects = Network.new.projects(user.url, opts.compact, scope)
projects = Network.new.projects(opts.compact, scope)
 
if projects
projects.map { |pr| OpenStruct.new(pr) }
Loading
Loading
Loading
Loading
@@ -61,7 +61,7 @@ class User
}
 
Rails.cache.fetch(cache_key('manage', project_gitlab_id, sync_at)) do
!!Network.new.project_hooks(self.url, opts, project_gitlab_id)
!!Network.new.project_hooks(opts, project_gitlab_id)
end
end
 
Loading
Loading
@@ -82,7 +82,7 @@ class User
}
 
Rails.cache.fetch(cache_key("project_info", project_gitlab_id, sync_at)) do
Network.new.project(self.url, opts, project_gitlab_id)
Network.new.project(opts, project_gitlab_id)
end
end
end
Loading
Loading
@@ -3,17 +3,15 @@ class UserSession
include StaticModel
extend ActiveModel::Naming
 
attr_accessor :url
def authenticate(auth_opts)
authenticate_via(auth_opts) do |url, network, options|
network.authenticate(url, options)
authenticate_via(auth_opts) do |network, options|
network.authenticate(options)
end
end
 
def authenticate_by_token(auth_opts)
result = authenticate_via(auth_opts) do |url, network, options|
network.authenticate_by_token(url, options)
result = authenticate_via(auth_opts) do |network, options|
network.authenticate_by_token(options)
end
 
result
Loading
Loading
@@ -22,14 +20,10 @@ class UserSession
private
 
def authenticate_via(options, &block)
url = options.delete(:url)
return nil unless GitlabCi.config.gitlab_server.url.include?(url)
user = block.call(url, Network.new, options)
user = block.call(Network.new, options)
 
if user
return User.new(user.merge({ "url" => url }))
return User.new(user)
else
nil
end
Loading
Loading
Loading
Loading
@@ -13,7 +13,7 @@ class CreateProjectService
project_url: project_route.gsub(":project_id", @project.id.to_s),
}
 
unless Network.new.enable_ci(current_user.url, @project.gitlab_id, opts, current_user.private_token)
unless Network.new.enable_ci(@project.gitlab_id, opts, current_user.private_token)
raise ActiveRecord::Rollback
end
end
Loading
Loading
.clearfix.light
.pull-left.fetch-status
Fetched from GitLab (#{link_to current_user.url, current_user.url, no_turbolink})
Fetched from GitLab (#{link_to GitlabCi.config.gitlab_server.url, GitlabCi.config.gitlab_server.url, no_turbolink})
- if params[:search].present?
by keyword: "#{params[:search]}",
#{time_ago_in_words(current_user.sync_at)} ago.
Loading
Loading
Loading
Loading
@@ -8,7 +8,7 @@
.projects
%p.fetch-status.light
%i.icon-refresh.icon-spin
Please wait while we fetch from GitLab (#{current_user.url})
Please wait while we fetch from GitLab (#{GitlabCi.config.gitlab_server.url})
:javascript
$.get("#{gitlab_projects_path}")
- else
Loading
Loading
Loading
Loading
@@ -12,4 +12,4 @@
 
%p
%span.light GitLab profile:
%strong= link_to @user.username, @user.url + '/u/' + @user.username, target: "_blank"
%strong= link_to @user.username, GitlabCi.config.gitlab_server.url + '/u/' + @user.username, target: "_blank"
Loading
Loading
@@ -18,7 +18,7 @@ module API
authenticate_project_token!(project)
 
user_session = UserSession.new
user = user_session.authenticate_by_token(private_token: params[:private_token], url: GitlabCi.config.gitlab_server.url)
user = user_session.authenticate_by_token(private_token: params[:private_token])
 
fork = CreateProjectService.new.execute(
user,
Loading
Loading
Loading
Loading
@@ -7,8 +7,7 @@ module API
def current_user
@current_user ||= begin
options = {
private_token: (params[PRIVATE_TOKEN_PARAM] || env[PRIVATE_TOKEN_HEADER]),
url: params[:url]
private_token: (params[PRIVATE_TOKEN_PARAM] || env[PRIVATE_TOKEN_HEADER])
}
UserSession.new.authenticate_by_token(options)
end
Loading
Loading
Loading
Loading
@@ -82,7 +82,7 @@ describe ProjectsController do
it "searches projects" do
allow(controller).to receive(:reset_cache) { true }
allow(controller).to receive(:current_user) { user }
Network.any_instance.should_receive(:projects).with(anything(), hash_including(search: 'str'), :authorized)
Network.any_instance.should_receive(:projects).with(hash_including(search: 'str'), :authorized)
 
xhr :get, :gitlab, { search: "str", format: "js" }.with_indifferent_access
 
Loading
Loading
Loading
Loading
@@ -4,7 +4,7 @@ describe Network do
let(:network) { Network.new }
 
describe :enable_ci do
subject { network.enable_ci '', '', '', '' }
subject { network.enable_ci '', '', '' }
 
context 'on success' do
before do
Loading
Loading
@@ -29,7 +29,7 @@ describe Network do
 
describe :disable_ci do
let(:response) { double }
subject { network.disable_ci '', '', '' }
subject { network.disable_ci '', '' }
 
context 'on success' do
let(:parsed_response) { 'parsed' }
Loading
Loading
Loading
Loading
@@ -11,7 +11,7 @@ describe API::API do
password: "123456"
}
}
let(:private_token) { Network.new.authenticate(gitlab_url, auth_opts)["private_token"] }
let(:private_token) { Network.new.authenticate(auth_opts)["private_token"] }
 
let(:options) {
{
Loading
Loading
Loading
Loading
@@ -10,7 +10,7 @@ describe API::API do
password: "123456"
}
}
let(:private_token) { Network.new.authenticate(gitlab_url, auth_opts)["private_token"] }
let(:private_token) { Network.new.authenticate(auth_opts)["private_token"] }
 
let(:options) {
{
Loading
Loading
Loading
Loading
@@ -17,7 +17,7 @@ describe API::API do
}
}
 
let(:private_token) { Network.new.authenticate(gitlab_url, auth_opts)["private_token"] }
let(:private_token) { Network.new.authenticate(auth_opts)["private_token"] }
let(:options) {
{
:private_token => private_token,
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment