debian-mirror-gitlab/app/workers/irker_worker.rb

174 lines
4.4 KiB
Ruby
Raw Normal View History

2018-11-08 19:23:39 +05:30
# frozen_string_literal: true
2015-04-26 12:48:37 +05:30
require 'json'
require 'socket'
2020-04-08 14:13:33 +05:30
class IrkerWorker # rubocop:disable Scalability/IdempotentWorker
2018-03-17 18:26:18 +05:30
include ApplicationWorker
2015-04-26 12:48:37 +05:30
2019-12-21 20:55:43 +05:30
feature_category :integrations
2020-06-23 00:09:42 +05:30
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'])
2015-04-26 12:48:37 +05:30
2020-06-23 00:09:42 +05:30
@project = Project.find(project_id)
@colors = colors
@channels = channels
2015-04-26 12:48:37 +05:30
2020-06-23 00:09:42 +05:30
@repo_path = @project.full_path
@repo_name = push_data['repository']['name']
@committer = push_data['user_name']
@branch = push_data['ref'].gsub(%r'refs/[^/]*/', '')
2015-04-26 12:48:37 +05:30
if @colors
2020-06-23 00:09:42 +05:30
@repo_name = "\x0304#{@repo_name}\x0f"
@branch = "\x0305#{@branch}\x0f"
2015-04-26 12:48:37 +05:30
end
2015-09-11 14:41:01 +05:30
# First messages are for branch creation/deletion
2020-06-23 00:09:42 +05:30
send_branch_updates(push_data)
2015-04-26 12:48:37 +05:30
# Next messages are for commits
2020-06-23 00:09:42 +05:30
send_commits(push_data)
2015-04-26 12:48:37 +05:30
close_connection
true
end
private
def start_connection(irker_server, irker_port)
begin
@socket = TCPSocket.new irker_server, irker_port
rescue Errno::ECONNREFUSED => e
logger.fatal "Can't connect to Irker daemon: #{e}"
return false
end
2020-06-23 00:09:42 +05:30
2015-04-26 12:48:37 +05:30
true
end
2020-06-23 00:09:42 +05:30
def send_to_irker(privmsg)
2015-04-26 12:48:37 +05:30
to_send = { to: @channels, privmsg: privmsg }
2020-06-23 00:09:42 +05:30
2020-05-24 23:13:21 +05:30
@socket.puts Gitlab::Json.dump(to_send)
2015-04-26 12:48:37 +05:30
end
def close_connection
@socket.close
end
2020-06-23 00:09:42 +05:30
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)
2015-04-26 12:48:37 +05:30
end
2020-06-23 00:09:42 +05:30
def new_branch_message
newbranch = "#{Gitlab.config.gitlab.url}/#{@repo_path}/-/branches"
2015-04-26 12:48:37 +05:30
newbranch = "\x0302\x1f#{newbranch}\x0f" if @colors
2020-06-23 00:09:42 +05:30
"[#{@repo_name}] #{@committer} has created a new branch #{@branch}: #{newbranch}"
2015-04-26 12:48:37 +05:30
end
2020-06-23 00:09:42 +05:30
def delete_branch_message
"[#{@repo_name}] #{@committer} has deleted the branch #{@branch}"
2015-04-26 12:48:37 +05:30
end
2020-06-23 00:09:42 +05:30
def send_commits(push_data)
2015-04-26 12:48:37 +05:30
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
2020-06-23 00:09:42 +05:30
push_data['before'] = before_on_new_branch(push_data)
2015-04-26 12:48:37 +05:30
end
2020-06-23 00:09:42 +05:30
send_commits_count(push_data)
2015-04-26 12:48:37 +05:30
# One message per commit, limited by 3 messages (same limit as the
# github irc hook)
commits = push_data['commits'].first(3)
2020-06-23 00:09:42 +05:30
commits.each do |commit_attrs|
send_one_commit(commit_attrs)
2015-04-26 12:48:37 +05:30
end
end
2020-06-23 00:09:42 +05:30
def before_on_new_branch(push_data)
commit = commit_from_id(push_data['commits'][0]['id'])
2015-04-26 12:48:37 +05:30
parents = commit.parents
2020-06-23 00:09:42 +05:30
2015-04-26 12:48:37 +05:30
# Return old value if there's no new one
return push_data['before'] if parents.empty?
2018-03-17 18:26:18 +05:30
2015-04-26 12:48:37 +05:30
# Or return the first parent-commit
parents[0].id
end
2020-06-23 00:09:42 +05:30
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'])
2015-04-26 12:48:37 +05:30
2020-06-23 00:09:42 +05:30
send_to_irker("[#{@repo_name}] #{@committer} pushed #{commits} #{new_commits} " \
"to #{@branch}: #{url}")
2015-04-26 12:48:37 +05:30
end
2020-06-23 00:09:42 +05:30
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" \
2018-11-08 19:23:39 +05:30
"/#{sha1}...#{sha2}"
2020-06-23 00:09:42 +05:30
colorize_url(compare_url)
2015-04-26 12:48:37 +05:30
end
2020-06-23 00:09:42 +05:30
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']
2020-04-22 19:07:51 +05:30
files = colorize_nb_files(files_count(commit))
2015-04-26 12:48:37 +05:30
title = commit.title
2020-06-23 00:09:42 +05:30
send_to_irker("#{@repo_name}/#{@branch} #{sha} #{author} (#{files}): #{title}")
2015-04-26 12:48:37 +05:30
end
2020-06-23 00:09:42 +05:30
def commit_from_id(id)
@project.commit(id)
2015-04-26 12:48:37 +05:30
end
def files_count(commit)
2017-09-10 17:25:29 +05:30
diff_size = commit.raw_deltas.size
2016-09-13 17:45:13 +05:30
2018-11-08 19:23:39 +05:30
files = "#{diff_size} file".pluralize(diff_size)
2015-04-26 12:48:37 +05:30
files
end
def colorize_sha(sha)
sha = "\x0314#{sha}\x0f" if @colors
sha
end
def colorize_nb_files(nb_files)
nb_files = "\x0312#{nb_files}\x0f" if @colors
nb_files
end
def colorize_url(url)
url = "\x0302\x1f#{url}\x0f" if @colors
url
end
def colorize_commits(commits)
commits = "\x02#{commits}\x0f" if @colors
commits
end
end