|
6 | 6 | # See https://aboutcode.org for more information about AboutCode FOSS projects. |
7 | 7 | # |
8 | 8 |
|
9 | | -from django.conf import settings |
| 9 | +import re |
10 | 10 |
|
11 | | -import requests |
| 11 | +from workflow.integrations.base import BaseIntegration |
| 12 | +from workflow.integrations.github import GitHubIntegration |
| 13 | +from workflow.integrations.gitlab import GitLabIntegration |
| 14 | +from workflow.integrations.jira import JiraIntegration |
12 | 15 |
|
13 | | -DEJACODE_SITE_URL = settings.SITE_URL.rstrip("/") |
| 16 | +__all__ = [ |
| 17 | + "BaseIntegration", |
| 18 | + "GitHubIntegration", |
| 19 | + "GitLabIntegration", |
| 20 | + "JiraIntegration", |
| 21 | + "is_valid_issue_tracker_id", |
| 22 | + "get_class_for_tracker", |
| 23 | + "get_class_for_platform", |
| 24 | +] |
14 | 25 |
|
15 | 26 |
|
16 | | -class BaseIntegration: |
17 | | - """Base class for managing issue tracker integrations from DejaCode requests.""" |
| 27 | +GITHUB_PATTERN = re.compile(r"^https://github\.com/[^/]+/[^/]+/?$") |
18 | 28 |
|
19 | | - default_timeout = 10 |
| 29 | +GITLAB_PATTERN = re.compile(r"^https://gitlab\.com/[^/]+/[^/]+/?$") |
20 | 30 |
|
21 | | - def __init__(self, dataspace): |
22 | | - if not dataspace: |
23 | | - raise ValueError("Dataspace must be provided.") |
24 | | - self.dataspace = dataspace |
25 | | - self.session = self.get_session() |
| 31 | +JIRA_PATTERN = re.compile( |
| 32 | + r"^https://[a-zA-Z0-9.-]+\.atlassian\.net(?:/[^/]+)*" |
| 33 | + r"/(?:projects|browse)/[A-Z][A-Z0-9]+(?:/[^/]*)*/*$" |
| 34 | +) |
26 | 35 |
|
27 | | - def get_session(self): |
28 | | - session = requests.Session() |
29 | | - session.headers.update(self.get_headers()) |
30 | | - return session |
| 36 | +ISSUE_TRACKER_PATTERNS = [ |
| 37 | + GITHUB_PATTERN, |
| 38 | + GITLAB_PATTERN, |
| 39 | + JIRA_PATTERN, |
| 40 | +] |
31 | 41 |
|
32 | | - def get_headers(self): |
33 | | - """ |
34 | | - Return authentication headers specific to the integration. |
35 | | - Must be implemented in subclasses. |
36 | | - """ |
37 | | - raise NotImplementedError |
38 | 42 |
|
39 | | - @staticmethod |
40 | | - def make_issue_title(request): |
41 | | - return f"[DEJACODE] {request.title}" |
| 43 | +def is_valid_issue_tracker_id(issue_tracker_id): |
| 44 | + return any(pattern.match(issue_tracker_id) for pattern in ISSUE_TRACKER_PATTERNS) |
42 | 45 |
|
43 | | - @staticmethod |
44 | | - def make_issue_body(request): |
45 | | - request_url = f"{DEJACODE_SITE_URL}{request.get_absolute_url()}" |
46 | | - label_fields = [ |
47 | | - ("📝 Request Template", request.request_template), |
48 | | - ("📦 Product Context", request.product_context), |
49 | | - ("📌 Applies To", request.content_object), |
50 | | - ("🙋 Submitted By", request.requester), |
51 | | - ("👤 Assigned To", request.assignee), |
52 | | - ("🚨 Priority", request.priority), |
53 | | - ("🗒️ Notes", request.notes), |
54 | | - ("🔗️ DejaCode URL", request_url), |
55 | | - ] |
56 | 46 |
|
57 | | - lines = [] |
58 | | - for label, value in label_fields: |
59 | | - if value: |
60 | | - lines.append(f"### {label}\n{value}") |
| 47 | +def get_class_for_tracker(issue_tracker_id): |
| 48 | + if "github.com" in issue_tracker_id: |
| 49 | + return GitHubIntegration |
| 50 | + elif "gitlab.com" in issue_tracker_id: |
| 51 | + return GitLabIntegration |
| 52 | + elif "atlassian.net" in issue_tracker_id: |
| 53 | + return JiraIntegration |
61 | 54 |
|
62 | | - lines.append("----") |
63 | 55 |
|
64 | | - for question in request.get_serialized_data_as_list(): |
65 | | - label = question.get("label") |
66 | | - value = question.get("value") |
67 | | - input_type = question.get("input_type") |
68 | | - |
69 | | - if input_type == "BooleanField": |
70 | | - value = "Yes" if str(value).lower() in ("1", "true", "yes") else "No" |
71 | | - |
72 | | - lines.append(f"### {label}\n{value}") |
73 | | - |
74 | | - return "\n\n".join(lines) |
| 56 | +def get_class_for_platform(platform): |
| 57 | + return { |
| 58 | + "github": GitHubIntegration, |
| 59 | + "gitlab": GitLabIntegration, |
| 60 | + "jira": JiraIntegration, |
| 61 | + }.get(platform) |
0 commit comments