|
| 1 | +# |
| 2 | +# Copyright (c) nexB Inc. and others. All rights reserved. |
| 3 | +# DejaCode is a trademark of nexB Inc. |
| 4 | +# SPDX-License-Identifier: AGPL-3.0-only |
| 5 | +# See https://github.com/aboutcode-org/dejacode for support or download. |
| 6 | +# See https://aboutcode.org for more information about AboutCode FOSS projects. |
| 7 | +# |
| 8 | + |
| 9 | +from urllib.parse import urlparse |
| 10 | + |
| 11 | +import requests |
| 12 | + |
| 13 | +from workflow.integrations import BaseIntegration |
| 14 | + |
| 15 | +GITLAB_API_URL = "https://gitlab.com/api/v4" |
| 16 | + |
| 17 | + |
| 18 | +class GitLabIntegration(BaseIntegration): |
| 19 | + """ |
| 20 | + A class for managing GitLab issue creation, updates, and comments |
| 21 | + from DejaCode requests. |
| 22 | + """ |
| 23 | + |
| 24 | + api_url = GITLAB_API_URL |
| 25 | + |
| 26 | + def get_headers(self): |
| 27 | + gitlab_token = self.dataspace.get_configuration(field_name="gitlab_token") |
| 28 | + if not gitlab_token: |
| 29 | + raise ValueError("The gitlab_token is not set on the Dataspace.") |
| 30 | + return {"PRIVATE-TOKEN": gitlab_token} |
| 31 | + |
| 32 | + def sync(self, request): |
| 33 | + """Sync the given request with GitLab by creating or updating an issue.""" |
| 34 | + try: |
| 35 | + project_path = self.extract_gitlab_project_path( |
| 36 | + request.request_template.issue_tracker_id |
| 37 | + ) |
| 38 | + except ValueError as error: |
| 39 | + raise ValueError(f"Invalid GitLab project URL: {error}") |
| 40 | + |
| 41 | + external_issue = request.external_issue |
| 42 | + if external_issue: |
| 43 | + self.update_issue( |
| 44 | + project_path=project_path, |
| 45 | + issue_id=external_issue.issue_id, |
| 46 | + title=self.make_issue_title(request), |
| 47 | + description=self.make_issue_body(request), |
| 48 | + state_event="close" if request.is_closed else "reopen", |
| 49 | + ) |
| 50 | + else: |
| 51 | + issue = self.create_issue( |
| 52 | + project_path=project_path, |
| 53 | + title=self.make_issue_title(request), |
| 54 | + description=self.make_issue_body(request), |
| 55 | + ) |
| 56 | + request.link_external_issue( |
| 57 | + platform="gitlab", |
| 58 | + repo=project_path, |
| 59 | + issue_id=issue["iid"], |
| 60 | + ) |
| 61 | + |
| 62 | + def create_issue(self, project_path, title, description=""): |
| 63 | + """Create a new GitLab issue.""" |
| 64 | + project_path = requests.utils.quote(project_path, safe="") |
| 65 | + url = f"{self.api_url}/projects/{project_path}/issues" |
| 66 | + data = {"title": title, "description": description} |
| 67 | + |
| 68 | + response = self.session.post( |
| 69 | + url, |
| 70 | + json=data, |
| 71 | + timeout=self.default_timeout, |
| 72 | + ) |
| 73 | + response.raise_for_status() |
| 74 | + return response.json() |
| 75 | + |
| 76 | + def update_issue(self, project_path, issue_id, title=None, description=None, state_event=None): |
| 77 | + """Update an existing GitLab issue.""" |
| 78 | + project_path = requests.utils.quote(project_path, safe="") |
| 79 | + url = f"{self.api_url}/projects/{project_path}/issues/{issue_id}" |
| 80 | + data = {} |
| 81 | + if title: |
| 82 | + data["title"] = title |
| 83 | + if description: |
| 84 | + data["description"] = description |
| 85 | + if state_event: |
| 86 | + data["state_event"] = state_event |
| 87 | + |
| 88 | + response = self.session.put( |
| 89 | + url, |
| 90 | + json=data, |
| 91 | + timeout=self.default_timeout, |
| 92 | + ) |
| 93 | + response.raise_for_status() |
| 94 | + return response.json() |
| 95 | + |
| 96 | + def post_comment(self, project_path, issue_id, comment_body): |
| 97 | + """Post a comment on an existing GitLab issue.""" |
| 98 | + project_path = requests.utils.quote(project_path, safe="") |
| 99 | + url = f"{self.api_url}/projects/{project_path}/issues/{issue_id}/notes" |
| 100 | + data = {"body": comment_body} |
| 101 | + |
| 102 | + response = self.session.post( |
| 103 | + url, |
| 104 | + json=data, |
| 105 | + timeout=self.default_timeout, |
| 106 | + ) |
| 107 | + response.raise_for_status() |
| 108 | + return response.json() |
| 109 | + |
| 110 | + @staticmethod |
| 111 | + def extract_gitlab_project_path(url): |
| 112 | + """Extract 'namespace/project' from a GitLab URL.""" |
| 113 | + parsed = urlparse(url) |
| 114 | + if "gitlab.com" not in parsed.netloc: |
| 115 | + raise ValueError("URL does not point to GitLab.") |
| 116 | + |
| 117 | + path_parts = [part for part in parsed.path.split("/") if part] |
| 118 | + if len(path_parts) < 2: |
| 119 | + raise ValueError("Incomplete GitLab project path.") |
| 120 | + |
| 121 | + return "/".join(path_parts[:2]) |
0 commit comments