Last active
April 6, 2023 00:30
-
-
Save bassuny3003/a122b9d45d5cbcb27dadda01b4c0b2aa to your computer and use it in GitHub Desktop.
Using Github API to get Last Release And Check If The There Is New Version
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Newtonsoft.Json; //NuGet Newtonsoft.Json | |
using RestSharp; //NuGet RestSharp | |
using System; | |
using System.Collections.Generic; | |
using System.Net; | |
using System.Reflection; | |
using System.Text.RegularExpressions; | |
using System.Threading.Tasks; | |
namespace [ YOUR PROJECT NAMESPACE ] | |
{ | |
public class GithubUpdateChecker | |
{ | |
#region Properties | |
public static bool ResponseIsSuccessful { get; private set; } | |
public bool HasNewVersion { get; private set; } | |
public List<dynamic> JsonLastReleasesObj { get; private set; } | |
public string NewVersion { get; private set; } | |
public string NewVersionTitle { get; private set; } | |
public string NewVersionDownloadFileName { get; private set; } | |
public string NewVersionDownloadUrl { get; private set; } | |
public string NewVersionDownloadSize { get; private set; } | |
public string NewVersionPublishedDate { get; private set; } | |
public string NewVersionDescription { get; private set; } | |
#endregion | |
#region Methods | |
public async Task CheckAppNewVersionAsync(string OwnerName, string ProjectRepositoryName) | |
{ | |
var client = new RestClient(); | |
var request = new RestRequest("https://api.github.com/repos/" + OwnerName + "/" + ProjectRepositoryName + "/releases"); | |
RestResponse response = await client.ExecuteGetAsync(request); | |
if (response.IsSuccessful) | |
{ | |
ResponseIsSuccessful = true; | |
JsonLastReleasesObj = JsonConvert.DeserializeObject<List<dynamic>>(response.Content); | |
List<int> currentAppVesion = ExtractVersions(Assembly.GetExecutingAssembly().GetName().Version.ToString()); | |
List<int> newAppVersion = ExtractVersions(JsonLastReleasesObj[0]["tag_name"].ToString()); | |
for (int i = 0; i < currentAppVesion.Count; i++) | |
{ | |
if (Convert.ToInt32(newAppVersion[i]) > Convert.ToInt32(currentAppVesion[i])) | |
{ | |
HasNewVersion = true; | |
NewVersion = JsonLastReleasesObj[0]["tag_name"].ToString(); | |
NewVersionTitle = JsonLastReleasesObj[0]["name"].ToString(); | |
NewVersionPublishedDate = JsonLastReleasesObj[0]["published_at"].ToString(); | |
NewVersionDescription = JsonLastReleasesObj[0]["body"].ToString(); | |
if (JsonLastReleasesObj[0]["assets"].HasValues) | |
{ | |
NewVersionDownloadFileName = JsonLastReleasesObj[0]["assets"][0]["name"].ToString(); | |
NewVersionDownloadUrl = JsonLastReleasesObj[0]["assets"][0]["browser_download_url"].ToString(); | |
NewVersionDownloadSize = JsonLastReleasesObj[0]["assets"][0]["size"].ToString(); | |
} | |
return; | |
} | |
} | |
} | |
else | |
{ | |
ResponseIsSuccessful = false; | |
} | |
} | |
private static List<int> ExtractVersions(string appVersion) | |
{ | |
List<int> appVersionNumbers = new List<int>(); | |
Regex regex = new Regex(@"\d+"); | |
MatchCollection matches = regex.Matches(appVersion); | |
foreach (Match match in matches) | |
{ | |
int number; | |
if (int.TryParse(match.Value, out number)) | |
{ | |
appVersionNumbers.Add(number); | |
} | |
} | |
return appVersionNumbers; | |
} | |
public void DownloadNewVersion(string NewVersionDownloadUrl, string NewVersionSavePath) | |
{ | |
//for Test | |
//string url = "https://github.com/bassuny3003/ShutupLongLink/releases/download/v2.3.3.21/ShutUp.Long.Link.V.2.3.3.21.zip"; | |
//string fileName = "ShutUp.Long.Link.V.2.3.3.21.zip"; | |
using (WebClient client = new WebClient()) | |
{ | |
client.DownloadFileAsync(new Uri(NewVersionDownloadUrl), NewVersionSavePath); | |
} | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment