-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.w
110 lines (96 loc) · 3.3 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
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
102
103
104
105
106
107
108
109
110
bring cloud;
bring util;
bring http;
bring math;
let discordToken = new cloud.Secret(name: "discordToken") as "discord token";
let githubToken = new cloud.Secret(name: "github-token") as "github token";
let bucket = new cloud.Bucket();
let GITHUB_REPO_OWNER = "winglang";
let GITHUB_REPO_NAME = "wing";
let discordChannel = "1238185652688912394";
let discordBaseAPI = "https://discord.com/api/v10";
struct Label {
id: num;
name: str;
description: str;
}
struct GithubIssue {
url: str;
title: str;
html_url: str;
created_at: str;
labels: Array<Label>;
}
let fetchIssues = inflight (): Array<GithubIssue>? => {
try {
let response = http.get("https://api.github.com/repos/{GITHUB_REPO_OWNER}/{GITHUB_REPO_NAME}/issues?labels=good%20first%20issue&state=open&sort=created&direction=desc&per_page=10", {
headers: {
Authorization: "token {githubToken.value()}"
}
});
log(response.body);
let issues = MutArray<GithubIssue>[];
for entry in Json.values(Json.parse(response.body)) {
issues.push(GithubIssue.fromJson(entry));
log("issues: {issues.join("\n")}");
}
if (response.status == 200) {
return issues.copy();
} else {
log("Error fetching GitHub issues: {response.status}");
return nil;
}
} catch error {
log("Error fetching GitHub issues: {error}");
return nil;
}
};
let getStoredIssues = inflight (): MutArray<GithubIssue> => {
try {
let oldIssuesData = bucket.get("previousIssues.json");
log("oldIssues: {oldIssuesData}");
} catch error {
log("No previous issues found or error fetching from bucket: {error}");
return MutArray<GithubIssue>[];
}
};
let filterIssues = inflight (days: num, issues: Array<GithubIssue>): Array<GithubIssue> => {
let filteredIssues: MutArray<GithubIssue> = MutArray<GithubIssue>[];
for issue in issues {
let createdDate = datetime.fromIso(issue.created_at);
let currentTime = datetime.utcNow();
let difference = currentTime.timestampMs - createdDate.timestampMs;
let oneDayInMilliseconds = 24 * 60 * 60 * 1000;
let daysOld = math.floor(difference / oneDayInMilliseconds);
if daysOld <= days {
filteredIssues.push(issue);
}
}
return filteredIssues.copy();
};
let postIssues = inflight () => {
if let issues = fetchIssues() {
let newFirstIssues = filterIssues(1, issues);
let discordMessage: MutArray<str> = MutArray<str>[];
for issue in newFirstIssues {
discordMessage.push(issue.title, issue.html_url);
log("These are the issues");
log("issues url: {issue.html_url}");
log("issues title {issue.title}");
bucket.put("new_issue_file", issue.html_url);
let fileData = bucket.get("new_issue_file");
log("this is the bucket fileData: {fileData}");
}
let response = http.post("{discordBaseAPI}/channels/{discordChannel}/messages", {
headers: {
Authorization: "Bot {discordToken.value()}",
"Content-Type": "application/json"
},
body: Json.stringify({
content: "*A New Good First Issue Has Been Published*\n🚀========================🚀\n{discordMessage.join("\n")}"
})
});
}
};
let s = new cloud.Schedule(rate: duration.fromHours(24));
s.onTick(inflight () => { postIssues(); });