Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
301e9c7c
Commit
301e9c7c
authored
Jun 02, 2020
by
Dmytro Zaporozhets
Committed by
Ash McKenzie
Jun 02, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add IrkerWorker spec file
Signed-off-by:
Dmitriy Zaporozhets
<
dmitriy.zaporozhets@gmail.com
>
parent
f6d1e7d2
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
158 additions
and
58 deletions
+158
-58
app/workers/irker_worker.rb
app/workers/irker_worker.rb
+59
-58
spec/workers/irker_worker_spec.rb
spec/workers/irker_worker_spec.rb
+99
-0
No files found.
app/workers/irker_worker.rb
View file @
301e9c7c
...
@@ -8,26 +8,30 @@ class IrkerWorker # rubocop:disable Scalability/IdempotentWorker
...
@@ -8,26 +8,30 @@ class IrkerWorker # rubocop:disable Scalability/IdempotentWorker
feature_category
:integrations
feature_category
:integrations
def
perform
(
project_id
,
chans
,
colors
,
push_data
,
settings
)
def
perform
(
project_id
,
channels
,
colors
,
push_data
,
settings
)
project
=
Project
.
find
(
project_id
)
# Establish connection to irker server
return
false
unless
start_connection
(
settings
[
'server_host'
],
settings
[
'server_port'
])
# Get config parameters
@project
=
Project
.
find
(
project_id
)
return
false
unless
init_perform
settings
,
chans
,
colors
@colors
=
colors
@channels
=
channels
repo_name
=
push_data
[
'repository'
][
'name'
]
@repo_path
=
@project
.
full_path
committer
=
push_data
[
'user_name'
]
@repo_name
=
push_data
[
'repository'
][
'name'
]
branch
=
push_data
[
'ref'
].
gsub
(
%r'refs/[^/]*/'
,
''
)
@committer
=
push_data
[
'user_name'
]
@branch
=
push_data
[
'ref'
].
gsub
(
%r'refs/[^/]*/'
,
''
)
if
@colors
if
@colors
repo_name
=
"
\x03
04
#{
repo_name
}
\x0f
"
@repo_name
=
"
\x03
04
#{
@
repo_name
}
\x0f
"
branch
=
"
\x03
05
#{
branch
}
\x0f
"
@branch
=
"
\x03
05
#{
@
branch
}
\x0f
"
end
end
# First messages are for branch creation/deletion
# 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
# Next messages are for commits
send_commits
push_data
,
project
,
repo_name
,
committer
,
branch
send_commits
(
push_data
)
close_connection
close_connection
true
true
...
@@ -35,12 +39,6 @@ class IrkerWorker # rubocop:disable Scalability/IdempotentWorker
...
@@ -35,12 +39,6 @@ class IrkerWorker # rubocop:disable Scalability/IdempotentWorker
private
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
)
def
start_connection
(
irker_server
,
irker_port
)
begin
begin
@socket
=
TCPSocket
.
new
irker_server
,
irker_port
@socket
=
TCPSocket
.
new
irker_server
,
irker_port
...
@@ -48,11 +46,13 @@ class IrkerWorker # rubocop:disable Scalability/IdempotentWorker
...
@@ -48,11 +46,13 @@ class IrkerWorker # rubocop:disable Scalability/IdempotentWorker
logger
.
fatal
"Can't connect to Irker daemon:
#{
e
}
"
logger
.
fatal
"Can't connect to Irker daemon:
#{
e
}
"
return
false
return
false
end
end
true
true
end
end
def
send
to
irker
(
privmsg
)
def
send
_to_
irker
(
privmsg
)
to_send
=
{
to:
@channels
,
privmsg:
privmsg
}
to_send
=
{
to:
@channels
,
privmsg:
privmsg
}
@socket
.
puts
Gitlab
::
Json
.
dump
(
to_send
)
@socket
.
puts
Gitlab
::
Json
.
dump
(
to_send
)
end
end
...
@@ -60,51 +60,51 @@ class IrkerWorker # rubocop:disable Scalability/IdempotentWorker
...
@@ -60,51 +60,51 @@ class IrkerWorker # rubocop:disable Scalability/IdempotentWorker
@socket
.
close
@socket
.
close
end
end
def
send_branch_updates
(
push_data
,
project
,
repo_name
,
committer
,
branch
)
def
send_branch_updates
(
push_data
)
if
Gitlab
::
Git
.
blank_ref?
(
push_data
[
'before'
])
message
=
send_new_branch
project
,
repo_name
,
committer
,
branch
if
Gitlab
::
Git
.
blank_ref?
(
push_data
[
'before'
])
elsif
Gitlab
::
Git
.
blank_ref?
(
push_data
[
'after'
])
new_branch_message
send_del_branch
repo_name
,
committer
,
branch
elsif
Gitlab
::
Git
.
blank_ref?
(
push_data
[
'after'
])
end
delete_branch_message
end
send_to_irker
(
message
)
end
end
def
send_new_branch
(
project
,
repo_name
,
committer
,
branch
)
def
new_branch_message
repo_path
=
project
.
full_path
newbranch
=
"
#{
Gitlab
.
config
.
gitlab
.
url
}
/
#{
@repo_path
}
/-/branches"
newbranch
=
"
#{
Gitlab
.
config
.
gitlab
.
url
}
/
#{
repo_path
}
/-/branches"
newbranch
=
"
\x03
02
\x1f
#{
newbranch
}
\x0f
"
if
@colors
newbranch
=
"
\x03
02
\x1f
#{
newbranch
}
\x0f
"
if
@colors
privmsg
=
"[
#{
repo_name
}
]
#{
committer
}
has created a new branch "
\
"[
#{
@repo_name
}
]
#{
@committer
}
has created a new branch
#{
@branch
}
:
#{
newbranch
}
"
"
#{
branch
}
:
#{
newbranch
}
"
sendtoirker
privmsg
end
end
def
send_del_branch
(
repo_name
,
committer
,
branch
)
def
delete_branch_message
privmsg
=
"[
#{
repo_name
}
]
#{
committer
}
has deleted the branch
#{
branch
}
"
"[
#{
@repo_name
}
]
#{
@committer
}
has deleted the branch
#{
@branch
}
"
sendtoirker
privmsg
end
end
def
send_commits
(
push_data
,
project
,
repo_name
,
committer
,
branch
)
def
send_commits
(
push_data
)
return
if
push_data
[
'total_commits_count'
]
==
0
return
if
push_data
[
'total_commits_count'
]
==
0
# Next message is for number of commit pushed, if any
# Next message is for number of commit pushed, if any
if
Gitlab
::
Git
.
blank_ref?
(
push_data
[
'before'
])
if
Gitlab
::
Git
.
blank_ref?
(
push_data
[
'before'
])
# Tweak on push_data["before"] in order to have a nice compare URL
# 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
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
# One message per commit, limited by 3 messages (same limit as the
# github irc hook)
# github irc hook)
commits
=
push_data
[
'commits'
].
first
(
3
)
commits
=
push_data
[
'commits'
].
first
(
3
)
commits
.
each
do
|
hook
_attrs
|
commits
.
each
do
|
commit
_attrs
|
send_one_commit
project
,
hook_attrs
,
repo_name
,
branch
send_one_commit
(
commit_attrs
)
end
end
end
end
def
before_on_new_branch
(
push_data
,
project
)
def
before_on_new_branch
(
push_data
)
commit
=
commit_from_id
project
,
push_data
[
'commits'
][
0
][
'id'
]
commit
=
commit_from_id
(
push_data
[
'commits'
][
0
][
'id'
])
parents
=
commit
.
parents
parents
=
commit
.
parents
# Return old value if there's no new one
# Return old value if there's no new one
return
push_data
[
'before'
]
if
parents
.
empty?
return
push_data
[
'before'
]
if
parents
.
empty?
...
@@ -112,35 +112,36 @@ class IrkerWorker # rubocop:disable Scalability/IdempotentWorker
...
@@ -112,35 +112,36 @@ class IrkerWorker # rubocop:disable Scalability/IdempotentWorker
parents
[
0
].
id
parents
[
0
].
id
end
end
def
send_commits_count
(
data
,
project
,
repo
,
committer
,
branch
)
def
send_commits_count
(
push_data
)
url
=
compare_url
data
,
project
.
full_path
url
=
compare_url
(
push_data
[
'before'
],
push_data
[
'after'
])
commits
=
colorize_commits
data
[
'total_commits_count'
]
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'
])
send_to_irker
(
"[
#{
@repo_name
}
]
#{
@committer
}
pushed
#{
commits
}
#{
new_commits
}
"
\
sendtoirker
"[
#{
repo
}
]
#{
committer
}
pushed
#{
commits
}
#{
new_commits
}
"
\
"to
#{
@branch
}
:
#{
url
}
"
)
"to
#{
branch
}
:
#{
url
}
"
end
end
def
compare_url
(
data
,
repo_path
)
def
compare_url
(
sha_before
,
sha_after
)
sha1
=
Commit
.
truncate_sha
(
data
[
'before'
]
)
sha1
=
Commit
.
truncate_sha
(
sha_before
)
sha2
=
Commit
.
truncate_sha
(
data
[
'after'
]
)
sha2
=
Commit
.
truncate_sha
(
sha_after
)
compare_url
=
"
#{
Gitlab
.
config
.
gitlab
.
url
}
/
#{
repo_path
}
/-/compare"
\
compare_url
=
"
#{
Gitlab
.
config
.
gitlab
.
url
}
/
#{
@
repo_path
}
/-/compare"
\
"/
#{
sha1
}
...
#{
sha2
}
"
"/
#{
sha1
}
...
#{
sha2
}
"
colorize_url
compare_url
colorize_url
(
compare_url
)
end
end
def
send_one_commit
(
project
,
hook_attrs
,
repo_name
,
branch
)
def
send_one_commit
(
commit_attrs
)
commit
=
commit_from_id
project
,
hook_attrs
[
'id'
]
commit
=
commit_from_id
(
commit_attrs
[
'id'
])
sha
=
colorize_sha
Commit
.
truncate_sha
(
hook_attrs
[
'id'
]
)
sha
=
colorize_sha
(
Commit
.
truncate_sha
(
commit_attrs
[
'id'
])
)
author
=
hook
_attrs
[
'author'
][
'name'
]
author
=
commit
_attrs
[
'author'
][
'name'
]
files
=
colorize_nb_files
(
files_count
(
commit
))
files
=
colorize_nb_files
(
files_count
(
commit
))
title
=
commit
.
title
title
=
commit
.
title
send
toirker
"
#{
repo_name
}
/
#{
branch
}
#{
sha
}
#{
author
}
(
#{
files
}
):
#{
title
}
"
send
_to_irker
(
"
#{
@repo_name
}
/
#{
@branch
}
#{
sha
}
#{
author
}
(
#{
files
}
):
#{
title
}
"
)
end
end
def
commit_from_id
(
project
,
id
)
def
commit_from_id
(
id
)
project
.
commit
(
id
)
@
project
.
commit
(
id
)
end
end
def
files_count
(
commit
)
def
files_count
(
commit
)
...
...
spec/workers/irker_worker_spec.rb
0 → 100644
View file @
301e9c7c
# frozen_string_literal: true
require
'spec_helper'
describe
IrkerWorker
,
'#perform'
do
let_it_be
(
:project
)
{
create
(
:project
,
:repository
)
}
let_it_be
(
:user
)
{
create
(
:user
)
}
let_it_be
(
:push_data
)
{
HashWithIndifferentAccess
.
new
(
Gitlab
::
DataBuilder
::
Push
.
build_sample
(
project
,
user
))
}
let_it_be
(
:channels
)
{
[
'irc://test.net/#test'
]
}
let_it_be
(
:server_settings
)
do
{
server_host:
'localhost'
,
server_port:
6659
}
end
let_it_be
(
:arguments
)
do
[
project
.
id
,
channels
,
false
,
push_data
,
server_settings
]
end
let
(
:tcp_socket
)
{
double
(
'socket'
)
}
subject
(
:worker
)
{
described_class
.
new
}
before
do
allow
(
TCPSocket
).
to
receive
(
:new
).
and_return
(
tcp_socket
)
allow
(
tcp_socket
).
to
receive
(
:puts
).
and_return
(
true
)
allow
(
tcp_socket
).
to
receive
(
:close
).
and_return
(
true
)
end
context
'connection fails'
do
before
do
allow
(
TCPSocket
).
to
receive
(
:new
).
and_raise
(
Errno
::
ECONNREFUSED
.
new
(
'test'
))
end
it
{
expect
(
subject
.
perform
(
*
arguments
)).
to
be_falsey
}
end
context
'connection successful'
do
it
{
expect
(
subject
.
perform
(
*
arguments
)).
to
be_truthy
}
context
'new branch'
do
it
'sends a correct message with branches url'
do
branches_url
=
Gitlab
::
Routing
.
url_helpers
.
project_branches_url
(
project
)
push_data
[
'before'
]
=
'0000000000000000000000000000000000000000'
message
=
"has created a new branch master:
#{
branches_url
}
"
expect
(
tcp_socket
).
to
receive
(
:puts
).
with
(
wrap_message
(
message
))
subject
.
perform
(
*
arguments
)
end
end
context
'deleted branch'
do
it
'sends a correct message'
do
push_data
[
'after'
]
=
'0000000000000000000000000000000000000000'
message
=
"has deleted the branch master"
expect
(
tcp_socket
).
to
receive
(
:puts
).
with
(
wrap_message
(
message
))
subject
.
perform
(
*
arguments
)
end
end
context
'new commits to existing branch'
do
it
'sends a correct message with a compare url'
do
compare_url
=
Gitlab
::
Routing
.
url_helpers
.
project_compare_url
(
project
,
from:
Commit
.
truncate_sha
(
push_data
[
:before
]),
to:
Commit
.
truncate_sha
(
push_data
[
:after
]))
message
=
"pushed
#{
push_data
[
'total_commits_count'
]
}
"
\
"new commits to master:
#{
compare_url
}
"
expect
(
tcp_socket
).
to
receive
(
:puts
).
with
(
wrap_message
(
message
))
subject
.
perform
(
*
arguments
)
end
end
end
def
wrap_message
(
text
)
message
=
"[
#{
project
.
path
}
]
#{
push_data
[
'user_name'
]
}
#{
text
}
"
to_send
=
{
to:
channels
,
privmsg:
message
}
Gitlab
::
Json
.
dump
(
to_send
)
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment