-
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
Showing
30 changed files
with
487 additions
and
33 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,21 @@ | ||
module OriginConcern | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
before_action :set_origin | ||
helper_method :origin | ||
end | ||
|
||
def set_origin | ||
session[:origin] = params[:origin] if params[:origin].present? | ||
end | ||
|
||
def origin | ||
return @origin if @origin.present? | ||
url = params[:origin] || session[:origin] | ||
return if url.nil? | ||
url = URI.parse(url) | ||
# Only allow redirection to the same scheme/host to avoid open redirection | ||
@origin ||= url.host == request.host && url.scheme == request.scheme ? url.to_s : nil | ||
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,62 @@ | ||
class SlackIntegrationsController < BaseController | ||
def show | ||
@integration = UserIntegration::Slack.find(params[:id]) | ||
authorize @integration | ||
end | ||
|
||
def new | ||
@integration = current_user.user_integrations.build.becomes! UserIntegration::Slack | ||
authorize @integration | ||
end | ||
|
||
def create | ||
@integration = current_user.user_integrations.build.becomes! UserIntegration::Slack | ||
authorize @integration | ||
|
||
@integration.assign_attributes(permitted_params) | ||
|
||
if @integration.save | ||
flash[:notice] = 'Successfully created new integration' | ||
redirect_to origin || slack_integration_path(@integration) | ||
return | ||
end | ||
|
||
render :new | ||
end | ||
|
||
def edit | ||
@integration = UserIntegration::Slack.find(params[:id]) | ||
authorize @integration | ||
end | ||
|
||
def update | ||
@integration = UserIntegration::Slack.find(params[:id]) | ||
authorize @integration | ||
|
||
@integration.assign_attributes(permitted_params) | ||
|
||
if @integration.save | ||
flash[:notice] = 'Successfully updated the integration' | ||
redirect_to origin || slack_integration_path(@integration) | ||
return | ||
end | ||
|
||
render :edit | ||
end | ||
|
||
def destroy | ||
@integration = UserIntegration::Slack.find(params[:id]) | ||
authorize @integration | ||
|
||
@integration.destroy! | ||
|
||
flash[:notice] = 'Successfully deleted the integration' | ||
redirect_to origin || user_integrations_path | ||
end | ||
|
||
private | ||
|
||
def permitted_params | ||
params.require(:integration).permit(:name, :webhook_url) | ||
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,5 @@ | ||
class UserIntegrationsController < BaseController | ||
def index | ||
@integrations = policy_scope!(current_user.user_integrations).all | ||
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
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,7 @@ | ||
module UserIntegration | ||
def self.integration_classes | ||
[ | ||
UserIntegration::Slack | ||
] | ||
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,37 @@ | ||
module UserIntegration | ||
class Base < ApplicationRecord | ||
include SerializedAttribute | ||
|
||
self.table_name = 'user_integrations' | ||
|
||
belongs_to :user, inverse_of: :user_integrations | ||
|
||
validates :name, length: { maximum: 100 }, presence: true | ||
|
||
before_validation :reset_last_error, if: :data_changed? | ||
|
||
def self.policy_class | ||
UserIntegrationPolicy | ||
end | ||
|
||
def notify!(name, *args) | ||
public_send(name, *args) | ||
self.last_error = nil | ||
save! | ||
rescue => e | ||
logger.warn "#{e.message}\n #{e.backtrace.join("\n ")}" | ||
self.last_error = e.message | ||
save! | ||
end | ||
|
||
def test_execution_result(_test_execution) | ||
raise NotImplementedError | ||
end | ||
|
||
private | ||
|
||
def reset_last_error | ||
self.last_error = nil | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
module UserIntegration | ||
class Slack < Base | ||
include UrlHelper | ||
|
||
serialized_attribute :webhook_url | ||
|
||
validates :webhook_url, url: true, presence: true | ||
|
||
def test_execution_result(test_execution) | ||
# TODO: i18n | ||
text = "Hi #{user.name}! The test execution result of <#{test_version_position_url(test_execution.test_version)}|#{test_execution.test_version.title}> is ready." | ||
color = case test_execution.state | ||
when 'done' | ||
'good' | ||
when 'failed' | ||
'danger' | ||
end | ||
faraday.post(webhook_url, username: 'E2E Tester', | ||
text: text, | ||
attachments: [ | ||
author_name: test_execution.user.name, | ||
title: "Test Execution \##{test_execution.to_param}", | ||
title_link: test_execution_url(test_execution), | ||
color: color | ||
]) | ||
end | ||
|
||
private | ||
|
||
def faraday | ||
@faraday ||= Faraday.new(nil, ssl: { verify: false }) do |conn| | ||
conn.request :json | ||
conn.response :raise_error | ||
conn.adapter Faraday.default_adapter | ||
conn.options.timeout = 5 | ||
conn.options.open_timeout = 3 | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class UserIntegrationPolicy < ApplicationPolicy | ||
def index? | ||
true | ||
end | ||
|
||
def create? | ||
true | ||
end | ||
|
||
def update? | ||
@record.user == @user | ||
end | ||
|
||
def destroy? | ||
@record.user == @user | ||
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,4 @@ | ||
= f.error_notification | ||
= f.input :name | ||
= f.input :webhook_url | ||
= f.button :submit, class: 'btn-success' |
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,10 @@ | ||
- content_for :title, 'Edit Slack Test Integration' | ||
|
||
= link_to 'Back to integrations', user_integrations_path | ||
|
||
h1 | ||
small.glyphicon.glyphicon-link | ||
| Edit Slack Test Integration | ||
|
||
= simple_form_for @integration, url: slack_integration_path(@integration), as: :integration, method: :put do |f| | ||
= render 'form', f: f |
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,10 @@ | ||
- content_for :title, 'New Slack Test Integration' | ||
|
||
= link_to 'Back to integrations', user_integrations_path | ||
|
||
h1 | ||
small.glyphicon.glyphicon-link | ||
| New Slack Test Integration | ||
|
||
= simple_form_for @integration, url: slack_integrations_path, as: :integration, method: :post do |f| | ||
= render 'form', f: f |
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,32 @@ | ||
- content_for :title, 'Slack Test Integration' | ||
|
||
= link_to 'Back to integrations', user_integrations_path | ||
|
||
h1 | ||
small.glyphicon.glyphicon-link | ||
| Slack Test Integration | ||
|
||
.block | ||
.pull-right | ||
.e2e-margined-btn-group | ||
- if policy(@integration).edit? | ||
= link_to 'Edit', edit_slack_integration_path(@integration), class: 'btn btn-default' | ||
- if policy(@integration).destroy? | ||
= link_to 'Delete', slack_integration_path(@integration), method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-default' | ||
h2.e2e-truncate | ||
= @integration.name | ||
|
||
.block | ||
.row | ||
.col-lg-3.text-right | ||
| Webhook URL | ||
.col-lg-9 | ||
= @integration.webhook_url | ||
- if @integration.last_error.present? | ||
.row.text-danger | ||
.col-lg-3.text-right | ||
.glyphicon.glyphicon-exclamation-sign | ||
| Last Error | ||
.col-lg-9 | ||
code = @integration.last_error | ||
|
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,35 @@ | ||
- content_for :title, 'User Integrations' | ||
|
||
- if origin | ||
= link_to 'Back', origin | ||
|
||
h1 | ||
small.glyphicon.glyphicon-link | ||
| User Integrations | ||
|
||
- UserIntegration.integration_classes.each do |klass| | ||
- records = @integrations.select { |i| i.class == klass } | ||
.block | ||
.pull-right | ||
= link_to 'New Integration', new_slack_integration_path, class: 'btn btn-success' | ||
h2 | ||
= image_tag image_path("integrations/#{klass.name.humanize.demodulize}.png"), width: 32, height: 32 | ||
= " #{klass.name.humanize.demodulize} " | ||
small = link_to '', doc_url(:integration_slack), target: '_blank', class: 'glyphicon glyphicon-new-window' | ||
|
||
- if records.present? | ||
.panel.panel-default | ||
table.table.table-striped | ||
tr | ||
th Name | ||
th | ||
- records.each do |r| | ||
tr | ||
td = link_to r.name, slack_integration_path(r) | ||
td | ||
= link_to 'Edit', edit_slack_integration_path(r) | ||
| | ||
= link_to 'Delete', slack_integration_path(r), method: :delete, data: { confirm: 'Are you sure?' } | ||
- else | ||
.alert.alert-warning | ||
| You don't have any integration. |
Oops, something went wrong.