Commit a1704273ecd8809f918e5776f4bc27490169ae2a

Authored by Dmitriy Zaporozhets
1 parent 348eb125

Refactor post-receive worker

app/workers/post_receive.rb
1 class PostReceive 1 class PostReceive
2 include Sidekiq::Worker 2 include Sidekiq::Worker
  3 + include Gitlab::Identifier
3 4
4 sidekiq_options queue: :post_receive 5 sidekiq_options queue: :post_receive
5 6
@@ -8,7 +9,7 @@ class PostReceive @@ -8,7 +9,7 @@ class PostReceive
8 if repo_path.start_with?(Gitlab.config.gitlab_shell.repos_path.to_s) 9 if repo_path.start_with?(Gitlab.config.gitlab_shell.repos_path.to_s)
9 repo_path.gsub!(Gitlab.config.gitlab_shell.repos_path.to_s, "") 10 repo_path.gsub!(Gitlab.config.gitlab_shell.repos_path.to_s, "")
10 else 11 else
11 - Gitlab::GitLogger.error("POST-RECEIVE: Check gitlab.yml config for correct gitlab_shell.repos_path variable. \"#{Gitlab.config.gitlab_shell.repos_path}\" does not match \"#{repo_path}\"") 12 + log("Check gitlab.yml config for correct gitlab_shell.repos_path variable. \"#{Gitlab.config.gitlab_shell.repos_path}\" does not match \"#{repo_path}\"")
12 end 13 end
13 14
14 repo_path.gsub!(/.git$/, "") 15 repo_path.gsub!(/.git$/, "")
@@ -17,31 +18,21 @@ class PostReceive @@ -17,31 +18,21 @@ class PostReceive
17 project = Project.find_with_namespace(repo_path) 18 project = Project.find_with_namespace(repo_path)
18 19
19 if project.nil? 20 if project.nil?
20 - Gitlab::GitLogger.error("POST-RECEIVE: Triggered hook for non-existing project with full path \"#{repo_path} \"") 21 + log("Triggered hook for non-existing project with full path \"#{repo_path} \"")
21 return false 22 return false
22 end 23 end
23 24
24 - user = if identifier.blank?  
25 - # Local push from gitlab  
26 - email = project.repository.commit(newrev).author_email rescue nil  
27 - User.find_by_email(email) if email  
28 -  
29 - elsif identifier =~ /\Auser-\d+\Z/  
30 - # git push over http  
31 - user_id = identifier.gsub("user-", "")  
32 - User.find_by_id(user_id)  
33 -  
34 - elsif identifier =~ /\Akey-\d+\Z/  
35 - # git push over ssh  
36 - key_id = identifier.gsub("key-", "")  
37 - Key.find_by_id(key_id).try(:user)  
38 - end 25 + user = identify(identifier, project, newrev)
39 26
40 unless user 27 unless user
41 - Gitlab::GitLogger.error("POST-RECEIVE: Triggered hook for non-existing user \"#{identifier} \"") 28 + log("Triggered hook for non-existing user \"#{identifier} \"")
42 return false 29 return false
43 end 30 end
44 31
45 GitPushService.new.execute(project, user, oldrev, newrev, ref) 32 GitPushService.new.execute(project, user, oldrev, newrev, ref)
46 end 33 end
  34 +
  35 + def log(message)
  36 + Gitlab::GitLogger.error("POST-RECEIVE: #{message}")
  37 + end
47 end 38 end
lib/gitlab/identifier.rb 0 → 100644
@@ -0,0 +1,23 @@ @@ -0,0 +1,23 @@
  1 +# Detect user based on identifier like
  2 +# key-13 or user-36 or last commit
  3 +module Gitlab
  4 + module Indentifier
  5 + def identify(identifier, project, newrev)
  6 + if identifier.blank?
  7 + # Local push from gitlab
  8 + email = project.repository.commit(newrev).author_email rescue nil
  9 + User.find_by_email(email) if email
  10 +
  11 + elsif identifier =~ /\Auser-\d+\Z/
  12 + # git push over http
  13 + user_id = identifier.gsub("user-", "")
  14 + User.find_by_id(user_id)
  15 +
  16 + elsif identifier =~ /\Akey-\d+\Z/
  17 + # git push over ssh
  18 + key_id = identifier.gsub("key-", "")
  19 + Key.find_by_id(key_id).try(:user)
  20 + end
  21 + end
  22 + end
  23 +end