-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.w
61 lines (52 loc) · 1.5 KB
/
main.w
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
bring cloud;
bring util;
bring http;
bring expect;
struct GithubAtomFeed {
id: str;
updated: str;
}
struct GithubAtom {
feed: GithubAtomFeed;
}
class Feedreader {
extern "./feed.mjs" pub static inflight parseAtomFeed(): GithubAtom;
}
let githubToken = new cloud.Secret(name: "github-token");
let bucket = new cloud.Bucket();
bucket.onCreate(inflight () => {
let token = githubToken.value();
let owner = "winglang";
let repo = "examples";
log("Triggering run in https://github.com/{owner}/{repo}");
let result = http.post("https://api.github.com/repos/{owner}/{repo}/dispatches",
headers: {
"Authorization": "Bearer {token}",
"Accept": "application/vnd.github+json",
"X-GitHub-Api-Version": "2022-11-28",
},
body: Json.stringify({
"event_type": "feedreader",
"client_payload": {}
}),
);
log("Result: ${result.ok} ${result.status} ${result.body}");
});
// This cron schedule runs every 2 minutes
let schedule = new cloud.Schedule(cron: "0/2 * ? * *");
let scheduleHandler = inflight () => {
let json: GithubAtom = Feedreader.parseAtomFeed();
let id = util.sha256(json.feed.updated);
if bucket.exists(id) {
log("No new versions");
} else {
log("New versions");
bucket.put(id, "new version ${json.feed.updated}");
}
};
schedule.onTick(scheduleHandler);
test "parse atom feed" {
let json = Feedreader.parseAtomFeed();
let feedId = json.feed.id;
expect.equal("tag:github.com,2008:https://github.com/winglang/wing/releases", feedId);
}