Add comments from old issue to new issue

This commit is contained in:
benjamin melançon 2023-10-04 15:46:42 -04:00
parent d8cc600aa0
commit be559a0812

View file

@ -10,6 +10,8 @@ FORGEJO_API_TOKEN = os.getenv("FORGEJO_API_TOKEN")
API_GET_ISSUE = "{instance}/api/v1/repos/{owner}/{repo}/issues/{index}" API_GET_ISSUE = "{instance}/api/v1/repos/{owner}/{repo}/issues/{index}"
API_CREATE_ISSUE = "{instance}/api/v1/repos/{owner}/{repo}/issues" API_CREATE_ISSUE = "{instance}/api/v1/repos/{owner}/{repo}/issues"
API_GET_COMMENTS = "{instance}/api/v1/repos/{owner}/{repo}/issues/{index}/comments"
API_CREATE_COMMENT = "{instance}/api/v1/repos/{owner}/{repo}/issues/{index}/comments"
def move_issue(source_owner, source_repo, issue_number, destination_owner, destination_repo): def move_issue(source_owner, source_repo, issue_number, destination_owner, destination_repo):
get_issue_url = API_GET_ISSUE.format(instance=FORGEJO_INSTANCE, owner=source_owner, repo=source_repo, index=issue_number) get_issue_url = API_GET_ISSUE.format(instance=FORGEJO_INSTANCE, owner=source_owner, repo=source_repo, index=issue_number)
@ -38,8 +40,28 @@ def move_issue(source_owner, source_repo, issue_number, destination_owner, desti
message = json_response.pop('message', False) message = json_response.pop('message', False)
if message: if message:
print(message) print(message)
sys.exit("Error creating issue.")
else: else:
print(f"Issue created: {json_response['html_url']}") print(f"Issue created: {json_response['html_url']}")
new_issue_number = json_response['number']
print(f"new_issue_number = {new_issue_number}")
new_issue_id = json_response['id']
print(f"new_issue_id = {new_issue_id}")
print(f"Moving issue comments.")
get_comments_url = API_GET_COMMENTS.format(instance=FORGEJO_INSTANCE, owner=source_owner, repo=source_repo, index=issue_number)
create_comment_url = API_CREATE_COMMENT.format(instance=FORGEJO_INSTANCE, owner=destination_owner, repo=destination_repo, index=new_issue_number)
comments = requests.get(get_comments_url, params={"access_token": FORGEJO_API_TOKEN})
json_comments = comments.json()
for comment in json_comments:
new_comment = requests.post(create_comment_url, headers=headers, data=comment)
json_new_comment = new_comment.json()
message = json_new_comment.pop('message', False)
if message:
print(message)
sys.exit("Error creating comment.")
if __name__=="__main__": if __name__=="__main__":
# '0' comes in as move_issue.py when this is called as `python move_issue.py` # '0' comes in as move_issue.py when this is called as `python move_issue.py`