|
1 | 1 | import logging |
2 | | -import os |
3 | | -import sys |
4 | | -from datetime import datetime, timedelta |
5 | 2 |
|
6 | 3 | import requests |
7 | | -import yaml |
8 | 4 |
|
9 | | -REQUEST_FMT = "https://api.github.com/repos/{0}/{1}/issues" |
10 | | - |
11 | | - |
12 | | -class Triager: |
13 | | - def __init__(self, cfg): |
14 | | - self.oauth_token = os.getenv("GH_TOKEN") |
15 | | - |
16 | | - # select config.yaml from cwd |
17 | | - if not cfg: |
18 | | - logging.info("config file not specified, setting default") |
19 | | - cfg = "./config.yaml" |
20 | 5 |
|
21 | | - logging.info("attempting to read config file: {0}".format(cfg)) |
22 | | - |
23 | | - try: |
24 | | - with open(cfg, "r") as config_file: |
25 | | - config = yaml.safe_load(config_file) |
26 | | - logging.info("config file successfully loaded") |
27 | | - except FileNotFoundError as e: |
28 | | - logging.critical(e) |
29 | | - sys.exit() |
| 6 | +REQUEST_FMT = "https://api.github.com/repos/{0}/{1}/issues" |
30 | 7 |
|
31 | | - logging.info("parsing information from config file") |
32 | 8 |
|
33 | | - # Populate org and repos to triage |
34 | | - self.repos = [] |
35 | | - logging.debug("parsing orgs and repositories from config file") |
36 | | - for org in config["orgs"]: |
37 | | - for repo in org["repos"]: |
38 | | - self.repos.append((org["name"], repo)) |
| 9 | +def triage(config): |
| 10 | + issues = {} |
| 11 | + for org, repo in config.repos: |
| 12 | + repo_name = repo["name"] |
| 13 | + repo_labels = repo.get("labels", []) |
39 | 14 |
|
40 | | - # Populate maintainers list |
41 | | - logging.debug("parsing list of maintainers from config file") |
42 | | - self.maintainers = config["maintainers"] |
| 15 | + params = dict(since=config.last_triage_date.isoformat()) |
| 16 | + if repo_labels: |
| 17 | + params["labels"] = ",".join(repo_labels) |
| 18 | + else: |
| 19 | + params["assignee"] = "none" |
43 | 20 |
|
44 | | - # Set address to send triage emails from |
45 | | - logging.debug("parsing triager email and password from config file") |
46 | | - self.sender = { |
47 | | - "email": config.get("triager", {}).get("address"), |
48 | | - "password": config.get("triager", {}).get("password"), |
49 | | - } |
| 21 | + issues[repo_name] = [] |
50 | 22 |
|
51 | | - # Set last triage date |
52 | | - logging.debug("setting last triage date") |
53 | | - self.last_triage_date = datetime.utcnow() - timedelta( |
54 | | - days=int(config["timedelta"]) |
| 23 | + logging.info( |
| 24 | + "requesting issue details for {0}/{1}".format(org, repo_name) |
| 25 | + ) |
| 26 | + resp = requests.get( |
| 27 | + REQUEST_FMT.format(org, repo_name), |
| 28 | + params=params, |
| 29 | + headers=config.token, |
55 | 30 | ) |
56 | 31 |
|
57 | | - logging.info("config file successfully parsed") |
58 | | - |
59 | | - def triage(self): |
60 | | - issues = {} |
61 | | - for org, repo in self.repos: |
62 | | - repo_name = repo["name"] |
63 | | - repo_labels = repo.get("labels", []) |
64 | | - |
65 | | - params = dict(since=self.last_triage_date.isoformat()) |
66 | | - if repo_labels: |
67 | | - params["labels"] = ",".join(repo_labels) |
68 | | - else: |
69 | | - params["assignee"] = "none" |
70 | | - |
71 | | - issues[repo_name] = [] |
72 | | - |
73 | | - logging.info( |
74 | | - "requesting issue details for {0}/{1}".format(org, repo_name) |
75 | | - ) |
76 | | - resp = requests.get( |
77 | | - REQUEST_FMT.format(org, repo_name), |
78 | | - params=params, |
79 | | - headers=self._get_token(), |
| 32 | + if not resp.ok: |
| 33 | + logging.critical(resp.json()["message"]) |
| 34 | + return {} |
| 35 | + |
| 36 | + for item in resp.json(): |
| 37 | + issues[repo_name].append( |
| 38 | + { |
| 39 | + "url": item["html_url"], |
| 40 | + "title": item["title"], |
| 41 | + "type": "Pull Request" |
| 42 | + if item.get("pull_request") |
| 43 | + else "Issue", |
| 44 | + } |
80 | 45 | ) |
81 | 46 |
|
82 | | - if not resp.ok: |
83 | | - logging.critical(resp.json()["message"]) |
84 | | - return {} |
85 | | - |
86 | | - for item in resp.json(): |
87 | | - issues[repo_name].append( |
88 | | - { |
89 | | - "url": item["html_url"], |
90 | | - "title": item["title"], |
91 | | - "type": "Pull Request" |
92 | | - if item.get("pull_request") |
93 | | - else "Issue", |
94 | | - } |
95 | | - ) |
96 | | - |
97 | | - logging.info("triage successfully completed") |
98 | | - return issues |
99 | | - |
100 | | - def _get_token(self): |
101 | | - logging.debug("fetching oauth token") |
102 | | - if self.oauth_token: |
103 | | - return {"Authorization": "token {0}".format(self.oauth_token)} |
| 47 | + logging.info("triage successfully completed") |
| 48 | + return issues |
0 commit comments