Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat[Op#23673]: Merge btey/openproject-gitlab-integration 🎉 #14881

Merged
Show file tree
Hide file tree
Changes from 104 commits
Commits
Show all changes
121 commits
Select commit Hold shift + click to select a range
e46a889
Initial commit
btey Mar 15, 2021
3ccf3fa
Initial release
btey Mar 17, 2021
5493951
Initial readme
btey Mar 17, 2021
cb0949a
Update README.md
btey Mar 17, 2021
af35aa6
New images for readme.
Mar 25, 2021
58279c7
Merge branch 'master' of https://github.com/btey/openproject-gitlab-i…
Mar 25, 2021
e992ca8
Updated Readme
btey Mar 25, 2021
12d65f0
Update README.md
btey Mar 25, 2021
06ca5d3
Pre-release version with copyright
Nov 26, 2021
f4b5dc0
Merge branch 'master' of https://github.com/btey/openproject-gitlab-i…
Nov 26, 2021
68ea542
Update README.md
btey Nov 26, 2021
4c78ed6
Fixes based on github plugin for v12
Jan 27, 2022
a19f15f
Merge tag 'v2.0.1' into develop
Jan 27, 2022
fe4eb8b
Merge branch 'hotfix/v2.0.1'
Jan 27, 2022
8297f54
Better place for setup IDs of WP Status
Feb 9, 2022
5ab410c
Merge branch 'release/v2.0.2'
Feb 9, 2022
c6b753a
Remove requiring changes to the OpenProject core
oliverguenther Feb 14, 2022
f86cd0c
Apply suggestions from code review
oliverguenther Feb 15, 2022
2cef1b3
Merge pull request #6 from oliverguenther/fix/overrides
btey Feb 15, 2022
ab70705
Update README.md
btey Feb 15, 2022
3763aa2
Update README.md
btey Feb 15, 2022
dcf7342
Update README.md
btey Feb 15, 2022
fbfcfce
Updating Readme for Docker setup
btey Feb 25, 2022
df92416
Don't try to output link on new record
oliverguenther Apr 26, 2022
451cbf9
Merge pull request #11 from oliverguenther/fix/wp-new-record
btey Apr 26, 2022
e0d2b03
Update README with current link to GitHub integration docs
BjoernLudwigPTB Apr 26, 2022
6c9fdb6
Merge pull request #12 from BjoernLudwigPTB/correct_link_to_important…
btey Apr 26, 2022
23c7855
Updated README.md
btey Apr 26, 2022
9139898
Fix compatibility with OP permission arguments
May 4, 2022
1090e20
Fix APIV3Service library name change
May 4, 2022
f9f6348
Update readme and increment version
May 4, 2022
9a60873
Update README.md
btey Jun 6, 2022
9aca6a8
Use built-in mechanism to register module and permission
oliverguenther Aug 23, 2022
d52bcb5
Merge pull request #20 from oliverguenther/fix/12-2-0-compatibility
btey Aug 23, 2022
b26bec1
Update README.md
btey Aug 23, 2022
0ea6189
Update README.md
btey Dec 7, 2022
a9b4b6f
Update README.md
wagner-petrosoft Jan 19, 2023
9b1ca38
Merge pull request #29 from wagner-petrosoft/feature/explicitToken
btey Feb 1, 2023
f8e11f9
New colors and style based on current gitlab version
Feb 22, 2023
1009022
Update README.md
btey Feb 22, 2023
339050a
Counter in gitlab tab
Mar 14, 2023
004349a
OPSharedModule renamed to OpSharedModule
Mar 24, 2023
8f604d8
Readme update
Mar 24, 2023
c4b8b3a
fix version in Gemfile.lock 2.0.6 -> 2.0.9
foxweb Jun 30, 2023
278f0c3
Merge pull request #32 from foxweb/patch-1
btey Jun 30, 2023
88613af
New version 2.1
btey Nov 20, 2023
75e2fcd
Update readme with details for v2.1
btey Nov 20, 2023
81a2ef3
add gitlab documentation links + minor improvments
pustekuchen91 Nov 21, 2023
389066a
add link for configure-a-webhook-in-gitlab
pustekuchen91 Nov 21, 2023
04a155d
syntax highlighting for shell commands
pustekuchen91 Nov 21, 2023
acd4574
Merge pull request #44 from pustekuchen91/improve-readme
btey Nov 22, 2023
8859ff8
russian locale translated
foxweb Nov 23, 2023
d42eed2
snippet for commit message contained wrong task code
foxweb Nov 23, 2023
ccf0a94
Merge pull request #47 from foxweb/snippet-fix
btey Nov 24, 2023
80d8dde
Merge pull request #46 from foxweb/ru-locale-translation
btey Nov 24, 2023
03fb32d
russian locale fix
foxweb Nov 27, 2023
0792882
Merge pull request #49 from foxweb/master
btey Nov 28, 2023
419f94b
fix 500 errors with empty MR and Issue description body, fix empty av…
foxweb Dec 1, 2023
8554e43
replaced avatar_url from SVG to URL
foxweb Dec 1, 2023
8c86bf3
russian locale fix
foxweb Dec 1, 2023
bdf9452
openproject-gitlab-integration README.md updated
foxweb Dec 1, 2023
e1c5d4b
Merge pull request #50 from foxweb/empty-descriptions-fix
btey Dec 1, 2023
54cfe5d
Bug - Lack of issues referenced in gitlab tab
btey Dec 2, 2023
a137639
services refactoring
foxweb Dec 3, 2023
700788c
gem openproject-gitlab_integration 2.1.3 updated
foxweb Dec 4, 2023
3f8b75e
module Services fix
foxweb Dec 4, 2023
3e71033
fix
foxweb Dec 4, 2023
d8cb06d
prevent 500 errors: undefined method `iid?` for nil:NilClass when pip…
foxweb Dec 5, 2023
782a06d
Initial hotfix for OP v13.1.0 - Permission method requires permissibl…
btey Dec 14, 2023
557f302
Remove old permission call and update readme.
btey Dec 14, 2023
a0b293d
Fix version in gemspecs file
btey Dec 14, 2023
d97c0f8
More fixes for OP v13.1.0
btey Dec 17, 2023
f38ef6f
refactoring
foxweb Dec 20, 2023
9a8923b
refactoring
foxweb Dec 20, 2023
d529bcf
Add support for system hook to allow for global push events.
WiredTombstone Dec 29, 2023
4f78fe3
Clone push hook to system hook to allow for global pushes.
WiredTombstone Dec 29, 2023
56ba8aa
Relabel class.
WiredTombstone Dec 29, 2023
4393164
Add notification handler.
WiredTombstone Dec 29, 2023
3d7eb27
Update initializer.
WiredTombstone Dec 29, 2023
aa27ba0
Add require
WiredTombstone Dec 29, 2023
66ee35b
Update notification_handlers.rb
WiredTombstone Dec 29, 2023
69c48f0
Merge pull request #56 from WiredTombstone/System-Hook
btey Jan 15, 2024
097c47f
Update README.md
btey Jan 15, 2024
cc83cfa
Update README.md
btey Jan 15, 2024
6cf6aa0
Update README.md
btey Feb 15, 2024
0e31437
Update openproject-gitlab_integration.gemspec
btey Feb 15, 2024
8c5d871
Merge branch 'master' into services-refactoring
btey Feb 15, 2024
760a023
Merge pull request #52 from foxweb/services-refactoring
btey Feb 15, 2024
3bc1ae5
Ops[Op#53021]: Define Gitlab community edition docker compose
akabiru Feb 23, 2024
2271c18
chore[Op#53021]: Allow for override in case of MacOS setup
akabiru Feb 23, 2024
cc4b0ed
chore[Op#53021]: Include aliases in tls docker compose override
akabiru Feb 27, 2024
ebee326
docs[Op#53021]: Add some documentation for running the gitlab docker …
akabiru Feb 27, 2024
dd9b6c9
docs[Op#53021]: amend typos
akabiru Feb 28, 2024
348f0bf
docs[Op#53021]: Add helper documentation on resetting root password
akabiru Feb 28, 2024
96d5de5
[#53019] Bring btey/openproject-gitlab-integration into a structure t…
akabiru Feb 28, 2024
3020d70
[Op#53019] Bring btey/openproject-gitlab-integration into a structure…
akabiru Feb 28, 2024
d112ad2
MILESTONE[Op#53019 Op#23673]: Merge `github.com/btey/openproject-gitl…
akabiru Feb 28, 2024
0ff9861
legal[Op#53019]: Reference OP license and copyright
akabiru Feb 28, 2024
a2ad065
chore[Op#53019]: rubocop amend `modules/gitlab_integration`
akabiru Feb 28, 2024
529af48
chore[Op#53019]: Don't downgrade bundled with
akabiru Feb 28, 2024
b08bfa4
Merge branch 'dev' into implementation/53019-bring-bteyopenproject-gi…
akabiru Mar 1, 2024
8f26e0d
[#53117] Add automated tests for the critical path
akabiru Mar 1, 2024
26d4834
tests[Op#53117]: add gitlab module spec helper
akabiru Mar 2, 2024
6cbee5b
tests[Op#53117]: add gitlab model specs
akabiru Mar 2, 2024
865ff5a
tests[Op#53117]: add gitlab pipeline and merge request specs
akabiru Mar 2, 2024
e3c4388
chore[Op#53019]: reinstate license comments
akabiru Mar 2, 2024
4c81e63
chore[Op#53117]: build ci details from dynamic factory
akabiru Mar 4, 2024
7027243
tests[Op#53117]: add gitlab merge requests representer unit specs
akabiru Mar 4, 2024
aea2122
tests[Op#53117]: add gitlab issues representer unit specs
akabiru Mar 4, 2024
8f319cb
tests[Op#53117]: add gitlab merge request hook spec
akabiru Mar 5, 2024
14b5742
tests[Op#53117]: add gitlab issue hook spec
akabiru Mar 5, 2024
5920015
tests[Op#53117]: add gitlab pipeline happy path unit spec
akabiru Mar 5, 2024
bee5a12
tests[Op#53117]: add gitlab tab feature spec
akabiru Mar 6, 2024
0b5d405
tests[Op#53117]: fixup some inconsistencies on the hooks spec
akabiru Mar 6, 2024
69ab47f
tests[Op#53117]: fixup ambiguos class name, just assert against text
akabiru Mar 6, 2024
d18c623
tests[Op#53117]: cover gitlab activity tab happy path
akabiru Mar 6, 2024
7c791a4
chore[Op#53019]: bump version and add openproject as author
akabiru Mar 6, 2024
0cc247f
security[Op#53019]: clarify to security tool that there's no injectio…
akabiru Mar 6, 2024
a5eb528
fix[Op#53019]: Good Job ClearOldMergeRequestsJob
akabiru Mar 6, 2024
a69f06b
Merge branch 'dev' into implementation/53019-bring-bteyopenproject-gi…
akabiru Mar 6, 2024
53f950a
Merge pull request #14899 from opf/implementation/53117-add-automated…
akabiru Mar 6, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ PATH
openproject-github_integration (1.0.0)
openproject-webhooks

PATH
remote: modules/gitlab_integration
specs:
openproject-gitlab_integration (3.0.0)
openproject-webhooks

PATH
remote: modules/grids
specs:
Expand Down Expand Up @@ -1226,6 +1232,7 @@ DEPENDENCIES
openproject-documents!
openproject-gantt!
openproject-github_integration!
openproject-gitlab_integration!
openproject-job_status!
openproject-ldap_groups!
openproject-meeting!
Expand Down
1 change: 1 addition & 0 deletions Gemfile.modules
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ group :opf_plugins do
gem 'openproject-two_factor_authentication', path: 'modules/two_factor_authentication'
gem 'openproject-webhooks', path: 'modules/webhooks'
gem 'openproject-github_integration', path: 'modules/github_integration'
gem 'openproject-gitlab_integration', path: 'modules/gitlab_integration'
gem 'openproject-ldap_groups', path: 'modules/ldap_groups'
gem 'openproject-recaptcha', path: 'modules/recaptcha'
gem 'openproject-job_status', path: 'modules/job_status'
Expand Down
28 changes: 28 additions & 0 deletions modules/gitlab_integration/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-- 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.
++
234 changes: 234 additions & 0 deletions modules/gitlab_integration/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
## Introducing OpenProject GitLab Integration v2.1.6 GA

<div><a href="https://jb.gg/OpenSourceSupport"><img width="300" src="https://github.com/btey/openproject-gitlab-integration/assets/14983519/bc7b3e7f-163d-4bcf-ab31-9f5982b8d2fc"></a></div>

Based on the OpenProject Github Integration, this plugin offers the same functionalities plus other new features. This is the first version that includes the visualization of the status of the *Pipelines* (by now, it is considered in Beta status). You can test it by activating the Pipelines event in the GitLab webhook. Just keep in mind that not all pipelines will be reflected in OpenProject, only Merge Request type pipelines (for more information see the GitLab issue https://gitlab.com/gitlab-org/gitlab/-/issues/345028). Any feedback about the pipelines feature would be very appreciated, whether it works or if issues arise (you can use this ticket https://github.com/btey/openproject-gitlab-integration/issues/43).

In this version it has also been implemented that **all linked or referenced** Issues appear in the GitLab tab (https://github.com/btey/openproject-gitlab-integration/issues/34). The opportunity has also been taken to redesign how the information is presented so that it is visually easy to read and at the same time can continue to provide all the information, including labels and pipeline status.

If there are labels related to the Issue or MR, a button with the label icon will appear. By clicking the button you can show/hide the associated labels.

<img width="531" src="https://github.com/btey/openproject-gitlab-integration/assets/14983519/00b19597-2b1b-4b93-9a20-155c40efeb3f">
<br><br>
<img width="532" src="https://github.com/btey/openproject-gitlab-integration/assets/14983519/98fd20f7-83c2-4003-aca9-f33657e1b4cb">

## Overview

OpenProject module for integration with GitLab:
* Latest Gitlab release tested: **16.6.2**
* Latest OpenProject release tested: **13.1.2** (for OP v13.0.X use v2.1.2)

The reference system is based on the same system as for GitHub integration. You can use a link to the work package or just use “OP#87” or "PP#87" in the title/description of the Issue/MR in GitLab.

> **Note about the references.** Whether or not to include the reference in certain places depends on the information that GitLab sends through its webhook. If you include the reference in the title/description of an issue, the comments on the issue do not need to include the reference. The same will happen when you generate a Merge Request based on an Issue that already includes the reference; comments from that MR need not include the reference.

#### Difference between OP and PP

If you use `OP#` as a reference in an Issue or MR title, all comments will be replicated in OpenProject. However, sometimes you may only want to keep information about the status of an Issue/MR in OpenProject, but you don't want your comments to be published. In this case, you can use `PP#` as a reference. This way the comments will not be published in OpenProject. But if at any time one of your comments in a private Issue/MR is of interest to you to be published in OpenProject you can use `OP#` *directly in that comment*. So only that comment will be published in OpenProject. The rest of the comments will remain private and will not be published.

## Available events captured in OpenProject

OpenProject will **add comments** to work package for the following events:

* Merge Request (Opened, Closed and Merged)
* Issue (Opened, Closed)
* Push commits in Merge Requests
* Since v2.1.5 of the plugin you can activate system hooks for Push events.
* Comments (on Issues, Merge Request, Commits and Snippets)
* Pipelines (Beta feature)

OpenProject will **update WP status** in this events:

* Merge Request (opened) - Status: In progress (currently ID=7)
* Merge Request (merged) - Status: Developed (currently ID=8)

> **Note about the status.** If you want to change the ID of the status you can do this in this section of the [code](https://github.com/btey/openproject-gitlab-integration/blob/master/lib/open_project/gitlab_integration/notification_handler/merge_request_hook.rb#L40-L41). By default is *disabled*, you can enable it by setting to `true` this [lines](https://github.com/btey/openproject-gitlab-integration/blob/master/lib/open_project/gitlab_integration/notification_handler/merge_request_hook.rb#L38-L39).

## Example workflow

A typical workflow on GitLab side would be:

1. **Create Issue.**

<img src="doc/op-issue-opened.png" width="500">

> **Issue Opened:** Issue 6 New contact form - OP#18 for Scrum project has been opened by Administrator.

2. **Comment on issue.**

If the reference is included in the title, the comments will not need a reference. By default, all comments will use the title as a reference.

<img src="doc/op-commented-in-issue.png" width="500">

> **Commented in Issue:** Administrator commented this WP in Issue 6 New contact form - OP#18 on Scrum project:
>
> New comment on the issue with attachment.

3. **Create Merge Request.**

<img src="doc/op-mr-opened.png" width="500">

> **MR Opened:** Merge request 25 Draft: Resolve "New contact form - OP#18" for Scrum project has been opened by Administrator.
>
> **Status** changed from _Specified_
> **to** _In progress_

4. **Comment in Merge Request.**

<img src="doc/op-commented-in-mr.png" width="500">

> **Commented in MR:** Administrator commented this WP in Merge request 25 Draft: Resolve "New contact form - OP#18" on Scrum project:
>
> New comment on MR.

5. **Reference in other Issues or Merge Request (comments).**

If the reference is NOT included in the title of the Issue/MR, the comments will need a reference. In OpenProject the comment will be saved as "referenced" in Issue/MR.

<img src="doc/op-referenced-in-issue.png" width="500">

> **Referenced in Issue:** Administrator referenced this WP in Issue 2 New backend pipeline on Scrum project:
>
> OP#18 New comment about...
>
> **Note:** If you use the reference `PP#` in the title of the Issue/MR, you can use `OP#` in the comment to generate the same type of comment in OpenProject.

6. **New commit in Merge Request.**

<img src="doc/op-pushed-in-mr.png" width="500">

> **Pushed in MR:** Administrator pushed fca3d6fb to Scrum project at 2021-03-08T08:01:57+00:00:
>
> Update readme.md OP#18

7. **Comment in a new commit of the Merge Request.**

<img src="doc/op-referenced-in-commit.png" width="500">

> **Referenced in Commit:** Administrator referenced this WP in a Commit Note 0bf0e3e9 on Scrum project:
>
> This change is for OP#18.

8. **Merge Request merged (generates up to 3 events).**

<img src="doc/op-mr-merged-event-2.png" width="500">

> **Pushed in MR:** Administrator pushed 1da09cb4 to Scrum project at 2021-03-05T14:57:37+00:00:
>
> Merge branch '5-new-contact-form-op-18' into 'master'
>
> Resolve "New contact form - OP#18"
>
> Closes #6
>
> See merge request root/scrum!9

<img src="doc/op-mr-merged-event-3.png" width="500">

> **MR Merged:** Merge request 24 Resolve "New contact form - OP#18" for Scrum project has been merged by Administrator.
>
> **Status** changed from _In progress_
> **to** _Developed_

<img src="doc/op-mr-merged-event-4.png" width="500">

> **Issue Closed:** Issue 6 New contact form - OP#18 for Scrum project has been closed by Administrator.

## Configuration

You will have to configure both **OpenProject** and **Gitlab** for the integration to work.

In case of **Docker** installation, follow the official OpenProject documentation [here](https://www.openproject.org/docs/installation-and-operations/installation/docker/#openproject-plugins). If for some reason the installation with Docker described in the official documentation does not work for you, you can try building your own docker image:
* Clone from the Openproject Repo: `git clone https://github.com/opf/openproject.git --branch=stable/13 --depth=1 .`
* Clone the plugin inside the modules folder: `git clone https://github.com/btey/openproject-gitlab-integration.git --depth=1 modules/gitlab_integration`
* Apply the changes below in Gemfile.lock and Gemfile.modules (the same ones you would do in a manual install).
* Build the container: `docker build -t openproject-docker --file=docker/prod/Dockerfile .`
* Now run the image following the official documentation.

In case of **DEB/RPM** based instalation, follow the official OpenProject documentation [here](https://www.openproject.org/docs/installation-and-operations/configuration/plugins/).

In case of [**manual**](https://www.openproject.org/docs/installation-and-operations/installation/manual/) installation, this plugin should be installed in the same place as the Github plugin that comes bundled with OpenProject.

- **Github plugin path:** `modules/github_integration`

- **Path to put Gitlab plugin:** `modules/gitlab_integration`

But first you must modify **Gemfile.lock** and **Gemfile.modules** so that OpenProject detects the new module.

Add the following in **Gemfile.lock**:

```yml
PATH
remote: modules/gitlab_integration
specs:
openproject-gitlab_integration (2.1.6)
openproject-webhooks
```

And add this other line in DEPENDENCIES section:

```yml
DEPENDENCIES
...
openproject-github_integration!
openproject-gitlab_integration!
openproject-job_status!
...
```

Add the following in **Gemfile.modules**:

```yml
group :opf_plugins do
...
gem 'openproject-gitlab_integration', path: 'modules/gitlab_integration'
...
end
```

**Note:** It's possible that you need to use these commands before and after the `bundle install` if you get an error in this step warning about a change in the Gemfile:

```shell
bundle config unset deployment
bundle install --deployment --without mysql2 sqlite development test therubyracer docker
bundle config set deployment
```

### The GitLab Bot user in OpenProject

First you will need to create a user in OpenProject that will make the comments. The user will have to be added to each project with a role that allows them to comment on work packages and change status.

Once the user is created you need to generate an OpenProject API token for it to use later on the Gitlab side:

* Login as the newly created user.
* Go to My Account (click on Avatar in top right corner).
* Go to Access Token.
* Click on generate in the API row.
* Copy the generated key. You can now configure the necessary webhook in Gitlab.

### The webhook in GitLab

In GitLab you have to [set up a webhook](https://docs.gitlab.com/ee/user/project/integrations/webhooks.html#configure-a-webhook-in-gitlab) in each project or in a group ([Premium Users](https://docs.gitlab.com/ee/user/project/integrations/webhooks.html#group-webhooks)) to be integrated with OpenProject.

You need to configure just two things in the webhook:

1. The URL must point to your OpenProject server’s GitLab webhook endpoint (/webhooks/gitlab). Append it to the URL as a simple GET parameter named key. In the end the URL should look something like this:

```
http://openproject-url.com/webhooks/gitlab?key=[previous_generated_access_token_key]
```

2. Enable the required triggers:
1. Push events (project hooks or systems hooks)
2. Comments
3. Issues events
4. Merge request events
5. Pipeline events

Now the integration is set up on both sides and you can use it.

> **Note:** If you are installing and configuring OpenProject on the same server as GitLab you will need to enable in Gitlab the option [`Allow requests to the local network from web hooks and services`](https://docs.gitlab.com/ee/security/webhooks.html#allow-requests-to-the-local-network-from-webhooks-and-integrations) so that it can send the data locally to the OpenProject webhook since they will be on the same machine.

## How to report bugs or issues

Any error, bug or issue can be reported by creating a new [issue](https://github.com/btey/openproject-gitlab-integration/issues/new).
86 changes: 86 additions & 0 deletions modules/gitlab_integration/app/models/gitlab_issue.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#-- encoding: UTF-8

#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2023 Ben Tey
#
# 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
# Copyright (C) 2012-2021 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
# 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 docs/COPYRIGHT.rdoc for more details.
#++

class GitlabIssue < ApplicationRecord
LABEL_KEYS = %w[color title].freeze

has_and_belongs_to_many :work_packages
belongs_to :gitlab_user, optional: true

enum state: {
opened: 'opened',
closed: 'closed'
}

validates_presence_of :gitlab_html_url,
:number,
:repository,
:state,
:title,
:gitlab_updated_at
validates_presence_of :body,
unless: :partial?
validate :validate_labels_schema

scope :without_work_package, -> { where.missing(:work_packages) }

def self.find_by_gitlab_identifiers(id: nil, url: nil, initialize: false)
raise ArgumentError, "needs an id or an url" if id.nil? && url.blank?

found = where(gitlab_id: id).or(where(gitlab_html_url: url)).take

if found
found
elsif initialize
new(gitlab_id: id, gitlab_html_url: url)
end
end

def partial?
[body].all?(&:nil?)
end

private

def validate_labels_schema
return if labels.nil?
return if labels.all? { |label| label.keys.sort == LABEL_KEYS }

errors.add(:labels, 'invalid schema')
end

def with_logging
yield if block_given?
rescue StandardError => e
Rails.logger.error "Error at gitlab issue: #{e} #{e.message}"
raise e
end
end
Loading
Loading