diff options
Diffstat (limited to 'accounts/gkleen@sif/utils/nixpkgs-pr-watch/nixpkgs_pr_watch')
| -rw-r--r-- | accounts/gkleen@sif/utils/nixpkgs-pr-watch/nixpkgs_pr_watch/__init__.py | 0 | ||||
| -rw-r--r-- | accounts/gkleen@sif/utils/nixpkgs-pr-watch/nixpkgs_pr_watch/__main__.py | 73 |
2 files changed, 73 insertions, 0 deletions
diff --git a/accounts/gkleen@sif/utils/nixpkgs-pr-watch/nixpkgs_pr_watch/__init__.py b/accounts/gkleen@sif/utils/nixpkgs-pr-watch/nixpkgs_pr_watch/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/accounts/gkleen@sif/utils/nixpkgs-pr-watch/nixpkgs_pr_watch/__init__.py | |||
diff --git a/accounts/gkleen@sif/utils/nixpkgs-pr-watch/nixpkgs_pr_watch/__main__.py b/accounts/gkleen@sif/utils/nixpkgs-pr-watch/nixpkgs_pr_watch/__main__.py new file mode 100644 index 00000000..2fff9f07 --- /dev/null +++ b/accounts/gkleen@sif/utils/nixpkgs-pr-watch/nixpkgs_pr_watch/__main__.py | |||
| @@ -0,0 +1,73 @@ | |||
| 1 | import argparse | ||
| 2 | import re | ||
| 3 | import requests | ||
| 4 | from urllib.parse import urljoin | ||
| 5 | from xdg_base_dirs import xdg_config_home, xdg_config_dirs | ||
| 6 | import toml | ||
| 7 | import sys | ||
| 8 | |||
| 9 | from github import Github | ||
| 10 | |||
| 11 | class ApiKeyAuth(requests.auth.AuthBase): | ||
| 12 | def __init__(self, token): | ||
| 13 | self.token = token | ||
| 14 | def __call__(self, r): | ||
| 15 | r.headers["x-api-key"] = self.token | ||
| 16 | return r | ||
| 17 | |||
| 18 | class ChangeDetectionSession(requests.Session): | ||
| 19 | def __init__(self, base_url: str, api_token: str): | ||
| 20 | super().__init__() | ||
| 21 | self.base_url = base_url | ||
| 22 | self.auth = ApiKeyAuth(api_token) | ||
| 23 | |||
| 24 | def request(self, method, url, *args, **kwargs): | ||
| 25 | joined_url = urljoin(urljoin(self.base_url, '/api/v1/'), url) | ||
| 26 | return super().request(method, joined_url, *args, **kwargs) | ||
| 27 | |||
| 28 | def main(): | ||
| 29 | def pr_number(s): | ||
| 30 | if m := re.fullmatch(r'(?:https?://github\.com/NixOS/nixpkgs/pull/)?(?P<pr>[0-9]+)', s, flags=re.I): | ||
| 31 | return int(m.group('pr')) | ||
| 32 | else: | ||
| 33 | raise ValueError | ||
| 34 | |||
| 35 | parser = argparse.ArgumentParser(prog = "nixpkgs-pr-watch") | ||
| 36 | parser.add_argument('prs', metavar = 'PR', type = pr_number, nargs = '+') | ||
| 37 | |||
| 38 | args = parser.parse_args() | ||
| 39 | |||
| 40 | config = None | ||
| 41 | for d in [xdg_config_home(), *xdg_config_dirs()]: | ||
| 42 | try: | ||
| 43 | config = toml.load(d / 'nixpkgs-pr-watch.toml') | ||
| 44 | break | ||
| 45 | except FileNotFoundError: | ||
| 46 | pass | ||
| 47 | |||
| 48 | for pr in args.prs: | ||
| 49 | pr_title = None | ||
| 50 | with Github() as g: | ||
| 51 | pr_title = g.get_repo('NixOS/nixpkgs').get_pull(pr).title | ||
| 52 | |||
| 53 | api = ChangeDetectionSession( | ||
| 54 | base_url = config.get("BaseUrl"), | ||
| 55 | api_token = config.get("ApiToken"), | ||
| 56 | ) | ||
| 57 | try: | ||
| 58 | api.post( | ||
| 59 | "watch", | ||
| 60 | headers = { | ||
| 61 | 'Content-Type': 'application/json', | ||
| 62 | }, | ||
| 63 | json = { | ||
| 64 | "url": urljoin("https://nixpkgs.molybdenum.software/api/v2/landings/", str(pr)), | ||
| 65 | **({ "title": pr_title } if pr_title else {}), | ||
| 66 | "time_between_check_use_default": False, | ||
| 67 | "time_between_check": { "hours": 1 }, | ||
| 68 | "fetch_backend": "html_requests", | ||
| 69 | }, | ||
| 70 | ).raise_for_status() | ||
| 71 | except requests.HTTPError as e: | ||
| 72 | print(e.response.text, file=sys.stderr) | ||
| 73 | raise e | ||
