|
| 1 | +# Copyright Contributors to the Packit project. |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +import requests |
| 5 | + |
| 6 | +from datetime import datetime |
| 7 | +from typing import Any, List, Optional |
| 8 | +from ogr.abstract import CommitFlag, CommitStatus |
| 9 | + |
| 10 | +class ForgejoCommitFlag(CommitFlag): |
| 11 | + """ |
| 12 | + CommitFlag implementation for Forgejo. |
| 13 | + """ |
| 14 | + |
| 15 | + @classmethod |
| 16 | + def _state_from_str(cls, state: str) -> CommitStatus: |
| 17 | + # Convert a status string returned by Forgejo API into CommitStatus enum. |
| 18 | + state = state.lower() |
| 19 | + if state == "success": |
| 20 | + return CommitStatus.success |
| 21 | + elif state == "failure": |
| 22 | + return CommitStatus.failure |
| 23 | + elif state == "pending": |
| 24 | + return CommitStatus.pending |
| 25 | + else: |
| 26 | + raise ValueError(f"Unknown commit state from Forgejo: {state}") |
| 27 | + |
| 28 | + @classmethod |
| 29 | + def _validate_state(cls, state: CommitStatus) -> CommitStatus: |
| 30 | + # Validate that the provided state is acceptable for Forgejo. |
| 31 | + valid_states = {CommitStatus.success, CommitStatus.failure, CommitStatus.pending} |
| 32 | + if state in valid_states: |
| 33 | + return state |
| 34 | + raise ValueError(f"Invalid commit state for Forgejo: {state}") |
| 35 | + |
| 36 | + def _from_raw_commit_flag(self) -> None: |
| 37 | + """ |
| 38 | + Populate attributes from the raw commit flag data obtained from Forgejo's API. |
| 39 | + Expected keys in self._raw_commit_flag: 'commit', 'state', 'context', 'comment', 'id', |
| 40 | + 'created', and 'updated'. |
| 41 | + """ |
| 42 | + raw = self._raw_commit_flag |
| 43 | + self.commit = raw.get("commit") |
| 44 | + self.state = self._state_from_str(raw.get("state", "pending")) |
| 45 | + self.context = raw.get("context") |
| 46 | + self.comment = raw.get("comment") |
| 47 | + self.uid = raw.get("id") |
| 48 | + self.url = raw.get("url") |
| 49 | + # Parse timestamps in ISO8601 format (adjust format if needed) |
| 50 | + self._created = datetime.strptime(raw.get("created"), "%Y-%m-%dT%H:%M:%SZ") |
| 51 | + self._edited = datetime.strptime(raw.get("updated"), "%Y-%m-%dT%H:%M:%SZ") |
| 52 | + |
| 53 | + @staticmethod |
| 54 | + def get(project: Any, commit: str) -> List["CommitFlag"]: |
| 55 | + """ |
| 56 | + Retrieve commit statuses for the given commit from Forgejo. |
| 57 | + This method should use Forgejo's API to fetch statuses. |
| 58 | + """ |
| 59 | + # Construct the URL using the project's forge_api_url, owner, repo, and commit hash. |
| 60 | + url = f"{project.forge_api_url}/repos/{project.owner}/{project.repo}/commits/{commit}/statuses" |
| 61 | + headers = project.get_auth_header() # Get auth headers from project config |
| 62 | + response = requests.get(url, headers=headers) |
| 63 | + flags = [] |
| 64 | + for raw_flag in response.json(): |
| 65 | + flags.append(ForgejoCommitFlag(raw_commit_flag=raw_flag, project=project, commit=commit)) |
| 66 | + return flags |
| 67 | + |
| 68 | + @staticmethod |
| 69 | + def set( |
| 70 | + project: Any, |
| 71 | + commit: str, |
| 72 | + state: CommitStatus, |
| 73 | + target_url: str, |
| 74 | + description: str, |
| 75 | + context: str, |
| 76 | + ) -> "CommitFlag": |
| 77 | + """ |
| 78 | + Set a new commit status on Forgejo via its API. |
| 79 | + """ |
| 80 | + url = f"{project.forge_api_url}/repos/{project.owner}/{project.repo}/commits/{commit}/statuses" |
| 81 | + payload = { |
| 82 | + "state": state.name.lower(), |
| 83 | + "target_url": target_url, |
| 84 | + "description": description, |
| 85 | + "context": context, |
| 86 | + } |
| 87 | + headers = project.get_auth_header() |
| 88 | + response = requests.post(url, json=payload, headers=headers) |
| 89 | + return ForgejoCommitFlag(raw_commit_flag=response.json(), project=project, commit=commit) |
| 90 | + |
| 91 | + @property |
| 92 | + def created(self) -> datetime: |
| 93 | + return self._created |
| 94 | + |
| 95 | + @property |
| 96 | + def edited(self) -> datetime: |
| 97 | + return self._edited |
0 commit comments