Take one at an import issues script

This commit is contained in:
benjamin melançon 2024-05-15 15:54:16 -04:00
parent f1777d2c18
commit 24a99ee2d4

57
import_issues.py Normal file
View file

@ -0,0 +1,57 @@
import csv
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}"
API_GET_ISSUE = "{instance}/api/v1/repos/{owner}/{repo}/issues/{index}"
API_PATCH_ISSUE = "{instance}/api/v1/repos/{owner}/{repo}/issues/{index}"
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 import_issues(owner, repo, csv_filename):
issues = []
with open(csv_filename) as csvfile:
rows = csv.reader(csvfile)
rows.pop(0)
for row in rows:
issues.append(row[0])
print(f"Creating issue {json_response['title']} now…")
for issue in issues:
new_issue = {
"title": issue
}
create_issue_url = API_CREATE_ISSUE.format(instance=FORGEJO_INSTANCE, owner=owner, repo=repo)
# print(create_issue_url)
headers = {"Authorization": f"token {FORGEJO_API_TOKEN}"}
created_response = requests.post(create_issue_url, headers=headers, json=new_issue)
json_response = created_response.json()
message = json_response.pop('message', False)
if message:
print(message)
sys.exit("Error creating issue.")
else:
new_issue_url = json_response['html_url']
print(f"Issue created: {new_issue_url}")
if __name__=="__main__":
# '0' comes in as move_issue.py when this is called as `python move_issue.py`
owner = sys.argv[1]
repo = sys.argv[2]
csv_filename = sys.argv[2]
import_issues(owner, repo, csvfile)