Archived
Add OpenProject Gitea integration plugin
Implements a Gitea integration for OpenProject following the same patterns as the bundled GitHub and GitLab integrations. - Webhook endpoint at /webhooks/gitea with HMAC-SHA256 verification - Handles pull_request and issue_comment events - Links Gitea PRs to work packages via OP# references or URLs - Posts journal comments on WPs for PR lifecycle events - Gitea tab on the work package split view - V3 API endpoint: GET /api/v3/work_packages/:id/gitea_pull_requests - Admin settings UI for bot user and webhook secret - Nightly cron job to clean up unlinked PR records
This commit is contained in:
45 files changed
+2561
No files matched your search
@@ -0,0 +1,104 @@
|
||||
#-- copyright
|
||||
# OpenProject is an open source project management software.
|
||||
# Copyright (C) the OpenProject GmbH
|
||||
#
|
||||
# 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-2013 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 COPYRIGHT and LICENSE files for more details.
|
||||
#++
|
||||
|
||||
module OpenProject::GiteaIntegration
|
||||
module NotificationHandler
|
||||
module Helper
|
||||
def extract_work_package_ids(text)
|
||||
host_name = Regexp.escape(Setting.host_name)
|
||||
wp_id = WorkPackage::SemanticIdentifier::ID_ROUTE_CONSTRAINT
|
||||
url_part = /http(?:s?):\/\/#{host_name}\/(?:\S+?\/)*(?:work_packages|wp)\/(#{wp_id})/
|
||||
|
||||
wp_regex = /OP#(#{wp_id})|#{url_part}/
|
||||
|
||||
String(text)
|
||||
.scan(wp_regex)
|
||||
.filter_map { |groups| groups.compact.first }
|
||||
.uniq
|
||||
end
|
||||
|
||||
def find_visible_work_packages(ids, user)
|
||||
WorkPackage
|
||||
.includes(:project)
|
||||
.where_display_id_in(ids)
|
||||
.select { |wp| user.allowed_in_work_package?(:add_work_package_comments, wp) }
|
||||
end
|
||||
|
||||
def find_mentioned_work_packages(text, user)
|
||||
find_visible_work_packages(extract_work_package_ids(text), user)
|
||||
end
|
||||
|
||||
def comment_on_referenced_work_packages(work_packages, user, notes)
|
||||
return if notes.nil?
|
||||
|
||||
work_packages.each do |work_package|
|
||||
::WorkPackages::UpdateService
|
||||
.new(user:, model: work_package)
|
||||
.call(journal_notes: notes, send_notifications: false)
|
||||
end
|
||||
end
|
||||
|
||||
##
|
||||
# A wrapper around a ruby Hash to access webhook payloads.
|
||||
# All methods called on it are converted to `.fetch` hash-access, raising an error if the key does not exist.
|
||||
# If the method ends with a question mark, no error is raised if the key does not exist.
|
||||
# If the fetched value is again a hash, the value is wrapped into a new Payload object.
|
||||
class Payload
|
||||
def initialize(payload)
|
||||
@payload = payload
|
||||
end
|
||||
|
||||
def to_h
|
||||
@payload.dup
|
||||
end
|
||||
|
||||
def method_missing(name, *args, &block)
|
||||
super unless args.empty? && block.nil?
|
||||
|
||||
value = if name.end_with?("?")
|
||||
@payload.fetch(name.to_s[..-2], nil)
|
||||
else
|
||||
@payload.fetch(name.to_s)
|
||||
end
|
||||
|
||||
return Payload.new(value) if value.is_a?(Hash)
|
||||
|
||||
value
|
||||
end
|
||||
|
||||
def respond_to_missing?(_method_name, _include_private = false)
|
||||
true
|
||||
end
|
||||
end
|
||||
|
||||
def wrap_payload(payload)
|
||||
Payload.new(payload)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,69 @@
|
||||
#-- copyright
|
||||
# OpenProject is an open source project management software.
|
||||
# Copyright (C) the OpenProject GmbH
|
||||
#
|
||||
# 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-2013 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 COPYRIGHT and LICENSE files for more details.
|
||||
#++
|
||||
|
||||
module OpenProject::GiteaIntegration
|
||||
module NotificationHandler
|
||||
##
|
||||
# Handles Gitea issue/PR comment notifications.
|
||||
# Gitea sends issue_comment events for comments on both issues and pull requests.
|
||||
class IssueComment
|
||||
include OpenProject::GiteaIntegration::NotificationHandler::Helper
|
||||
|
||||
def process(payload_params)
|
||||
@payload = wrap_payload(payload_params)
|
||||
|
||||
return unless payload.action == "created"
|
||||
|
||||
user = User.find_by(id: payload.open_project_user_id)
|
||||
text = payload.comment.body
|
||||
work_packages = find_mentioned_work_packages(text, user)
|
||||
|
||||
return if work_packages.empty?
|
||||
|
||||
notes = generate_notes(payload)
|
||||
comment_on_referenced_work_packages(work_packages, user, notes)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_reader :payload
|
||||
|
||||
def generate_notes(payload)
|
||||
I18n.t("gitea_integration.issue_comment_referenced_comment",
|
||||
comment_url: payload.comment.html_url,
|
||||
comment_body: payload.comment.body,
|
||||
issue_number: payload.issue.number,
|
||||
issue_title: payload.issue.title,
|
||||
repository: payload.repository.full_name,
|
||||
repository_url: payload.repository.html_url,
|
||||
gitea_user: payload.sender.login,
|
||||
gitea_user_url: payload.sender.avatar_url)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,94 @@
|
||||
#-- copyright
|
||||
# OpenProject is an open source project management software.
|
||||
# Copyright (C) the OpenProject GmbH
|
||||
#
|
||||
# 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-2013 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 COPYRIGHT and LICENSE files for more details.
|
||||
#++
|
||||
|
||||
module OpenProject::GiteaIntegration
|
||||
module NotificationHandler
|
||||
##
|
||||
# Handles Gitea pull request notifications.
|
||||
class PullRequest
|
||||
include OpenProject::GiteaIntegration::NotificationHandler::Helper
|
||||
|
||||
COMMENT_ACTIONS = %w[opened closed reopened].freeze
|
||||
TRACKED_ACTIONS = %w[opened closed reopened edited synchronized].freeze
|
||||
|
||||
def process(payload_params)
|
||||
@payload = wrap_payload(payload_params)
|
||||
|
||||
return unless TRACKED_ACTIONS.include?(payload.action)
|
||||
|
||||
user = User.find_by(id: payload.open_project_user_id)
|
||||
text = [payload.pull_request.title, payload.pull_request.body]
|
||||
.compact_blank
|
||||
.join(" - ")
|
||||
work_packages = find_mentioned_work_packages(text, user)
|
||||
notes = generate_notes(payload)
|
||||
|
||||
if COMMENT_ACTIONS.include?(payload.action)
|
||||
comment_on_referenced_work_packages(work_packages, user, notes)
|
||||
end
|
||||
|
||||
upsert_pull_request(work_packages)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_reader :payload
|
||||
|
||||
def generate_notes(payload)
|
||||
key = {
|
||||
"opened" => "opened",
|
||||
"closed" => (payload.pull_request.merged? ? "merged" : "closed"),
|
||||
"reopened" => "reopened"
|
||||
}[payload.action]
|
||||
|
||||
return nil unless key
|
||||
|
||||
I18n.t("gitea_integration.pull_request_#{key}_comment",
|
||||
pr_number: payload.pull_request.number,
|
||||
pr_title: payload.pull_request.title,
|
||||
pr_url: payload.pull_request.html_url,
|
||||
repository: payload.repository.full_name,
|
||||
repository_url: payload.repository.html_url,
|
||||
gitea_user: payload.sender.login,
|
||||
gitea_user_url: payload.sender.avatar_url)
|
||||
end
|
||||
|
||||
def pull_request_record
|
||||
return @pull_request_record if defined?(@pull_request_record)
|
||||
|
||||
@pull_request_record = GiteaPullRequest.find_by(gitea_html_url: payload.pull_request.html_url)
|
||||
end
|
||||
|
||||
def upsert_pull_request(work_packages)
|
||||
return if work_packages.empty? && pull_request_record.nil?
|
||||
|
||||
OpenProject::GiteaIntegration::Services::UpsertPullRequest.new.call(payload, work_packages:)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user