|
| 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 | +from workflow.integrations.base import BaseIntegration |
| 12 | + |
| 13 | +FORGEJO_API_PATH = "/api/v1" |
| 14 | + |
| 15 | + |
| 16 | +class ForgejoIntegration(BaseIntegration): |
| 17 | + """ |
| 18 | + A class for managing Forgejo issue creation, updates, and comments |
| 19 | + from DejaCode requests. |
| 20 | + """ |
| 21 | + |
| 22 | + open_status = "open" |
| 23 | + closed_status = "closed" |
| 24 | + |
| 25 | + def get_headers(self): |
| 26 | + forgejo_token = self.dataspace.get_configuration("forgejo_token") |
| 27 | + if not forgejo_token: |
| 28 | + raise ValueError("The forgejo_token is not set on the Dataspace.") |
| 29 | + return {"Authorization": f"token {forgejo_token}"} |
| 30 | + |
| 31 | + def sync(self, request): |
| 32 | + """Sync the given request with Forgejo by creating or updating an issue.""" |
| 33 | + try: |
| 34 | + base_url, repo_path = self.extract_forgejo_info( |
| 35 | + request.request_template.issue_tracker_id |
| 36 | + ) |
| 37 | + except ValueError as error: |
| 38 | + raise ValueError(f"Invalid Forgejo tracker URL: {error}") |
| 39 | + |
| 40 | + self.api_url = base_url.rstrip("/") + FORGEJO_API_PATH |
| 41 | + |
| 42 | + external_issue = request.external_issue |
| 43 | + if external_issue: |
| 44 | + self.update_issue( |
| 45 | + repo_id=repo_path, |
| 46 | + issue_id=external_issue.issue_id, |
| 47 | + title=self.make_issue_title(request), |
| 48 | + body=self.make_issue_body(request), |
| 49 | + state=self.closed_status if request.is_closed else self.open_status, |
| 50 | + ) |
| 51 | + else: |
| 52 | + issue = self.create_issue( |
| 53 | + repo_id=repo_path, |
| 54 | + title=self.make_issue_title(request), |
| 55 | + body=self.make_issue_body(request), |
| 56 | + ) |
| 57 | + request.link_external_issue( |
| 58 | + platform="forgejo", |
| 59 | + repo=repo_path, |
| 60 | + issue_id=issue["number"], |
| 61 | + base_url=base_url, |
| 62 | + ) |
| 63 | + |
| 64 | + def create_issue(self, repo_id, title, body=""): |
| 65 | + """Create a new Forgejo issue.""" |
| 66 | + url = f"{self.api_url}/repos/{repo_id}/issues" |
| 67 | + data = { |
| 68 | + "title": title, |
| 69 | + "body": body, |
| 70 | + } |
| 71 | + |
| 72 | + return self.post(url, json=data) |
| 73 | + |
| 74 | + def update_issue(self, repo_id, issue_id, title=None, body=None, state=None): |
| 75 | + """Update an existing Forgejo issue.""" |
| 76 | + url = f"{self.api_url}/repos/{repo_id}/issues/{issue_id}" |
| 77 | + data = {} |
| 78 | + if title: |
| 79 | + data["title"] = title |
| 80 | + if body: |
| 81 | + data["body"] = body |
| 82 | + if state: |
| 83 | + data["state"] = state |
| 84 | + |
| 85 | + return self.patch(url, json=data) |
| 86 | + |
| 87 | + def post_comment(self, repo_id, issue_id, comment_body, base_url=None): |
| 88 | + """Post a comment on an existing Forgejo issue.""" |
| 89 | + url = f"{base_url}{FORGEJO_API_PATH}/repos/{repo_id}/issues/{issue_id}/comments" |
| 90 | + return self.post(url, json={"body": comment_body}) |
| 91 | + |
| 92 | + @staticmethod |
| 93 | + def extract_forgejo_info(url): |
| 94 | + """ |
| 95 | + Extract the Forgejo base domain and repo path (org/repo) from a repo URL. |
| 96 | +
|
| 97 | + Example: |
| 98 | + - https://forgejo.example.org/org/repo → ("https://forgejo.example.org", "org/repo") |
| 99 | +
|
| 100 | + """ |
| 101 | + parsed = urlparse(url) |
| 102 | + if not parsed.netloc: |
| 103 | + raise ValueError("Missing hostname in Forgejo URL.") |
| 104 | + |
| 105 | + base_url = f"{parsed.scheme}://{parsed.netloc}" |
| 106 | + path_parts = [p for p in parsed.path.split("/") if p] |
| 107 | + if len(path_parts) < 2: |
| 108 | + raise ValueError("Incomplete Forgejo repository path.") |
| 109 | + |
| 110 | + repo_path = f"{path_parts[0]}/{path_parts[1]}" |
| 111 | + return base_url, repo_path |
0 commit comments