Skip to content

Commit b74b44d

Browse files
author
Ben Lavender
authored
Merge pull request #10 from github/rails-5
Rails 5
2 parents 90a4fae + b0146c6 commit b74b44d

45 files changed

Lines changed: 167 additions & 45 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Gemfile

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
source 'https://rubygems.org'
22

3-
# Declare your gem's dependencies in chatops_controller.gemspec.
4-
# Bundler will treat runtime dependencies like base dependencies, and
5-
# development dependencies will be added by default to the :development group.
6-
gemspec
7-
8-
# Declare any dependencies that are still in development here instead of in
9-
# your gemspec. These might include edge Rails or gems from your path or
10-
# Git. Remember to move these dependencies to your gemspec before releasing
11-
# your gem to rubygems.org.
12-
13-
# To use a debugger
14-
# gem 'byebug', group: [:development, :test]
3+
gem 'rails', '~> 4.0'
154

5+
group :development, :test do
6+
gem "rspec-rails", "~> 3"
7+
gem "rspec_json_dumper", "~> 0.1"
8+
gem "pry", "~> 0"
9+
end

Gemfile.rails5

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
source 'https://rubygems.org'
2+
3+
gem 'rails', '~> 5.0'
4+
5+
group :development, :test do
6+
gem "rspec-rails", "~> 3"
7+
gem "rspec_json_dumper", "~> 0.1"
8+
gem "pry", "~> 0"
9+
end

README.md

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# ChatopsControllers
22

3-
Rails helpers for JSON-RPC based easy chatops.
3+
Rails helpers for easy, JSON-RPC based chatops.
44

55
A minimal controller example:
66

@@ -22,7 +22,7 @@ Some routing boilerplate is required in `config/routes.rb`:
2222

2323
```ruby
2424
Rails.application.routes.draw do
25-
post "/_chatops/:action", controller: "chatops"
25+
post "/_chatops/:chatop", controller: "anonymous", action: :execute_chatop
2626
get "/_chatops" => "chatops#list"
2727
end
2828
```
@@ -80,7 +80,7 @@ have an input validation or other handle-able error, you can use
8080
`jsonrpc_failure` to send a helpful error message.
8181

8282
ChatOps are regular old rails controller actions, and you can use niceties like
83-
`before_filter` and friends. `before_filter :echo, :load_user` for the above
83+
`before_action` and friends. `before_action :echo, :load_user` for the above
8484
case would call `load_user` before running `echo`.
8585

8686
## Authentication
@@ -105,3 +105,64 @@ foo` and `.echo foo in staging` to use two different servers to run `.echo foo`.
105105
script/bootstrap
106106
script/test
107107
```
108+
109+
## Upgrading from early versions
110+
111+
Early versions of RPC chatops had two major changes:
112+
113+
##### They used Rails' dynamic `:action` routing, which was deprecated in Rails 5.
114+
115+
To work around this, you need to update your router boilerplate:
116+
117+
This:
118+
119+
```ruby
120+
post "/_chatops/:action", controller: "anonymous"
121+
```
122+
123+
Becomes this:
124+
125+
```ruby
126+
post "/_chatops/:chatop", controller: "anonymous", action: :execute_chatop
127+
```
128+
129+
#####
130+
131+
They did not require a prefix. Version 2 of the Chatops RPC protocol assumes a unique prefix for each endpoint. This decision was made for several reasons:
132+
133+
* The previous suffix-based system creates semantic ambiguities with keyword arguments
134+
* Prefixes allow big improvements to `.help`
135+
* Prefixes make regex-clobbering impossible
136+
137+
To upgrade to version 2, upgrade to version 2.x of this gem. To migrate:
138+
139+
* Migrate your chatops to remove any prefixes you have:
140+
141+
```ruby
142+
chatop :foo, "help", /ci build whatever/, do "yay" end
143+
```
144+
145+
Becomes:
146+
147+
```ruby
148+
chatop :foo, "help", /build whatever/, do "yay" end
149+
```
150+
151+
* Update your tests:
152+
153+
```ruby
154+
chat "ci build foobar"
155+
```
156+
157+
Becomes:
158+
159+
```ruby
160+
chat "build foobar"
161+
```
162+
163+
* Remove and re-add your endpoint with a prefix:
164+
165+
```
166+
.rpc delete https://my-endpoint.dev
167+
.rpc add https://my-endpoint.dev with prefix ci
168+
```

chatops_controller.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Gem::Specification.new do |s|
1717
s.files = Dir["{app,config,db,lib}/**/*", "README.md"]
1818
s.test_files = Dir["spec/**/*"]
1919

20-
s.add_dependency "rails", "~> 4.0"
20+
s.add_dependency "rails", ">= 4.0"
2121

2222
s.add_development_dependency "rspec-rails", "~> 3"
2323
s.add_development_dependency "rspec_json_dumper", "~> 0.1"

lib/chatops/controller.rb

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ module Controller
33
extend ActiveSupport::Concern
44

55
included do
6-
before_filter :ensure_chatops_authenticated
7-
before_filter :ensure_user_given
8-
before_filter :ensure_method_exists
6+
before_action :ensure_chatops_authenticated
7+
before_action :ensure_user_given
8+
before_action :ensure_method_exists
99
end
1010

1111
def list
@@ -15,16 +15,34 @@ def list
1515
namespace: self.class.chatops_namespace,
1616
help: self.class.chatops_help,
1717
error_response: self.class.chatops_error_response,
18-
methods: chatops }
18+
methods: chatops,
19+
version: "2" }
1920
end
2021

2122
def process(*args)
22-
params.merge!(jsonrpc_params.except(:user, :method, :controller, :action, :params, :room_id))
23-
super
23+
scrubbed_params = jsonrpc_params.except(
24+
:user, :method, :controller, :action, :params, :room_id)
25+
26+
scrubbed_params.each { |k, v| params[k] = v }
27+
28+
if params[:chatop].present?
29+
params[:action] = params[:chatop]
30+
args[0] = params[:action]
31+
unless self.respond_to?(params[:chatop].to_sym)
32+
raise AbstractController::ActionNotFound
33+
end
34+
end
35+
36+
super *args
2437
rescue AbstractController::ActionNotFound
2538
return jsonrpc_method_not_found
2639
end
2740

41+
def execute_chatop
42+
# This needs to exist for route declarations, but we'll be overriding
43+
# things in #process to make a method the action.
44+
end
45+
2846
protected
2947

3048
def jsonrpc_params
@@ -85,7 +103,7 @@ def ensure_chatops_authenticated
85103
Rack::Utils.secure_compare(ENV["CHATOPS_ALT_AUTH_TOKEN"], p)
86104
end
87105
unless authenticated
88-
render :status => :forbidden, :text => "Not authorized"
106+
render :status => :forbidden, :plain => "Not authorized"
89107
end
90108
end
91109

lib/chatops/controller/test_case_helpers.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,19 @@ def chatop(method, params = {})
1010
args = params.dup.symbolize_keys
1111
user = args.delete :user
1212
room_id = args.delete :room_id
13-
post method, :method => method, :room_id => room_id, :user => user, :params => args
13+
14+
params = {
15+
:params => args,
16+
:room_id => room_id,
17+
:user => user
18+
}
19+
20+
major_version = Rails.version.split('.')[0].to_i
21+
if major_version >= 5
22+
post :execute_chatop, params: params.merge(chatop: method)
23+
else
24+
post :execute_chatop, params.merge(chatop: method)
25+
end
1426
end
1527

1628
def chat(message, user, room_id = "123")

lib/chatops_controller/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module ChatopsController
2-
VERSION = "0.2.0"
2+
VERSION = "2.0.0"
33
end

script/cibuild

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ set -eu
55

66
test -d "/usr/share/rbenv/shims" && {
77
export PATH="/usr/share/rbenv/shims:$PATH"
8-
export RBENV_VERSION="2.2.3"
8+
export RBENV_VERSION="2.3.1"
99
}
1010

11+
# -f to not error missing versions
12+
rm -f Gemfile.lock Gemfile.rails5.lock
13+
1114
# clean out the ruby environment
1215
export RUBYLIB=
1316
export RUBYOPT=
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/sh
2+
#!/bin/sh
3+
4+
export BUNDLE_GEMFILE="./Gemfile.rails5"
5+
script/cibuild

spec/dummy/config/environments/test.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@
1313
config.eager_load = false
1414

1515
# Configure static file server for tests with Cache-Control for performance.
16-
config.serve_static_files = true
17-
config.static_cache_control = 'public, max-age=3600'
16+
if Rails.version.starts_with?("5")
17+
config.public_file_server.enabled = true
18+
config.public_file_server.headers = { 'Cache-Control' => 'public, max-age=3600' }
19+
else
20+
config.serve_static_files = true
21+
config.static_cache_control = 'public, max-age=3600'
22+
end
1823

1924
# Show full error reports and disable caching.
2025
config.consider_all_requests_local = true

0 commit comments

Comments
 (0)