Skip to content

Commit

Permalink
Version 1.12
Browse files Browse the repository at this point in the history
  • Loading branch information
Masterain98 committed Nov 11, 2023
1 parent 8cc2d93 commit bb89306
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 21 deletions.
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
@@ -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 .
28 changes: 23 additions & 5 deletions issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<V>\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):
Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand Down
31 changes: 18 additions & 13 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand All @@ -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)}

Expand Down
26 changes: 26 additions & 0 deletions operater.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
32 changes: 31 additions & 1 deletion release.py
Original file line number Diff line number Diff line change
@@ -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":
Expand All @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion run.sh
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit bb89306

Please sign in to comment.