forked from zendesk/samson
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Zachary Wright
committed
Feb 9, 2018
1 parent
26ba9f0
commit 489dc92
Showing
10 changed files
with
171 additions
and
12 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 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,5 @@ | ||
class AddNotifyAssertibleToStages < ActiveRecord::Migration[5.1] | ||
def change | ||
add_column :stages, :notify_assertible, :boolean, default: false, null: false | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
Notify Assertible after successful deploys. | ||
|
||
Following environment variables must be set: | ||
- `ASSERTIBLE_DEPLOY_TOKEN` | ||
- `ASSERTIBLE_SERVICE_KEY` | ||
|
||
They can be found in Assertible by going to the `Deployments` tab from a | ||
service's dashboard. |
1 change: 1 addition & 0 deletions
1
plugins/assertible/app/views/samson_assertible/_stage_form_checkbox.html.erb
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 @@ | ||
<%= form.input :notify_assertible, label: 'Notify Assertible of deploys', as: :check_box %> |
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,52 @@ | ||
# frozen_string_literal: true | ||
|
||
module SamsonAssertible | ||
class Engine < Rails::Engine | ||
end | ||
|
||
class Notification | ||
class << self | ||
def deliver(deploy) | ||
return unless service_key.present? | ||
return unless deploy_token.present? | ||
return unless deploy.stage.notify_assertible? | ||
return unless deploy.succeeded? | ||
|
||
conn = Faraday.new(url: 'https://assertible.com') | ||
conn.basic_auth(deploy_token, '') | ||
conn.post( | ||
'/deployments', | ||
{ | ||
service: service_key, | ||
environmentName: deploy.stage.name, | ||
version: 'v1', | ||
url: url_helpers.project_deploy_url( | ||
id: deploy.id, | ||
project_id: deploy.project.id | ||
) | ||
}.to_json | ||
) | ||
end | ||
|
||
private | ||
|
||
def service_key | ||
ENV['ASSERTIBLE_SERVICE_KEY'] | ||
end | ||
|
||
def deploy_token | ||
ENV['ASSERTIBLE_DEPLOY_TOKEN'] | ||
end | ||
|
||
def url_helpers | ||
Rails.application.routes.url_helpers | ||
end | ||
end | ||
end | ||
end | ||
|
||
Samson::Hooks.view :stage_form_checkbox, 'samson_assertible/stage_form_checkbox' | ||
|
||
Samson::Hooks.callback :after_deploy do |deploy, _buddy| | ||
SamsonAssertible::Notification.deliver(deploy) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Gem::Specification.new 'samson_assertible', '0.0.0' do |s| | ||
s.summary = 'Samson Assertible plugin' | ||
s.authors = ['Zachary Wright'] | ||
s.email = ['[email protected]'] | ||
s.files = Dir['{app,config,db,lib}/**/*'] | ||
end |
87 changes: 87 additions & 0 deletions
87
plugins/assertible/test/samson_assertible/samson_plugin_test.rb
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,87 @@ | ||
# frozen_string_literal: true | ||
require_relative '../test_helper' | ||
|
||
SingleCov.covered! | ||
|
||
describe SamsonAssertible::Notification do | ||
describe '.deliver' do | ||
subject { SamsonAssertible::Notification.deliver(deploy) } | ||
|
||
let(:url_helpers) { Rails.application.routes.url_helpers } | ||
let(:deploy) { deploys(:succeeded_test) } | ||
|
||
before do | ||
deploy.stage.update_column(:notify_assertible, true) | ||
end | ||
|
||
with_env(ASSERTIBLE_SERVICE_KEY: 'test_token') | ||
with_env(ASSERTIBLE_DEPLOY_TOKEN: 'test_deploy') | ||
|
||
it "sends a notification" do | ||
assert_request( | ||
:post, "https://assertible.com/deployments", | ||
with: { | ||
body: { | ||
'service' => 'test_token', | ||
'environmentName' => deploy.stage.name, | ||
'version' => 'v1', | ||
url: url_helpers.project_deploy_url( | ||
id: deploy.id, | ||
project_id: deploy.project.id | ||
) | ||
}.to_json, | ||
basic_auth: ['test_deploy', ''] | ||
} | ||
) { subject } | ||
end | ||
|
||
context 'When no service key' do | ||
with_env(ASSERTIBLE_SERVICE_KEY: nil) | ||
|
||
it 'Does not send notification' do | ||
Faraday.expects(:post).never | ||
subject | ||
end | ||
end | ||
|
||
context 'When no deploy token' do | ||
with_env(ASSERTIBLE_DEPLOY_TOKEN: nil) | ||
|
||
it 'Does not send notification' do | ||
Faraday.expects(:post).never | ||
subject | ||
end | ||
end | ||
|
||
context 'When deploy fails' do | ||
let(:deploy) { deploys(:failed_staging_test) } | ||
|
||
it 'Does not send notification' do | ||
Faraday.expects(:post).never | ||
subject | ||
end | ||
end | ||
|
||
context 'When notifications are not enabled' do | ||
before do | ||
deploy.stage.update_column(:notify_assertible, false) | ||
end | ||
|
||
it 'Does not send notification' do | ||
Faraday.expects(:post).never | ||
subject | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe :after_deploy do | ||
subject { Samson::Hooks.fire :after_deploy, deploy, users(:admin) } | ||
|
||
let(:deploy) { deploys(:succeeded_test) } | ||
|
||
it 'Triggers delivery' do | ||
SamsonAssertible::Notification.expects(:deliver).once | ||
subject | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require_relative '../../../test/test_helper' |