Created
April 2, 2023 18:46
-
-
Save FrostyX/59d14932c93cb7b669ca10e77be32347 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import time | |
import bugzilla | |
BODY_TEMPLATE = "Original {what}: {link}\n" "Opened: {date}\n" "Opened by: {user}" | |
DESCRIPTION_TEMPLATE = "\n\n{description}" | |
COMMENT_TEMPLATE = ( | |
"\n\n---\n\n#### {user}" | |
" commented at {date}:\n{comment}" | |
) | |
BZ_URL = "https://bugzilla.redhat.com/xmlrpc.cgi" | |
BZ_PAGE_SIZE = 1000 | |
def _iterate_query(querydata): | |
""" | |
Iterate Bugzilla query until all results are fetched. | |
Taken from: | |
https://github.com/python-bugzilla/python-bugzilla/issues/149#issuecomment-921971629 | |
""" | |
bz = bugzilla.Bugzilla(url=BZ_URL) | |
results = bz.query(querydata) | |
if len(results) == BZ_PAGE_SIZE: | |
last_result_id = results[-1].id | |
querydata['f1'] = 'bug_id' | |
querydata['o1'] = 'greaterthan' | |
querydata['v1'] = last_result_id | |
print("Last ID: {0}".format(last_result_id)) | |
results += _iterate_query(querydata) | |
return results | |
def get_bugs(): | |
bz = bugzilla.Bugzilla(url=BZ_URL) | |
query = bz.build_query( | |
product="Copr", | |
status=["NEW", "ASSIGNED"], | |
) | |
query["limit"] = BZ_PAGE_SIZE | |
query["offset"] = 0 | |
query["order_by"] = "bug_id" | |
return _iterate_query(query) | |
def dumps(bug): | |
bz = bugzilla.Bugzilla(url=BZ_URL) | |
title = bug.summary | |
body = BODY_TEMPLATE.format( | |
what="issue", | |
link=bug.weburl, | |
date=date2str(bug.creation_time), | |
user=bug.creator_detail["real_name"], | |
) | |
full = "{title}\n\n{body}".format(title=title, body=body) | |
for comment in bug.getcomments(): | |
full += COMMENT_TEMPLATE.format( | |
user=bz.getuser(comment["creator"]).real_name, | |
date=date2str(comment["creation_time"]), | |
comment=comment["text"], | |
) | |
return full | |
def date2str(date): | |
return time.strftime('%Y-%m-%d %H:%M:%S', date.timetuple()) | |
def dump(bug): | |
path = "/tmp/bugzilla-export/{0}.md".format(bug.id) | |
os.makedirs(os.path.dirname(path), exist_ok=True) | |
with open(path, "w+") as fp: | |
output = dumps(bug) | |
fp.write(output) | |
return path | |
def main(): | |
bugs = get_bugs() | |
for bug in bugs: | |
name = dump(bug) | |
print("File: {0}".format(name)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment