diff options
Diffstat (limited to 'app/workers/irker_worker.rb')
-rw-r--r-- | app/workers/irker_worker.rb | 117 |
1 files changed, 59 insertions, 58 deletions
diff --git a/app/workers/irker_worker.rb b/app/workers/irker_worker.rb index 09f53681e7f..687fb1cd02a 100644 --- a/app/workers/irker_worker.rb +++ b/app/workers/irker_worker.rb @@ -8,26 +8,30 @@ class IrkerWorker # rubocop:disable Scalability/IdempotentWorker feature_category :integrations - def perform(project_id, chans, colors, push_data, settings) - project = Project.find(project_id) + def perform(project_id, channels, colors, push_data, settings) + # Establish connection to irker server + return false unless start_connection(settings['server_host'], + settings['server_port']) - # Get config parameters - return false unless init_perform settings, chans, colors + @project = Project.find(project_id) + @colors = colors + @channels = channels - repo_name = push_data['repository']['name'] - committer = push_data['user_name'] - branch = push_data['ref'].gsub(%r'refs/[^/]*/', '') + @repo_path = @project.full_path + @repo_name = push_data['repository']['name'] + @committer = push_data['user_name'] + @branch = push_data['ref'].gsub(%r'refs/[^/]*/', '') if @colors - repo_name = "\x0304#{repo_name}\x0f" - branch = "\x0305#{branch}\x0f" + @repo_name = "\x0304#{@repo_name}\x0f" + @branch = "\x0305#{@branch}\x0f" end # First messages are for branch creation/deletion - send_branch_updates push_data, project, repo_name, committer, branch + send_branch_updates(push_data) # Next messages are for commits - send_commits push_data, project, repo_name, committer, branch + send_commits(push_data) close_connection true @@ -35,12 +39,6 @@ class IrkerWorker # rubocop:disable Scalability/IdempotentWorker private - def init_perform(set, chans, colors) - @colors = colors - @channels = chans - start_connection set['server_host'], set['server_port'] - end - def start_connection(irker_server, irker_port) begin @socket = TCPSocket.new irker_server, irker_port @@ -48,11 +46,13 @@ class IrkerWorker # rubocop:disable Scalability/IdempotentWorker logger.fatal "Can't connect to Irker daemon: #{e}" return false end + true end - def sendtoirker(privmsg) + def send_to_irker(privmsg) to_send = { to: @channels, privmsg: privmsg } + @socket.puts Gitlab::Json.dump(to_send) end @@ -60,51 +60,51 @@ class IrkerWorker # rubocop:disable Scalability/IdempotentWorker @socket.close end - def send_branch_updates(push_data, project, repo_name, committer, branch) - if Gitlab::Git.blank_ref?(push_data['before']) - send_new_branch project, repo_name, committer, branch - elsif Gitlab::Git.blank_ref?(push_data['after']) - send_del_branch repo_name, committer, branch - end + def send_branch_updates(push_data) + message = + if Gitlab::Git.blank_ref?(push_data['before']) + new_branch_message + elsif Gitlab::Git.blank_ref?(push_data['after']) + delete_branch_message + end + + send_to_irker(message) end - def send_new_branch(project, repo_name, committer, branch) - repo_path = project.full_path - newbranch = "#{Gitlab.config.gitlab.url}/#{repo_path}/-/branches" + def new_branch_message + newbranch = "#{Gitlab.config.gitlab.url}/#{@repo_path}/-/branches" newbranch = "\x0302\x1f#{newbranch}\x0f" if @colors - privmsg = "[#{repo_name}] #{committer} has created a new branch " \ - "#{branch}: #{newbranch}" - sendtoirker privmsg + "[#{@repo_name}] #{@committer} has created a new branch #{@branch}: #{newbranch}" end - def send_del_branch(repo_name, committer, branch) - privmsg = "[#{repo_name}] #{committer} has deleted the branch #{branch}" - sendtoirker privmsg + def delete_branch_message + "[#{@repo_name}] #{@committer} has deleted the branch #{@branch}" end - def send_commits(push_data, project, repo_name, committer, branch) + def send_commits(push_data) return if push_data['total_commits_count'] == 0 # Next message is for number of commit pushed, if any if Gitlab::Git.blank_ref?(push_data['before']) # Tweak on push_data["before"] in order to have a nice compare URL - push_data['before'] = before_on_new_branch push_data, project + push_data['before'] = before_on_new_branch(push_data) end - send_commits_count(push_data, project, repo_name, committer, branch) + send_commits_count(push_data) # One message per commit, limited by 3 messages (same limit as the # github irc hook) commits = push_data['commits'].first(3) - commits.each do |hook_attrs| - send_one_commit project, hook_attrs, repo_name, branch + commits.each do |commit_attrs| + send_one_commit(commit_attrs) end end - def before_on_new_branch(push_data, project) - commit = commit_from_id project, push_data['commits'][0]['id'] + def before_on_new_branch(push_data) + commit = commit_from_id(push_data['commits'][0]['id']) parents = commit.parents + # Return old value if there's no new one return push_data['before'] if parents.empty? @@ -112,35 +112,36 @@ class IrkerWorker # rubocop:disable Scalability/IdempotentWorker parents[0].id end - def send_commits_count(data, project, repo, committer, branch) - url = compare_url data, project.full_path - commits = colorize_commits data['total_commits_count'] + def send_commits_count(push_data) + url = compare_url(push_data['before'], push_data['after']) + commits = colorize_commits(push_data['total_commits_count']) + new_commits = 'new commit'.pluralize(push_data['total_commits_count']) - new_commits = 'new commit'.pluralize(data['total_commits_count']) - sendtoirker "[#{repo}] #{committer} pushed #{commits} #{new_commits} " \ - "to #{branch}: #{url}" + send_to_irker("[#{@repo_name}] #{@committer} pushed #{commits} #{new_commits} " \ + "to #{@branch}: #{url}") end - def compare_url(data, repo_path) - sha1 = Commit.truncate_sha(data['before']) - sha2 = Commit.truncate_sha(data['after']) - compare_url = "#{Gitlab.config.gitlab.url}/#{repo_path}/-/compare" \ + def compare_url(sha_before, sha_after) + sha1 = Commit.truncate_sha(sha_before) + sha2 = Commit.truncate_sha(sha_after) + compare_url = "#{Gitlab.config.gitlab.url}/#{@repo_path}/-/compare" \ "/#{sha1}...#{sha2}" - colorize_url compare_url + + colorize_url(compare_url) end - def send_one_commit(project, hook_attrs, repo_name, branch) - commit = commit_from_id project, hook_attrs['id'] - sha = colorize_sha Commit.truncate_sha(hook_attrs['id']) - author = hook_attrs['author']['name'] + def send_one_commit(commit_attrs) + commit = commit_from_id(commit_attrs['id']) + sha = colorize_sha(Commit.truncate_sha(commit_attrs['id'])) + author = commit_attrs['author']['name'] files = colorize_nb_files(files_count(commit)) title = commit.title - sendtoirker "#{repo_name}/#{branch} #{sha} #{author} (#{files}): #{title}" + send_to_irker("#{@repo_name}/#{@branch} #{sha} #{author} (#{files}): #{title}") end - def commit_from_id(project, id) - project.commit(id) + def commit_from_id(id) + @project.commit(id) end def files_count(commit) |