Skip to content

Commit

Permalink
Add Assertible plugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zachary Wright committed Feb 9, 2018
1 parent 26ba9f0 commit 489dc92
Show file tree
Hide file tree
Showing 10 changed files with 171 additions and 12 deletions.
14 changes: 3 additions & 11 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ PERIODICAL=stop_expired_deploys:60,remove_expired_locks:60,report_system_stats:6

# AIRBRAKE_API_KEY= # optional, report errors to airbrake

# ASSERTIBLE_SERVICE_KEY= # optional, report deploys to Assertible
# ASSERTIBLE_DEPLOY_TOKEN= # optional, report deploys to Assertible

# FORCE_SSL=1 # optional, to require SSL

# SESSION_EXPIRATION=3600 # optional, after how much time (seconds) to expire sessions, default: 1 month
Expand Down Expand Up @@ -173,14 +176,3 @@ PERIODICAL=stop_expired_deploys:60,remove_expired_locks:60,report_system_stats:6

## Feature: Only admins can (un)lock stages which affect production.
# PRODUCTION_STAGE_LOCK_REQUIRES_ADMIN=1

## Feature: Use LDAP_UID as user.external_id.
# The default is to use the Distinguished Name for users.external_id. If your organization changes
# any part of the DNs for any reason, this could cause any configured users to loose their current
# configuration since it will be assumed to be a new user with a new external_id. This feature
# forces the value of LDAP_UID (set above), which is used to query the user in the LDAP, which
# almost certainly is unique per user, to also be used for the external_id. Note, this name must
# also exist in the "extra" raw info:
# https://github.com/omniauth/omniauth-ldap/blob/master/README.md
# https://github.com/omniauth/omniauth-ldap/blob/master/lib/omniauth/strategies/ldap.rb#L17
# USE_LDAP_UID_AS_EXTERNAL_ID=1
6 changes: 6 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ PATH
samson_airbrake (0.0.0)
airbrake

PATH
remote: plugins/assertible
specs:
samson_assertible (0.0.0)

PATH
remote: plugins/aws_ecr
specs:
Expand Down Expand Up @@ -567,6 +572,7 @@ DEPENDENCIES
rails-controller-testing
rubocop
samson_airbrake!
samson_assertible!
samson_aws_ecr!
samson_datadog!
samson_docker_binary_builder!
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20180209012126_add_notify_assertible_to_stages.rb
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
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20180202000103) do
ActiveRecord::Schema.define(version: 20180209012126) do

create_table "audits", force: :cascade do |t|
t.integer "auditable_id", null: false
Expand Down Expand Up @@ -509,6 +509,7 @@
t.boolean "periodical_deploy", default: false, null: false
t.boolean "builds_in_environment", default: false, null: false
t.boolean "block_on_gcr_vulnerabilities", default: false, null: false
t.boolean "notify_assertible", default: false, null: false
t.index ["project_id", "permalink"], name: "index_stages_on_project_id_and_permalink", unique: true, length: { permalink: 191 }
t.index ["template_stage_id"], name: "index_stages_on_template_stage_id"
end
Expand Down
8 changes: 8 additions & 0 deletions plugins/assertible/README.md
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.
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 %>
52 changes: 52 additions & 0 deletions plugins/assertible/lib/samson_assertible/samson_plugin.rb
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
6 changes: 6 additions & 0 deletions plugins/assertible/samson_assertible.gemspec
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 plugins/assertible/test/samson_assertible/samson_plugin_test.rb
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
1 change: 1 addition & 0 deletions plugins/assertible/test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require_relative '../../../test/test_helper'

0 comments on commit 489dc92

Please sign in to comment.