-
Notifications
You must be signed in to change notification settings - Fork 0
/
PRPreview.cs
108 lines (90 loc) · 4.33 KB
/
PRPreview.cs
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
using Celeste.Mod.Helpers;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Net;
using System.Text.RegularExpressions;
using System.Linq;
namespace Celeste.Mod.PRPreview {
public class PRPreviewModule : EverestModule {
class PRSource {
public string PR;
public string PRTitle;
public Everest.Updater.Source Source;
}
private const string BUILD_API = "https://dev.azure.com/EverestAPI/Everest/_apis/build/builds";
private static readonly string BuildList = new URIHelper(BUILD_API, new NameValueCollection() {
{"definitions", "3"},
{"reasonFilter", "pullRequest"},
{"statusFilter", "completed"},
{"resultsFilter", "succeeded"},
{"api-version", "5.0"},
}).ToString();
private Dictionary<string, PRSource> updaterSources = new();
public override void Load() {
#if DEBUG
Logger.SetLogLevel("PRPreview", LogLevel.Debug);
#else
Logger.SetLogLevel("PRPreview", LogLevel.Info);
#endif
if (!Everest.Flags.SupportUpdatingEverest) {
Logger.Log(LogLevel.Warn, "PRPreview", Dialog.Clean("EVERESTUPDATER_NOTSUPPORTED"));
return;
}
string data;
try {
Logger.Log(LogLevel.Debug, "PRPreview", "Attempting to download Everest PR build list");
using (WebClient wc = new WebClient()) {
wc.Headers.Add("User-Agent", "Everest/" + Everest.VersionString);
data = wc.DownloadString(BuildList);
}
} catch (Exception e) {
Logger.Log(LogLevel.Warn, "PRPreview", "Failed requesting build list: " + e.ToString());
return;
}
try {
JArray builds = JObject.Parse(data)["value"] as JArray;
foreach (JObject build in builds) {
string sourceBranch;
string pr;
string prTitle = "";
try {
sourceBranch = build["sourceBranch"].ToString();
if (updaterSources.ContainsKey(sourceBranch))
continue;
pr = Regex.Match(sourceBranch, @"^refs/pull/(\d+)/merge$").Groups[1].Value;
try { prTitle = build.SelectToken("triggerInfo['pr.title']").ToString(); } catch { }
} catch (Exception e) {
Logger.Log(LogLevel.Error, "PRPreview", "Failed parsing information for build: " + e.ToString());
continue;
}
updaterSources.Add(sourceBranch, new PRSource {
PR = pr,
PRTitle = prTitle,
Source = new Everest.Updater.Source {
Name = "PR #" + pr,
Description = string.IsNullOrWhiteSpace(prTitle) ? Dialog.Clean("updater_src_buildbot_azure") : prTitle,
UpdatePriority = Everest.Updater.UpdatePriority.High,
Index = new URIHelper(BUILD_API, new() {
{ "definitions", "3" },
{ "branchName", sourceBranch },
{ "statusFilter", "completed" },
{ "resultsFilter", "succeeded" },
{ "api-version", "5.0" },
}).ToString,
ParseData = Everest.Updater.AzureBuildsParser("https://dev.azure.com/EverestAPI/Everest/_apis/build/builds/{0}/artifacts?artifactName=main&api-version=5.0&%24format=zip", offset: 700)
}
});
}
} catch (Exception e) {
Logger.Log(LogLevel.Error, "PRPreview", "Something went wrong adding update source for Everest PRs:" + e.ToString());
return;
}
foreach (PRSource source in updaterSources.Values.OrderByDescending(source => int.TryParse(source.PR, out int pr) ? pr : 0)) {
Everest.Updater.Sources.Add(source.Source);
}
}
public override void Unload() { }
}
}