-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
UpdateHandler.cs
248 lines (229 loc) · 10.4 KB
/
UpdateHandler.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
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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics; // For debug only
using System.Net;
using System.Threading.Tasks;
using XVLauncher.Resources;
namespace XVLauncher
{
/// <summary>
/// Class for handling updates with git-based repositories.
/// </summary>
public class UpdateHandler
{
/// <summary>
/// The instance of MainWindow that contains GUI elements which show update progress and status.
/// </summary>
protected MainWindow Window;
private WebClient Client;
private dynamic Res, Commits, Infos;
private string Link, TargetCommit, Tag;
/// <summary>
/// Constructor of the UpdateHandler class.
/// </summary>
/// <param name="window">The window where the updater will be called.</param>
public UpdateHandler(MainWindow window)
{
Window = window;
}
/// <summary>
/// Check if it there is a new release on GitLab repo.
/// </summary>
/// <returns>true if there is an update available, false otherwise.</returns>
public async Task<bool> CheckUpdateAvailability()
{
if (Properties.Settings.Default.CurrentCommit != (await GetLatestRelease()).Commit)
{
Window.infoLabel.Content = "There's an update avaible.";
return true;
}
return false;
}
/// <summary>
/// Compares the currently stored commit id with a target commit id and returns a tuple of list with the old paths and new paths of changed files.
/// </summary>
/// <param name="targetCommit">The target commit id using for comparing.</param>
/// <returns></returns>
public async Task<(List<string> oldPath, List<string> newPath)> Compare(string targetCommit)
{
Window.button.IsEnabled = false;
// The current latest commit SHA. It should be stored in the Program settings.
string Current = Properties.Settings.Default.CurrentCommit;
// The target latest commit SHA. It can be retrieved with the "GetLatestRelease" method.
string Target = targetCommit;
// This is the list of items modified between the commits.
// Probably all the "oldPaths" should be deleted, whilst all the "newPaths" redownloaded.
// The list of old paths retrieved.
List<string> oldPaths = new List<string>();
// The list of new paths retrieved.
List<string> newPaths = new List<string>();
// The url for the API request.
string api_request = $"https://gitlab.com/api/v4/projects/{Properties.Settings.Default.ProjectID}/repository/compare?ref_name=Release&from={Current}&to={Target}&per_page=1000";
Uri api_request_uri = new Uri(api_request);
SetUpClient(api_request);
// Getting the commits list
Client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadCommitsListEventHandler);
await Client.DownloadStringTaskAsync(api_request_uri);
Client.Dispose();
SetUpClient(api_request);
//Getting the id list for the commits found.
List<string> ids = new List<string>();
foreach (var c in Commits)
{
dynamic val = JsonConvert.DeserializeObject(c.ToString());
ids.Add(val.id.ToString());
}
//Utility for the GUI update.
int progress = 0;
int full = ids.Count;
foreach (var id in ids)
{
Debug.WriteLine($"Checking commit {id}");
int page = 1;
string url = $"https://gitlab.com/api/v4/projects/{Properties.Settings.Default.ProjectID}/repository/commits/" + id + $"/diff?per_page=1000000&page={page}";
Client.BaseAddress = url;
Debug.WriteLine($"Url is: {url}");
Client.Headers.Add("Content-Type:application/json; charset=utf-8"); //Content-Type
Client.Headers.Add("Accept:application/json");
Client.Headers["Private-Token"] = Properties.Resources.AccessToken;
//await Client.DownloadStringTaskAsync(api_request_uri);
Client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadCommitInfoEventHandler);
await Client.DownloadStringTaskAsync(url);
progress++;
Window.Dispatcher.Invoke(() =>
{
double percentage = (float)progress / full * 100;
Window.infoLabel.Content = String.Format("Comparing the differences between the old release and the new one... {0:0.##}%", percentage);
Window.UpdateBarProgress(percentage);
});
while (Infos.Count > 0)
{
foreach (var info in Infos)
{
var desInfo = JsonConvert.DeserializeObject(info.ToString());
string oldPath = desInfo.old_path.ToString();
string newPath = desInfo.new_path.ToString();
if ((bool)desInfo.new_file)
{
if (!newPaths.Contains(newPath))
newPaths.Add(newPath);
}
else if ((bool)desInfo.renamed_file)
{
if (!newPaths.Contains(newPath))
newPaths.Add(newPath);
if (newPaths.Contains(oldPath))
newPaths.Remove(oldPath);
if (!oldPaths.Contains(oldPath))
oldPaths.Add(oldPath);
}
else if ((bool)desInfo.deleted_file)
{
if (newPaths.Contains(newPath))
newPaths.Remove(newPath);
if (!oldPaths.Contains(oldPath))
oldPaths.Add(oldPath);
}
else
{
if (!newPaths.Contains(newPath))
newPaths.Add(newPath);
}
}
page += 1;
url = $"https://gitlab.com/api/v4/projects/{Properties.Settings.Default.ProjectID}/repository/commits/" + id + $"/diff?per_page=1000000&page={page}";
await Client.DownloadStringTaskAsync(url);
}
}
oldPaths.Sort();
newPaths.Sort();
string results = "";
foreach (string s in newPaths)
{
results += "\"" + s + "\"" + "\n";
}
Window.Dispatcher.Invoke(() =>
{
Debug.WriteLine(results);
});
Client.Dispose();
return (oldPaths, newPaths);
}
private void SetUpClient(string api_request)
{
Client = new WebClient
{
BaseAddress = api_request
};
Client.Headers.Add("Content-Type:application/json"); //Content-Type
Client.Headers.Add("Accept:application/json");
Client.Headers["Private-Token"] = Properties.Resources.AccessToken;
}
private void DownloadCommitsListEventHandler(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
{
//TODO: show error on label
PhpManager.ReportError(e.Error.Message);
}
else
{
Res = JsonConvert.DeserializeObject(e.Result.ToString());
Commits = JsonConvert.DeserializeObject(Res.commits.ToString());
}
}
private void DownloadCommitInfoEventHandler(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
{
//TODO: show error on label
PhpManager.ReportError(e.Error.Message);
}
else
{
Infos = JsonConvert.DeserializeObject(e.Result.ToString());
}
}
private async Task SetLatestRelease()
{
string Url = $"https://gitlab.com/api/v4/projects/{Properties.Settings.Default.ProjectID}/releases";
string Link = "";
string TargetCommit = "";
string Tag = "";
using (var client = new System.Net.WebClient()) //WebClient
{
client.BaseAddress = Url;
client.Headers.Add("Content-Type:application/json"); //Content-Type
client.Headers.Add("Accept:application/json");
client.Headers["Private-Token"] = Properties.Resources.AccessToken;
// Getting releases list
dynamic res = JsonConvert.DeserializeObject(await client.DownloadStringTaskAsync(Url));
// Getting the latest release (it will always be the first item in the retrieved collection)
dynamic latest = JsonConvert.DeserializeObject(res[0].ToString());
dynamic latestCommit = JsonConvert.DeserializeObject(latest.commit.ToString());
TargetCommit = latestCommit.id.ToString();
Tag = latest.tag_name.ToString();
//Getting the direct link to the .zip asset of the release
dynamic latestZip = JsonConvert.DeserializeObject(latest.assets.sources[0].ToString());
Link = latestZip.url.ToString();
string results = latest.ToString();
}
this.Link = Link;
this.TargetCommit = TargetCommit;
this.Tag = Tag;
}
/// <summary>
/// Retrieves the latest github release informations.
/// </summary>
/// <returns>last release download link, last commit name, last release tag.</returns>
public async Task<(string Link, string Commit, string Tag)> GetLatestRelease()
{
if (this.TargetCommit == null)
{
await SetLatestRelease();
}
return (this.Link, this.TargetCommit, this.Tag);
}
}
}