Skip to content

Commit 9a97cae

Browse files
Allow setting a gitlab user and webhook secrets
1 parent 6b97c17 commit 9a97cae

File tree

11 files changed

+631
-8
lines changed

11 files changed

+631
-8
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<%#-- copyright
2+
OpenProject is an open source project management software.
3+
Copyright (C) the OpenProject GmbH
4+
5+
This program is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU General Public License version 3.
7+
8+
OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
9+
Copyright (C) 2006-2013 Jean-Philippe Lang
10+
Copyright (C) 2010-2013 the ChiliProject Team
11+
12+
This program is free software; you can redistribute it and/or
13+
modify it under the terms of the GNU General Public License
14+
as published by the Free Software Foundation; either version 2
15+
of the License, or (at your option) any later version.
16+
17+
This program is distributed in the hope that it will be useful,
18+
but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
GNU General Public License for more details.
21+
22+
You should have received a copy of the GNU General Public License
23+
along with this program; if not, write to the Free Software
24+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25+
26+
See COPYRIGHT and LICENSE files for more details.
27+
28+
++#%>
29+
30+
<%=
31+
render(Primer::OpenProject::PageHeader.new) do |header|
32+
header.with_title { t(:label_gitlab_integration) }
33+
header.with_breadcrumbs(breadcrumb_items)
34+
end
35+
%>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# frozen_string_literal: true
2+
3+
#-- copyright
4+
# OpenProject is an open source project management software.
5+
# Copyright (C) the OpenProject GmbH
6+
#
7+
# This program is free software; you can redistribute it and/or
8+
# modify it under the terms of the GNU General Public License version 3.
9+
#
10+
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
11+
# Copyright (C) 2006-2013 Jean-Philippe Lang
12+
# Copyright (C) 2010-2013 the ChiliProject Team
13+
#
14+
# This program is free software; you can redistribute it and/or
15+
# modify it under the terms of the GNU General Public License
16+
# as published by the Free Software Foundation; either version 2
17+
# of the License, or (at your option) any later version.
18+
#
19+
# This program is distributed in the hope that it will be useful,
20+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
# GNU General Public License for more details.
23+
#
24+
# You should have received a copy of the GNU General Public License
25+
# along with this program; if not, write to the Free Software
26+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27+
#
28+
# See COPYRIGHT and LICENSE files for more details.
29+
#++
30+
31+
module GitlabIntegration
32+
module Admin
33+
class PageHeaderComponent < ApplicationComponent
34+
def breadcrumb_items
35+
[
36+
{ href: admin_index_path, text: t("label_administration") },
37+
t(:label_gitlab_integration)
38+
]
39+
end
40+
end
41+
end
42+
end
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# frozen_string_literal: true
2+
3+
#-- copyright
4+
# OpenProject is an open source project management software.
5+
# Copyright (C) the OpenProject GmbH
6+
#
7+
# This program is free software; you can redistribute it and/or
8+
# modify it under the terms of the GNU General Public License version 3.
9+
#
10+
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
11+
# Copyright (C) 2006-2013 Jean-Philippe Lang
12+
# Copyright (C) 2010-2013 the ChiliProject Team
13+
#
14+
# This program is free software; you can redistribute it and/or
15+
# modify it under the terms of the GNU General Public License
16+
# as published by the Free Software Foundation; either version 2
17+
# of the License, or (at your option) any later version.
18+
#
19+
# This program is distributed in the hope that it will be useful,
20+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
# GNU General Public License for more details.
23+
#
24+
# You should have received a copy of the GNU General Public License
25+
# along with this program; if not, write to the Free Software
26+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27+
#
28+
# See COPYRIGHT and LICENSE files for more details.
29+
#++
30+
31+
module GitlabIntegration
32+
module Admin
33+
class SettingsController < ApplicationController
34+
layout "admin"
35+
36+
menu_item :admin_gitlab_integration
37+
38+
before_action :require_admin
39+
40+
def show
41+
settings = plugin_settings
42+
user_id = settings[:gitlab_user_id].presence
43+
@gitlab_comment_user = user_id ? User.find_by(id: user_id) : nil
44+
@webhook_secret = settings[:webhook_secret]
45+
end
46+
47+
def update
48+
merged = plugin_settings.merge(permitted_params)
49+
Setting.plugin_openproject_gitlab_integration = merged
50+
flash[:notice] = I18n.t(:notice_successful_update)
51+
redirect_to gitlab_integration_admin_settings_path
52+
end
53+
54+
private
55+
56+
def permitted_params
57+
params.permit(:gitlab_user_id, :webhook_secret).to_h
58+
end
59+
60+
def plugin_settings
61+
Hash(Setting.plugin_openproject_gitlab_integration).with_indifferent_access
62+
end
63+
end
64+
end
65+
end
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# frozen_string_literal: true
2+
3+
#-- copyright
4+
# OpenProject is an open source project management software.
5+
# Copyright (C) the OpenProject GmbH
6+
#
7+
# This program is free software; you can redistribute it and/or
8+
# modify it under the terms of the GNU General Public License version 3.
9+
#
10+
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
11+
# Copyright (C) 2006-2013 Jean-Philippe Lang
12+
# Copyright (C) 2010-2013 the ChiliProject Team
13+
#
14+
# This program is free software; you can redistribute it and/or
15+
# modify it under the terms of the GNU General Public License
16+
# as published by the Free Software Foundation; either version 2
17+
# of the License, or (at your option) any later version.
18+
#
19+
# This program is distributed in the hope that it will be useful,
20+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
# GNU General Public License for more details.
23+
#
24+
# You should have received a copy of the GNU General Public License
25+
# along with this program; if not, write to the Free Software
26+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27+
#
28+
# See COPYRIGHT and LICENSE files for more details.
29+
#++
30+
31+
module GitlabIntegration
32+
module Admin
33+
class SettingsForm < ApplicationForm
34+
form do |f|
35+
f.autocompleter(
36+
name: :gitlab_user_id,
37+
label: I18n.t(:label_gitlab_actor),
38+
caption: I18n.t(:text_gitlab_actor_info),
39+
autocomplete_options: {
40+
component: "opce-user-autocompleter",
41+
allowEmpty: true,
42+
defaultData: false,
43+
model: @comment_user_model
44+
}
45+
)
46+
47+
f.text_field(
48+
name: :webhook_secret,
49+
label: I18n.t(:label_gitlab_webhook_secret),
50+
caption: I18n.t(:text_gitlab_webhook_secret_info),
51+
value: @webhook_secret,
52+
input_width: :xxlarge
53+
)
54+
55+
f.submit(
56+
name: :submit,
57+
label: I18n.t(:button_save),
58+
scheme: :primary
59+
)
60+
end
61+
62+
def initialize(comment_user: nil, webhook_secret: nil)
63+
super()
64+
@comment_user_model = comment_user.present? ? { id: comment_user.id, name: comment_user.name } : nil
65+
@webhook_secret = webhook_secret
66+
end
67+
end
68+
end
69+
end
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<%#-- copyright
2+
OpenProject is an open source project management software.
3+
Copyright (C) the OpenProject GmbH
4+
5+
This program is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU General Public License version 3.
7+
8+
OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
9+
Copyright (C) 2006-2013 Jean-Philippe Lang
10+
Copyright (C) 2010-2013 the ChiliProject Team
11+
12+
This program is free software; you can redistribute it and/or
13+
modify it under the terms of the GNU General Public License
14+
as published by the Free Software Foundation; either version 2
15+
of the License, or (at your option) any later version.
16+
17+
This program is distributed in the hope that it will be useful,
18+
but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
GNU General Public License for more details.
21+
22+
You should have received a copy of the GNU General Public License
23+
along with this program; if not, write to the Free Software
24+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25+
26+
See COPYRIGHT and LICENSE files for more details.
27+
28+
++#%>
29+
<% html_title t(:label_administration), t(:label_gitlab_integration), t(:label_setting_plural) %>
30+
31+
<%= render(GitlabIntegration::Admin::PageHeaderComponent.new) %>
32+
33+
<%= settings_primer_form_with(url: gitlab_integration_admin_settings_path, method: :patch) do |f| %>
34+
<% if @webhook_secret.blank? %>
35+
<%= render(Primer::Alpha::Banner.new(scheme: :warning, icon: :alert, mb: 4)) do %>
36+
<%= t(:text_gitlab_webhook_secret_missing_warning) %>
37+
<% end %>
38+
<% end %>
39+
<%= render GitlabIntegration::Admin::SettingsForm.new(f,
40+
comment_user: @gitlab_comment_user,
41+
webhook_secret: @webhook_secret) %>
42+
<% end %>

modules/gitlab_integration/config/locales/en.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,22 @@ en:
5656
labels:
5757
invalid_schema: "must be an array of hashes with keys: color, title"
5858

59+
label_gitlab_integration: "GitLab Integration"
60+
label_gitlab_actor: "GitLab actor"
61+
label_gitlab_webhook_secret: "Webhook secret"
62+
text_gitlab_actor_info: >
63+
The OpenProject user whose API key must be used to authenticate incoming webhook requests.
64+
When set, requests authenticated with any other user's credentials are rejected.
65+
This user also posts automated comments on work packages. Defaults to the system user when not set.
66+
text_gitlab_webhook_secret_info: >
67+
A secret token shared with GitLab when configuring the webhook.
68+
When set, OpenProject verifies the X-Gitlab-Token header on every incoming request,
69+
rejecting payloads that do not match. Leave blank to skip verification (not recommended).
70+
text_gitlab_webhook_secret_missing_warning: >
71+
No webhook secret is configured. Any request to the GitLab webhook endpoint will be accepted
72+
without verification, which may allow unauthorized actors to forge events. It is strongly
73+
recommended to set a secret.
74+
5975
project_module_gitlab: "GitLab"
6076
permission_show_gitlab_content: "Show GitLab content"
6177

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# frozen_string_literal: true
2+
3+
#-- copyright
4+
# OpenProject is an open source project management software.
5+
# Copyright (C) the OpenProject GmbH
6+
#
7+
# This program is free software; you can redistribute it and/or
8+
# modify it under the terms of the GNU General Public License version 3.
9+
#
10+
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
11+
# Copyright (C) 2006-2013 Jean-Philippe Lang
12+
# Copyright (C) 2010-2013 the ChiliProject Team
13+
#
14+
# This program is free software; you can redistribute it and/or
15+
# modify it under the terms of the GNU General Public License
16+
# as published by the Free Software Foundation; either version 2
17+
# of the License, or (at your option) any later version.
18+
#
19+
# This program is distributed in the hope that it will be useful,
20+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
21+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22+
# GNU General Public License for more details.
23+
#
24+
# You should have received a copy of the GNU General Public License
25+
# along with this program; if not, write to the Free Software
26+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27+
#
28+
# See COPYRIGHT and LICENSE files for more details.
29+
#++
30+
31+
Rails.application.routes.draw do
32+
namespace "gitlab_integration" do
33+
namespace "admin" do
34+
resource :settings, only: %i[show update]
35+
end
36+
end
37+
end

modules/gitlab_integration/lib/open_project/gitlab_integration/engine.rb

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,27 @@ class Engine < ::Rails::Engine
4040

4141
include OpenProject::Plugins::ActsAsOpEngine
4242

43+
def self.settings
44+
{
45+
default: {
46+
"gitlab_user_id" => nil,
47+
"webhook_secret" => nil
48+
}
49+
}
50+
end
51+
4352
register "openproject-gitlab_integration",
4453
author_url: "https://github.com/btey/openproject",
45-
bundled: true do
54+
bundled: true,
55+
settings: do
56+
::Redmine::MenuManager.map(:admin_menu) do |menu|
57+
menu.push :admin_gitlab_integration,
58+
{ controller: "/gitlab_integration/admin/settings", action: "show" },
59+
if: ->(_) { User.current.admin? },
60+
caption: :label_gitlab_integration,
61+
icon: :"op-logo-gitlab"
62+
end
63+
4664
project_module(:gitlab, dependencies: :work_package_tracking) do
4765
permission(:show_gitlab_content,
4866
{},

0 commit comments

Comments
 (0)