Skip to content

Commit

Permalink
Merge f12b0bc into 04b9c15
Browse files Browse the repository at this point in the history
  • Loading branch information
nessamurmur committed Jan 10, 2018
2 parents 04b9c15 + f12b0bc commit 832eded
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/lita/handlers/travis.rb
Expand Up @@ -9,14 +9,22 @@ class Travis < Handler
config :token, type: String, required: true
config :repos, type: Hash, default: {}
config :default_rooms, type: [Array, String]
config :branch, type: String

http.post "/travis", :receive

def receive(request, response)
data = parse_payload(request.params["payload"]) or return
repo = get_repo(data)
validate_repo(repo, request.env["HTTP_AUTHORIZATION"]) or return
notify_rooms(repo, data)
case config.branch
when nil
notify_rooms(repo, data)
when -> (branch) { branch == data['branch'] }
notify_rooms(repo, data)
else
Lita.logger.info("Skipping notification for branch #{data['branch']}")
end
end

private
Expand All @@ -25,7 +33,7 @@ def parse_payload(json)
begin
MultiJson.load(json)
rescue MultiJson::LoadError => e
Lita.logger.error(t("parse_error", message: e.message))
Lita.logger.error("parse_error, message: #{e.message}")
return
end
end
Expand Down Expand Up @@ -62,14 +70,14 @@ def rooms_for_repo(repo)
elsif default_rooms
Array(default_rooms)
else
Lita.logger.warn(t("no_room_configured"), repo: repo)
Lita.logger.warn("no_room_configured: ignored because no rooms were specified")
return
end
end

def validate_repo(repo, auth_hash)
unless Digest::SHA2.hexdigest("#{repo}#{config.token}") == auth_hash
Lita.logger.warn(t("auth_failed"), repo: repo)
Lita.logger.warn("auth_failed: did not pass authentication")
return
end

Expand Down
1 change: 1 addition & 0 deletions lita-travis.gemspec
Expand Up @@ -21,4 +21,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rspec", ">= 3.0.0"
spec.add_development_dependency "simplecov"
spec.add_development_dependency "coveralls"
spec.add_development_dependency "pry"
end
39 changes: 39 additions & 0 deletions spec/lita/handlers/travis_spec.rb
Expand Up @@ -44,6 +44,22 @@
JSON
end

let(:different_branch_payload) do
<<-JSON.chomp
{
"status_message": "Passed",
"branch": "staging",
"commit": "abcdefg",
"committer_name": "Bongo",
"compare_url": "https://example.com/",
"repository": {
"name": "bar",
"owner_name": "foo"
}
}
JSON
end

before do
registry.config.handlers.travis.repos = {}
end
Expand Down Expand Up @@ -97,6 +113,29 @@
subject.receive(request, response)
end
end
context "branch config set" do
before do
registry.config.handlers.travis.branch = "master"
allow(request).to receive(:env).and_return(valid_env)
end

it "doesn't send notifications for branches that don't match the config" do
allow(params).to receive(:[]).with("payload")
.and_return(different_branch_payload)
expect(robot).not_to receive(:send_message)
expect(Lita.logger).to receive(:info)
subject.receive(request, response)
end

it "only sends notifications for branches that match the config" do
allow(params).to receive(:[]).with("payload")
.and_return(valid_payload)
expect(robot).to receive(:send_message) do |target, message|
expect(message).to include("[Travis CI]")
end
subject.receive(request, response)
end
end

context "without setting a value for the repo in config.repos and no default" do
before do
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Expand Up @@ -8,5 +8,6 @@

require "lita-travis"
require "lita/rspec"
require "pry"

Lita.version_3_compatibility_mode = false

0 comments on commit 832eded

Please sign in to comment.