Skip to content

Commit 14f0248

Browse files
committed
Remove code duplication #346
Signed-off-by: tdruez <tdruez@nexb.com>
1 parent 1ed282c commit 14f0248

2 files changed

Lines changed: 21 additions & 26 deletions

File tree

workflow/integrations/gitlab.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ def sync(self, request):
4141
external_issue = request.external_issue
4242
if external_issue:
4343
self.update_issue(
44-
project_path=project_path,
44+
repo_id=project_path,
4545
issue_id=external_issue.issue_id,
4646
title=self.make_issue_title(request),
4747
description=self.make_issue_body(request),
4848
state_event="close" if request.is_closed else "reopen",
4949
)
5050
else:
5151
issue = self.create_issue(
52-
project_path=project_path,
52+
repo_id=project_path,
5353
title=self.make_issue_title(request),
5454
description=self.make_issue_body(request),
5555
)
@@ -59,9 +59,9 @@ def sync(self, request):
5959
issue_id=issue["iid"],
6060
)
6161

62-
def create_issue(self, project_path, title, description=""):
62+
def create_issue(self, repo_id, title, description=""):
6363
"""Create a new GitLab issue."""
64-
project_path = requests.utils.quote(project_path, safe="")
64+
project_path = requests.utils.quote(repo_id, safe="")
6565
url = f"{self.api_url}/projects/{project_path}/issues"
6666
data = {"title": title, "description": description}
6767

@@ -73,9 +73,9 @@ def create_issue(self, project_path, title, description=""):
7373
response.raise_for_status()
7474
return response.json()
7575

76-
def update_issue(self, project_path, issue_id, title=None, description=None, state_event=None):
76+
def update_issue(self, repo_id, issue_id, title=None, description=None, state_event=None):
7777
"""Update an existing GitLab issue."""
78-
project_path = requests.utils.quote(project_path, safe="")
78+
project_path = requests.utils.quote(repo_id, safe="")
7979
url = f"{self.api_url}/projects/{project_path}/issues/{issue_id}"
8080
data = {}
8181
if title:
@@ -93,9 +93,9 @@ def update_issue(self, project_path, issue_id, title=None, description=None, sta
9393
response.raise_for_status()
9494
return response.json()
9595

96-
def post_comment(self, project_path, issue_id, comment_body):
96+
def post_comment(self, repo_id, issue_id, comment_body):
9797
"""Post a comment on an existing GitLab issue."""
98-
project_path = requests.utils.quote(project_path, safe="")
98+
project_path = requests.utils.quote(repo_id, safe="")
9999
url = f"{self.api_url}/projects/{project_path}/issues/{issue_id}/notes"
100100
data = {"body": comment_body}
101101

workflow/models.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939
from dje.models import HistoryDateFieldsMixin
4040
from dje.models import HistoryFieldsMixin
4141
from dje.models import get_unsecured_manager
42-
from workflow.integrations.gitlab import GitLabIntegration
4342
from workflow.integrations.github import GitHubIntegration
43+
from workflow.integrations.gitlab import GitLabIntegration
4444
from workflow.notification import request_comment_slack_payload
4545
from workflow.notification import request_slack_payload
4646

@@ -136,6 +136,13 @@ def icon_css_class(self):
136136
}
137137
return platform_icons.get(self.platform, "fa-solid fa-square-up-right")
138138

139+
@property
140+
def integration_class(self):
141+
if self.platform == self.Platform.GITHUB:
142+
return GitHubIntegration
143+
elif self.platform == self.Platform.GITLAB:
144+
return GitLabIntegration
145+
139146

140147
class RequestQuerySet(DataspacedQuerySet):
141148
BASE_SELECT_RELATED = [
@@ -618,7 +625,7 @@ def handle_integrations(self):
618625

619626
if "github.com" in issue_tracker_id:
620627
GitHubIntegration(dataspace=self.dataspace).sync(request=self)
621-
if "gitlab.com" in issue_tracker_id:
628+
elif "gitlab.com" in issue_tracker_id:
622629
GitLabIntegration(dataspace=self.dataspace).sync(request=self)
623630

624631

@@ -693,18 +700,12 @@ def handle_integrations(self):
693700
if not self.event_type == self.CLOSED:
694701
return
695702

696-
if external_issue.platform == ExternalIssueLink.Platform.GITHUB:
697-
GitHubIntegration(dataspace=self.dataspace).post_comment(
703+
if integration_class := external_issue.integration_class:
704+
integration_class(dataspace=self.dataspace).post_comment(
698705
repo_id=external_issue.repo,
699706
issue_id=external_issue.issue_id,
700707
comment_body=self.text,
701708
)
702-
elif external_issue.platform == ExternalIssueLink.Platform.GITLAB:
703-
GitLabIntegration(dataspace=self.dataspace).post_comment(
704-
project_path=external_issue.repo,
705-
issue_id=external_issue.issue_id,
706-
comment_body=self.text,
707-
)
708709

709710

710711
class RequestComment(AbstractRequestEvent):
@@ -781,18 +782,12 @@ def handle_integrations(self):
781782
if not external_issue:
782783
return
783784

784-
if external_issue.platform == ExternalIssueLink.Platform.GITHUB:
785-
GitHubIntegration(dataspace=self.dataspace).post_comment(
785+
if integration_class := external_issue.integration_class:
786+
integration_class(dataspace=self.dataspace).post_comment(
786787
repo_id=external_issue.repo,
787788
issue_id=external_issue.issue_id,
788789
comment_body=self.text,
789790
)
790-
elif external_issue.platform == ExternalIssueLink.Platform.GITLAB:
791-
GitLabIntegration(dataspace=self.dataspace).post_comment(
792-
project_path=external_issue.repo,
793-
issue_id=external_issue.issue_id,
794-
comment_body=self.text,
795-
)
796791

797792

798793
class RequestTemplateQuerySet(DataspacedQuerySet):

0 commit comments

Comments
 (0)