Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Add time entry webhook

Oliver Günther4944f02e2d7f
diff --git a/app/models/system_user.rb b/app/models/system_user.rb
index c3dd6c73a74..265cb60a924 100644
--- a/app/models/system_user.rb
+++ b/app/models/system_user.rb
@@ -69,7 +69,7 @@ class SystemUser < User
User.current = self
begin
- yield
+ yield self
ensure
remove_privileges
User.current = old_user
diff --git a/app/services/time_entries/create_service.rb b/app/services/time_entries/create_service.rb
index 4bd0f21730f..9726df620de 100644
--- a/app/services/time_entries/create_service.rb
+++ b/app/services/time_entries/create_service.rb
@@ -28,4 +28,14 @@
# See doc/COPYRIGHT.rdoc for more details.
#++
-class TimeEntries::CreateService < ::BaseServices::Create; end
+class TimeEntries::CreateService < ::BaseServices::Create
+
+ def after_perform(call)
+ OpenProject::Notifications.send(
+ OpenProject::Events::NEW_TIME_ENTRY_CREATED,
+ time_entry: call.result
+ )
+
+ call
+ end
+end
diff --git a/lib/open_project/events.rb b/lib/open_project/events.rb
index 1728c3845e9..e068f477749 100644
--- a/lib/open_project/events.rb
+++ b/lib/open_project/events.rb
@@ -38,5 +38,6 @@ module OpenProject
# @see OpenProject::Notifications
module Events
AGGREGATED_WORK_PACKAGE_JOURNAL_READY = "aggregated_work_package_journal_ready".freeze
+ NEW_TIME_ENTRY_CREATED = "new_time_entry_created".freeze
end
end
diff --git a/modules/webhooks/app/workers/represented_webhook_job.rb b/modules/webhooks/app/workers/represented_webhook_job.rb
new file mode 100644
index 00000000000..df537ac23ab
--- /dev/null
+++ b/modules/webhooks/app/workers/represented_webhook_job.rb
@@ -0,0 +1,105 @@
+require 'rest-client'
+
+#-- encoding: UTF-8
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2017 the OpenProject Foundation (OPF)
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2017 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See doc/COPYRIGHT.rdoc for more details.
+#++
+
+class RepresentedWebhookJob < WebhookJob
+ include ::OpenProjectErrorHelper
+
+ attr_reader :resource
+
+ def perform(webhook_id, resource, event_name)
+ @resource = resource
+ super(webhook_id, event_name)
+
+ return unless accepted_in_project?
+
+ body = request_body
+ headers = request_headers
+ exception = nil
+ response = nil
+
+ if signature = request_signature(body)
+ headers['X-OP-Signature'] = signature
+ end
+
+ begin
+ response = RestClient.post webhook.url, request_body, headers
+ rescue RestClient::Exception => e
+ response = e.response
+ exception = e
+ rescue StandardError => e
+ op_handle_error(e.message, reference: :webhook_job)
+ exception = e
+ end
+
+ ::Webhooks::Log.create(
+ webhook: webhook,
+ event_name: event_name,
+ url: webhook.url,
+ request_headers: headers,
+ request_body: body,
+ response_code: response.try(:code).to_i,
+ response_headers: response.try(:headers),
+ response_body: response.try(:to_s) || exception.try(:message)
+ )
+ end
+
+ def accepted_in_project?
+ webhook.enabled_for_project?(resource.project_id)
+ end
+
+ def request_signature(request_body)
+ if secret = webhook.secret.presence
+ 'sha1=' + OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), secret, request_body)
+ end
+ end
+
+ def request_headers
+ {
+ content_type: "application/json",
+ accept: "application/json"
+ }
+ end
+
+ def payload_key
+ raise NotImplementedError
+ end
+
+ def payload_representer
+ raise NotImplementedError
+ end
+
+ def request_body
+ {
+ :action => event_name,
+ payload_key => payload_representer
+ }.to_json
+ end
+end
diff --git a/modules/webhooks/app/workers/time_entry_webhook_job.rb b/modules/webhooks/app/workers/time_entry_webhook_job.rb
new file mode 100644
index 00000000000..d99b03064ea
--- /dev/null
+++ b/modules/webhooks/app/workers/time_entry_webhook_job.rb
@@ -0,0 +1,43 @@
+require 'rest-client'
+
+#-- encoding: UTF-8
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2017 the OpenProject Foundation (OPF)
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2017 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See doc/COPYRIGHT.rdoc for more details.
+#++
+
+class TimeEntryWebhookJob < RepresentedWebhookJob
+ def payload_key
+ :time_entry
+ end
+
+ def payload_representer
+ User.system.run_given do |user|
+ ::API::V3::TimeEntries::TimeEntryRepresenter
+ .create(resource, current_user: user, embed_links: true)
+ end
+ end
+end
diff --git a/modules/webhooks/app/workers/webhook_job.rb b/modules/webhooks/app/workers/webhook_job.rb
index 11466caf5d9..237517ab5d5 100644
--- a/modules/webhooks/app/workers/webhook_job.rb
+++ b/modules/webhooks/app/workers/webhook_job.rb
@@ -28,7 +28,14 @@
#++
class WebhookJob < ApplicationJob
- def perform
+ attr_reader :webhook_id, :event_name
+ def perform(webhook_id, event_name)
+ @webhook_id = webhook_id
+ @event_name = event_name
+ end
+
+ def webhook
+ @webhook ||= Webhooks::Webhook.find(webhook_id)
end
end
diff --git a/modules/webhooks/app/workers/work_package_webhook_job.rb b/modules/webhooks/app/workers/work_package_webhook_job.rb
index 12e25d93c9c..ac153bfcc82 100644
--- a/modules/webhooks/app/workers/work_package_webhook_job.rb
+++ b/modules/webhooks/app/workers/work_package_webhook_job.rb
@@ -29,85 +29,15 @@ require 'rest-client'
# See doc/COPYRIGHT.rdoc for more details.
#++
-class WorkPackageWebhookJob < WebhookJob
- include ::OpenProjectErrorHelper
-
- attr_reader :webhook_id
- attr_reader :journal_id
- attr_reader :event_name
-
- def perform(webhook_id, journal_id, event_name)
- @webhook_id = webhook_id
- @journal_id = journal_id
- @event_name = event_name
-
- return unless webhook.enabled_for_project?(work_package.project_id)
-
- body = request_body
- headers = request_headers
- exception = nil
- response = nil
-
- if signature = request_signature(body)
- headers['X-OP-Signature'] = signature
- end
-
- begin
- response = RestClient.post webhook.url, request_body, headers
- rescue RestClient::Exception => e
- response = e.response
- exception = e
- rescue StandardError => e
- op_handle_error(e.message, reference: :webhook_job)
- exception = e
- end
-
- ::Webhooks::Log.create(
- webhook: webhook,
- event_name: event_name,
- url: webhook.url,
- request_headers: headers,
- request_body: body,
- response_code: response.try(:code).to_i,
- response_headers: response.try(:headers),
- response_body: response.try(:to_s) || exception.try(:message)
- )
+class WorkPackageWebhookJob < RepresentedWebhookJob
+ def payload_key
+ :work_package
end
- def request_signature(request_body)
- if secret = webhook.secret.presence
- 'sha1=' + OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), secret, request_body)
+ def payload_representer
+ User.system.run_given do |user|
+ ::API::V3::WorkPackages::WorkPackageRepresenter
+ .create(resource, current_user: user, embed_links: true)
end
end
-
- def request_headers
- {
- content_type: "application/json",
- accept: "application/json"
- }
- end
-
- def request_body
- {
- action: event_name,
- work_package: work_package_representer
- }.to_json
- end
-
- def work_package_representer
- ::API::V3::WorkPackages::WorkPackageRepresenter
- .create(work_package, current_user: User.admin.first, embed_links: true)
- end
-
- def work_package
- journal.journable
- end
-
- def journal
- @journal ||= Journal.find(journal_id)
- end
-
- def webhook
- @webhook ||= Webhooks::Webhook.find(webhook_id)
- end
end
diff --git a/modules/webhooks/config/locales/en.yml b/modules/webhooks/config/locales/en.yml
index aa048ff74ec..322c8fdf18b 100644
--- a/modules/webhooks/config/locales/en.yml
+++ b/modules/webhooks/config/locales/en.yml
@@ -16,6 +16,9 @@ en:
webhooks:
singular: Webhook
plural: Webhooks
+ resources:
+ time_entry:
+ name: "Time entry"
outgoing:
no_results_table: No webhooks have been defined yet.
label_add_new: Add new webhook
diff --git a/modules/webhooks/lib/open_project/webhooks/event_resources.rb b/modules/webhooks/lib/open_project/webhooks/event_resources.rb
index 4c8998a90c4..75beaa82a25 100644
--- a/modules/webhooks/lib/open_project/webhooks/event_resources.rb
+++ b/modules/webhooks/lib/open_project/webhooks/event_resources.rb
@@ -35,7 +35,7 @@ module OpenProject::Webhooks
end
def resources
- %i(work_package)
+ %i(work_package time_entry)
end
end
end
diff --git a/modules/webhooks/lib/open_project/webhooks/event_resources/time_entry.rb b/modules/webhooks/lib/open_project/webhooks/event_resources/time_entry.rb
new file mode 100644
index 00000000000..e32b07e12b8
--- /dev/null
+++ b/modules/webhooks/lib/open_project/webhooks/event_resources/time_entry.rb
@@ -0,0 +1,30 @@
+require_relative 'base'
+
+module OpenProject::Webhooks::EventResources
+ class TimeEntry < Base
+ class << self
+ def notification_names
+ [
+ OpenProject::Events::NEW_TIME_ENTRY_CREATED
+ ]
+ end
+
+ def available_actions
+ %i(created)
+ end
+
+ def resource_name
+ I18n.t 'webhooks.resources.time_entry.name'
+ end
+
+ protected
+
+ def handle_notification(payload, event_name)
+ event_name = prefixed_event_name(:created)
+ active_webhooks.with_event_name(event_name).pluck(:id).each do |id|
+ TimeEntryWebhookJob.perform_later(id, payload[:time_entry], event_name)
+ end
+ end
+ end
+ end
+end
diff --git a/modules/webhooks/spec/workers/time_entry_webhook_job.rb b/modules/webhooks/spec/workers/time_entry_webhook_job.rb
new file mode 100644
index 00000000000..da989c0cbdd
--- /dev/null
+++ b/modules/webhooks/spec/workers/time_entry_webhook_job.rb
@@ -0,0 +1,142 @@
+#-- encoding: UTF-8
+#-- copyright
+# OpenProject is a project management system.
+# Copyright (C) 2012-2017 the OpenProject Foundation (OPF)
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2017 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See doc/COPYRIGHT.rdoc for more details.
+#++
+
+require 'spec_helper'
+
+describe TimeEntryWebhookJob, type: :job, webmock: true do
+ shared_let(:user) { FactoryBot.create :admin }
+ shared_let(:request_url) { "http://example.net/test/42" }
+ shared_let(:time_entry) { FactoryBot.create :time_entry, hours: 10 }
+ shared_let(:webhook) { FactoryBot.create :webhook, all_projects: true, url: request_url, secret: nil }
+
+ shared_examples "a time entry webhook call" do
+ let(:event) { "time_entry:created" }
+ let(:job) { TimeEntryWebhookJob.perform_now(webhook.id, time_entry, event) }
+
+ let(:stubbed_url) { request_url }
+
+ let(:request_headers) do
+ { content_type: "application/json", accept: "application/json" }
+ end
+
+ let(:response_code) { 200 }
+ let(:response_body) { "hook called" }
+ let(:response_headers) do
+ { content_type: "text/plain", x_spec: "foobar" }
+ end
+
+ let(:stub) do
+ stub_request(:post, stubbed_url.sub("http://", ""))
+ .with(
+ body: hash_including(
+ "action" => event,
+ "time_entry" => hash_including(
+ "_type" => "TimeEntry",
+ "hours" => 'PT10H'
+ )
+ ),
+ headers: request_headers
+ )
+ .to_return(
+ status: response_code,
+ body: response_body,
+ headers: response_headers
+ )
+ end
+
+ subject do
+ begin
+ job
+ rescue
+ # ignoring it as it's expected to throw exceptions in certain scenarios
+ nil
+ end
+ end
+
+ before do
+ allow(::Webhooks::Webhook).to receive(:find).with(webhook.id).and_return(webhook)
+ login_as user
+ stub
+ end
+
+ it 'requests with all projects' do
+ expect(webhook)
+ .to receive(:enabled_for_project?).with(time_entry.project_id)
+ .and_call_original
+
+ subject
+ expect(stub).to have_been_requested
+ end
+
+ it 'does not request when project does not match' do
+ expect(webhook)
+ .to receive(:enabled_for_project?).with(time_entry.project_id)
+ .and_return(false)
+
+ subject
+ expect(stub).not_to have_been_requested
+ end
+
+ describe 'successful flow' do
+ before do
+ subject
+ end
+
+ it "calls the webhook url" do
+ expect(stub).to have_been_requested
+ end
+
+ it "creates a log for the call" do
+ log = Webhooks::Log.last
+
+ expect(log.webhook).to eq webhook
+ expect(log.url).to eq webhook.url
+ expect(log.event_name).to eq event
+ expect(log.request_headers).to eq request_headers
+ expect(log.response_code).to eq response_code
+ expect(log.response_body).to eq response_body
+ expect(log.response_headers).to eq response_headers
+ end
+ end
+ end
+
+ describe "triggering a time entry creation" do
+ it_behaves_like "a time entry webhook call" do
+ let(:event) { "time_entry:created" }
+ end
+ end
+
+ describe "triggering a work package create with an invalid url" do
+ it_behaves_like "a time entry webhook call" do
+ let(:event) { "time_entry:create" }
+ let(:response_code) { 404 }
+ let(:response_body) { "not found" }
+ end
+ end
+end