-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathGitHubVersionChecker.cs
132 lines (110 loc) · 3.7 KB
/
GitHubVersionChecker.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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace IPAM_NOTE
{
public class GitHubVersionChecker
{
private readonly string _repositoryOwner;
private readonly string _repositoryName;
public GitHubVersionChecker(string repositoryOwner, string repositoryName)
{
_repositoryOwner = repositoryOwner;
_repositoryName = repositoryName;
}
public async Task<string> GetLatestVersionAsync()
{
try
{
string apiUrl = $"https://api.github.com/repos/{_repositoryOwner}/{_repositoryName}/releases/latest";
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Add("User-Agent", "YourAppName");
HttpResponseMessage response = await client.GetAsync(apiUrl);
response.EnsureSuccessStatusCode();
string json = await response.Content.ReadAsStringAsync();
var release = JsonConvert.DeserializeObject<GitHubRelease>(json);
return release.TagName;
}
}
catch (Exception ex)
{
// 处理异常
Console.WriteLine($"An error occurred: {ex.Message}");
return null;
}
}
public async Task<string> GetDownloadUrlAsync()
{
try
{
string apiUrl = $"https://api.github.com/repos/{_repositoryOwner}/{_repositoryName}/releases/latest";
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Add("User-Agent", @"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36 Edg/95.0.1020.53");
HttpResponseMessage response = await client.GetAsync(apiUrl);
response.EnsureSuccessStatusCode();
string json = await response.Content.ReadAsStringAsync();
var release = JsonConvert.DeserializeObject<GitHubRelease>(json);
// 遍历附件列表,找到下载链接
foreach (var asset in release.Assets)
{
if (!string.IsNullOrEmpty(asset.DownloadUrl))
{
return asset.DownloadUrl;
}
}
// 如果找不到下载链接,返回 null
return null;
}
}
catch (Exception ex)
{
// 处理异常
Console.WriteLine($"An error occurred: {ex.Message}");
return null;
}
}
public async Task<string> GetReleaseBodyAsync()
{
try
{
string apiUrl = $"https://api.github.com/repos/{_repositoryOwner}/{_repositoryName}/releases/latest";
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Add("User-Agent", @"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36 Edg/95.0.1020.53");
HttpResponseMessage response = await client.GetAsync(apiUrl);
response.EnsureSuccessStatusCode();
string json = await response.Content.ReadAsStringAsync();
var release = JsonConvert.DeserializeObject<GitHubRelease>(json);
return release.Body;
}
}
catch (Exception ex)
{
// 处理异常
Console.WriteLine($"An error occurred: {ex.Message}");
return "";
}
}
private class GitHubRelease
{
[JsonProperty("tag_name")]
public string TagName { get; set; }
[JsonProperty("assets")]
public List<GitHubAsset> Assets { get; set; }
[JsonProperty("body")]
public string Body { get; set; }
}
private class GitHubAsset
{
[JsonProperty("browser_download_url")]
public string DownloadUrl { get; set; }
}
}
}