Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements for the github and intercom integration #246

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
//= require sweet-alert/sweet-alert
//= require jquery.cookie
//= require jquery.deparam
//= require jquery_md5
//= require highlightjs
//= require chartkick
//= require highcharts
Expand Down
14 changes: 14 additions & 0 deletions app/assets/javascripts/pages/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,18 @@ $(function () {
return false
}
});

$(document).ready(function() {
$('#broadcast-form').on("ajax:success", function(e, data, status, xhr) {
if (data.length && data.length > 0){
$('.subscriber-count h5').text("This message will be sent to " + data.length + " " + (data.length > 1 ? "users" : "user"))
$('.subscriber-icons').empty()
for (i = 0; i < data.length; i++) {
$('.subscriber-icons').append("<img src=http://gravatar.com/avatar/" + $.md5(data[i].toLowerCase()) + ".png?s=30>")
}
}
}).on("ajax:error", function(e, xhr, status, error) {
console.log('failed syncing')
});
});
});
4 changes: 4 additions & 0 deletions app/assets/stylesheets/pages/_error.scss
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@
.broadcast{
.subscribers{

.subscriber-icons{
float: left;
}

img{
border-radius:50%;
overflow:hidden;
Expand Down
41 changes: 28 additions & 13 deletions app/controllers/errors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,37 @@ def update
end

def notify_subscribers
unless params[:message].blank?
return redirect_to error_path(@error), flash: { error: 'Message too short!' } if params[:message].length < 10
if params[:intercom]
begin
current_website.intercom_integration.driver.send_message(@error.subscribers.pluck(:email), params[:message])
#true when form has been submitted
if params[:notify_submit]
unless params[:message].blank?
return redirect_to error_path(@error), flash: { error: 'Message too short!' } if params[:message].length < 10
if params[:subscriber_type][:intercom] == 'true'
begin
current_website.intercom_integration.driver.send_message(@error.subscribers.pluck(:email), params[:message])
redirect_to error_path(@error), flash: { success: 'Message successfully sent!' }
rescue => e
Raven.capture_exception(e)
redirect_to error_path(@error), flash: { error: 'Operation failed!' }
end
else
@message = Message.create( content: params[:message], issue_id: @error.issues.last.id )
@error.subscribers.each do |subscriber|
GroupedIssueMailer.notify_subscriber(@error, subscriber, current_user, @message).deliver_later
end
redirect_to error_path(@error), flash: { success: 'Message successfully sent!' }
rescue => e
Raven.capture_exception(e)
redirect_to error_path(@error), flash: { error: 'Operation failed!' }
end
else
@message = Message.create( content: params[:message], issue_id: @error.issues.last.id )
@error.subscribers.each do |subscriber|
GroupedIssueMailer.notify_subscriber(@error, subscriber, current_user, @message).deliver_later
end
redirect_to error_path(@error), flash: { success: 'Message successfully sent!' }
redirect_to error_path(@error), flash: { error: 'Message cannot be empty!', status: 403 }
end
#syncing the subscribers
elsif params[:subscriber_type]
if params[:subscriber_type][:intercom] == 'true'
intercom_subscribers = current_website.intercom_integration.driver.list_subscribers
common_subscribers = intercom_subscribers.map{ |x| x[:email] } & @error.subscribers.pluck(:email)
render json: common_subscribers
elsif params[:subscriber_type][:intercom] == 'false'
subscribers = @error.subscribers.pluck(:email)
render json: subscribers
end
end
end
Expand Down
5 changes: 4 additions & 1 deletion app/controllers/integrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ class IntegrationsController < ApplicationController
load_and_authorize_resource

def update
if integration_params[:app_name] && params[:app_list]
integration_params[:app_owner] = params[:app_list].select { |k,v| v.include?(integration_params[:app_name]) }.keys[0]
end
if @integration.update_attributes(integration_params)
redirect_to settings_path(main_tab: 'integrations', integration_tab: @integration.provider), notice: 'Integration updated!'
end
Expand All @@ -24,6 +27,6 @@ def create_task
end

def integration_params
@integration_params ||= params.require(:integration).permit(:application)
@integration_params ||= params.require(:integration).permit(:app_name, :app_owner)
end
end
8 changes: 4 additions & 4 deletions app/mailers/grouped_issue_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ def more_than_10_errors(member)

def notify_daily(user, websites)
date = Time.now - 1.day
user = User.find(user)
user = User.find(user.id)
@data = find_grouped_issues(websites, date)
mail to: user.email, subject: "Daily report email provides you some information about changes on your website."
end

def notify_weekly(user, websites)
date = Time.now - 1.week
user = User.find(user)
user = User.find(user.id)
@data = find_grouped_issues(websites, date)

@days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']
Expand All @@ -49,8 +49,8 @@ def notify_weekly(user, websites)

def find_grouped_issues(websites, date)
data = []
websites.each do |website_id|
website = Website.find(website_id)
websites.each do |website|
website = Website.find(website.id)
issues = website.grouped_issues.where('updated_at > ? AND muted = ?', date, false)
data.push( { title: website.title, domain: website.domain, platform: website.platform, created_at: website.created_at, issues: issues } )
end
Expand Down
7 changes: 4 additions & 3 deletions app/models/integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ class Integration < ActiveRecord::Base
validates :name, :provider, presence: true
store_accessor :configuration

attr_accessor :application
attr_accessor :app_name, :app_owner

before_update :select_application, if: -> { application }
before_update :select_application, if: -> { app_name && app_owner }

def driver
Integrations.create(self)
end

def select_application
self.configuration["selected_application"] = application
self.configuration["selected_application"] = app_name
self.configuration["application_owner"] = app_owner
end

def assign_configuration(auth_hash)
Expand Down
4 changes: 4 additions & 0 deletions app/models/website.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class Website < ActiveRecord::Base
define_method("#{integration.to_s}_integration") do
self.integrations.find_by(provider: integration)
end

define_method("#{integration.to_s}_configuration") do
self.integrations.find_by(provider: integration).try(:configuration)
end
EOF
end

Expand Down
24 changes: 13 additions & 11 deletions app/views/errors/_overview.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,25 @@
Broadcast
.panel-body
.subscribers
= form_for @error, url: notify_subscribers_error_path(), remote: true, html: { method: :post , class: ' broadcast-textarea'}, :data => {:confirm => 'Are you sure?'} do |f|
= form_for @error, url: notify_subscribers_error_path(), remote: true, html: { method: :post , class: 'broadcast-textarea', id: 'broadcast-form'} do |f|
= render partial: 'shared/flash_messages'
- subscribers.each do |subscriber|
= image_tag subscriber.avatar_url(30)
- if subscribers.size > 10
= " and #{subscribers.size - 10} more"
- break
- if subscribers.size > 0
%h5.pull-right
= t('subscribers.count', count: subscribers.size)
.subscriber-icons
- subscribers.each do |subscriber|
= image_tag subscriber.avatar_url(30)
- if subscribers.size > 10
= " and #{subscribers.size - 10} more"
- break
.subscriber-count
- if subscribers.size > 0
%h5.pull-right
= t('subscribers.count', count: subscribers.size)
.notify
= text_area_tag 'message', '', placeholder: 'Write a message ...'
- if current_website.intercom_integration?
.intercom-check.pull-left
%label.intercom-message
= check_box_tag 'intercom'
= check_box 'subscriber_type', 'intercom', { onclick: "$('#broadcast-form').submit()", id: 'intercom-check' }, 'true', 'false'
Send with Intercom
%button.btn.btn-primary{:type => 'submit'}
= button_to '', name: 'notify_submit', value: 'true', class: 'btn btn-primary' do
%span.fa.fa-paper-plane
Send
2 changes: 1 addition & 1 deletion app/views/errors/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
%li
%span.label.badge{:title => 'Checksum'}= @error.checksum
.col-xs-5.text-right
- if @error.website.github_integration?
- if @error.website.github_configuration.try(:[], 'selected_application')
.btn-group
= button_to 'Create Github Issue', create_task_integration_path(@error.website.github_integration, error_id: @error.id, title: @error.message), class: "btn pull-right btn-primary "
.btn-group
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
- if @integration.selected_application.nil?
= form_tag integration_path(@integration), :method => 'put' do
= select_tag "integration[application]", grouped_options_for_select(@integration.get_applications), { class: "pull-left selectpicker" }
- applications = @integration.get_applications
= form_tag integration_path(@integration, 'app_list' => applications), :method => 'put' do
= select_tag 'integration[app_name]', grouped_options_for_select(applications), { class: "pull-left selectpicker" }
= submit_tag "Submit", { class: "pull-right col-sm-4 btn btn-default btn-primary"}
- else
= 'Your repository is ' + @integration.selected_application
8 changes: 6 additions & 2 deletions lib/integrations/base_driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,15 @@ def build_configuration(auth_hash)
end

def get_request(url)
RestClient.get url, header rescue 'Operation failed!'
RestClient.get url, header
rescue => e
e.message
end

def post_request(url, data)
RestClient.post url, data.to_json, header rescue 'Operation failed!'
RestClient.post url, data.to_json, header
rescue => e
e.message
end
end
end
2 changes: 1 addition & 1 deletion lib/integrations/drivers/github.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def api_url
end

def create_task(title)
url = api_url + 'repos/' + self.configuration["username"].to_s + '/' + self.configuration["selected_application"].to_s + '/issues'
url = api_url + 'repos/' + self.configuration["application_owner"].to_s + '/' + self.configuration["selected_application"].to_s + '/issues'
data = { title: title }
response = post_request(url, data)
if response
Expand Down
128 changes: 65 additions & 63 deletions spec/controllers/errors_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,81 +13,83 @@
context 'if logged in' do
let(:params) { { message: 'message that is going to create record', id: group.id, format: :js } }

context 'email' do
it 'should email subscribers' do
mailer = double('GroupedIssueMailer')
expect(mailer).to receive(:deliver_later)
expect(GroupedIssueMailer).to receive(:notify_subscriber).with(group, subscriber, user, an_instance_of(Message)).and_return(mailer).once

post_with user, :notify_subscribers, params
end

it 'should email 2 subscribers' do
subscriber2 = create :subscriber, website: website
issue2 = create :issue, subscriber: subscriber2, group: group
mailer = double('GroupedIssueMailer')
expect(mailer).to receive(:deliver_later).twice
expect(GroupedIssueMailer).to receive(:notify_subscriber).with(group, an_instance_of(Subscriber), an_instance_of(User), an_instance_of(Message)).and_return(mailer).twice
post_with user, :notify_subscribers, params
end

it 'assigns message' do
post_with user, :notify_subscribers, params
expect(subject.instance_variable_get(:@message)).to be_an_instance_of(Message)
end

it 'redirects to the error path with success message' do
post_with user, :notify_subscribers, params
expect(response).to redirect_to(error_path(group.slug))
expect(flash[:success]).to eq('Message successfully sent!')
end
end
context 'form has been submitted (notify_submit is true)' do
context 'email' do
it 'should email subscribers' do
mailer = double('GroupedIssueMailer')
expect(mailer).to receive(:deliver_later)
expect(GroupedIssueMailer).to receive(:notify_subscriber).with(group, subscriber, user, an_instance_of(Message)).and_return(mailer).once

post_with user, :notify_subscribers, params
end

context 'intercom' do
let!(:integration) { create :integration, website: website, provider: :intercom }
let(:driver) { Integrations.get_driver(integration.provider.to_sym) }
let(:integration_driver) { driver.new(Integrations::Integration.new(integration, driver)) }
before(:each) do session[:epiclogger_website_id] = website.id end
let(:intercom_params) { params.merge( intercom: true ) }
it 'should email 2 subscribers' do
subscriber2 = create :subscriber, website: website
issue2 = create :issue, subscriber: subscriber2, group: group
mailer = double('GroupedIssueMailer')
expect(mailer).to receive(:deliver_later).twice
expect(GroupedIssueMailer).to receive(:notify_subscriber).with(group, an_instance_of(Subscriber), an_instance_of(User), an_instance_of(Message)).and_return(mailer).twice
post_with user, :notify_subscribers, params
end

context 'success' do
it 'calls the send_message method in the driver' do
expect_any_instance_of(Integrations::Drivers::Intercom).to receive(:send_message)
post_with user, :notify_subscribers, intercom_params
it 'assigns message' do
post_with user, :notify_subscribers, params
expect(subject.instance_variable_get(:@message)).to be_an_instance_of(Message)
end

it 'redirects to the error with success message' do
stub_request(:post, integration_driver.api_url + 'messages')
.with(:body => "{\"message_type\":\"inapp\",\"body\":\"message that is going to create record\",\"template\":\"plain\",\"from\":{\"type\":\"admin\",\"id\":\"12345\"},\"to\":{\"type\":\"user\",\"email\":\"" + group.subscribers.first.email + "\"}}",
:headers => {'Authorization'=>'Basic Og=='})
.to_return(:status => 200, :body => web_response_factory('intercom/intercom_post_message'), :headers => {})
post_with user, :notify_subscribers, intercom_params
it 'redirects to the error path with success message' do
post_with user, :notify_subscribers, params
expect(response).to redirect_to(error_path(group.slug))
expect(flash[:success]).to eq('Message successfully sent!')
end
end

context 'failure' do
context 'intercom' do
let!(:integration) { create :integration, website: website, provider: :intercom }
let(:driver) { Integrations.get_driver(integration.provider.to_sym) }
let(:integration_driver) { driver.new(Integrations::Integration.new(integration, driver)) }
before(:each) do session[:epiclogger_website_id] = website.id end
let(:intercom_params) { params.merge( intercom: true ) }

context 'success' do
it 'calls the send_message method in the driver' do
expect_any_instance_of(Integrations::Drivers::Intercom).to receive(:send_message)
post_with user, :notify_subscribers, intercom_params
end

it 'redirects to error path with error message if the message does not meet the criteria' do
stub_request(:post, integration_driver.api_url + 'messages').
with(:body => "{\"message_type\":\"inapp\",\"body\":\"smallmsg\",\"template\":\"plain\",\"from\":{\"type\":\"admin\",\"id\":\"12345\"},\"to\":{\"type\":\"user\",\"email\":\"" + group.subscribers.first.email + "\"}}",
:headers => {'Authorization'=>'Basic Og=='}).
to_return(:status => 500, :body => "eroare", :headers => {})
intercom_params[:message] = 'smallmsg'
post_with user, :notify_subscribers, intercom_params
expect(response).to redirect_to(error_path(group.slug))
expect(flash[:error]).to eq('Message too short!')
it 'redirects to the error with success message' do
stub_request(:post, integration_driver.api_url + 'messages')
.with(:body => "{\"message_type\":\"inapp\",\"body\":\"message that is going to create record\",\"template\":\"plain\",\"from\":{\"type\":\"admin\",\"id\":\"12345\"},\"to\":{\"type\":\"user\",\"email\":\"" + group.subscribers.first.email + "\"}}",
:headers => {'Authorization'=>'Basic Og=='})
.to_return(:status => 200, :body => web_response_factory('intercom/intercom_post_message'), :headers => {})
post_with user, :notify_subscribers, intercom_params
expect(response).to redirect_to(error_path(group.slug))
expect(flash[:success]).to eq('Message successfully sent!')
end
end

it 'redirects to error path with error message if sending the message fails' do
stub_request(:post, integration_driver.api_url + 'messages').
with(:body => "{\"message_type\":\"inapp\",\"body\":\"message that is going to create record\",\"template\":\"plain\",\"from\":{\"type\":\"admin\",\"id\":\"12345\"},\"to\":{\"type\":\"user\",\"email\":\"" + group.subscribers.first.email + "\"}}",
:headers => {'Authorization'=>'Basic Og=='}).
to_return(:status => 500, :body => "eroare", :headers => {})
post_with user, :notify_subscribers, intercom_params
expect(response).to redirect_to(error_path(group.slug))
expect(flash[:error]).to eq('Operation failed!')
context 'failure' do

it 'redirects to error path with error message if the message does not meet the criteria' do
stub_request(:post, integration_driver.api_url + 'messages').
with(:body => "{\"message_type\":\"inapp\",\"body\":\"smallmsg\",\"template\":\"plain\",\"from\":{\"type\":\"admin\",\"id\":\"12345\"},\"to\":{\"type\":\"user\",\"email\":\"" + group.subscribers.first.email + "\"}}",
:headers => {'Authorization'=>'Basic Og=='}).
to_return(:status => 500, :body => "eroare", :headers => {})
intercom_params[:message] = 'smallmsg'
post_with user, :notify_subscribers, intercom_params
expect(response).to redirect_to(error_path(group.slug))
expect(flash[:error]).to eq('Message too short!')
end

it 'redirects to error path with error message if sending the message fails' do
stub_request(:post, integration_driver.api_url + 'messages').
with(:body => "{\"message_type\":\"inapp\",\"body\":\"message that is going to create record\",\"template\":\"plain\",\"from\":{\"type\":\"admin\",\"id\":\"12345\"},\"to\":{\"type\":\"user\",\"email\":\"" + group.subscribers.first.email + "\"}}",
:headers => {'Authorization'=>'Basic Og=='}).
to_return(:status => 500, :body => "eroare", :headers => {})
post_with user, :notify_subscribers, intercom_params
expect(response).to redirect_to(error_path(group.slug))
expect(flash[:error]).to eq('Operation failed!')
end
end
end
end
Expand Down
Loading