From 48cbf4871d653bc5421248daa45d19df9a3e46d7 Mon Sep 17 00:00:00 2001
From: BoYanZh <bomingzh@sjtu.edu.cn>
Date: Tue, 14 Sep 2021 18:15:15 +0800
Subject: [PATCH] feat: more helps

---
 joint_teapot/__main__.py      | 26 +++++++++++++++++++-------
 joint_teapot/teapot.py        |  3 +++
 joint_teapot/workers/git.py   |  5 +++++
 joint_teapot/workers/gitea.py | 11 ++++++++---
 4 files changed, 35 insertions(+), 10 deletions(-)

diff --git a/joint_teapot/__main__.py b/joint_teapot/__main__.py
index 4e0a99f..e715186 100644
--- a/joint_teapot/__main__.py
+++ b/joint_teapot/__main__.py
@@ -11,37 +11,49 @@ app = Typer(add_completion=False)
 teapot = Teapot()
 
 
-@app.command("create-personal")
+@app.command(
+    "invite-to-teams", help="invite all canvas students to gitea teams by team name"
+)
+def add_all_canvas_students_to_teams(team_names: List[str]) -> None:
+    teapot.add_all_canvas_students_to_teams(team_names)
+
+
+@app.command(
+    "create-personal", help="create personal repos on gitea for all canvas students"
+)
 def create_personal_repos_for_all_canvas_students() -> None:
     teapot.create_personal_repos_for_all_canvas_students()
 
 
-@app.command("create-teams")
+@app.command("create-teams", help="create teams on gitea by canvas groups")
 def create_teams_and_repos_by_canvas_groups() -> None:
     teapot.create_teams_and_repos_by_canvas_groups()
 
 
-@app.command("get-public-keys")
+@app.command("get-public-keys", help="get all public keys on gitea")
 def get_public_key_of_all_canvas_students() -> None:
     echo(teapot.get_public_key_of_all_canvas_students())
 
 
-@app.command("archieve")
+@app.command("archieve", help="clone all gitea repos to local")
 def archieve_all_repos() -> None:
     teapot.archieve_all_repos()
 
 
-@app.command("create-issues")
+@app.command("create-issues", help="create issues on gitea")
 def create_issue_for_repos(repo_names: List[str], title: str, body: str) -> None:
     teapot.create_issue_for_repos(repo_names, title, body)
 
 
-@app.command("check-issues")
+@app.command("check-issues", help="check the existence of issue by title on gitea")
 def check_exist_issue_by_title(repo_names: List[str], title: str) -> None:
     echo(teapot.check_exist_issue_by_title(repo_names, title))
 
 
-@app.command("get-release")
+@app.command(
+    "get-release",
+    help="checkout git repo to git tag fetched from gitea by release name, with due date",
+)
 def checkout_to_repos_by_release_name(
     repo_names: List[str], release_name: str, due: datetime = datetime(3000, 1, 1)
 ) -> None:
diff --git a/joint_teapot/teapot.py b/joint_teapot/teapot.py
index 27ef028..62f994e 100644
--- a/joint_teapot/teapot.py
+++ b/joint_teapot/teapot.py
@@ -28,6 +28,9 @@ class Teapot:
             self._git = Git()
         return self._git
 
+    def add_all_canvas_students_to_teams(self, team_names: List[str]) -> None:
+        return self.gitea.add_canvas_students_to_teams(self.canvas.students, team_names)
+
     def create_personal_repos_for_all_canvas_students(self) -> List[str]:
         return self.gitea.create_personal_repos_for_canvas_students(
             self.canvas.students
diff --git a/joint_teapot/workers/git.py b/joint_teapot/workers/git.py
index fdcdafc..aa1df9d 100644
--- a/joint_teapot/workers/git.py
+++ b/joint_teapot/workers/git.py
@@ -1,7 +1,12 @@
 import os
+import sys
 
+current_path = sys.path[0]
+sys.path.remove(current_path)
 from git import Repo
 
+sys.path.insert(0, current_path)
+
 from joint_teapot.config import settings
 
 
diff --git a/joint_teapot/workers/gitea.py b/joint_teapot/workers/gitea.py
index c079715..79db675 100644
--- a/joint_teapot/workers/gitea.py
+++ b/joint_teapot/workers/gitea.py
@@ -47,12 +47,14 @@ class Gitea:
     def _get_team_id_by_name(self, name: str) -> int:
         res = self.organization_api.team_search(self.org_name, q=str(name), limit=1)
         if len(res["data"]) == 0:
-            raise Exception("Team not found by name")
+            raise Exception(f"Team not found by name {name}")
         return res["data"][0]["id"]
 
     @lru_cache()
     def _get_username_by_canvas_student(self, student: User) -> str:
         res = self.user_api.user_search(q=student.sis_login_id, limit=1)
+        if len(res["data"]) == 0:
+            raise Exception(f"User not found by canvas student {student}")
         return res["data"][0]["username"]
 
     def add_canvas_students_to_teams(
@@ -61,8 +63,11 @@ class Gitea:
         for team_name in team_names:
             team_id = self._get_team_id_by_name(team_name)
             for student in students:
-                username = self._get_username_by_canvas_student(student)
-                self.organization_api.org_add_team_member(team_id, username)
+                try:
+                    username = self._get_username_by_canvas_student(student)
+                    self.organization_api.org_add_team_member(team_id, username)
+                except Exception as e:
+                    print(e)
 
     def create_personal_repos_for_canvas_students(
         self,