feat(signals.py): add gitea repo to team
This commit is contained in:
@@ -117,6 +117,7 @@ def create_gitea_repo_on_lesson_creation(
|
|||||||
logger.debug("GITEA_URL is not set, skipping Gitea repository creation")
|
logger.debug("GITEA_URL is not set, skipping Gitea repository creation")
|
||||||
return
|
return
|
||||||
|
|
||||||
|
def create_repository() -> None:
|
||||||
# check if repository already exists
|
# check if repository already exists
|
||||||
try:
|
try:
|
||||||
response = requests.get(
|
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}",
|
f"Failed to create Gitea repository for lesson {instance.title}: {e}\n{response.text}",
|
||||||
e,
|
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()
|
||||||
|
|||||||
Reference in New Issue
Block a user