Repository navigation
feat: add stable release update checks to all apps #232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import Foundation | ||
|
|
||
| struct ReleaseUpdate: Sendable { | ||
| let version: String | ||
| let url: URL | ||
| } | ||
|
|
||
| enum ReleaseUpdates { | ||
| static let latestURL = URL(string: "https://github.com/4cecoder/talkies/releases/latest")! | ||
| private static let apiURL = URL(string: "https://api.github.com/repos/4cecoder/talkies/releases/latest")! | ||
|
|
||
| static func check(currentVersion: String) async throws -> ReleaseUpdate? { | ||
| var request = URLRequest(url: apiURL) | ||
| request.setValue("application/vnd.github+json", forHTTPHeaderField: "Accept") | ||
| request.setValue("Talkies-macOS", forHTTPHeaderField: "User-Agent") | ||
| request.timeoutInterval = 8 | ||
| let (data, response) = try await URLSession.shared.data(for: request) | ||
| guard let response = response as? HTTPURLResponse, (200..<300).contains(response.statusCode), | ||
| let metadata = try JSONSerialization.jsonObject(with: data) as? [String: Any], | ||
| metadata["draft"] as? Bool != true, | ||
| metadata["prerelease"] as? Bool != true, | ||
| let tag = metadata["tag_name"] as? String, | ||
| let page = metadata["html_url"] as? String, | ||
| let url = URL(string: page), url.host == "github.com" else { | ||
| throw URLError(.badServerResponse) | ||
| } | ||
| let version = String(tag.drop(while: { $0 == "v" })) | ||
| return compare(version, currentVersion) > 0 ? ReleaseUpdate(version: version, url: url) : nil | ||
| } | ||
|
|
||
| static func compare(_ lhs: String, _ rhs: String) -> Int { | ||
| let a = lhs.split(separator: ".").map { Int($0) ?? 0 } | ||
| let b = rhs.split(separator: ".").map { Int($0) ?? 0 } | ||
| for index in 0..<max(a.count, b.count) { | ||
| let result = (index < a.count ? a[index] : 0).compare(index < b.count ? b[index] : 0) | ||
| if result != 0 { return result } | ||
| } | ||
| return 0 | ||
| } | ||
| } | ||
|
|
||
| private extension Int { | ||
| func compare(_ other: Int) -> Int { self == other ? 0 : (self < other ? -1 : 1) } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import XCTest | ||
| @testable import Talkies | ||
|
|
||
| final class ReleaseUpdatesTests: XCTestCase { | ||
| func testComparesNumericVersionComponents() { | ||
| XCTAssertEqual(ReleaseUpdates.compare("0.1.10", "0.1.9"), 1) | ||
| XCTAssertEqual(ReleaseUpdates.compare("1.0.0", "1.0.1"), -1) | ||
| XCTAssertEqual(ReleaseUpdates.compare("1.2", "1.2.0"), 0) | ||
| } | ||
| } |
This file contains hidden or 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
44 changes: 44 additions & 0 deletions
44
mobile/android/app/src/main/java/com/talkies/android/ReleaseUpdates.kt
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package com.talkies.android | ||
|
|
||
| import org.json.JSONObject | ||
| import java.net.HttpURLConnection | ||
| import java.net.URL | ||
|
|
||
| internal data class ReleaseUpdate(val version: String, val url: String) | ||
|
|
||
| internal object ReleaseUpdates { | ||
| const val latestReleaseUrl = "https://github.com/4cecoder/talkies/releases/latest" | ||
| private const val apiUrl = "https://api.github.com/repos/4cecoder/talkies/releases/latest" | ||
|
|
||
| fun check(currentVersion: String): ReleaseUpdate? { | ||
| val connection = URL(apiUrl).openConnection() as HttpURLConnection | ||
| try { | ||
| connection.connectTimeout = 5_000 | ||
| connection.readTimeout = 5_000 | ||
| connection.setRequestProperty("Accept", "application/vnd.github+json") | ||
| connection.setRequestProperty("User-Agent", "Talkies-Android") | ||
| if (connection.responseCode !in 200..299) error("Release check failed (${connection.responseCode})") | ||
| val release = JSONObject(connection.inputStream.bufferedReader().use { it.readText() }) | ||
| if (release.optBoolean("draft", false) || release.optBoolean("prerelease", false)) return null | ||
| val version = release.optString("tag_name").removePrefix("v") | ||
| val releaseUrl = release.optString("html_url", latestReleaseUrl) | ||
| require(version.isNotBlank() && releaseUrl.startsWith("https://github.com/4cecoder/talkies/releases/")) { | ||
| "Invalid release metadata" | ||
| } | ||
| return if (compareVersions(version, currentVersion) > 0) ReleaseUpdate(version, releaseUrl) else null | ||
| } finally { | ||
| connection.disconnect() | ||
| } | ||
| } | ||
|
|
||
| internal fun compareVersions(left: String, right: String): Int { | ||
| fun parts(value: String) = value.split('.').map { it.toIntOrNull() ?: 0 } | ||
| val a = parts(left) | ||
| val b = parts(right) | ||
| for (index in 0 until maxOf(a.size, b.size)) { | ||
| val comparison = (a.getOrElse(index) { 0 }).compareTo(b.getOrElse(index) { 0 }) | ||
| if (comparison != 0) return comparison | ||
| } | ||
| return 0 | ||
| } | ||
| } |
12 changes: 12 additions & 0 deletions
12
mobile/android/app/src/test/java/com/talkies/android/ReleaseUpdatesTest.kt
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package com.talkies.android | ||
|
|
||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Test | ||
|
|
||
| class ReleaseUpdatesTest { | ||
| @Test fun comparesNumericVersionsRatherThanStrings() { | ||
| assertEquals(1, ReleaseUpdates.compareVersions("0.1.10", "0.1.9")) | ||
| assertEquals(-1, ReleaseUpdates.compareVersions("1.0.0", "1.0.1")) | ||
| assertEquals(0, ReleaseUpdates.compareVersions("1.2", "1.2.0")) | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
windows/Talkies.Windows.Tests/Services/ReleaseUpdatesTests.cs
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| using Talkies.Windows.Services; | ||
| using Xunit; | ||
|
|
||
| namespace Talkies.Windows.Tests.Services; | ||
|
|
||
| public class ReleaseUpdatesTests | ||
| { | ||
| [Theory] | ||
| [InlineData("0.1.10", "0.1.9", 1)] | ||
| [InlineData("1.0.0", "1.0.1", -1)] | ||
| [InlineData("1.2", "1.2.0", 0)] | ||
| public void Compare_UsesNumericComponents(string left, string right, int expected) => | ||
| Assert.Equal(expected, ReleaseUpdates.Compare(left, right)); | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| using System.IO; | ||
| using System.Net.Http; | ||
| using System.Text.Json; | ||
|
|
||
| namespace Talkies.Windows.Services; | ||
|
|
||
| internal sealed record ReleaseUpdate(string Version, Uri Url); | ||
|
|
||
| internal static class ReleaseUpdates | ||
| { | ||
| internal static readonly Uri LatestUrl = new("https://github.com/4cecoder/talkies/releases/latest"); | ||
| private static readonly Uri ApiUrl = new("https://api.github.com/repos/4cecoder/talkies/releases/latest"); | ||
| private static readonly HttpClient Client = CreateClient(); | ||
|
|
||
| private static HttpClient CreateClient() | ||
| { | ||
| var client = new HttpClient { Timeout = TimeSpan.FromSeconds(8) }; | ||
| client.DefaultRequestHeaders.UserAgent.ParseAdd("Talkies-Windows"); | ||
| client.DefaultRequestHeaders.Accept.ParseAdd("application/vnd.github+json"); | ||
| return client; | ||
| } | ||
|
|
||
| internal static async Task<ReleaseUpdate?> CheckAsync(string currentVersion, CancellationToken cancellationToken = default) | ||
| { | ||
| using var response = await Client.GetAsync(ApiUrl, cancellationToken).ConfigureAwait(false); | ||
| response.EnsureSuccessStatusCode(); | ||
| using var document = JsonDocument.Parse(await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false)); | ||
| var root = document.RootElement; | ||
| if (root.GetProperty("draft").GetBoolean() || root.GetProperty("prerelease").GetBoolean()) return null; | ||
| var version = root.GetProperty("tag_name").GetString()?.TrimStart('v') ?? throw new InvalidDataException("Release version missing"); | ||
| var url = new Uri(root.GetProperty("html_url").GetString() ?? throw new InvalidDataException("Release URL missing")); | ||
| if (url.Scheme != Uri.UriSchemeHttps || url.Host != "github.com" || !url.AbsolutePath.StartsWith("/4cecoder/talkies/releases/", StringComparison.Ordinal)) | ||
| throw new InvalidDataException("Release URL is outside the Talkies GitHub releases page"); | ||
| return Compare(version, currentVersion) > 0 ? new ReleaseUpdate(version, url) : null; | ||
| } | ||
|
|
||
| internal static int Compare(string left, string right) | ||
| { | ||
| static int[] Parts(string value) => value.Split('.').Select(part => int.TryParse(part, out var number) ? number : 0).ToArray(); | ||
| var a = Parts(left); | ||
| var b = Parts(right); | ||
| for (var i = 0; i < Math.Max(a.Length, b.Length); i++) | ||
| { | ||
| var result = (i < a.Length ? a[i] : 0).CompareTo(i < b.Length ? b[i] : 0); | ||
| if (result != 0) return result; | ||
| } | ||
| return 0; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Clear the previous download action when a check fails.
If a check found an update and a later check fails,
availableUpdatekeeps the earlier release. The UI then shows an error but still offers Download Update for that stale result. ClearavailableUpdatewhen the new check starts or fails.🤖 Prompt for AI Agents