diff --git a/build.sh b/build.sh index bc43e40..5c90977 100644 --- a/build.sh +++ b/build.sh @@ -1,5 +1,5 @@ # Image Settings imageName=dgp-github-bot -imageVersion=1.11 +imageVersion=1.12 docker build --no-cache -f Dockerfile -t $imageName:$imageVersion --target runtime . \ No newline at end of file diff --git a/issue.py b/issue.py index 9801e83..2bf766e 100644 --- a/issue.py +++ b/issue.py @@ -145,7 +145,23 @@ async def issue_handler(payload: dict): if author_association.lower() not in ["member", "owner"]: result += close_issue(repo_name, issue_number, "not_planned") result += block_user_from_organization(payload["repository"]["owner"]["login"], sender_name) - return result + return result + else: + # Start pre-publish process + print("Publish issue found") + upcoming_version_number = re.search(r"(?<=\[Publish]: Version )(?P\d+\.\d+\.\d+)", issue_title) + upcoming_version_number = upcoming_version_number.group("V") + print("Upcoming version number: ", upcoming_version_number) + new_ms_return = create_new_milestone(repo_name, upcoming_version_number, + f"milestone for version {upcoming_version_number}") + new_ms_number = json.loads(new_ms_return)["number"] + print("New milestone created, number: ", new_ms_number) + publishing_issues = get_issue_with_label(repo_name, "等待发布") + print(f"{len(publishing_issues)} publishing issues found, start processing") + for issue in publishing_issues: + this_issue_number = issue["number"] + result += update_issue_milestone(repo_name, this_issue_number, new_ms_number) + update_issue_milestone(repo_name, issue_number, new_ms_number) # Bad title issue processor if bad_title_checker(issue_title): @@ -154,10 +170,11 @@ async def issue_handler(payload: dict): result += make_issue_comment(repo_name, issue_number, f"@{sender_name} Please edit the issue is set a " f"proper title") else: - result += make_issue_comment(repo_name, issue_number, f"@{sender_name} 请通过编辑功能设置一个合适的标题") + result += make_issue_comment(repo_name, issue_number, + f"@{sender_name} 请通过编辑功能设置一个合适的标题") result += close_issue(repo_name, issue_number, "not_planned") result += add_issue_label(repo_name, issue_number, ["需要更多信息"]) - #result += lock_issue_conversation(repo_name, issue_number) + # result += lock_issue_conversation(repo_name, issue_number) # Log dump issue processor dumped_log = log_dump(payload["issue"]["body"]) if dumped_log["code"] != 0: @@ -231,7 +248,8 @@ async def issue_handler(payload: dict): issue_labels = get_issue_label(repo_name, issue_number) # Condition Checker for bot_comment in bot_comments: - if "请通过编辑功能设置一个合适的标题" in bot_comment["body"] or "Please edit the issue is set a proper title" in bot_comment["body"]: + if ("请通过编辑功能设置一个合适的标题" in bot_comment["body"] or "Please edit the issue is set a proper title" + in bot_comment["body"]): had_bad_title = True if "标题已经修改" in bot_comment["body"] or "Title is fixed" in bot_comment["body"]: bad_title_fixed_before = True @@ -240,7 +258,7 @@ async def issue_handler(payload: dict): # Action if had_bad_title and not bad_title_fixed_before: if not bad_title_checker(payload["issue"]["title"]): - #result += unlock_issue_conversation(repo_name, issue_number) + # result += unlock_issue_conversation(repo_name, issue_number) result += reopen_issue(repo_name, issue_number) if is_eng: result += make_issue_comment(repo_name, issue_number, "Title is fixed") diff --git a/main.py b/main.py index 436994e..a0d00ee 100644 --- a/main.py +++ b/main.py @@ -21,25 +21,28 @@ async def payload(request: Request): request_payload = await request.json() print(f"Receiving: Hook type: {hook_type}") - if hook_type == "ping": - result = {"message": "pong"} - elif hook_type == "issues": - result = await issue_handler(request_payload) - elif hook_type == "push": - result = await push_handler(request_payload) - elif hook_type == "release": - result = await release_handler(request_payload) - elif hook_type == "issue_comment": - result = await comment_handler(request_payload) - else: - print("Unknown hook type") + try: + if hook_type == "ping": + result = {"message": "pong"} + elif hook_type == "issues": + result = await issue_handler(request_payload) + elif hook_type == "push": + result = await push_handler(request_payload) + elif hook_type == "release": + result = await release_handler(request_payload) + elif hook_type == "issue_comment": + result = await comment_handler(request_payload) + else: + print("Unknown hook type") + except Exception as e: + result = await add_discord_message({"message": f"Error: {e}"}) + return {"message": str(result)} return {"message": str(result)} @app.post("/private") async def private_send(request: Request): - result = None request_payload = await request.json() hook_type = request_payload["type"] @@ -49,6 +52,8 @@ async def private_send(request: Request): result = await add_discord_message(request_payload["data"]) elif hook_type == "get_discord_message": result = await get_discord_message() + else: + return {"message": "Unknown hook type"} return {"message": str(result)} diff --git a/operater.py b/operater.py index 1ffaa08..2a32647 100644 --- a/operater.py +++ b/operater.py @@ -248,3 +248,29 @@ def block_user_from_organization(org_name: str, username: str) -> str: url = f"https://api.github.com/orgs/{org_name}/blocks/{username}" response = github_request(url, "PUT") return response + + +def create_new_milestone(repo_name: str, milestone_name: str, desc: str = None) -> str: + url = f"https://api.github.com/repos/{repo_name}/milestones" + data = {"title": milestone_name, "description": str(desc)} + response = github_request(url, "POST", data) + return response + + +def update_milestone(repo_name: str, milestone_number: int, body: dict) -> str: + url = f"https://api.github.com/repos/{repo_name}/milestones/{milestone_number}" + response = github_request(url, "PATCH", body) + return response + + +def list_repo_milestones(repo_name: str) -> str: + url = f"https://api.github.com/repos/{repo_name}/milestones" + response = github_request(url, "GET") + return response + + +def update_issue_milestone(repo_name: str, issue_number: int, milestone_number: int) -> str: + url = f"https://api.github.com/repos/{repo_name}/issues/{issue_number}" + data = {"milestone": milestone_number} + response = github_request(url, "PATCH", data) + return response diff --git a/release.py b/release.py index 4540b97..ccbff54 100644 --- a/release.py +++ b/release.py @@ -1,10 +1,25 @@ -from operater import get_issue_with_label, make_issue_comment, remove_one_issue_label, get_issue_language, close_issue +import json + +from operater import get_issue_with_label, make_issue_comment, remove_one_issue_label, get_issue_language, close_issue, \ + list_repo_milestones, update_milestone async def notify_issuers(repo_name: str, release_name: str, html_url: str) -> str: return_result = "" all_ready_issue = get_issue_with_label(repo_name, "等待发布") for issue in all_ready_issue: + # Verify milestone + if issue["milestone"] is None: + continue + elif type(issue["milestone"]) is list: + if release_name not in [i["title"] for i in issue["milestone"]]: + continue + elif type(issue["milestone"]) is dict: + if release_name != issue["milestone"]["title"]: + continue + else: + raise Exception("Unknown milestone type") + issue_number = issue["number"] issue_language = get_issue_language(repo_name, issue_number) if issue_language == "CHS": @@ -24,6 +39,19 @@ async def notify_issuers(repo_name: str, release_name: str, html_url: str) -> st return return_result +async def close_version_milestone(repo_name: str, release_name: str) -> str: + all_milestones = json.loads(list_repo_milestones(repo_name)) + version_milestone_num = None + for milestone in all_milestones: + if milestone["title"] == release_name: + version_milestone_num = milestone["number"] + if version_milestone_num is None: + return "No milestone found" + else: + result = update_milestone(repo_name, version_milestone_num, {"state": "closed"}) + return result + + async def release_handler(payload: dict) -> str: return_result = "" # Quick return @@ -39,3 +67,5 @@ async def release_handler(payload: dict) -> str: # Notify issuers return_result += await notify_issuers(repo_name, release_name, html_url) + return_result += await close_version_milestone(repo_name, release_name) + return return_result diff --git a/run.sh b/run.sh index 40419f9..9859ad8 100644 --- a/run.sh +++ b/run.sh @@ -1,7 +1,7 @@ # Docker Image Settings imageName=dgp-github-bot containerName=DGP-GitHub-Bot -imageVersion=1.11 +imageVersion=1.12 externalPort=3524 internalPort=8000