|
9 | 9 | import strictyaml as yaml
|
10 | 10 |
|
11 | 11 |
|
12 |
| -GITHUB_TOKEN = os.getenv('GITHUB_TOKEN') |
13 |
| -GITHUB_API_HEADERS = {'Accept': 'application/vnd.github.squirrel-girl-preview', |
14 |
| - 'Authorization': f'token {GITHUB_TOKEN}'} |
15 |
| -GITHUB_API_URL = 'https://api.github.com/repos/pyvec/money/issues' |
16 |
| -REACTIONS_MAPPING = {'+1': 'ano', '-1': 'ne', 'eyes': 'zdržel(a) se'} |
| 12 | +GITHUB_TOKEN = os.getenv("GITHUB_TOKEN") |
| 13 | +GITHUB_API_HEADERS = { |
| 14 | + "Accept": "application/vnd.github.squirrel-girl-preview", |
| 15 | + "Authorization": f"token {GITHUB_TOKEN}", |
| 16 | +} |
| 17 | +GITHUB_API_URL = "https://api.github.com/repos/pyvec/money/issues" |
| 18 | +REACTIONS_MAPPING = {"+1": "ano", "-1": "ne", "eyes": "zdržel(a) se"} |
17 | 19 | CONTENT_PATH = Path(__file__).parent.parent
|
18 | 20 |
|
19 |
| -BOARD_HISTORY_SCHEMA = yaml.Seq(yaml.Map({ |
20 |
| - 'from': yaml.Datetime(), |
21 |
| - 'members': yaml.MapPattern(yaml.Str(), yaml.Str()), |
22 |
| -})) |
23 |
| -BOARD_HISTORY_PATH = Path(__file__).parent.parent / 'board.yml' |
24 |
| -BOARD_HISTORY = sorted(yaml.load(BOARD_HISTORY_PATH.read_text(), |
25 |
| - BOARD_HISTORY_SCHEMA).data, |
26 |
| - key=itemgetter('from'), reverse=True) |
| 21 | +BOARD_HISTORY_SCHEMA = yaml.Seq( |
| 22 | + yaml.Map( |
| 23 | + { |
| 24 | + "from": yaml.Datetime(), |
| 25 | + "members": yaml.MapPattern(yaml.Str(), yaml.Str()), |
| 26 | + } |
| 27 | + ) |
| 28 | +) |
| 29 | +BOARD_HISTORY_PATH = Path(__file__).parent.parent / "board.yml" |
| 30 | +BOARD_HISTORY = sorted( |
| 31 | + yaml.load(BOARD_HISTORY_PATH.read_text(), BOARD_HISTORY_SCHEMA).data, |
| 32 | + key=itemgetter("from"), |
| 33 | + reverse=True, |
| 34 | +) |
27 | 35 |
|
28 | 36 |
|
29 | 37 | def to_date(iso_datetime_string):
|
30 |
| - iso_date_string, _ = iso_datetime_string.split('T') |
| 38 | + iso_date_string, _ = iso_datetime_string.split("T") |
31 | 39 | return date.fromisoformat(iso_date_string)
|
32 | 40 |
|
33 | 41 |
|
34 | 42 | def remove_comments(html):
|
35 |
| - return re.sub(r'<!--[^<]+-->', '', html).strip() |
| 43 | + return re.sub(r"<!--[^<]+-->", "", html).strip() |
36 | 44 |
|
37 | 45 |
|
38 | 46 | def get_board_member_name(username, voted_at, board_history=None):
|
39 | 47 | board_history = board_history or BOARD_HISTORY
|
40 | 48 | for board in board_history: # sorted from the most recent
|
41 |
| - if voted_at > board['from'].date(): |
42 |
| - return board['members'].get(username) |
| 49 | + if voted_at > board["from"].date(): |
| 50 | + return board["members"].get(username) |
43 | 51 | return None
|
44 | 52 |
|
45 | 53 |
|
46 | 54 | def get_votes(reactions, voted_at, board_history=None):
|
47 | 55 | for reaction in reactions:
|
48 |
| - username = reaction['user']['login'] |
| 56 | + username = reaction["user"]["login"] |
49 | 57 | name = get_board_member_name(username, voted_at, board_history)
|
50 | 58 | if name: # else not reaction from a board member
|
51 |
| - text = REACTIONS_MAPPING.get(reaction['content']) |
| 59 | + text = REACTIONS_MAPPING.get(reaction["content"]) |
52 | 60 | if text:
|
53 |
| - yield {'name': name, 'text': text} |
| 61 | + yield {"name": name, "text": text} |
54 | 62 |
|
55 | 63 |
|
56 | 64 | def get_lock_date(events):
|
57 | 65 | for event in reversed(events):
|
58 |
| - if event['event'] == 'locked': |
59 |
| - return to_date(event['created_at']) |
| 66 | + if event["event"] == "locked": |
| 67 | + return to_date(event["created_at"]) |
60 | 68 |
|
61 | 69 |
|
62 |
| -if __name__ == '__main__': |
63 |
| - res = requests.get(GITHUB_API_URL, headers=GITHUB_API_HEADERS, |
64 |
| - params={'per_page': 100, 'state': 'closed'}) |
| 70 | +if __name__ == "__main__": |
| 71 | + res = requests.get( |
| 72 | + GITHUB_API_URL, |
| 73 | + headers=GITHUB_API_HEADERS, |
| 74 | + params={"per_page": 100, "state": "closed"}, |
| 75 | + ) |
65 | 76 | res.raise_for_status()
|
66 | 77 |
|
67 | 78 | grants = []
|
68 | 79 | for issue in res.json():
|
69 |
| - |
70 |
| - labels = [label['name'] for label in issue['labels']] |
71 |
| - if 'approved' not in labels and 'rejected' not in labels: |
| 80 | + labels = [label["name"] for label in issue["labels"]] |
| 81 | + if "approved" not in labels and "rejected" not in labels: |
72 | 82 | # skip unlabeled, e.g. https://github.com/pyvec/money/issues/1
|
73 | 83 | continue
|
74 | 84 |
|
75 |
| - if issue['locked']: |
76 |
| - res = requests.get(issue['events_url'], |
77 |
| - headers=GITHUB_API_HEADERS, |
78 |
| - params={'per_page': 100}) |
| 85 | + if issue["locked"]: |
| 86 | + res = requests.get( |
| 87 | + issue["events_url"], |
| 88 | + headers=GITHUB_API_HEADERS, |
| 89 | + params={"per_page": 100}, |
| 90 | + ) |
79 | 91 | res.raise_for_status()
|
80 | 92 |
|
81 |
| - if res.headers.get('link'): |
82 |
| - raise NotImplementedError(f"The number of events of issue #{issue['number']} " |
83 |
| - "is paginated and this code isn't yet designed " |
84 |
| - "to handle this!") |
| 93 | + if res.headers.get("link"): |
| 94 | + raise NotImplementedError( |
| 95 | + f"The number of events of issue #{issue['number']} " |
| 96 | + "is paginated and this code isn't yet designed " |
| 97 | + "to handle this!" |
| 98 | + ) |
85 | 99 | else:
|
86 | 100 | voted_at = get_lock_date(res.json())
|
87 | 101 | else:
|
88 |
| - voted_at = to_date(issue['closed_at']) |
| 102 | + voted_at = to_date(issue["closed_at"]) |
89 | 103 |
|
90 |
| - res = requests.get(issue['reactions']['url'], |
91 |
| - headers=GITHUB_API_HEADERS) |
| 104 | + res = requests.get(issue["reactions"]["url"], headers=GITHUB_API_HEADERS) |
92 | 105 | res.raise_for_status()
|
93 | 106 | votes = list(get_votes(res.json(), voted_at))
|
94 | 107 |
|
95 |
| - grants.append({ |
96 |
| - 'title': issue['title'], |
97 |
| - 'description': remove_comments(issue['body']), |
98 |
| - 'url': issue['html_url'], |
99 |
| - 'user': { |
100 |
| - 'username': issue['user']['login'], |
101 |
| - 'url': issue['user']['html_url'], |
102 |
| - }, |
103 |
| - 'is_approved': 'approved' in labels, |
104 |
| - 'filed_at': to_date(issue['created_at']), |
105 |
| - 'voted_at': voted_at, |
106 |
| - 'votes': votes, |
107 |
| - }) |
108 |
| - grants = sorted(grants, key=itemgetter('voted_at'), reverse=True) |
109 |
| - |
110 |
| - tpl_path = CONTENT_PATH / 'operations' / 'grants.rst.jinja' |
| 108 | + grants.append( |
| 109 | + { |
| 110 | + "title": issue["title"], |
| 111 | + "description": remove_comments(issue["body"]), |
| 112 | + "url": issue["html_url"], |
| 113 | + "user": { |
| 114 | + "username": issue["user"]["login"], |
| 115 | + "url": issue["user"]["html_url"], |
| 116 | + }, |
| 117 | + "is_approved": "approved" in labels, |
| 118 | + "filed_at": to_date(issue["created_at"]), |
| 119 | + "voted_at": voted_at, |
| 120 | + "votes": votes, |
| 121 | + } |
| 122 | + ) |
| 123 | + grants = sorted(grants, key=itemgetter("voted_at"), reverse=True) |
| 124 | + |
| 125 | + tpl_path = CONTENT_PATH / "operations" / "grants.rst.jinja" |
111 | 126 | tpl = Template(tpl_path.read_text())
|
112 | 127 | print(tpl.render(grants=grants))
|
0 commit comments