summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorromina xj ix k <romina@sunshinegardens.org>2022-02-25 14:46:15 -0700
committerromina xj ix k <romina@sunshinegardens.org>2022-02-25 14:46:15 -0700
commit1ef72a97605bfe8a1267d113826afe6897d27976 (patch)
tree94d508e4ed85cab4351390422e337021050a8c47
parentf9bab1a90e2c87aad252ee30b04b05a58d36d0d7 (diff)
use native json format, dump from to_dict() directly
-rw-r--r--.gitignore1
-rw-r--r--main.py54
2 files changed, 29 insertions, 26 deletions
diff --git a/.gitignore b/.gitignore
index afed073..343a25c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
*.csv
+*.json
diff --git a/main.py b/main.py
index 58abe95..1426467 100644
--- a/main.py
+++ b/main.py
@@ -1,41 +1,43 @@
import giteapy
-import csv
+import datetime
+import json
+import os
configuration = giteapy.Configuration()
# ======================================================================================================================
# Modify configuration for your needs
-configuration.host = 'https://<YOUR GITEA URL ORIGIN>/api/v1'
-configuration.api_key['access_token'] = '<MY GITEA TOKEN>' # https://<YOUR GITEA URL ORIGIN>/user/settings/applications
-my_organization_name = '<ORGANIZATION NAME FOR A GIVEN REPO>'
-my_repo_name = '<REPO NAME>'
+configuration.host = 'https://{gitea_domain}/api/v1'.format(
+ gitea_domain=os.environ.get('GITEA_DOMAIN', 'example.com'))
+# https://<YOUR GITEA URL ORIGIN>/user/settings/applications
+configuration.api_key['access_token'] = os.environ.get(
+ 'GITEA_ACCESS_TOKEN', 'MY TOKEN')
+my_organization_name = os.environ.get(
+ 'GITEA_EXPORT_ORG', 'ORG_NAME')
+my_repo_name = os.environ.get(
+ 'GITEA_EXPORT_REPO', 'MY_REPO')
# ======================================================================================================================
api_client = giteapy.ApiClient(configuration)
issues_api_instance = giteapy.IssueApi(giteapy.ApiClient(configuration))
+class DateTimeEncoder(json.JSONEncoder):
+ def default(self, z):
+ if isinstance(z, datetime.datetime):
+ return (str(z))
+ else:
+ return super().default(z)
-def dump_to_csv(filename, array_of_objects):
- """
- convert api objects
- """
- converted = [o.to_dict() for o in array_of_objects]
- # dedup
- array_of_dicts = []
- map_info = {}
- for conv in converted:
- if conv['id'] not in map_info:
- array_of_dicts.append(conv)
- map_info[conv['id']] = True
- keys = array_of_dicts[0].keys()
-
- with open(filename, 'w', newline='') as output_file:
+def dump(filename, array_of_objects):
+ map_info = {}
+ for x in (o.to_dict() for o in array_of_objects):
+ map_info[x['id']] = x
+ with open(filename, 'w') as output_file:
output_file.seek(0)
- dict_writer = csv.DictWriter(output_file, keys)
- dict_writer.writeheader()
- dict_writer.writerows(array_of_dicts)
-
+ json.dump(list(map_info.values()),
+ output_file,
+ cls=DateTimeEncoder)
def fetch_all(**kwargs):
"""
@@ -59,8 +61,8 @@ def export_open_closed_issues(organization_name, repository_name):
closed_issues = list(filter(lambda x: x.pull_request is None or (not x.pull_request.merged),
fetch_all(owner=organization_name, repo=repository_name, state='closed')))
- dump_to_csv('open_issues.csv', open_issues)
- dump_to_csv('closed_issues.csv', closed_issues)
+ dump('open_issues.json', open_issues)
+ dump('closed_issues.json', closed_issues)
if __name__ == '__main__':