@@ 0,0 1,144 @@
+import json
+from urllib.parse import urljoin
+
+import requests
+
+SR_PS = "NOTREALLYMYKEY"
+
+SR_KEY = SR_PS
+TR_NAME = "django-impersonate"
+TR_DESC = "Simple app to allow superusers to login as other (non-superuser) accounts via a quick user switch process."
+
+SR_ST_MAP = {
+ "new": "reported",
+ "open": "confirmed",
+ "resolved": "resolved",
+ "closed": "resolved",
+ "on hold": "pending",
+ "invalid": "resolved",
+ "duplicate": "resolved",
+ "wontfix": "resolved",
+}
+
+SR_RES_MAP = {
+ "new": "unresolved",
+ "open": "unresolved",
+ "resolved": "fixed",
+ "closed": "fixed",
+ "on hold": "unresolved",
+ "invalid": "invalid",
+ "duplicate": "duplicate",
+ "wontfix": "wont_fix",
+}
+
+
+def todo_request(method, path, data=None):
+ headers = {
+ "Authorization": f"token {SR_KEY}",
+ "Content-Type": "application/json",
+ }
+ url = urljoin("https://todo.code.netlandish.com", path)
+ res = requests.request(
+ method.upper(), url, json=data, headers=headers
+ ).json()
+ if "errors" in res and res["errors"]:
+ print("Error running task...\n\n")
+ print("Sent the following\n\n")
+ print(data)
+ print("\n\nErrors...\n\n")
+ print(res["errors"])
+ print("\n\n")
+ print(res)
+ input("\n\nCtrl-C to quit or any key to continue...\n")
+ return res
+
+
+def parse_tickets(data):
+ def get_comments(data, tid):
+ comments = []
+ for comment in data:
+ if comment["issue"] == tid:
+ c = {
+ "comment": comment["content"] or "No Message",
+ "created": comment["created_on"],
+ }
+ if comment["user"]:
+ c[
+ "external_id"
+ ] = f"bitbucket:{comment['user']['display_name']}"
+ c["external_url"] = (
+ f"https://bitbucket.org/"
+ f"{{{comment['user']['account_id']}}}/"
+ )
+ if len(c["comment"]) > 16384:
+ c["comment"] = c["comment"][:16380]
+ if len(c["comment"]) < 3:
+ c["comment"] = c["comment"] + " ..."
+ comments.append(c)
+ return sorted(comments, key=lambda x: x["created"])
+
+ issues = []
+ for issue in data["issues"]:
+ i = {
+ "title": issue["title"],
+ "created": issue["created_on"],
+ }
+ if issue["content"]:
+ i["description"] = issue["content"]
+ if issue["reporter"]:
+ i["external_id"] = f"bitbucket:{issue['reporter']['display_name']}"
+ i["external_url"] = (
+ f"https://bitbucket.org/"
+ f"{{{issue['reporter']['account_id']}}}/"
+ )
+ i["event"] = {
+ "status": SR_ST_MAP[issue["status"]],
+ "resolution": SR_RES_MAP[issue["status"]],
+ }
+ i["comments"] = get_comments(data["comments"], issue["id"])
+ issues.append(i)
+ return sorted(issues, key=lambda x: x["created"])
+
+
+def main():
+ data = json.load(open("db-2.0.json"))
+ issues = parse_tickets(data)
+ print(json.dumps(issues, indent=4))
+
+ # Create tracker
+ print(f"\n\nCreating tracker {TR_NAME}")
+ tr = {
+ "name": TR_NAME,
+ "description": TR_DESC,
+ }
+ res = todo_request("POST", "/api/trackers", tr)
+ print(res)
+
+ # Create issues
+ for issue in issues:
+ event = issue.pop("event")
+ comments = issue.pop("comments")
+
+ print(f"\n\nCreating issue {issue['title']}...")
+ res = todo_request("POST", f"/api/trackers/{TR_NAME}/tickets", issue)
+ print(res)
+ ticket_id = res["id"]
+
+ print(f"Setting status and resolution...")
+ res = todo_request(
+ "PUT", f"/api/trackers/{TR_NAME}/tickets/{ticket_id}", event
+ )
+ print(res)
+
+ print("Sending comments...")
+ for comment in comments:
+ print(f"Sending {comment['comment']}...")
+ res = todo_request(
+ "PUT", f"/api/trackers/{TR_NAME}/tickets/{ticket_id}", comment
+ )
+ print(res)
+ print("\n\n")
+
+
+if __name__ == "__main__":
+ main()