diff --git a/lib/lita/handlers/travis.rb b/lib/lita/handlers/travis.rb index 12ae645..2c6e179 100644 --- a/lib/lita/handlers/travis.rb +++ b/lib/lita/handlers/travis.rb @@ -6,11 +6,9 @@ module Lita module Handlers # Provides Travis CI webhooks for Lita. class Travis < Handler - def self.default_config(config) - config.token = nil - config.repos = {} - config.default_rooms = nil - end + config :token, type: String, required: true + config :repos, type: Hash, default: {} + config :default_rooms, type: [Array, String] http.post "/travis", :receive @@ -56,8 +54,8 @@ def notify_rooms(repo, data) end def rooms_for_repo(repo) - rooms = Lita.config.handlers.travis.repos[repo] - default_rooms = Lita.config.handlers.travis.default_rooms + rooms = config.repos[repo] + default_rooms = config.default_rooms if rooms Array(rooms) @@ -70,14 +68,7 @@ def rooms_for_repo(repo) end def validate_repo(repo, auth_hash) - token = Lita.config.handlers.travis.token - - unless token - Lita.logger.warn(t("no_token")) - return - end - - unless Digest::SHA2.hexdigest("#{repo}#{token}") == auth_hash + unless Digest::SHA2.hexdigest("#{repo}#{config.token}") == auth_hash Lita.logger.warn(t("auth_failed"), repo: repo) return end diff --git a/lita-travis.gemspec b/lita-travis.gemspec index cef4acb..9408470 100644 --- a/lita-travis.gemspec +++ b/lita-travis.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |spec| spec.name = "lita-travis" - spec.version = "0.2.0" + spec.version = "1.0.0" spec.authors = ["Jimmy Cuadra"] spec.email = ["jimmy@jimmycuadra.com"] spec.description = %q{A Lita handler for receiving notifications from Travis CI.} @@ -14,11 +14,11 @@ Gem::Specification.new do |spec| spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) spec.require_paths = ["lib"] - spec.add_runtime_dependency "lita", ">= 2.5" + spec.add_runtime_dependency "lita", ">= 4.0" spec.add_development_dependency "bundler", "~> 1.3" spec.add_development_dependency "rake" - spec.add_development_dependency "rspec", ">= 3.0.0.beta2" + spec.add_development_dependency "rspec", ">= 3.0.0" spec.add_development_dependency "simplecov" spec.add_development_dependency "coveralls" end diff --git a/locales/en.yml b/locales/en.yml index 316d82a..b056b3a 100644 --- a/locales/en.yml +++ b/locales/en.yml @@ -10,6 +10,3 @@ en: no_room_configured: >- Notification from Travis CI for %{repo} ignored because no rooms were specified in the config.repos hash, and no default rooms were specified in config.default_rooms. - no_token: >- - Notification from Travis CI could not be validated because Lita.config.handlers.token - is not set. diff --git a/spec/lita/handlers/travis_spec.rb b/spec/lita/handlers/travis_spec.rb index e0dbefb..77c9ed8 100644 --- a/spec/lita/handlers/travis_spec.rb +++ b/spec/lita/handlers/travis_spec.rb @@ -1,17 +1,7 @@ require "spec_helper" describe Lita::Handlers::Travis, lita_handler: true do - it { routes_http(:post, "/travis").to(:receive) } - - describe ".default_config" do - it "sets token to nil" do - expect(Lita.config.handlers.travis.token).to be_nil - end - - it "sets repos to an empty hash" do - expect(Lita.config.handlers.travis.repos).to eq({}) - end - end + it { is_expected.to route_http(:post, "/travis").to(:receive) } describe "#receive" do let(:request) do @@ -54,10 +44,14 @@ JSON end + before do + registry.config.handlers.travis.repos = {} + end + context "happy path" do before do - Lita.config.handlers.travis.token = "abc123" - Lita.config.handlers.travis.repos["foo/bar"] = "#baz" + registry.config.handlers.travis.token = "abc123" + registry.config.handlers.travis.repos["foo/bar"] = "#baz" allow(request).to receive(:env).and_return(valid_env) allow(params).to receive(:[]).with("payload").and_return(valid_payload) end @@ -71,25 +65,10 @@ end end - context "with a missing token" do - before do - Lita.config.handlers.travis.repos["foo/bar"] = "#baz" - allow(request).to receive(:env).and_return(valid_env) - allow(params).to receive(:[]).with("payload").and_return(valid_payload) - end - - it "logs a warning that the token is not set" do - expect(Lita.logger).to receive(:warn) do |warning| - expect(warning).to include("token is not set") - end - subject.receive(request, response) - end - end - context "with an invalid authorization header" do before do - Lita.config.handlers.travis.token = "abc123" - Lita.config.handlers.travis.repos["foo/bar"] = "#baz" + registry.config.handlers.travis.token = "abc123" + registry.config.handlers.travis.repos["foo/bar"] = "#baz" allow(request).to receive(:env).and_return(invalid_env) allow(params).to receive(:[]).with("payload").and_return(valid_payload) end @@ -104,8 +83,8 @@ context "with only config.default_rooms set" do before do - Lita.config.handlers.travis.token = "abc123" - Lita.config.handlers.travis.default_rooms = "#default" + registry.config.handlers.travis.token = "abc123" + registry.config.handlers.travis.default_rooms = "#default" allow(request).to receive(:env).and_return(valid_env) allow(params).to receive(:[]).with("payload").and_return(valid_payload) end @@ -121,7 +100,8 @@ context "without setting a value for the repo in config.repos and no default" do before do - Lita.config.handlers.travis.token = "abc123" + registry.config.handlers.travis.token = "abc123" + allow(registry.config.handlers.travis).to receive(:default_rooms).and_return(nil) allow(request).to receive(:env).and_return(valid_env) allow(params).to receive(:[]).with("payload").and_return(valid_payload) end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 8634d75..cd0bd5a 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -8,3 +8,5 @@ require "lita-travis" require "lita/rspec" + +Lita.version_3_compatibility_mode = false