Skip to content

Commit 9175fa5

Browse files
committed
github.py: introduce an object per issue
And give the GitHubIssue objects some convenience accessors.
1 parent af62066 commit 9175fa5

File tree

1 file changed

+55
-10
lines changed

1 file changed

+55
-10
lines changed

github.py

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,47 @@
88
# ------------------------------------------------------------------------
99
# A class to download and organize information from GitHub.
1010

11-
import json, logging, math, sys, time
11+
import datetime, json, logging, math, sys, time
1212
import requests
1313

14+
class GitHubIssue:
15+
16+
def __init__(self, data):
17+
self._data = data
18+
19+
@staticmethod
20+
def _datetime(timestamp):
21+
# Credit: https://stackoverflow.com/a/969324/1207769
22+
return datetime.datetime.strptime('2019-01-04T16:41:24+0200', "%Y-%m-%dT%H:%M:%S%z")
23+
24+
@property
25+
def created_at(self):
26+
return GitHubIssue._datetime(self._data['created_at'])
27+
28+
@property
29+
def updated_at(self):
30+
return GitHubIssue._datetime(self._data['updated_at'])
31+
32+
@property
33+
def is_pr(self):
34+
return 'pull_request' in self._data
35+
36+
@property
37+
def is_draft(self):
38+
return 'draft' in self._data
39+
40+
@property
41+
def milestone(self):
42+
return self._data['milestone']['title'] if self._data['milestone'] else None
43+
44+
@property
45+
def labels(self):
46+
return [label['name'] for label in self._data['labels']]
47+
48+
@property
49+
def assignees(self):
50+
return [assignee['login'] for assignee in self._data['assignees']]
51+
1452
class GitHubIssues:
1553

1654
def __init__(self, items=[], token=None):
@@ -20,22 +58,29 @@ def __init__(self, items=[], token=None):
2058
self._max_requests = 100
2159

2260
def repo(self, org, repo):
23-
return GitHubIssues(self.issues(lambda item: item['repository_url'].endswith(f'/repos/{org}/{repo}')),
24-
token=self._token)
25-
26-
def prs(self):
27-
return GitHubIssues(self.issues(lambda item: item['pull_request'] is True),
28-
token=self._token)
61+
"""
62+
Filter the collection of issues to only those in the given repository.
63+
"""
64+
return GitHubIssues([item for item in self._items if item['repository_url'].endswith(f'/repos/{org}/{repo}')], token=self._token)
2965

30-
def issues(self, predicate=lambda x: True):
31-
return list(filter(predicate, self._items))
66+
def issues(self):
67+
"""
68+
Return the collection of issues as a list of GitHubIssue objects.
69+
"""
70+
return list(map(GitHubIssue, self._items))
3271

3372
def load(self, filepath):
73+
"""
74+
Load issues from the given JSON file.
75+
"""
3476
with open(filepath) as f:
3577
result = json.loads(f.read())
3678
self._items.extend(result)
3779

3880
def save(self, filepath):
81+
"""
82+
Save issues to the given JSON file.
83+
"""
3984
with open(filepath, 'w') as f:
4085
return json.dump(self._items, f, sort_keys=True, indent=4)
4186

@@ -45,7 +90,7 @@ def _search_url(query):
4590

4691
def download(self, query):
4792
"""
48-
Downloads issues from GitHub according to the given query.
93+
Download issues from GitHub according to the given query.
4994
"""
5095
url = GitHubIssues._search_url(query)
5196
for _ in range(self._max_requests):

0 commit comments

Comments
 (0)