2018-06-27 03:23:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-02-04 15:31:55 -05:00
|
|
|
require 'json'
|
|
|
|
require 'socket'
|
|
|
|
|
|
|
|
class IrkerWorker
|
2017-11-28 11:08:30 -05:00
|
|
|
include ApplicationWorker
|
2015-02-04 15:31:55 -05:00
|
|
|
|
2019-10-18 07:11:44 -04:00
|
|
|
feature_category :integrations
|
|
|
|
|
2015-02-04 15:31:55 -05:00
|
|
|
def perform(project_id, chans, colors, push_data, settings)
|
|
|
|
project = Project.find(project_id)
|
|
|
|
|
|
|
|
# Get config parameters
|
|
|
|
return false unless init_perform settings, chans, colors
|
|
|
|
|
|
|
|
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"
|
|
|
|
end
|
|
|
|
|
2015-07-03 02:17:10 -04:00
|
|
|
# First messages are for branch creation/deletion
|
2015-02-04 15:31:55 -05:00
|
|
|
send_branch_updates push_data, project, repo_name, committer, branch
|
|
|
|
|
|
|
|
# Next messages are for commits
|
|
|
|
send_commits push_data, project, repo_name, committer, branch
|
|
|
|
|
|
|
|
close_connection
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def init_perform(set, chans, colors)
|
|
|
|
@colors = colors
|
|
|
|
@channels = chans
|
2015-07-03 02:17:10 -04:00
|
|
|
start_connection set['server_host'], set['server_port']
|
2015-02-04 15:31:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def sendtoirker(privmsg)
|
|
|
|
to_send = { to: @channels, privmsg: privmsg }
|
|
|
|
@socket.puts JSON.dump(to_send)
|
|
|
|
end
|
|
|
|
|
|
|
|
def close_connection
|
|
|
|
@socket.close
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_branch_updates(push_data, project, repo_name, committer, branch)
|
2015-03-19 05:34:04 -04:00
|
|
|
if Gitlab::Git.blank_ref?(push_data['before'])
|
2015-02-04 15:31:55 -05:00
|
|
|
send_new_branch project, repo_name, committer, branch
|
2015-03-19 05:34:04 -04:00
|
|
|
elsif Gitlab::Git.blank_ref?(push_data['after'])
|
2015-02-04 15:31:55 -05:00
|
|
|
send_del_branch repo_name, committer, branch
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_new_branch(project, repo_name, committer, branch)
|
2017-07-20 05:34:09 -04:00
|
|
|
repo_path = project.full_path
|
2015-02-04 15:31:55 -05:00
|
|
|
newbranch = "#{Gitlab.config.gitlab.url}/#{repo_path}/branches"
|
|
|
|
newbranch = "\x0302\x1f#{newbranch}\x0f" if @colors
|
|
|
|
|
2018-06-27 03:23:28 -04:00
|
|
|
privmsg = "[#{repo_name}] #{committer} has created a new branch " \
|
|
|
|
"#{branch}: #{newbranch}"
|
2015-02-04 15:31:55 -05:00
|
|
|
sendtoirker privmsg
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_del_branch(repo_name, committer, branch)
|
|
|
|
privmsg = "[#{repo_name}] #{committer} has deleted the branch #{branch}"
|
|
|
|
sendtoirker privmsg
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_commits(push_data, project, repo_name, committer, branch)
|
|
|
|
return if push_data['total_commits_count'] == 0
|
|
|
|
|
|
|
|
# Next message is for number of commit pushed, if any
|
2015-03-19 05:34:04 -04:00
|
|
|
if Gitlab::Git.blank_ref?(push_data['before'])
|
2015-02-04 15:31:55 -05:00
|
|
|
# Tweak on push_data["before"] in order to have a nice compare URL
|
|
|
|
push_data['before'] = before_on_new_branch push_data, project
|
|
|
|
end
|
|
|
|
|
|
|
|
send_commits_count(push_data, project, repo_name, committer, branch)
|
|
|
|
|
|
|
|
# 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
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def before_on_new_branch(push_data, project)
|
|
|
|
commit = commit_from_id project, push_data['commits'][0]['id']
|
|
|
|
parents = commit.parents
|
|
|
|
# Return old value if there's no new one
|
|
|
|
return push_data['before'] if parents.empty?
|
2017-11-14 04:02:39 -05:00
|
|
|
|
2015-02-04 15:31:55 -05:00
|
|
|
# Or return the first parent-commit
|
|
|
|
parents[0].id
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_commits_count(data, project, repo, committer, branch)
|
2017-07-20 05:34:09 -04:00
|
|
|
url = compare_url data, project.full_path
|
2015-02-04 15:31:55 -05:00
|
|
|
commits = colorize_commits data['total_commits_count']
|
|
|
|
|
2018-06-27 03:23:28 -04:00
|
|
|
new_commits = 'new commit'.pluralize(data['total_commits_count'])
|
2015-02-04 15:31:55 -05:00
|
|
|
sendtoirker "[#{repo}] #{committer} pushed #{commits} #{new_commits} " \
|
|
|
|
"to #{branch}: #{url}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def compare_url(data, repo_path)
|
2017-02-21 19:51:36 -05:00
|
|
|
sha1 = Commit.truncate_sha(data['before'])
|
|
|
|
sha2 = Commit.truncate_sha(data['after'])
|
2018-06-27 03:23:28 -04:00
|
|
|
compare_url = "#{Gitlab.config.gitlab.url}/#{repo_path}/compare" \
|
|
|
|
"/#{sha1}...#{sha2}"
|
2015-02-04 15:31:55 -05:00
|
|
|
colorize_url compare_url
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_one_commit(project, hook_attrs, repo_name, branch)
|
|
|
|
commit = commit_from_id project, hook_attrs['id']
|
2017-02-21 19:51:36 -05:00
|
|
|
sha = colorize_sha Commit.truncate_sha(hook_attrs['id'])
|
2015-02-04 15:31:55 -05:00
|
|
|
author = hook_attrs['author']['name']
|
|
|
|
files = colorize_nb_files(files_count commit)
|
|
|
|
title = commit.title
|
|
|
|
|
|
|
|
sendtoirker "#{repo_name}/#{branch} #{sha} #{author} (#{files}): #{title}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def commit_from_id(project, id)
|
2015-04-21 09:09:15 -04:00
|
|
|
project.commit(id)
|
2015-02-04 15:31:55 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def files_count(commit)
|
2017-05-09 06:27:17 -04:00
|
|
|
diff_size = commit.raw_deltas.size
|
2016-07-27 13:00:34 -04:00
|
|
|
|
2018-06-27 03:23:28 -04:00
|
|
|
files = "#{diff_size} file".pluralize(diff_size)
|
2015-02-04 15:31:55 -05:00
|
|
|
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
|