2023-10-04 17:49:23 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
from dotenv import load_dotenv
|
|
|
|
import requests
|
|
|
|
|
|
|
|
load_dotenv()
|
|
|
|
|
|
|
|
FORGEJO_INSTANCE = os.getenv("FORGEJO_INSTANCE", "https://codeberg.org")
|
|
|
|
FORGEJO_API_TOKEN = os.getenv("FORGEJO_API_TOKEN")
|
|
|
|
|
|
|
|
API_GET_ISSUE = "{instance}/api/v1/repos/{owner}/{repo}/issues/{index}"
|
2023-10-04 20:19:03 +00:00
|
|
|
API_GET_ISSUE = "{instance}/api/v1/repos/{owner}/{repo}/issues/{index}"
|
|
|
|
API_PATCH_ISSUE = "{instance}/api/v1/repos/{owner}/{repo}/issues/{index}"
|
2023-10-04 17:49:23 +00:00
|
|
|
API_CREATE_ISSUE = "{instance}/api/v1/repos/{owner}/{repo}/issues"
|
2023-10-04 19:46:42 +00:00
|
|
|
API_GET_COMMENTS = "{instance}/api/v1/repos/{owner}/{repo}/issues/{index}/comments"
|
|
|
|
API_CREATE_COMMENT = "{instance}/api/v1/repos/{owner}/{repo}/issues/{index}/comments"
|
2023-10-04 17:49:23 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
# print(get_issue_url)
|
|
|
|
response = requests.get(get_issue_url, params={"access_token": FORGEJO_API_TOKEN})
|
|
|
|
json_response = response.json()
|
|
|
|
|
|
|
|
# Not 100% certain this can ever give a message, but create below does on error
|
|
|
|
message = json_response.pop('message', False)
|
|
|
|
if message:
|
|
|
|
print(message)
|
2023-10-04 20:19:03 +00:00
|
|
|
sys.exit("Error getting issue.")
|
|
|
|
else:
|
|
|
|
old_issue_url = json_response['html_url']
|
2023-10-04 17:49:23 +00:00
|
|
|
|
2023-10-04 18:29:19 +00:00
|
|
|
milestone = json_response.pop('milestone', {})
|
|
|
|
if milestone:
|
|
|
|
print(f"We have removed the milestone {milestone['title']} from this issue (milestones are specific to repositories).")
|
|
|
|
# Note that pop has already thrown out the milestone from json_response
|
2023-10-04 17:49:23 +00:00
|
|
|
|
|
|
|
print(f"Creating issue {json_response['title']} now…")
|
|
|
|
|
|
|
|
create_issue_url = API_CREATE_ISSUE.format(instance=FORGEJO_INSTANCE, owner=destination_owner, repo=destination_repo)
|
|
|
|
# print(create_issue_url)
|
|
|
|
headers = {"Authorization": f"token {FORGEJO_API_TOKEN}"}
|
|
|
|
created_response = requests.post(create_issue_url, headers=headers, data=json_response)
|
|
|
|
|
|
|
|
json_response = created_response.json()
|
|
|
|
message = json_response.pop('message', False)
|
|
|
|
if message:
|
|
|
|
print(message)
|
2023-10-04 19:46:42 +00:00
|
|
|
sys.exit("Error creating issue.")
|
2023-10-04 17:49:23 +00:00
|
|
|
else:
|
2023-10-04 20:19:03 +00:00
|
|
|
new_issue_url = json_response['html_url']
|
|
|
|
print(f"Issue created: {new_issue_url}")
|
2023-10-04 19:46:42 +00:00
|
|
|
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.")
|
2023-10-04 17:49:23 +00:00
|
|
|
|
2023-10-04 20:19:03 +00:00
|
|
|
moved_to_message = {
|
|
|
|
'body': f"*Issue moved to:* {new_issue_url}"
|
|
|
|
}
|
|
|
|
moved_from_message = {
|
|
|
|
'body': f"*Issue moved here from:* {old_issue_url}"
|
|
|
|
}
|
|
|
|
new_comment = requests.post(create_comment_url, headers=headers, data=moved_from_message)
|
|
|
|
|
|
|
|
create_old_comment_url = API_CREATE_COMMENT.format(instance=FORGEJO_INSTANCE, owner=source_owner, repo=source_repo, index=issue_number)
|
|
|
|
new_old_comment = requests.post(create_old_comment_url, headers=headers, data=moved_to_message)
|
|
|
|
|
2023-10-04 22:26:43 +00:00
|
|
|
# And finally, close the old issue.
|
|
|
|
# If we are confident the API is remaining stabel we can just use the same path
|
|
|
|
# with patch, post, and get when the path is the same which is the case here.
|
|
|
|
# Note though we often change the owner and repo and index when making the URL!
|
|
|
|
# This runs and the state string is correct and it *should* work but it does not.
|
|
|
|
# See https://codeberg.org/forgejo/forgejo/issues/1280#issuecomment-1262414
|
|
|
|
patch = {
|
|
|
|
# 'title': "MOVED",
|
|
|
|
'state': "closed"
|
|
|
|
}
|
|
|
|
patch_issue_url = API_PATCH_ISSUE.format(instance=FORGEJO_INSTANCE, owner=source_owner, repo=source_repo, index=issue_number)
|
|
|
|
closed_response = requests.patch(patch_issue_url, headers=headers, data=patch)
|
|
|
|
# print(closed_response.json())
|
2023-10-04 20:19:03 +00:00
|
|
|
|
2023-10-04 17:49:23 +00:00
|
|
|
if __name__=="__main__":
|
|
|
|
# '0' comes in as move_issue.py when this is called as `python move_issue.py`
|
|
|
|
source_owner = sys.argv[1]
|
|
|
|
source_repo = sys.argv[2]
|
|
|
|
issue_number = sys.argv[3]
|
|
|
|
destination_owner = sys.argv[4]
|
|
|
|
destination_repo = sys.argv[5]
|
|
|
|
|
|
|
|
move_issue(source_owner, source_repo, issue_number, destination_owner, destination_repo)
|