-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAuroraDiscordSummary.swift
383 lines (317 loc) · 10.8 KB
/
AuroraDiscordSummary.swift
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
//
// AuroraDiscordSummary.swift
// Aurora Editor Discord Bot
//
// Created by Wesley de Groot on 08/11/2023.
// Copyright © 2023 Aurora Company. All rights reserved.
//
import Foundation
#if canImport(FoundationNetworking)
// Support network calls in Linux.
import FoundationNetworking
#endif
struct Configuration: Codable {
let settings: Settings
let github: GitHub
let discord: Discord
struct Settings: Codable {
let stats: [String]
}
struct GitHub: Codable {
let url: String
let token: String?
let repos: [String]
}
struct Discord: Codable {
let webhook: String
let tag: Int
let tagtreshold: Int
let username: String
let avatar: String?
let title: String
let description: String?
let url: String?
let message: String?
let tts: Bool?
let file: String?
let type: String?
let color: String?
let image: String?
let footer: Footer?
struct Footer: Codable {
let text: String
let icon_url: String
}
}
}
struct GitHubRepo: Codable {
let name: String
let full_name: String
let owner: Owner
let html_url: String
let forks_count: Int
let stargazers_count: Int
let open_issues_count: Int
let has_issues: Bool
let subscribers_url: String
let pulls_url: String
// MARK: - License
struct License: Codable {
let key, name, spdxID: String
let url: String
let nodeID: String
}
// MARK: - Owner
struct Owner: Codable {
let login: String
let avatar_url: String
}
}
struct GitHubPullRequests: Codable {
var title: String
var html_url: String
var user: User
var draft: Bool
var created_at: String
struct User: Codable {
var login: String
var html_url: String
}
}
struct GitHubWatchers: Codable {
var login: String
}
// Fail if the config file doesn't exist.
if !FileManager.default.fileExists(atPath: "config.json") {
print("Config file not found.")
exit(1)
}
// Load the configuration.
let configuration = try JSONDecoder().decode(
Configuration.self,
from: Data(contentsOf: URL(fileURLWithPath: "config.json"))
)
// Load the data from GitHub.
if let githubData: [GitHubRepo] = fetchData(url: configuration.github.url) {
// Walk through the repos.
for repo in githubData {
// Check if the repo is in the list of repos to check.
// Otherwise, skip it.
if !configuration.github.repos.contains(repo.name) {
continue
}
// Parse the repo.
parseRepo(repo: repo)
}
} else {
print("Unable to parse the data from \(configuration.github.url)")
}
func parseRepo(repo: GitHubRepo) {
// Get the current timestamp.
let timestamp = Date()
let formatter = ISO8601DateFormatter()
formatter.formatOptions.insert(.withFractionalSeconds)
// Get the PR count.
let PRCount: [GitHubPullRequests]? = fetchData(url: repo.pulls_url.replacingOccurrences(of: "{/number}", with: ""))
// Get the Watchers
let watchersCount: [GitHubWatchers]? = fetchData(url: repo.subscribers_url)
// Create a new array for the discord message.
var discordArray = [String: Any]()
if let message = configuration.discord.message {
discordArray["content"] = message
}
discordArray["username"] = configuration.discord.username
discordArray["avatar_url"] = configuration.discord.avatar ?? repo.owner.avatar_url
discordArray["tts"] = configuration.discord.tts ?? false
if let file = configuration.discord.file {
discordArray["file"] = file
}
// Create a new array for the embeds.
var discordEmbedArray = [String: Any]()
discordEmbedArray["title"] = configuration.discord.title
discordEmbedArray["type"] = configuration.discord.type ?? "rich"
discordEmbedArray["description"] = String(
format: configuration.discord.description ?? "These are the statistics for [%s](https://github.com/%s),\r\nupdated on %s.",
repo.full_name,
repo.full_name,
timestamp.description(with: .current)
) + "\r\n"
discordEmbedArray["url"] = configuration.discord.url ?? "https://auroraeditor.com"
discordEmbedArray["timestamp"] = formatter.string(from: timestamp)
discordEmbedArray["color"] = Int(configuration.discord.color ?? "3366ff", radix: 16)
if let footerText = configuration.discord.footer?.text {
discordEmbedArray["footer"] = [
"text": footerText,
"icon_url": configuration.discord.footer?.icon_url ?? ""
]
}
if let imageURL = configuration.discord.image {
discordEmbedArray["image"] = [
"url": imageURL
]
}
discordEmbedArray["thumbnail"] = [
"url": repo.owner.avatar_url
]
var discordEmbedFieldsArray = [[String: Any]]()
// Add the forks.
discordEmbedFieldsArray.append([
"name": "🪓 Forks",
"value": "[\(repo.forks_count)](\(repo.html_url + "/forks"))",
"inline": true
])
// Add the watchers.
discordEmbedFieldsArray.append([
"name": "👁️ Watchers",
"value": "[\(watchersCount?.count ?? 0)](\(repo.html_url + "/watchers"))",
"inline": true
])
// Add the stargazers.
discordEmbedFieldsArray.append([
"name": "⭐️ Stars",
"value": "[\(repo.stargazers_count)](\(repo.html_url + "/stargazers"))",
"inline": true
])
// Check if issues are enabled.
if repo.has_issues {
// Add the issues.
discordEmbedFieldsArray.append([
"name": "🎯 Issues",
"value": "[\(repo.open_issues_count)](\(repo.html_url + "/issues"))",
"inline": true
])
} else {
// Filler, issues not enabled.
discordEmbedFieldsArray.append([
"name": " ",
"value": " ",
"inline": true
])
}
discordEmbedFieldsArray.append([
"name": "🔨 PRs",
"value": "[\(PRCount?.count ?? 0)](\(repo.html_url + "/pulls"))",
"inline": true
])
discordEmbedFieldsArray.append([
"name": " ",
"value": " ",
"inline": true
])
discordEmbedArray["fields"] = discordEmbedFieldsArray
// Tag the reviewers if there are open PR's.
var commits = ""
for commit in PRCount ?? [] {
let createdHoursAgo = calculateHours(inputDate: commit.created_at)
let isDraft = commit.draft ? " _(Draft)_" : ""
let createdAgo = (createdHoursAgo > 24 ? "\(createdHoursAgo / 24)" : "\(createdHoursAgo)") + " " + (createdHoursAgo > 24 ? "days" : "hours")
let notify = !commit.draft && createdHoursAgo > configuration.discord.tagtreshold ? " ⚠️ <@&\(configuration.discord.tag)>" : ""
commits += "- [\(commit.title)](\(commit.html_url)) by [\(commit.user.login)](\(commit.user.html_url))\(isDraft), \(createdAgo) ago\(notify)\r\n"
}
if PRCount?.count ?? 0 > 0 {
discordArray["content"] = "The current amount of open PR's is [\(PRCount?.count ?? 0)](\(repo.html_url + "/pulls")), below a list with open PR's.\r\n\(commits) "
}
// Add the embed array to the discord array.
discordArray["embeds"] = [discordEmbedArray]
do {
let json_data = try JSONSerialization.data(
withJSONObject: discordArray,
options: []
)
let url = URL(string: configuration.discord.webhook)!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = json_data
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")
var keeprunning = true
URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data,
let response = response as? HTTPURLResponse,
error == nil
else {
print("HTTP ERROR")
print(error!.localizedDescription)
exit(1)
keeprunning = false
return
}
if response.statusCode == 400 {
print("JSON is unparsable by discord.")
print(String(decoding: json_data, as: UTF8.self))
exit(2)
}
keeprunning = false
}.resume()
while (keeprunning) {
// We need this because otherwise the application will end before doing the call
}
} catch {
print("FAILED TO CREATE JSON")
print(error)
print("Dict \(discordArray.self)")
print(discordArray)
return
}
}
func fetchData<T: Codable>(url fromURL: String) -> T? {
guard let url = URL(string: fromURL) else {
print("Invalid URL")
return nil
}
var wait = true
var data: Data?
var request = URLRequest(url: url)
request.setValue("en", forHTTPHeaderField: "Accept-language")
request.setValue("Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us)", forHTTPHeaderField: "User-Agent")
if let token = configuration.github.token {
request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
}
request.httpMethod = "GET"
let task = URLSession.shared.dataTask(with: request) { ddata, response, error in
guard
let ddata = ddata,
let response = response as? HTTPURLResponse,
error == nil
else {
print("HTTP ERROR")
print(error!.localizedDescription)
wait = false
return
}
guard (200 ... 299) ~= response.statusCode else {
print("statusCode should be 2xx, but is \(response.statusCode)")
print("response = \(response)")
wait = false
return
}
data = ddata
wait = false
}
task.resume()
while (wait) {
// We need this because otherwise the application will end before doing the call
}
do {
let json = try JSONDecoder().decode(
T.self,
from: data!
)
return json
} catch {
print(error)
}
return nil
}
func calculateHours(inputDate: String) -> Int {
let repoFormatter = DateFormatter()
repoFormatter.locale = Locale(identifier: "en_US_POSIX")
repoFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
if let repoDate = repoFormatter.date(from: inputDate),
let hours = Calendar.current.dateComponents([.hour], from: repoDate, to: Date()).hour {
return hours
}
return 0
}
// Tell the OS that the program exited successfully.
exit(0)