-
-
Notifications
You must be signed in to change notification settings - Fork 909
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3507 from segiddins/segiddins/webhook-notify-job
Send webhook notifications through an ActiveJob
- Loading branch information
Showing
9 changed files
with
107 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
class NotifyWebHookJob < ApplicationJob | ||
extend StatsD::Instrument | ||
include TraceTagger | ||
|
||
queue_as :default | ||
self.queue_adapter = :good_job | ||
|
||
before_perform { @kwargs = arguments.last.then { _1 if Hash.ruby2_keywords_hash?(_1) } } | ||
before_perform { @webhook = @kwargs.fetch(:webhook) } | ||
before_perform { @protocol = @kwargs.fetch(:protocol) } | ||
before_perform { @host_with_port = @kwargs.fetch(:host_with_port) } | ||
before_perform { @version = @kwargs.fetch(:version) } | ||
before_perform { @rubygem = @version.rubygem } | ||
|
||
attr_reader :webhook, :protocol, :host_with_port, :version, :rubygem | ||
|
||
def perform(*) | ||
url = webhook.url | ||
set_tag "gemcutter.notifier.url", url | ||
set_tag "gemcutter.notifier.webhook_id", webhook.id | ||
|
||
timeout(5) do | ||
RestClient.post url, | ||
payload, | ||
:timeout => 5, | ||
:open_timeout => 5, | ||
"Content-Type" => "application/json", | ||
"Authorization" => authorization | ||
end | ||
true | ||
rescue *(HTTP_ERRORS + [RestClient::Exception, SocketError, SystemCallError]) => _e | ||
webhook.increment! :failure_count | ||
false | ||
end | ||
statsd_count_success :perform, "Webhook.perform" | ||
|
||
def payload | ||
rubygem.payload(version, protocol, host_with_port).to_json | ||
end | ||
|
||
def authorization | ||
Digest::SHA2.hexdigest([rubygem.name, version.number, webhook.api_key].compact.join) | ||
end | ||
|
||
private | ||
|
||
def timeout(sec, &) | ||
Timeout.timeout(sec, &) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require "test_helper" | ||
|
||
class NotifyWebHookJobTest < ActiveJob::TestCase | ||
context "with a rubygem and version" do | ||
setup do | ||
@rubygem = create(:rubygem, name: "foogem", downloads: 42) | ||
@version = create(:version, | ||
rubygem: @rubygem, | ||
number: "3.2.1", | ||
authors: %w[AUTHORS], | ||
description: "DESC") | ||
@hook = create(:web_hook, rubygem: @rubygem) | ||
@job = NotifyWebHookJob.new(webhook: @hook, protocol: "http", host_with_port: "localhost:1234", version: @version) | ||
end | ||
|
||
should "have gem properties encoded in JSON" do | ||
payload = @job.run_callbacks(:perform) { JSON.parse(@job.payload) } | ||
assert_equal "foogem", payload["name"] | ||
assert_equal "3.2.1", payload["version"] | ||
assert_equal "ruby", payload["platform"] | ||
assert_equal "DESC", payload["info"] | ||
assert_equal "AUTHORS", payload["authors"] | ||
assert_equal 42, payload["downloads"] | ||
assert_equal "http://localhost:1234/gems/foogem", payload["project_uri"] | ||
assert_equal "http://localhost:1234/gems/foogem-3.2.1.gem", payload["gem_uri"] | ||
end | ||
|
||
should "send the right version out even for older gems" do | ||
new_version = create(:version, number: "2.0.0", rubygem: @rubygem) | ||
new_hook = create(:web_hook) | ||
job = NotifyWebHookJob.new(webhook: new_hook, protocol: "http", host_with_port: "localhost:1234", version: new_version) | ||
payload = job.run_callbacks(:perform) { JSON.parse(job.payload) } | ||
|
||
assert_equal "foogem", payload["name"] | ||
assert_equal "2.0.0", payload["version"] | ||
assert_equal "http://localhost:1234/gems/foogem", payload["project_uri"] | ||
assert_equal "http://localhost:1234/gems/foogem-2.0.0.gem", payload["gem_uri"] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters