Skip to content

Commit 083cd61

Browse files
authored
Merge pull request #45 from github/add-message-id
Proposal to add an optional message id to the CRPC protocol
2 parents 61336a5 + 7c34327 commit 083cd61

5 files changed

Lines changed: 49 additions & 4 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ always be the login of the user typing the command, and `room_id` will be where
9595
it was typed.
9696
The optional `mention_slug` parameter will provide the name to use to refer to
9797
the user when sending a message; this may or may not be the same thing as the
98-
username, depending on the chat system being used.
98+
username, depending on the chat system being used. The optional `message_id` parameter will provide a reference to the message that invoked the rpc.
9999

100100
You can return `jsonrpc_success` with a string to return text to chat. If you
101101
have an input validation or other handle-able error, you can use

docs/protocol-description.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ invocation. A method invocation is a JSON object with the following fields:
4444

4545
* `user`: A slug username corresponding to to the command giver's GitHub login.
4646
* `mention_slug`: Optional. If provided, a string which should be used to mention the user when sending a message in response. For example, Slack requires that users be mentioned using user IDs instead of usernames.
47+
* `message_id`: Optional. If provided, an id that uniquely identifies the message that generated the CRPC call. Useful for linking back to the original command to provide context.
4748
* `room_id`: A slug room name where the command originated.
4849
* `method`: The method name, without namespace, of the matching regex.
4950
* `params`: A mapping of parameter names to matches extracted from named capture groups in the command's regex. Parameters that are empty or null should not be passed.

lib/chatops/controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def setup_params!
5757

5858
@jsonrpc_params = params.delete(:params) if params.has_key? :params
5959

60-
self.params = params.permit(:action, :chatop, :controller, :id, :mention_slug, :method, :room_id, :user)
60+
self.params = params.permit(:action, :chatop, :controller, :id, :mention_slug, :message_id, :method, :room_id, :user)
6161
end
6262

6363
def jsonrpc_params

lib/chatops/controller/test_case_helpers.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ def chatop(method, params = {})
2121
user = args.delete :user
2222
room_id = args.delete :room_id
2323
mention_slug = args.delete :mention_slug
24+
message_id = args.delete :message_id
2425

2526
params = {
2627
:params => args,
2728
:room_id => room_id,
2829
:user => user,
2930
:mention_slug => mention_slug,
31+
:message_id => message_id,
3032
}
3133

3234
major_version = Rails.version.split('.')[0].to_i
@@ -37,7 +39,7 @@ def chatop(method, params = {})
3739
end
3840
end
3941

40-
def chat(message, user, room_id = "123")
42+
def chat(message, user, room_id = "123", message_id = "456")
4143
get :list
4244
json_response = JSON.load(response.body)
4345
matchers = json_response["methods"].map { |name, metadata|
@@ -59,7 +61,7 @@ def chat(message, user, room_id = "123")
5961
matcher["params"].each do |param|
6062
jsonrpc_params[param] ||= match_data[param.to_sym]
6163
end
62-
jsonrpc_params.merge!(user: user, room_id: room_id, mention_slug: user)
64+
jsonrpc_params.merge!(user: user, room_id: room_id, mention_slug: user, message_id: message_id)
6365
chatop matcher["name"].to_sym, jsonrpc_params
6466
end
6567

spec/lib/chatops/controller_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@
2525
jsonrpc_success "You just foo and bar like it just don't matter"
2626
end
2727

28+
chatop :proxy_parameters,
29+
/(?:proxy_parameters)/,
30+
"proxy parameters back to test" do
31+
response = { :params => params, :jsonrpc_params => jsonrpc_params }.to_json
32+
jsonrpc_success response
33+
end
34+
2835
skip_before_action :ensure_method_exists, only: :non_chatop_method
2936
def non_chatop_method
3037
render :plain => "Why would you have something thats not a chatop?"
@@ -228,6 +235,12 @@ def rails_flexible_post(path, outer_params, jsonrpc_params = nil)
228235
"regex" => /(?:how can i foo and bar all at once)?/.source,
229236
"params" => [],
230237
"path" => "foobar"
238+
},
239+
"proxy_parameters" => {
240+
"help" => "proxy parameters back to test",
241+
"regex" => /(?:proxy_parameters)/.source,
242+
"params" => [],
243+
"path" => "proxy_parameters"
231244
}
232245
},
233246
"version" => "3"
@@ -293,6 +306,26 @@ def rails_flexible_post(path, outer_params, jsonrpc_params = nil)
293306
expect(response.status).to eq 200
294307
end
295308

309+
it "passes all expected paramters" do
310+
rails_flexible_post :execute_chatop, {
311+
:chatop => "proxy_parameters",
312+
:user => "foo",
313+
:mention_slug => "mention_slug_here",
314+
:message_id => "message_id_here",
315+
:room_id => "#someroom",
316+
:unknown_key => "few" # This should get ignored
317+
}, {
318+
"app" => "foo"
319+
}
320+
expect(json_response).to eq({
321+
"jsonrpc" => "2.0",
322+
"id" => nil,
323+
"result" => "{\"params\":{\"action\":\"proxy_parameters\",\"chatop\":\"proxy_parameters\",\"controller\":\"anonymous\",\"mention_slug\":\"mention_slug_here\",\"message_id\":\"message_id_here\",\"room_id\":\"#someroom\",\"user\":\"foo\"},\"jsonrpc_params\":{\"app\":\"foo\"}}"
324+
})
325+
expect(response.status).to eq 200
326+
end
327+
328+
296329
it "uses typical controller fun like before_action" do
297330
rails_flexible_post :execute_chatop, :chatop => "wcid", :user => "foo"
298331
expect(json_response).to eq({
@@ -360,6 +393,15 @@ def rails_flexible_post(path, outer_params, jsonrpc_params = nil)
360393
expect(request.params["params"]["this-is-sparta"]).to eq "true"
361394
end
362395

396+
it "sends along all the parameters" do
397+
chat "where can i deploy foobar", "my_username", "room_id_5", "message_id_6"
398+
expect(request.params["action"]).to eq "execute_chatop"
399+
expect(request.params["chatop"]).to eq "wcid"
400+
expect(request.params["user"]).to eq "my_username"
401+
expect(request.params["room_id"]).to eq "room_id_5"
402+
expect(request.params["message_id"]).to eq "message_id_6"
403+
end
404+
363405
it "anchors regexes" do
364406
expect {
365407
chat "too bad that this message doesn't start with where can i deploy foobar", "bhuga"

0 commit comments

Comments
 (0)