Source code for ferrosoft.apps.ticketing.services.taiga_project
# Copyright (c) 2025 Ferrosoft GmbH. All rights reserved.
import requests
from ferrosoft.apps.ticketing.models import TaigaProject
from ferrosoft.apps.ticketing.services.ticket import Config
[docs]
class ProjectSetupService:
def __init__(self, config: Config, auth_token: str | None = None):
self.config = config
self.auth_token = auth_token
[docs]
@classmethod
def get_instance(cls):
config = Config.from_django_settings()
return cls(config, config.authenticate())
def _auth_headers(self):
if self.auth_token is None:
raise RuntimeError("No auth token provided.")
return {
"Authorization": "Bearer %s" % self.auth_token,
}
def _fetch_project(self, project_id):
project_url = "%s/projects/%s" % (self.config.base_url, project_id)
response = requests.get(project_url, headers=self._auth_headers())
if response.status_code != 200:
raise RuntimeError(
"Failed to fetch project details for ID %s. Response: %s"
% (project_id, response.status_code)
)
return response.json()
[docs]
def save_project(self, project_id):
project_data = self._fetch_project(project_id)
project, created = TaigaProject.objects.get_or_create(
project_id=project_data["id"],
defaults={"name": project_data["name"]},
)
return project, created
[docs]
def setup_attributes(self, project, attribute_type):
url = "%s/%s?project=%s" % (
self.config.base_url,
attribute_type,
project.project_id,
)
response = requests.get(url, headers=self._auth_headers())
if response.status_code != 200:
raise RuntimeError(
"Failed to fetch %s for project ID %s. Response: %s"
% (attribute_type, project.project_id, response.status_code)
)
attributes = {attr["name"]: attr["id"] for attr in response.json()}
if attribute_type == "priorities" and not project.priorities:
project.priorities = attributes
elif attribute_type == "issue-types" and not project.types:
project.types = attributes
project.save()