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

implement deploy status checks for github integration

Markus Kahlf1340e9897d0
diff --git a/config/constants/settings/definition.rb b/config/constants/settings/definition.rb
index 8fef00702ad..af40b15e9e4 100644
--- a/config/constants/settings/definition.rb
+++ b/config/constants/settings/definition.rb
@@ -567,6 +567,10 @@ module Settings
description: "Require password confirmations for certain administrative actions",
default: true
},
+ introspection_enabled: {
+ description: "If enabled, expose detailed information the running OpenProject environment to users with the necessary permissions.",
+ default: true
+ },
invitation_expiration_days: {
default: 7
},
diff --git a/config/initializers/permissions.rb b/config/initializers/permissions.rb
index c92ee82c1f1..0f2b2dfcdfb 100644
--- a/config/initializers/permissions.rb
+++ b/config/initializers/permissions.rb
@@ -61,6 +61,14 @@ Rails.application.reloader.to_prepare do
require: :loggedin,
contract_actions: { users: %i[read create] }
+ map.permission :introspection,
+ {
+ admin: %i[info]
+ },
+ permissible_on: :global,
+ require: :loggedin,
+ enabled: -> { Setting.introspection_enabled? }
+
map.permission :manage_user,
{
users: %i[index show edit update change_status change_status_info],
diff --git a/lib/api/v3/root_representer.rb b/lib/api/v3/root_representer.rb
index ed6e8ddedfa..4c8179ed3d5 100644
--- a/lib/api/v3/root_representer.rb
+++ b/lib/api/v3/root_representer.rb
@@ -109,7 +109,12 @@ module API
property :core_version,
exec_context: :decorator,
getter: ->(*) { OpenProject::VERSION.to_semver },
- if: ->(*) { current_user.admin? }
+ if: ->(*) { current_user.admin? || current_user.allowed_globally?(:introspection) }
+
+ property :core_sha,
+ exec_context: :decorator,
+ getter: ->(*) { OpenProject::VERSION.core_sha },
+ if: ->(*) { current_user.admin? || current_user.allowed_globally?(:introspection) }
def _type
"Root"
diff --git a/lib/open_project/version.rb b/lib/open_project/version.rb
index efc9bec9f5e..898a286f6fe 100644
--- a/lib/open_project/version.rb
+++ b/lib/open_project/version.rb
@@ -138,7 +138,7 @@ module OpenProject
def read_optional(file)
path = Rails.root.join(file)
if File.exist? path
- File.read(path)
+ String(File.read(path)).strip
end
end
diff --git a/modules/github_integration/app/components/deploy_targets/row_component.rb b/modules/github_integration/app/components/deploy_targets/row_component.rb
new file mode 100644
index 00000000000..b17462f0947
--- /dev/null
+++ b/modules/github_integration/app/components/deploy_targets/row_component.rb
@@ -0,0 +1,54 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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 DeployTargets
+ class RowComponent < ::RowComponent
+ property :host, :type
+
+ def deploy_target
+ model
+ end
+
+ def created_at
+ helpers.format_time deploy_target.created_at
+ end
+
+ def button_links
+ [delete_link]
+ end
+
+ def delete_link
+ link_to "Delete",
+ deploy_target_path(deploy_target, back_url: request.fullpath),
+ method: :delete,
+ class: "icon icon-delete"
+ end
+ end
+end
diff --git a/modules/github_integration/app/components/deploy_targets/table_component.rb b/modules/github_integration/app/components/deploy_targets/table_component.rb
new file mode 100644
index 00000000000..a1f49cad49b
--- /dev/null
+++ b/modules/github_integration/app/components/deploy_targets/table_component.rb
@@ -0,0 +1,50 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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 DeployTargets
+ class TableComponent < ::TableComponent
+ columns :host, :type, :created_at
+ options :current_user
+
+ def initial_sort
+ %i[id asc]
+ end
+
+ def headers
+ columns.map do |name|
+ [name.to_s, header_options(name)]
+ end
+ end
+
+ def header_options(name)
+ { caption: User.human_attribute_name(name) }
+ end
+ end
+end
diff --git a/modules/github_integration/app/controllers/deploy_targets_controller.rb b/modules/github_integration/app/controllers/deploy_targets_controller.rb
new file mode 100644
index 00000000000..32599b70260
--- /dev/null
+++ b/modules/github_integration/app/controllers/deploy_targets_controller.rb
@@ -0,0 +1,68 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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.
+#++
+
+class DeployTargetsController < ApplicationController
+ layout "admin"
+
+ before_action :require_admin
+
+ def index
+ @deploy_targets = DeployTarget.all
+ end
+
+ def new
+ @deploy_target = DeployTarget.new type: "OpenProject"
+ end
+
+ def create
+ args = params
+ .permit("deploy_target" => ["host", "type", "api_key"])[:deploy_target]
+ .to_h
+ .merge(type: "OpenProject")
+
+ @deploy_target = DeployTarget.create **args
+
+ if @deploy_target.persisted?
+ flash[:success] = "Deploy target created"
+
+ redirect_to deploy_targets_path
+ else
+ render 'new'
+ end
+ end
+
+ def destroy
+ deploy_target = DeployTarget.find params[:id]
+
+ deploy_target.destroy!
+
+ flash[:success] = "Deploy target deleted"
+
+ redirect_to deploy_targets_path
+ end
+end
diff --git a/modules/github_integration/app/models/deploy_status_check.rb b/modules/github_integration/app/models/deploy_status_check.rb
new file mode 100644
index 00000000000..670bfb321ad
--- /dev/null
+++ b/modules/github_integration/app/models/deploy_status_check.rb
@@ -0,0 +1,40 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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.
+#++
+
+class DeployStatusCheck < ApplicationRecord
+ belongs_to :github_pull_request
+ belongs_to :deploy_target
+
+ validates_presence_of :core_sha
+
+ delegate :merge_commit_sha, to: :github_pull_request
+
+ def pull_request
+ github_pull_request
+ end
+end
diff --git a/modules/github_integration/app/models/deploy_target.rb b/modules/github_integration/app/models/deploy_target.rb
new file mode 100644
index 00000000000..decbcf0b11e
--- /dev/null
+++ b/modules/github_integration/app/models/deploy_target.rb
@@ -0,0 +1,40 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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.
+#++
+
+class DeployTarget < ApplicationRecord
+ self.inheritance_column = nil
+
+ store_accessor :options, :api_key
+
+ has_many :deploy_status_checks, dependent: :destroy
+
+ validates_presence_of :host
+
+ # this is very much specific to the only type of target we support for now, OpenProject
+ validates_presence_of :api_key
+end
diff --git a/modules/github_integration/app/models/github_pull_request.rb b/modules/github_integration/app/models/github_pull_request.rb
index 4b53509d7ba..5cef02b4dfb 100644
--- a/modules/github_integration/app/models/github_pull_request.rb
+++ b/modules/github_integration/app/models/github_pull_request.rb
@@ -31,12 +31,14 @@ class GithubPullRequest < ApplicationRecord
has_and_belongs_to_many :work_packages
has_many :github_check_runs, dependent: :destroy
+ has_many :deploy_status_checks, dependent: :destroy
belongs_to :github_user, optional: true
belongs_to :merged_by, optional: true, class_name: "GithubUser"
enum state: {
open: "open",
- closed: "closed"
+ closed: "closed",
+ deployed: "deployed"
}
validates_presence_of :github_html_url,
diff --git a/modules/github_integration/app/views/deploy_targets/_form.html.erb b/modules/github_integration/app/views/deploy_targets/_form.html.erb
new file mode 100644
index 00000000000..34c7efcf574
--- /dev/null
+++ b/modules/github_integration/app/views/deploy_targets/_form.html.erb
@@ -0,0 +1,58 @@
+<%#-- copyright
+OpenProject is an open source project management software.
+Copyright (C) 2012-2024 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.
+
+++#%>
+<section class="form--section" id="deploy_target_form">
+ <div class="form--field -required" id="deploy_target_host_attributes">
+ <%= f.text_field :host, required: true, container_class: '-middle' %>
+ </div>
+
+ <div class="form--field -required">
+ <%= f.select :type,
+ ['OpenProject'],
+ { container_class: '-slim' },
+ disabled: true,
+ required: true
+ %>
+ <span class="form--field-instructions">
+ <%= t(:text_deploy_target_type_info) %>
+ </span>
+ </div>
+
+ <%# This field is specific to the only type of DeployTarget we support for now, which is OpenProject. %>
+ <div class="form--field">
+ <%= f.text_field "api_key", container_class: '-wide' %>
+ <span class="form--field-instructions">
+ <%= link_translate(
+ :text_deploy_target_api_key_info,
+ links: {
+ docs_url: "https://www.openproject.org/docs/api/introduction/#api-key-through-basic-auth"
+ }
+ ) %>
+ </span>
+ </div>
+</section>
diff --git a/modules/github_integration/app/views/deploy_targets/index.html.erb b/modules/github_integration/app/views/deploy_targets/index.html.erb
new file mode 100644
index 00000000000..1b564473e84
--- /dev/null
+++ b/modules/github_integration/app/views/deploy_targets/index.html.erb
@@ -0,0 +1,40 @@
+<%#-- copyright
+OpenProject is an open source project management software.
+Copyright (C) 2012-2024 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.
+
+++#%>
+
+<% html_title t(:label_administration), t(:label_github_integration) %>
+<%= toolbar title: t(:label_deploy_target_plural) do %>
+ <li class="toolbar-item">
+ <a href="<%= new_deploy_target_path %>" aria-label="<%= t(:button_add_deploy_target) %>" title="<%= t(:button_add_deploy_target) %>" class="button -primary">
+ <%= op_icon('button--icon icon-add') %>
+ <span class="button--text"><%= t(:label_deploy_target) %></span>
+ </a>
+ </li>
+<% end %>
+
+<%= render DeployTargets::TableComponent.new(rows: @deploy_targets, current_user: ) %>
diff --git a/modules/github_integration/app/views/deploy_targets/new.html.erb b/modules/github_integration/app/views/deploy_targets/new.html.erb
new file mode 100644
index 00000000000..f47dcca016d
--- /dev/null
+++ b/modules/github_integration/app/views/deploy_targets/new.html.erb
@@ -0,0 +1,40 @@
+<%#-- copyright
+OpenProject is an open source project management software.
+Copyright (C) 2012-2024 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.
+
+++#%>
+
+<% html_title t(:label_administration), t("label_deploy_target_new") %>
+<% local_assigns[:additional_breadcrumb] = t(:label_deploy_target_new) %>
+
+<%= toolbar title: t(:label_deploy_target_new) %>
+
+<%= error_messages_for @deploy_target %>
+
+<%= labelled_tabular_form_for @deploy_target, url: deploy_targets_path do |f| %>
+ <%= render partial: 'deploy_targets/form', locals: { f: f, deploy_target: @deploy_target } %>
+ <%= styled_button_tag t(:button_save), class: '-highlight -with-icon icon-checkmark' %>
+<% end %>
diff --git a/modules/github_integration/app/workers/cron/check_deploy_status_job.rb b/modules/github_integration/app/workers/cron/check_deploy_status_job.rb
new file mode 100644
index 00000000000..b1ed86d3e32
--- /dev/null
+++ b/modules/github_integration/app/workers/cron/check_deploy_status_job.rb
@@ -0,0 +1,183 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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 Cron
+ class CheckDeployStatusJob < ApplicationJob
+ include OpenProject::GithubIntegration::NotificationHandler::Helper
+
+ priority_number :low
+
+ def perform
+ deploy_targets.find_each do |deploy_target|
+ sha = openproject_core_sha deploy_target.host, deploy_target.api_key
+
+ if sha.present?
+ pull_requests.find_each do |pull_request|
+ check_deploy_status deploy_target, pull_request, sha
+ end
+ else
+ OpenProject.logger.error "Failed to retrieve core SHA for deploy target #{deploy_target.host}"
+ end
+ end
+ end
+
+ def deploy_targets
+ DeployTarget.all
+ end
+
+ def pull_requests
+ GithubPullRequest
+ .closed
+ .where.not(merge_commit_sha: nil)
+ end
+
+ def check_deploy_status(deploy_target, pull_request, core_sha)
+ status_check = deploy_status_check deploy_target, pull_request
+
+ # we already checked this PR against the given core SHA, so no need to check again
+ return if status_check.core_sha == core_sha
+
+ # if the commit is contained, it has been deployed
+ if commit_contains? core_sha, pull_request.merge_commit_sha
+ update_deploy_status pull_request, deploy_target
+ else
+ status_check.update core_sha: # remember last checked SHA to not check twice
+ end
+ end
+
+ ##
+ # Marks the given PR as deployed and removes the last status check
+ # as it won't be needed anymore. This is because only closed (not yet deployed)
+ # PRs are ever checked for their deployment status.
+ def update_deploy_status(pull_request, deploy_target)
+ host = deploy_target.host
+
+ ActiveRecord::Base.transaction do
+ delete_status_check pull_request, deploy_target
+ pull_request.update! state: "deployed"
+
+ comment_on_referenced_work_packages(
+ pull_request.work_packages,
+ comment_user,
+ "[#{pull_request.repository}##{pull_request.number}](#{pull_request.github_html_url}) deployed to [#{host}](https://#{host})"
+ )
+ end
+ end
+
+ def delete_status_check(pull_request, deploy_target)
+ # we use `find` and delete it this way to also cover
+ # not-yet-persisted records
+ check = pull_request
+ .deploy_status_checks
+ .find { |c| c.deploy_target == deploy_target }
+
+ pull_request.deploy_status_checks.delete(check)
+ end
+
+ ##
+ # Ideally this would be the github user, but we don't really have a way
+ # to identify it outside of the webhook request cycle.
+ def comment_user
+ User.system
+ end
+
+ def deploy_status_check(deploy_target, pull_request)
+ pull_request
+ .deploy_status_checks
+ .find_or_initialize_by(
+ deploy_target:,
+ github_pull_request: pull_request
+ )
+ end
+
+ def openproject_core_sha(host, api_token)
+ res = introspection_request(host, api_token)
+
+ return nil if handle_request_error res, "Could not get OpenProject core SHA"
+
+ info = JSON.parse res.body.to_s
+
+ info["coreSha"].presence
+ end
+
+ def introspection_request(host, api_token)
+ OpenProject.httpx.basic_auth("apikey", api_token).get("http://#{host}/api/v3")
+ end
+
+ ##
+ # Uses the GitHub APIs compare endpoint to compare the currently deployed base commit
+ # and a merge commit from a PR.
+ #
+ # If the latter is included in the former, there will be 'ahead_by' and 'behind_by'
+ # in the response. 'aheady_by' will be 0, 'behind_by' greater than 0.
+ #
+ # If the commits are not included in the same branch, these fields
+ # will not be present at all.
+ def commit_contains?(base_commit_sha, merge_commit_sha)
+ data = compare_commits base_commit_sha, merge_commit_sha
+
+ return false if data.nil?
+
+ status = data["status"].presence
+ ahead_by = data["ahead_by"].presence
+ behind_by = data["behind_by"].presence
+
+ status == "behind" && ahead_by == 0 && (behind_by.present? && behind_by > 0)
+ end
+
+ def compare_commits(sha_a, sha_b)
+ res = compare_commits_request sha_a, sha_b
+
+ return nil if handle_request_error res, "Failed to compare commits"
+
+ JSON.parse res.body.read
+ end
+
+ def handle_request_error(res, error_prefix)
+ if res.is_a? HTTPX::ErrorResponse
+ OpenProject.logger.error "#{error_prefix}: #{res.error}"
+ elsif res.status == 404
+ OpenProject.logger.error "#{error_prefix}: not found"
+ elsif res.status != 200
+ OpenProject.logger.error "#{error_prefix}: #{res.body}"
+ else
+ return false
+ end
+
+ true
+ end
+
+ def compare_commits_request(sha_a, sha_b)
+ OpenProject.httpx.get(compare_commits_url(sha_a, sha_b))
+ end
+
+ def compare_commits_url(sha_a, sha_b)
+ "https://api.github.com/repos/opf/openproject/compare/#{sha_a}...#{sha_b}"
+ end
+ end
+end
diff --git a/modules/github_integration/config/locales/en.yml b/modules/github_integration/config/locales/en.yml
index 8b5a2e3e6dc..182110277b5 100644
--- a/modules/github_integration/config/locales/en.yml
+++ b/modules/github_integration/config/locales/en.yml
@@ -27,9 +27,22 @@
#++
en:
+ button_add_deploy_target: Add deploy target
+ label_deploy_target: Deploy target
+ label_deploy_target_new: New deploy target
+ label_deploy_target_plural: Deploy targets
+ label_github_integration: GitHub Integration
+ notice_deploy_target_created: Deploy target created
+ notice_deploy_target_destroyed: Deploy target deleted
plugin_openproject_github_integration:
name: "OpenProject GitHub Integration"
description: "Integrates OpenProject and GitHub for a better workflow"
project_module_github: "GitHub"
permission_show_github_content: "Show GitHub content"
+ permission_introspection: Read running OpenProject core version and build SHA
+ text_deploy_target_type_info: >
+ So far we only support OpenProject itself.
+ text_deploy_target_api_key_info: >
+ An OpenProject [API key](docs_url)
+ belonging to a user who has the global introspection permission.
diff --git a/modules/github_integration/config/routes.rb b/modules/github_integration/config/routes.rb
new file mode 100644
index 00000000000..7fe0aaba47a
--- /dev/null
+++ b/modules/github_integration/config/routes.rb
@@ -0,0 +1,31 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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.
+#++
+
+Rails.application.routes.draw do
+ resources :deploy_targets, only: %i[index new edit update create destroy]
+end
diff --git a/modules/github_integration/db/migrate/20240501083852_create_deploy_targets.rb b/modules/github_integration/db/migrate/20240501083852_create_deploy_targets.rb
new file mode 100644
index 00000000000..782e976724b
--- /dev/null
+++ b/modules/github_integration/db/migrate/20240501083852_create_deploy_targets.rb
@@ -0,0 +1,13 @@
+class CreateDeployTargets < ActiveRecord::Migration[7.1]
+ def change
+ create_table :deploy_targets do |t|
+ t.text :type, null: false
+ t.text :host, null: false
+ t.jsonb :options, null: false, default: {}
+
+ t.timestamps
+ end
+
+ add_index :deploy_targets, :host, unique: true
+ end
+end
diff --git a/modules/github_integration/db/migrate/20240501093751_create_deploy_status_checks.rb b/modules/github_integration/db/migrate/20240501093751_create_deploy_status_checks.rb
new file mode 100644
index 00000000000..2881a758cde
--- /dev/null
+++ b/modules/github_integration/db/migrate/20240501093751_create_deploy_status_checks.rb
@@ -0,0 +1,12 @@
+class CreateDeployStatusChecks < ActiveRecord::Migration[7.1]
+ def change
+ create_table :deploy_status_checks do |t|
+ t.references :deploy_target
+ t.references :github_pull_request
+
+ t.text :core_sha, null: false
+
+ t.timestamps
+ end
+ end
+end
diff --git a/modules/github_integration/db/migrate/20240502081436_add_merge_commit_sha_to_github_pull_requests.rb b/modules/github_integration/db/migrate/20240502081436_add_merge_commit_sha_to_github_pull_requests.rb
new file mode 100644
index 00000000000..5ae78152b88
--- /dev/null
+++ b/modules/github_integration/db/migrate/20240502081436_add_merge_commit_sha_to_github_pull_requests.rb
@@ -0,0 +1,5 @@
+class AddMergeCommitShaToGithubPullRequests < ActiveRecord::Migration[7.1]
+ def change
+ add_column :github_pull_requests, :merge_commit_sha, :text
+ end
+end
diff --git a/modules/github_integration/frontend/module/pull-request/pull-request-state.component.sass b/modules/github_integration/frontend/module/pull-request/pull-request-state.component.sass
index 80af28bde1d..dc0c8a9933b 100644
--- a/modules/github_integration/frontend/module/pull-request/pull-request-state.component.sass
+++ b/modules/github_integration/frontend/module/pull-request/pull-request-state.component.sass
@@ -49,3 +49,6 @@
&_closed
background-color: #d73a49
+
+ &_deployed
+ background-color: #d73af9
diff --git a/modules/github_integration/frontend/module/pull-request/pull-request-state.component.ts b/modules/github_integration/frontend/module/pull-request/pull-request-state.component.ts
index 387a2dc378d..33bbec81afc 100644
--- a/modules/github_integration/frontend/module/pull-request/pull-request-state.component.ts
+++ b/modules/github_integration/frontend/module/pull-request/pull-request-state.component.ts
@@ -35,7 +35,7 @@ import {
import { PathHelperService } from 'core-app/core/path-helper/path-helper.service';
import { I18nService } from 'core-app/core/i18n/i18n.service';
-export type PullRequestState = 'opened'|'closed'|'referenced'|'ready_for_review'|'merged'|'draft';
+export type PullRequestState = 'opened'|'closed'|'referenced'|'ready_for_review'|'merged'|'draft'|'deployed';
@Component({
selector: 'op-github-pull-request-state',
diff --git a/modules/github_integration/frontend/module/pull-request/pull-request.component.ts b/modules/github_integration/frontend/module/pull-request/pull-request.component.ts
index ecc70c8ec87..5b81b1e8e20 100644
--- a/modules/github_integration/frontend/module/pull-request/pull-request.component.ts
+++ b/modules/github_integration/frontend/module/pull-request/pull-request.component.ts
@@ -71,6 +71,9 @@ export class PullRequestComponent {
if (this.pullRequest.state === 'open') {
return (this.pullRequest.draft ? 'draft' : 'open');
}
+ if (this.pullRequest.state === 'deployed') {
+ return 'deployed';
+ }
return (this.pullRequest.merged ? 'merged' : 'closed');
}
diff --git a/modules/github_integration/lib/open_project/github_integration/engine.rb b/modules/github_integration/lib/open_project/github_integration/engine.rb
index 75950c7d335..81ecb138c88 100644
--- a/modules/github_integration/lib/open_project/github_integration/engine.rb
+++ b/modules/github_integration/lib/open_project/github_integration/engine.rb
@@ -35,14 +35,36 @@ module OpenProject::GithubIntegration
include OpenProject::Plugins::ActsAsOpEngine
- register "openproject-github_integration",
- author_url: "https://www.openproject.org/",
- bundled: true do
+ initializer "github.feature_decisions" do
+ OpenProject::FeatureDecisions.add :deploy_targets
+ end
+
+ register(
+ "openproject-github_integration",
+ author_url: "https://www.openproject.org/",
+ bundled: true
+ ) do
+ ::Redmine::MenuManager.map(:admin_menu) do |menu|
+ menu.push :admin_github_integration,
+ { controller: "/deploy_targets", action: "index" },
+ if: Proc.new { Setting.feature_deploy_targets_active? && User.current.admin? },
+ caption: :label_github_integration,
+ icon: "github_logo"
+ end
+
project_module(:github, dependencies: :work_package_tracking) do
permission(:show_github_content,
{},
permissible_on: %i[work_package project])
end
+
+ menu :admin_menu,
+ :deploy_targets,
+ { controller: "/deploy_targets", action: "index" },
+ if: ->(*) { Setting.feature_deploy_targets_active? && User.current.admin? },
+ parent: :admin_github_integration,
+ caption: :label_deploy_target_plural,
+ icon: "hosting"
end
initializer "github.register_hook" do
@@ -86,12 +108,24 @@ module OpenProject::GithubIntegration
end
add_cron_jobs do
- {
+ jobs = {
"Cron::ClearOldPullRequestsJob": {
cron: "25 1 * * *", # runs at 1:25 nightly
class: ::Cron::ClearOldPullRequestsJob.name
}
}
+
+ # Enabling the feature flag at runtime won't enable
+ # the cron job. So if you want this feature, enable it
+ # at start-time.
+ if Setting.feature_deploy_targets_active?
+ jobs["Cron::CheckDeployStatusJob"] = {
+ cron: "15,45 * * * *", # runs every half hour
+ class: ::Cron::CheckDeployStatusJob.name
+ }
+ end
+
+ jobs
end
end
end
diff --git a/modules/github_integration/lib/open_project/github_integration/services/upsert_pull_request.rb b/modules/github_integration/lib/open_project/github_integration/services/upsert_pull_request.rb
index 2ed0ea027f7..19000c1eb6f 100644
--- a/modules/github_integration/lib/open_project/github_integration/services/upsert_pull_request.rb
+++ b/modules/github_integration/lib/open_project/github_integration/services/upsert_pull_request.rb
@@ -70,6 +70,7 @@ module OpenProject::GithubIntegration::Services
.fetch("repo")
.fetch("html_url"),
draft: payload.fetch("draft"),
+ merge_commit_sha: payload.fetch("merge_commit_sha"),
merged: payload.fetch("merged"),
merged_by: github_user_id(payload["merged_by"]),
merged_at: payload["merged_at"],
diff --git a/modules/github_integration/spec/factories/deploy_status_check_factory.rb b/modules/github_integration/spec/factories/deploy_status_check_factory.rb
new file mode 100644
index 00000000000..2bc91787f42
--- /dev/null
+++ b/modules/github_integration/spec/factories/deploy_status_check_factory.rb
@@ -0,0 +1,34 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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.
+#++
+
+FactoryBot.define do
+ factory :deploy_status_check do
+ deploy_target
+ github_pull_request
+ end
+end
diff --git a/modules/github_integration/spec/factories/deploy_target_factory.rb b/modules/github_integration/spec/factories/deploy_target_factory.rb
new file mode 100644
index 00000000000..ac3cd2ecdea
--- /dev/null
+++ b/modules/github_integration/spec/factories/deploy_target_factory.rb
@@ -0,0 +1,35 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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.
+#++
+
+FactoryBot.define do
+ factory :deploy_target do
+ type { "OpenProject" }
+
+ sequence(:host) { |n| "https://qa-#{n}.openproject-edge.com" }
+ end
+end
diff --git a/modules/github_integration/spec/workers/cron/check_deploy_status_job_spec.rb b/modules/github_integration/spec/workers/cron/check_deploy_status_job_spec.rb
new file mode 100644
index 00000000000..6e4d2c69c6e
--- /dev/null
+++ b/modules/github_integration/spec/workers/cron/check_deploy_status_job_spec.rb
@@ -0,0 +1,45 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2024 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 "spec_helper"
+
+RSpec.describe Cron::CheckDeployStatusJob, type: :job, with_flags { deploy_targets: true } do
+ let!(:deploy_target) { create(:deploy_target) }
+
+ let(:deploy_status_check) { create(:deploy_status_check, deploy_target:, pull_request:) }
+ let(:pull_request) { create(:github_pull_request, work_packages: [work_package]) }
+ let(:work_package) { create(:work_package) }
+
+ let(:job) { described_class.new }
+
+ context "with no prior checks" do
+ before do
+ job.run
+ end
+ end
+end