BigW Consortium Gitlab
Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gitlab-ce
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Forest Godfrey
gitlab-ce
Commits
60503057
Commit
60503057
authored
Feb 27, 2018
by
Kamil Trzciński
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'slash-commands-changes-for-chatops' into 'master'
Expose ChatName objects to slash commands See merge request gitlab-org/gitlab-ce!17295
parents
a4885b8f
57719d34
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
81 additions
and
24 deletions
+81
-24
chat_name.rb
app/models/chat_name.rb
+21
-0
slash_commands_service.rb
app/models/project_services/slash_commands_service.rb
+3
-3
find_user_service.rb
app/services/chat_names/find_user_service.rb
+2
-2
base_command.rb
lib/gitlab/slash_commands/base_command.rb
+4
-3
command.rb
lib/gitlab/slash_commands/command.rb
+3
-2
command_spec.rb
spec/lib/gitlab/slash_commands/command_spec.rb
+3
-2
deploy_spec.rb
spec/lib/gitlab/slash_commands/deploy_spec.rb
+2
-1
issue_new_spec.rb
spec/lib/gitlab/slash_commands/issue_new_spec.rb
+2
-1
issue_search_spec.rb
spec/lib/gitlab/slash_commands/issue_search_spec.rb
+2
-1
issue_show_spec.rb
spec/lib/gitlab/slash_commands/issue_show_spec.rb
+2
-1
chat_name_spec.rb
spec/models/chat_name_spec.rb
+20
-0
find_user_service_spec.rb
spec/services/chat_names/find_user_service_spec.rb
+17
-8
No files found.
app/models/chat_name.rb
View file @
60503057
class
ChatName
<
ActiveRecord
::
Base
LAST_USED_AT_INTERVAL
=
1
.
hour
belongs_to
:service
belongs_to
:user
...
...
@@ -9,4 +11,23 @@ class ChatName < ActiveRecord::Base
validates
:user_id
,
uniqueness:
{
scope:
[
:service_id
]
}
validates
:chat_id
,
uniqueness:
{
scope:
[
:service_id
,
:team_id
]
}
# Updates the "last_used_timestamp" but only if it wasn't already updated
# recently.
#
# The throttling this method uses is put in place to ensure that high chat
# traffic doesn't result in many UPDATE queries being performed.
def
update_last_used_at
return
unless
update_last_used_at?
obtained
=
Gitlab
::
ExclusiveLease
.
new
(
"chat_name/last_used_at/
#{
id
}
"
,
timeout:
LAST_USED_AT_INTERVAL
.
to_i
)
.
try_obtain
touch
(
:last_used_at
)
if
obtained
end
def
update_last_used_at?
last_used_at
.
nil?
||
last_used_at
>
LAST_USED_AT_INTERVAL
.
ago
end
end
app/models/project_services/slash_commands_service.rb
View file @
60503057
...
...
@@ -30,10 +30,10 @@ class SlashCommandsService < Service
def
trigger
(
params
)
return
unless
valid_token?
(
params
[
:token
])
user
=
find_chat_user
(
params
)
chat_
user
=
find_chat_user
(
params
)
if
user
Gitlab
::
SlashCommands
::
Command
.
new
(
project
,
user
,
params
).
execute
if
chat_user
&
.
user
Gitlab
::
SlashCommands
::
Command
.
new
(
project
,
chat_
user
,
params
).
execute
else
url
=
authorize_chat_name_url
(
params
)
Gitlab
::
SlashCommands
::
Presenters
::
Access
.
new
(
url
).
authorize
...
...
app/services/chat_names/find_user_service.rb
View file @
60503057
...
...
@@ -9,8 +9,8 @@ module ChatNames
chat_name
=
find_chat_name
return
unless
chat_name
chat_name
.
touch
(
:last_used_at
)
chat_name
.
user
chat_name
.
update_last_used_at
chat_name
end
private
...
...
lib/gitlab/slash_commands/base_command.rb
View file @
60503057
...
...
@@ -31,10 +31,11 @@ module Gitlab
raise
NotImplementedError
end
attr_accessor
:project
,
:current_user
,
:params
attr_accessor
:project
,
:current_user
,
:params
,
:chat_name
def
initialize
(
project
,
user
,
params
=
{})
@project
,
@current_user
,
@params
=
project
,
user
,
params
.
dup
def
initialize
(
project
,
chat_name
,
params
=
{})
@project
,
@current_user
,
@params
=
project
,
chat_name
.
user
,
params
.
dup
@chat_name
=
chat_name
end
private
...
...
lib/gitlab/slash_commands/command.rb
View file @
60503057
...
...
@@ -13,12 +13,13 @@ module Gitlab
if
command
if
command
.
allowed?
(
project
,
current_user
)
command
.
new
(
project
,
c
urrent_user
,
params
).
execute
(
match
)
command
.
new
(
project
,
c
hat_name
,
params
).
execute
(
match
)
else
Gitlab
::
SlashCommands
::
Presenters
::
Access
.
new
.
access_denied
end
else
Gitlab
::
SlashCommands
::
Help
.
new
(
project
,
current_user
,
params
).
execute
(
available_commands
,
params
[
:text
])
Gitlab
::
SlashCommands
::
Help
.
new
(
project
,
chat_name
,
params
)
.
execute
(
available_commands
,
params
[
:text
])
end
end
...
...
spec/lib/gitlab/slash_commands/command_spec.rb
View file @
60503057
...
...
@@ -3,10 +3,11 @@ require 'spec_helper'
describe
Gitlab
::
SlashCommands
::
Command
do
let
(
:project
)
{
create
(
:project
)
}
let
(
:user
)
{
create
(
:user
)
}
let
(
:chat_name
)
{
double
(
:chat_name
,
user:
user
)
}
describe
'#execute'
do
subject
do
described_class
.
new
(
project
,
user
,
params
).
execute
described_class
.
new
(
project
,
chat_name
,
params
).
execute
end
context
'when no command is available'
do
...
...
@@ -88,7 +89,7 @@ describe Gitlab::SlashCommands::Command do
end
describe
'#match_command'
do
subject
{
described_class
.
new
(
project
,
user
,
params
).
match_command
.
first
}
subject
{
described_class
.
new
(
project
,
chat_name
,
params
).
match_command
.
first
}
context
'IssueShow is triggered'
do
let
(
:params
)
{
{
text:
'issue show 123'
}
}
...
...
spec/lib/gitlab/slash_commands/deploy_spec.rb
View file @
60503057
...
...
@@ -4,6 +4,7 @@ describe Gitlab::SlashCommands::Deploy do
describe
'#execute'
do
let
(
:project
)
{
create
(
:project
)
}
let
(
:user
)
{
create
(
:user
)
}
let
(
:chat_name
)
{
double
(
:chat_name
,
user:
user
)
}
let
(
:regex_match
)
{
described_class
.
match
(
'deploy staging to production'
)
}
before
do
...
...
@@ -16,7 +17,7 @@ describe Gitlab::SlashCommands::Deploy do
end
subject
do
described_class
.
new
(
project
,
user
).
execute
(
regex_match
)
described_class
.
new
(
project
,
chat_name
).
execute
(
regex_match
)
end
context
'if no environment is defined'
do
...
...
spec/lib/gitlab/slash_commands/issue_new_spec.rb
View file @
60503057
...
...
@@ -4,6 +4,7 @@ describe Gitlab::SlashCommands::IssueNew do
describe
'#execute'
do
let
(
:project
)
{
create
(
:project
)
}
let
(
:user
)
{
create
(
:user
)
}
let
(
:chat_name
)
{
double
(
:chat_name
,
user:
user
)
}
let
(
:regex_match
)
{
described_class
.
match
(
"issue create bird is the word"
)
}
before
do
...
...
@@ -11,7 +12,7 @@ describe Gitlab::SlashCommands::IssueNew do
end
subject
do
described_class
.
new
(
project
,
user
).
execute
(
regex_match
)
described_class
.
new
(
project
,
chat_name
).
execute
(
regex_match
)
end
context
'without description'
do
...
...
spec/lib/gitlab/slash_commands/issue_search_spec.rb
View file @
60503057
...
...
@@ -6,10 +6,11 @@ describe Gitlab::SlashCommands::IssueSearch do
let!
(
:confidential
)
{
create
(
:issue
,
:confidential
,
project:
project
,
title:
'mepmep find'
)
}
let
(
:project
)
{
create
(
:project
)
}
let
(
:user
)
{
create
(
:user
)
}
let
(
:chat_name
)
{
double
(
:chat_name
,
user:
user
)
}
let
(
:regex_match
)
{
described_class
.
match
(
"issue search find"
)
}
subject
do
described_class
.
new
(
project
,
user
).
execute
(
regex_match
)
described_class
.
new
(
project
,
chat_name
).
execute
(
regex_match
)
end
context
'when the user has no access'
do
...
...
spec/lib/gitlab/slash_commands/issue_show_spec.rb
View file @
60503057
...
...
@@ -5,6 +5,7 @@ describe Gitlab::SlashCommands::IssueShow do
let
(
:issue
)
{
create
(
:issue
,
project:
project
)
}
let
(
:project
)
{
create
(
:project
)
}
let
(
:user
)
{
issue
.
author
}
let
(
:chat_name
)
{
double
(
:chat_name
,
user:
user
)
}
let
(
:regex_match
)
{
described_class
.
match
(
"issue show
#{
issue
.
iid
}
"
)
}
before
do
...
...
@@ -12,7 +13,7 @@ describe Gitlab::SlashCommands::IssueShow do
end
subject
do
described_class
.
new
(
project
,
user
).
execute
(
regex_match
)
described_class
.
new
(
project
,
chat_name
).
execute
(
regex_match
)
end
context
'the issue exists'
do
...
...
spec/models/chat_name_spec.rb
View file @
60503057
...
...
@@ -14,4 +14,24 @@ describe ChatName do
it
{
is_expected
.
to
validate_uniqueness_of
(
:user_id
).
scoped_to
(
:service_id
)
}
it
{
is_expected
.
to
validate_uniqueness_of
(
:chat_id
).
scoped_to
(
:service_id
,
:team_id
)
}
describe
'#update_last_used_at'
,
:clean_gitlab_redis_shared_state
do
it
'updates the last_used_at timestamp'
do
expect
(
subject
.
last_used_at
).
to
be_nil
subject
.
update_last_used_at
expect
(
subject
.
last_used_at
).
to
be_present
end
it
'does not update last_used_at if it was recently updated'
do
subject
.
update_last_used_at
time
=
subject
.
last_used_at
subject
.
update_last_used_at
expect
(
subject
.
last_used_at
).
to
eq
(
time
)
end
end
end
spec/services/chat_names/find_user_service_spec.rb
View file @
60503057
require
'spec_helper'
describe
ChatNames
::
FindUserService
do
describe
ChatNames
::
FindUserService
,
:clean_gitlab_redis_shared_state
do
describe
'#execute'
do
let
(
:service
)
{
create
(
:service
)
}
...
...
@@ -13,21 +13,30 @@ describe ChatNames::FindUserService do
context
'when existing user is requested'
do
let
(
:params
)
{
{
team_id:
chat_name
.
team_id
,
user_id:
chat_name
.
chat_id
}
}
it
'returns the existing
user
'
do
is_expected
.
to
eq
(
user
)
it
'returns the existing
chat_name
'
do
is_expected
.
to
eq
(
chat_name
)
end
it
'updates
when last time chat name was used
'
do
it
'updates
the last used timestamp if one is not already set
'
do
expect
(
chat_name
.
last_used_at
).
to
be_nil
subject
initial_last_used
=
chat_name
.
reload
.
last_used_at
expect
(
initial_last_used
).
to
be_present
expect
(
chat_name
.
reload
.
last_used_at
).
to
be_present
end
it
'only updates an existing timestamp once within a certain time frame'
do
service
=
described_class
.
new
(
service
,
params
)
expect
(
chat_name
.
last_used_at
).
to
be_nil
service
.
execute
time
=
chat_name
.
reload
.
last_used_at
Timecop
.
travel
(
2
.
days
.
from_now
)
{
described_class
.
new
(
service
,
params
).
execute
}
service
.
execute
expect
(
chat_name
.
reload
.
last_used_at
).
to
be
>
initial_last_used
expect
(
chat_name
.
reload
.
last_used_at
).
to
eq
(
time
)
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