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:
quafzi committed 2026-08-28 22:33:27 +02:00
commit 5d409989df
45 files changed
+2561

No files matched your search

@@ -0,0 +1,123 @@
#-- 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.
#++
require "open_project/plugins"
require_relative "patches/api/work_package_representer"
require_relative "notification_handlers"
require_relative "hook_handler"
require_relative "services"
module OpenProject::GiteaIntegration
class Engine < ::Rails::Engine
engine_name :openproject_gitea_integration
include OpenProject::Plugins::ActsAsOpEngine
def self.settings
{
default: {
"gitea_user_id" => nil,
"webhook_secret" => nil
}
}
end
register "openproject-gitea_integration",
author_url: "https://www.openproject.org",
bundled: false,
settings: do
::Redmine::MenuManager.map(:admin_menu) do |menu|
menu.push :admin_gitea_integration,
{ controller: "/gitea_integration/admin/settings", action: "show" },
parent: :admin_integrations,
if: ->(_) { User.current.admin? },
caption: "Gitea",
icon: :"op-logo-gitea"
end
project_module(:gitea, dependencies: :work_package_tracking) do
permission(:show_gitea_content,
{},
permissible_on: %i[work_package project])
end
menu :work_package_split_view,
:gitea,
{ tab: :gitea },
if: ->(project) {
User.current.allowed_in_project?(:show_gitea_content, project)
},
skip_permissions_check: true,
badge: ->(work_package:, **) {
work_package.gitea_pull_requests.count
},
before: :watchers,
caption: :project_module_gitea
end
patches %w[WorkPackage]
initializer "gitea.register_hook" do
::OpenProject::Webhooks.register_hook "gitea" do |hook, environment, params, user|
HookHandler.new.process(hook, environment, params, user)
end
end
initializer "gitea.subscribe_to_notifications" do
::OpenProject::Notifications.subscribe("gitea.pull_request",
&NotificationHandlers.method(:pull_request))
::OpenProject::Notifications.subscribe("gitea.issue_comment",
&NotificationHandlers.method(:issue_comment))
end
extend_api_response(:v3, :work_packages, :work_package,
&::OpenProject::GiteaIntegration::Patches::API::WorkPackageRepresenter.extension)
add_api_path :gitea_pull_requests_by_work_package do |id|
"#{work_package(id)}/gitea_pull_requests"
end
add_api_path :gitea_user do |id|
"gitea_users/#{id}"
end
add_api_endpoint "API::V3::WorkPackages::WorkPackagesAPI", :id do
mount ::API::V3::GiteaPullRequests::GiteaPullRequestsByWorkPackageAPI
end
add_cron_jobs do
{
"Cron::ClearOldGiteaPullRequestsJob": {
cron: "25 1 * * *",
class: ::Cron::ClearOldGiteaPullRequestsJob.name
}
}
end
end
end
@@ -0,0 +1,87 @@
#-- 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
class HookHandler
# Gitea webhook events we handle.
KNOWN_EVENTS = %w[
pull_request
issue_comment
].freeze
def process(_hook, request, params, user)
event_type = request.env["HTTP_X_GITEA_EVENT"].to_s.downcase.tr(" ", "_")
Rails.logger.debug { "Received Gitea webhook #{event_type}" }
return 404 unless KNOWN_EVENTS.include?(event_type)
return 403 unless authorized?(request, user)
notify(params, user, event_type)
200
end
private
def authorized?(request, user)
valid_signature?(request) &&
user.present? &&
(configured_user_id.blank? || user.id == configured_user_id)
end
def notify(params, user, event_type)
payload = params[:payload]
.permit!
.to_h
.merge("open_project_user_id" => user.id,
"gitea_event" => event_type)
OpenProject::Notifications.send(:"gitea.#{event_type}", payload)
end
# Gitea uses HMAC-SHA256, same as GitHub (X-Gitea-Signature header).
def valid_signature?(request)
secret = plugin_settings[:webhook_secret].presence
return true if secret.blank?
signature_header = request.env["HTTP_X_GITEA_SIGNATURE"]
return false if signature_header.blank?
expected = "sha256=#{OpenSSL::HMAC.hexdigest("SHA256", secret, request.raw_post)}"
ActiveSupport::SecurityUtils.secure_compare(expected, signature_header)
end
def configured_user_id
plugin_settings[:gitea_user_id].presence&.to_i
end
def plugin_settings
Hash(Setting.plugin_openproject_gitea_integration).with_indifferent_access
end
end
end
@@ -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
@@ -0,0 +1,58 @@
#-- 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.
#++
require_relative "notification_handler/helper"
require_relative "notification_handler/pull_request"
require_relative "notification_handler/issue_comment"
module OpenProject::GiteaIntegration
module NotificationHandlers
class << self
def pull_request(payload)
with_logging("pull_request") do
OpenProject::GiteaIntegration::NotificationHandler::PullRequest.new.process(payload)
end
end
def issue_comment(payload)
with_logging("issue_comment") do
OpenProject::GiteaIntegration::NotificationHandler::IssueComment.new.process(payload)
end
end
private
def with_logging(event_hook)
yield if block_given?
rescue StandardError => e
Rails.logger.error "Failed to handle #{event_hook} from Gitea: #{e} #{e.message}"
raise e
end
end
end
end
@@ -0,0 +1,60 @@
#-- 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 Patches
module API
module WorkPackageRepresenter
module_function
def extension
->(*) do
link :gitea,
cache_if: -> { current_user.allowed_in_work_package?(:show_gitea_content, represented) } do
next if represented.new_record?
{
href: "#{work_package_path(id: represented.id)}/tabs/gitea",
title: "gitea"
}
end
link :gitea_pull_requests do
next if represented.new_record?
{
href: api_v3_paths.gitea_pull_requests_by_work_package(represented.id),
title: "Gitea pull requests"
}
end
end
end
end
end
end
end
@@ -0,0 +1,13 @@
module OpenProject::GiteaIntegration
module Patches
module WorkPackagePatch
extend ActiveSupport::Concern
included do
has_and_belongs_to_many :gitea_pull_requests
end
end
end
end
WorkPackage.include OpenProject::GiteaIntegration::Patches::WorkPackagePatch
@@ -0,0 +1,31 @@
#-- 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.
#++
require_relative "services/params_helper"
require_relative "services/upsert_gitea_user"
require_relative "services/upsert_pull_request"
@@ -0,0 +1,50 @@
# frozen_string_literal: true
#-- 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
module GiteaIntegration
module Services
module ParamsHelper
private
EMPTY_AVATAR_URL = "https://www.gravatar.com/avatar/?d=mp"
EMPTY_DESCRIPTION = "No description provided"
def avatar_url(raw_url)
raw_url.presence || EMPTY_AVATAR_URL
end
def description(raw_description)
raw_description.presence || EMPTY_DESCRIPTION
end
end
end
end
end
@@ -0,0 +1,60 @@
# frozen_string_literal: true
#-- 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
module GiteaIntegration
module Services
##
# Takes user data from a Gitea webhook payload and stores it as a GiteaUser.
# If the GiteaUser already exists, it is updated.
class UpsertGiteaUser
include ParamsHelper
def call(payload)
GiteaUser.find_or_initialize_by(gitea_id: payload.id)
.tap do |gitea_user|
gitea_user.update!(extract_params(payload))
end
end
private
def extract_params(payload)
{
gitea_id: payload.id,
gitea_name: payload.full_name.presence || payload.login,
gitea_login: payload.login,
gitea_avatar_url: avatar_url(payload.avatar_url)
}
end
end
end
end
end
@@ -0,0 +1,93 @@
# frozen_string_literal: true
#-- 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
module GiteaIntegration
module Services
class UpsertPullRequest
include ParamsHelper
def call(payload, work_packages: [])
find_or_initialize(payload).tap do |pr|
pr.update!(work_packages: pr.work_packages | work_packages, **extract_params(payload))
end
end
private
def find_or_initialize(payload)
GiteaPullRequest.find_by_gitea_identifiers(
id: payload.pull_request.number,
url: payload.pull_request.html_url,
initialize: true
)
end
# rubocop:disable Metrics/AbcSize
def extract_params(payload)
merged = payload.pull_request.merged?
{
gitea_id: payload.pull_request.number,
gitea_user: gitea_user_record(payload.pull_request.user),
number: payload.pull_request.number,
gitea_html_url: payload.pull_request.html_url,
gitea_updated_at: payload.pull_request.updated_at,
state: pr_state(payload),
title: payload.pull_request.title,
body: description(payload.pull_request.body),
repository: payload.repository.full_name,
draft: payload.pull_request.draft?,
merged:,
merged_by: merged ? gitea_user_record(payload.pull_request.merged_by?) : nil,
merged_at: merged ? payload.pull_request.merged_at? : nil,
labels: Array(payload.pull_request.labels?).map { |l| extract_label(l) }
}
end
# rubocop:enable Metrics/AbcSize
def pr_state(payload)
return "merged" if payload.pull_request.merged?
payload.pull_request.state
end
def extract_label(label)
{ "name" => label["name"], "color" => "##{label["color"]}" }
end
def gitea_user_record(payload)
return if payload.blank?
::OpenProject::GiteaIntegration::Services::UpsertGiteaUser.new.call(payload)
end
end
end
end
end