/
__init__.py
101 lines (76 loc) · 2.43 KB
/
__init__.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import os
import sys
import json
from urllib.request import Request, urlopen
from datetime import datetime, timedelta
from typing import Dict, Optional, Any
from flask import Flask, request
app = Flask(__name__, static_folder=None)
def api_headers() -> Dict[str, str]:
token = app.config["GITHUB_TOKEN"]
return {
"Content-Type": "application/json",
"Accept": "application/vnd.github.v3+json",
"Authorization": f"token {token}",
}
URL = "https://api.github.com/repos/nix-community/NUR"
def last_build_time() -> Optional[datetime]:
url = f"{URL}/actions/runs?per_page=1&branch=master"
app.logger.info("get latest actions: %s", url)
req = Request(url, headers=api_headers())
last_builds = json.loads(urlopen(req).read())
for build in last_builds["workflow_runs"]:
return datetime.strptime(build["created_at"], "%Y-%m-%dT%H:%M:%SZ")
return None
@app.route("/", methods=["GET"])
def index() -> Any:
return f"""
<html>
<head>
<title>NUR update endpoint</title>
</head>
<body>
<h1>NUR update endpoint</h1>
<p>
<h2>Usage</h2>
<code>
POST {request.url_root}update?repo=<REPO_NAME>
</code>
</p>
<p>
<h2>Example</h2>
<code>
$ curl -XPOST {request.url_root}update?repo=mic92
</code>
</p>
<p>
<h2>Source code</h2>
Source code can be found on <a href="https://github.com/nix-community/nur-update">github</a>
</p>
</body>
</html>
"""
@app.route("/update", methods=["POST"])
def update_travis() -> Any:
ts = last_build_time()
if ts is not None and (ts + timedelta(minutes=5)) > datetime.utcnow():
return "The last build was less then 5 minutes ago, try later", 429
repo = request.args.get("repo")
if repo is None or repo == "":
return "repo parameter is missing", 400
data = json.dumps({"ref": "master"})
url = f"{URL}/actions/workflows/update.yml/dispatches"
app.logger.info("trigger workflow update: %s", url)
req = Request(url, headers=api_headers(), data=data.encode("utf-8"), method="POST",)
resp = urlopen(req).read()
assert len(resp) == 0
return "", 204
def load_token() -> None:
token = os.environ.get("GITHUB_TOKEN", None)
if token is None:
print("no GITHUB_TOKEN environment variable set", file=sys.stderr)
sys.exit(1)
app.config["GITHUB_TOKEN"] = token
load_token()
if __name__ == "__main__":
app.run()