-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub_adapter.py
48 lines (38 loc) · 1.64 KB
/
github_adapter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# -*- coding: utf-8 -*-
import logging
import sys
import time
import github3
import requests
from config import LEVEL, GITHUB_TOKEN, SLEEP_SECONDS, LIGHT_SECONDS, BASE_URL, DEFAULT_LABELS, REPOSITORIES
gh = github3.login(token=GITHUB_TOKEN)
def check_and_send_light_request(new_tickets, old_tickets):
for ticket in new_tickets - old_tickets:
# request to beacon light
logging.info('New ticket found: {}'.format(ticket))
payload = {'actor': 'github_adapter', 'duration': LIGHT_SECONDS}
requests.post(BASE_URL + '/light', json=payload)
def main():
old_tickets = set()
new_tickets = set()
while True:
try:
for repository_data in REPOSITORIES:
username = repository_data.get('username')
repository = repository_data.get('repository')
labels = repository_data.get('labels') or DEFAULT_LABELS
issues = gh.issues_on(username, repository, state='open', labels=labels)
for issue in issues:
ticket = username + '/' + repository + '#' + str(issue.number)
new_tickets.add(ticket)
check_and_send_light_request(new_tickets, old_tickets)
old_tickets = new_tickets.copy()
logging.info(old_tickets)
new_tickets.clear()
except (requests.exceptions.RequestException, github3.exceptions.ServerError) as e:
logging.error(e)
time.sleep(SLEEP_SECONDS)
if __name__ == '__main__':
log_format = '%(asctime)s - %(levelname)s - %(message)s'
logging.basicConfig(stream=sys.stdout, level=LEVEL, format=log_format)
main()