diff options
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/network.rb | 60 | ||||
-rw-r--r-- | app/models/project.rb | 2 | ||||
-rw-r--r-- | app/models/user.rb | 4 | ||||
-rw-r--r-- | app/models/user_session.rb | 18 |
4 files changed, 39 insertions, 45 deletions
diff --git a/app/models/network.rb b/app/models/network.rb index 631eee6..72eaeed 100644 --- a/app/models/network.rb +++ b/app/models/network.rb @@ -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 @@ -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 @@ -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 diff --git a/app/models/project.rb b/app/models/project.rb index a3cb161..31ce6ab 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -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) } diff --git a/app/models/user.rb b/app/models/user.rb index 71b0c19..9238e31 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 @@ -73,7 +73,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 diff --git a/app/models/user_session.rb b/app/models/user_session.rb index 30b5b5d..d1c0711 100644 --- a/app/models/user_session.rb +++ b/app/models/user_session.rb @@ -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 @@ -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 |