Skip to content

Commit

Permalink
Merge pull request #12 from opf/maintenance/catch-up-with-15.1
Browse files Browse the repository at this point in the history
update slack plugin to work with Openproject 15.1
  • Loading branch information
machisuji authored Dec 6, 2024
2 parents bdff414 + 023e7f9 commit 9ef8828
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 88 deletions.
63 changes: 25 additions & 38 deletions app/views/settings/_slack.html.erb
Original file line number Diff line number Diff line change
@@ -1,40 +1,27 @@
<%=
render Primer::OpenProject::PageHeader.new do |header|
header.with_title { t(:label_slack_plugin) }
header.with_breadcrumbs([{ href: admin_index_path, text: t("label_administration") },
t(:label_slack_plugin)])
end
%>
<input type="hidden" name="settings[enabled]" value="1"/>

<div id="settings">
<%= styled_form_tag({controller: '/admin/settings', action: 'update_plugin' }) do %>
<input type="hidden" name="settings[enabled]" value="1"/>

<div class="form--field">
<%= styled_label_tag("settings[webhook_url]", t("slack.default_webhook_url")) %>
<div class="form--field-container">
<%=
styled_text_field_tag(
"settings[webhook_url]",
Setting.plugin_openproject_slack["webhook_url"],
container_class: '-xwide',
type: 'url',
pattern: "[ -~]*",
title: t("slack.only_ascii_chars_url"),
)
%>
</div>
</div>

<span class="form--field-instructions">
<%# Creates the custom field if not yet present. %>
<% custom_field = OpenProject::Slack.project_custom_field %>
<%= t(
"slack.per_project_instructions_html",
custom_field_url: edit_admin_settings_project_custom_field_path(custom_field),
custom_field_name: custom_field.name
) %>
</span>
<%= styled_submit_tag t(:button_apply), class: '-primary' %>
<% end %>
<div class="form--field">
<%= styled_label_tag("settings[webhook_url]", t("slack.default_webhook_url")) %>
<div class="form--field-container">
<%=
styled_text_field_tag(
"settings[webhook_url]",
Setting.plugin_openproject_slack["webhook_url"],
container_class: '-xwide',
type: 'url',
pattern: "[ -~]*",
title: t("slack.only_ascii_chars_url"),
)
%>
</div>
</div>

<span class="form--field-instructions">
<%# Creates the custom field if not yet present. %>
<% custom_field = OpenProject::Slack.project_custom_field %>
<%= t(
"slack.per_project_instructions_html",
custom_field_url: edit_admin_settings_project_custom_field_path(custom_field),
custom_field_name: custom_field.name
) %>
</span>
2 changes: 1 addition & 1 deletion lib/open_project/slack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def project_custom_field_params
name: webhook_url_label,
type: 'ProjectCustomField',
field_format: 'link',
regex: "^[ -~]*$", # only ASCII chars, because later URI.parse will not accept it
regexp: "^[ -~]*$", # only ASCII chars, because later URI.parse will not accept it
custom_field_section_id: CustomFieldSection.first.id
}
end
Expand Down
7 changes: 4 additions & 3 deletions lib/open_project/slack/hook_listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ def controller_wiki_edit_after_save(context = { })
return unless webhook_url.present?

page = context[:page]
user = page.content.author
user = page.author
project_url = "<#{object_url project}|#{escape project}>"
page_url = "<#{object_url page}|#{page.title}>"
message = "[#{project_url}] #{page_url} updated by *#{user}*"
attachment = nil
comments = page.journals.last.notes

if page.content.comments.present?
if comments.present?
attachment = {}
attachment[:text] = "#{escape page.content.comments}"
attachment[:text] = "#{escape comments}"
end

OpenProject::Slack::Notifier.say(
Expand Down
27 changes: 13 additions & 14 deletions spec/controllers/wiki_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,17 @@

require 'spec_helper'

describe WikiController, type: :controller do
let(:protocol) { "https" }
let(:host_name) { "test.openproject.com" }
RSpec.describe WikiController, type: :controller do
let(:protocol) { "http" }
let(:host_name) { "test.host" }

before do
Role.delete_all # removing me makes us faster
User.delete_all # removing me makes us faster
I18n.locale = :en
end

describe 'actions', with_settings: { host_name: "test.openproject.com", protocol: "https" } do
describe 'actions', with_settings: { host_name: "test.host" } do
let(:page_title) { "abc" }

let(:expected_text) do
Expand All @@ -59,14 +59,7 @@
@project.reload # to get the wiki into the proxy

# creating pages
@existing_page = FactoryBot.create(
:wiki_page, wiki_id: @project.wiki.id, title: 'ExistingPage'
)

# creating page contents
FactoryBot.create(
:wiki_content, page_id: @existing_page.id, author_id: @user.id
)
@existing_page = create :wiki_page, wiki: @project.wiki, author: @user, title: 'ExistingPage'

allow(::OpenProject::Slack).to receive(:default_webhook_url).and_return("https://foo.bar.com/webhook/42")

Expand All @@ -80,7 +73,10 @@
post 'create',
params: {
project_id: @project,
content: { text: 'h1. abc', page: { title: 'abc' } }
page: {
title: 'abc',
text: 'h1. abc'
}
}

expect(response).to redirect_to action: 'show', project_id: @project, id: page_title
Expand All @@ -95,7 +91,10 @@
params: {
id: page_title,
project_id: @project,
content: { text: 'h1. abc', page: { title: 'ExistingPage' } }
page: {
title: 'ExistingPage',
text: 'h1. abc'
}
}

expect(response).to redirect_to action: 'show', project_id: @project, id: @existing_page.slug
Expand Down
35 changes: 7 additions & 28 deletions spec/lib/notifier_spec.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
require 'spec_helper'

describe OpenProject::Slack::Notifier do
context 'when slack url is not specified' do
before do
Setting.plugin_openproject_slack['webhook_url'] = nil
end

it 'should raise error' do
expect { OpenProject::Slack::Notifier.say text: "test" }.to raise_error(URI::InvalidURIError, /bad URI/i)
end
end

RSpec.describe OpenProject::Slack::Notifier do
describe '#say' do
let(:url) { 'https://hooks.slack.com/services/foo' }
let(:message) { 'message' }
Expand All @@ -21,26 +11,15 @@ def d.post(*args)
end
end

before do
Setting.plugin_openproject_slack['webhook_url'] = url
end

it 'should use the URL from the settings' do
expect(::Slack::Notifier).to receive(:new).with(url).and_return(dummy)
expect(dummy).to receive(:post).with text: message, link_names: 1
let(:webhook_url) { 'https://hooks.slack.com/services/bar' }

OpenProject::Slack::Notifier.say text: message
end
it 'should use the override URL' do
expect(::Slack::Notifier).to receive(:new).with(webhook_url).and_return(dummy)
expect(dummy).to receive(:post).with({ text: message, link_names: 1 })

context 'with URL overriden' do
let(:override_url) { 'https://hooks.slack.com/services/bar' }
OpenProject::Slack::Notifier.say text: message, webhook_url: webhook_url

it 'should use the override URL' do
expect(::Slack::Notifier).to receive(:new).with(override_url).and_return(dummy)
expect(dummy).to receive(:post).with text: message, link_names: 1

OpenProject::Slack::Notifier.say text: message, webhook_url: override_url
end
perform_enqueued_jobs
end
end
end
8 changes: 4 additions & 4 deletions spec/models/work_package_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'spec_helper'
require 'slack-notifier'

describe WorkPackage, with_settings: { "host_name" => "test.openproject.com", "protocol" => "https" } do
RSpec.describe WorkPackage, with_settings: { "host_name" => "test.openproject.com" } do
let(:user) { FactoryBot.create :admin, firstname: "Peter", lastname: "Putzig" }
let(:project) { FactoryBot.create :project_with_types, name: "Parts", identifier: "parts" }

Expand All @@ -23,7 +23,7 @@ def n.post(*args)

let(:update_service) { WorkPackages::UpdateService.new user: user, model: work_package }
let(:expected_text) do
"[<https://test.openproject.com/projects/#{project.identifier}|#{project.name}>] #{user.name} updated <https://test.openproject.com/work_packages/#{work_package.id}|#{work_package.type.name} ##{work_package.id}: #{work_package.subject}>"
"[<http://test.openproject.com/projects/#{project.identifier}|#{project.name}>] #{user.name} updated <http://test.openproject.com/work_packages/#{work_package.id}|#{work_package.type.name} ##{work_package.id}: #{work_package.subject}>"
end

before do
Expand Down Expand Up @@ -52,7 +52,7 @@ def n.post(*args)
let(:description) { "Tires should be round" }

before do
allow(::OpenProject::Slack).to receive(:default_webhook_url).and_return("https://foo.bar.com/webhook/42")
allow(::OpenProject::Slack).to receive(:default_webhook_url).and_return("http://foo.bar.com/webhook/42")
end

let(:create_service) { WorkPackages::CreateService.new user: user }
Expand All @@ -66,7 +66,7 @@ def n.post(*args)

text = opts[:text]

expect(text).to start_with("[<https://test.openproject.com/projects/#{project.identifier}|#{project.name}>] #{user.name} created <https://test.openproject.com/work_packages/")
expect(text).to start_with("[<http://test.openproject.com/projects/#{project.identifier}|#{project.name}>] #{user.name} created <http://test.openproject.com/work_packages/")
expect(text).to match /.*\/work_packages\/\d+\|#{type.name} #\d+: #{subject}>$/
end
end
Expand Down

0 comments on commit 9ef8828

Please sign in to comment.