delete github/find_or_create[org|repo|user] services
This commit is contained in:
parent
84ebb6b24e
commit
060dd8aced
17
Rakefile
17
Rakefile
|
@ -26,7 +26,7 @@ end
|
||||||
# not sure how else to include the spec_helper
|
# not sure how else to include the spec_helper
|
||||||
namespace :spec do
|
namespace :spec do
|
||||||
desc 'Run all specs'
|
desc 'Run all specs'
|
||||||
task :api do
|
task :all do
|
||||||
sh 'bundle exec rspec -r spec_helper spec spec_core'
|
sh 'bundle exec rspec -r spec_helper spec spec_core'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -42,18 +42,3 @@ namespace :spec do
|
||||||
end
|
end
|
||||||
|
|
||||||
task :default => :'spec:all'
|
task :default => :'spec:all'
|
||||||
|
|
||||||
desc "generate gemspec"
|
|
||||||
task 'travis-api.gemspec' do
|
|
||||||
content = File.read 'travis-api.gemspec'
|
|
||||||
|
|
||||||
fields.each do |field, values|
|
|
||||||
updated = " s.#{field} = ["
|
|
||||||
updated << values.map { |v| "\n %p" % v }.join(',')
|
|
||||||
updated << "\n ]"
|
|
||||||
content.sub!(/ s\.#{field} = \[\n( .*\n)* \]/, updated)
|
|
||||||
end
|
|
||||||
|
|
||||||
File.open('travis-api.gemspec', 'w') { |f| f << content }
|
|
||||||
end
|
|
||||||
task default: 'travis-api.gemspec'
|
|
||||||
|
|
|
@ -1,11 +1,8 @@
|
||||||
|
require 'travis/github/services/set_hook'
|
||||||
|
|
||||||
module Travis
|
module Travis
|
||||||
module Github
|
module Github
|
||||||
module Services
|
module Services
|
||||||
require 'travis/github/services/find_or_create_org'
|
|
||||||
require 'travis/github/services/find_or_create_repo'
|
|
||||||
require 'travis/github/services/find_or_create_user'
|
|
||||||
require 'travis/github/services/set_hook'
|
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
def register
|
def register
|
||||||
constants(false).each { |name| const_get(name) }
|
constants(false).each { |name| const_get(name) }
|
||||||
|
|
|
@ -1,80 +0,0 @@
|
||||||
require 'gh'
|
|
||||||
require 'travis/services/base'
|
|
||||||
require 'travis/model/organization'
|
|
||||||
require 'travis/model/repository'
|
|
||||||
require 'travis/model/user'
|
|
||||||
|
|
||||||
module Travis
|
|
||||||
module Github
|
|
||||||
module Services
|
|
||||||
class FindOrCreateOrg < Travis::Services::Base
|
|
||||||
register :github_find_or_create_org
|
|
||||||
|
|
||||||
def run
|
|
||||||
find || create
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def find
|
|
||||||
::Organization.where(:github_id => params[:github_id]).first.tap do |organization|
|
|
||||||
if organization
|
|
||||||
ActiveRecord::Base.transaction do
|
|
||||||
login = params[:login] || data['login']
|
|
||||||
if organization.login != login
|
|
||||||
Repository.where(owner_name: organization.login).
|
|
||||||
update_all(owner_name: login)
|
|
||||||
organization.update_attributes(login: login)
|
|
||||||
end
|
|
||||||
|
|
||||||
nullify_logins(organization.github_id, organization.login)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def nullify_logins(github_id, login)
|
|
||||||
users = User.where(["login = ?", login])
|
|
||||||
if users.exists?
|
|
||||||
Travis.logger.info("About to nullify login (#{login}) for users: #{users.map(&:id).join(', ')}")
|
|
||||||
users.update_all(login: nil)
|
|
||||||
end
|
|
||||||
|
|
||||||
organizations = Organization.where(["github_id <> ? AND login = ?", github_id, login])
|
|
||||||
if organizations.exists?
|
|
||||||
Travis.logger.info("About to nullify login (#{login}) for organizations: #{organizations.map(&:id).join(', ')}")
|
|
||||||
organizations.update_all(login: nil)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def create
|
|
||||||
organization = Organization.create!(
|
|
||||||
:name => data['name'],
|
|
||||||
:login => data['login'],
|
|
||||||
:github_id => data['id'],
|
|
||||||
:email => data['email'],
|
|
||||||
:location => data['location'],
|
|
||||||
:avatar_url => data['_links'] && data['_links']['avatar'].try(:fetch, 'href'),
|
|
||||||
:company => data['company'],
|
|
||||||
:homepage => data['_links'] && data['_links']['blog'].try(:fetch, 'href')
|
|
||||||
)
|
|
||||||
|
|
||||||
nullify_logins(organization.github_id, organization.login)
|
|
||||||
|
|
||||||
organization
|
|
||||||
rescue ActiveRecord::RecordNotUnique
|
|
||||||
find
|
|
||||||
end
|
|
||||||
|
|
||||||
def avatar_url(github_data)
|
|
||||||
href = github_data.try(:fetch, 'href')
|
|
||||||
href ? href[/^(https:\/\/[\w\.\/]*)/, 1] : nil
|
|
||||||
end
|
|
||||||
|
|
||||||
def data
|
|
||||||
@data ||= GH["organizations/#{params[:github_id]}"] || raise(Travis::GithubApiError)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -1,44 +0,0 @@
|
||||||
module Travis
|
|
||||||
module Github
|
|
||||||
module Services
|
|
||||||
class FindOrCreateRepo < Travis::Services::Base
|
|
||||||
register :github_find_or_create_repo
|
|
||||||
|
|
||||||
def run
|
|
||||||
repo = find || create
|
|
||||||
repo.update_attributes(params)
|
|
||||||
repo
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def find
|
|
||||||
unless params[:github_id]
|
|
||||||
message = "No github_id passed to FindOrCreateRepo#find, params: #{params.inspect}"
|
|
||||||
ActiveSupport::Deprecation.warn(message)
|
|
||||||
Travis.logger.info(message)
|
|
||||||
end
|
|
||||||
|
|
||||||
query = if params[:github_id]
|
|
||||||
{ github_id: params[:github_id] }
|
|
||||||
else
|
|
||||||
{ owner_name: params[:owner_name], name: params[:name] }
|
|
||||||
end
|
|
||||||
|
|
||||||
run_service(:find_repo, query)
|
|
||||||
end
|
|
||||||
|
|
||||||
def create
|
|
||||||
unless params[:github_id]
|
|
||||||
message = "No github_id passed to FindOrCreateRepo#find, params: #{params.inspect}"
|
|
||||||
ActiveSupport::Deprecation.warn(message)
|
|
||||||
Travis.logger.info(message)
|
|
||||||
end
|
|
||||||
Repository.create!(:owner_name => params[:owner_name], :name => params[:name], github_id: params[:github_id])
|
|
||||||
rescue ActiveRecord::RecordNotUnique
|
|
||||||
find
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -1,66 +0,0 @@
|
||||||
require 'gh'
|
|
||||||
require 'travis/model/repository'
|
|
||||||
require 'travis/model/user'
|
|
||||||
require 'travis/model/user/renaming'
|
|
||||||
require 'travis/services/base'
|
|
||||||
|
|
||||||
module Travis
|
|
||||||
module Github
|
|
||||||
module Services
|
|
||||||
class FindOrCreateUser < Travis::Services::Base
|
|
||||||
register :github_find_or_create_user
|
|
||||||
|
|
||||||
def run
|
|
||||||
find || create
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
include ::User::Renaming
|
|
||||||
|
|
||||||
def find
|
|
||||||
::User.where(github_id: params[:github_id]).first.tap do |user|
|
|
||||||
if user
|
|
||||||
ActiveRecord::Base.transaction do
|
|
||||||
login = params[:login] || data['login']
|
|
||||||
if user.login != login
|
|
||||||
Travis.logger.info("Changing #<User id=#{user.id} login=\"#{user.login}\" github_id=#{user.github_id}> login: current=\"#{user.login}\", new=\"#{login}\" (FindOrCreateUser), data: #{data.inspect}")
|
|
||||||
rename_repos_owner(user.login, login)
|
|
||||||
user.update_attributes(login: login)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
nullify_logins(user.github_id, user.login)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def create
|
|
||||||
user = User.create!(
|
|
||||||
:name => data['name'],
|
|
||||||
:login => data['login'],
|
|
||||||
:email => data['email'],
|
|
||||||
:github_id => data['id'],
|
|
||||||
:gravatar_id => data['gravatar_id']
|
|
||||||
)
|
|
||||||
|
|
||||||
nullify_logins(user.github_id, user.login)
|
|
||||||
|
|
||||||
user
|
|
||||||
rescue ActiveRecord::RecordNotUnique
|
|
||||||
find
|
|
||||||
end
|
|
||||||
|
|
||||||
def data
|
|
||||||
@data ||= fetch_data
|
|
||||||
end
|
|
||||||
|
|
||||||
def fetch_data
|
|
||||||
data = GH["user/#{params[:github_id]}"] || raise(Travis::GithubApiError)
|
|
||||||
Travis.logger.info("Fetching data for github_id=#{params[:github_id]} (FindOrCreateUser), data: #{data.inspect}")
|
|
||||||
data
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
1
vendor/travis-core/lib/travis/model/user.rb
vendored
1
vendor/travis-core/lib/travis/model/user.rb
vendored
|
@ -4,6 +4,7 @@ require 'travis/github/oauth'
|
||||||
|
|
||||||
class User < Travis::Model
|
class User < Travis::Model
|
||||||
require 'travis/model/user/oauth'
|
require 'travis/model/user/oauth'
|
||||||
|
require 'travis/model/user/renaming'
|
||||||
|
|
||||||
has_many :tokens, dependent: :destroy
|
has_many :tokens, dependent: :destroy
|
||||||
has_many :memberships, dependent: :destroy
|
has_many :memberships, dependent: :destroy
|
||||||
|
|
Loading…
Reference in New Issue
Block a user