feat(signals.py): add gitea repo to team

This commit is contained in:
2026-05-05 12:44:01 +02:00
parent fa942809d9
commit a9d7aef6dd

View File

@@ -117,6 +117,7 @@ def create_gitea_repo_on_lesson_creation(
logger.debug("GITEA_URL is not set, skipping Gitea repository creation")
return
def create_repository() -> None:
# check if repository already exists
try:
response = requests.get(
@@ -169,3 +170,45 @@ def create_gitea_repo_on_lesson_creation(
f"Failed to create Gitea repository for lesson {instance.title}: {e}\n{response.text}",
e,
)
def add_repository_to_team() -> None:
# add repository to course team
team_name = f"course-{course.id}"
try:
response = requests.get(
f"{api_url}/orgs/{GITEA_ORG_NAME}/teams",
timeout=5,
headers={"Authorization": f"token {os.getenv('GITEA_API_TOKEN')}"},
)
response.raise_for_status()
teams = response.json()
team = next((team for team in teams if team["name"] == team_name), None)
if not team:
logger.error(
f"Gitea team {team_name} not found when trying to add repository {repo_name}"
)
return
team_id = team["id"]
add_repo_url = (
f"{api_url}/teams/{team_id}/repos/{GITEA_ORG_NAME}/{repo_name}"
)
add_response = requests.put(
add_repo_url,
timeout=5,
headers={"Authorization": f"token {os.getenv('GITEA_API_TOKEN')}"},
)
add_response.raise_for_status()
logger.info(
f"Successfully added repository {repo_name} to Gitea team {team_name}"
)
except Exception as e:
logger.exception(
f"Failed to add repository {repo_name} to Gitea team {team_name}: {e}\n{response.text}",
e,
)
create_repository()
add_repository_to_team()