From e687c6d4d5eead59eaccc54e87998a5457d8224e Mon Sep 17 00:00:00 2001 From: Philipp Hoenisch Date: Wed, 20 Sep 2023 10:13:49 +0200 Subject: [PATCH] feat: also return commit hash and number in version api --- CHANGELOG.md | 1 + coordinator/build.rs | 25 ++++++++++++------------ coordinator/src/routes.rs | 15 ++++++++++++-- mobile/lib/main.dart | 15 +++++++------- mobile/lib/util/coordinator_version.dart | 9 +++++++++ 5 files changed, 44 insertions(+), 21 deletions(-) create mode 100644 mobile/lib/util/coordinator_version.dart diff --git a/CHANGELOG.md b/CHANGELOG.md index 326398fdf..ca4b1adc0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Changed expiry to next Sunday 3 pm UTC - Automatically rollover if user opens app during rollover weekend - Sync position with dlc channel state +- Extend coordinator's `/api/version` with commit hash and number ## [1.2.6] - 2023-09-06 diff --git a/coordinator/build.rs b/coordinator/build.rs index 186ba3285..6d06ce39d 100644 --- a/coordinator/build.rs +++ b/coordinator/build.rs @@ -1,14 +1,15 @@ -// This file is used to generate the environment variables for the Rust analyzer -// to make autometrics docstrings resolve the chosen Prometheus URL. - +use std::process::Command; fn main() { - // Uncomment the `premetheus_url` line with the desired URL - // Note: Reload Rust analyzer after changing the Prometheus URL to regenerate the links - - // regtest URL - let prometheus_url = "http://testnet.itchysats.network:9090"; - - // local debugging - // let prometheus_url = "http://localhost:9090"; - println!("cargo:rustc-env=PROMETHEUS_URL={prometheus_url}"); + let output = Command::new("git") + .args(["rev-parse", "HEAD"]) + .output() + .expect("To be able to get commit hash"); + let git_hash = String::from_utf8(output.stdout).expect("To be a valid string"); + let output = Command::new("git") + .args(["rev-parse", "--abbrev-ref", "HEAD"]) + .output() + .expect("To be able to get branch name"); + let branch_name = String::from_utf8(output.stdout).expect("To be a valid string"); + println!("cargo:rustc-env=COMMIT_HASH={}", git_hash); + println!("cargo:rustc-env=BRANCH_NAME={}", branch_name); } diff --git a/coordinator/src/routes.rs b/coordinator/src/routes.rs index 9320f0164..c40f564c6 100644 --- a/coordinator/src/routes.rs +++ b/coordinator/src/routes.rs @@ -509,6 +509,17 @@ pub async fn get_health() -> Result, AppError> { Ok(Json("Server is healthy".to_string())) } -pub async fn version() -> Result, AppError> { - Ok(Json(env!("CARGO_PKG_VERSION").to_string())) +#[derive(Serialize)] +pub struct Version { + version: String, + commit_hash: String, + branch: String, +} + +pub async fn version() -> Result, AppError> { + Ok(Json(Version { + version: env!("CARGO_PKG_VERSION").to_string(), + commit_hash: env!("COMMIT_HASH").to_string(), + branch: env!("BRANCH_NAME").to_string(), + })) } diff --git a/mobile/lib/main.dart b/mobile/lib/main.dart index ecf05701c..ae61dea76 100644 --- a/mobile/lib/main.dart +++ b/mobile/lib/main.dart @@ -48,6 +48,7 @@ import 'package:get_10101/features/wallet/wallet_screen.dart'; import 'package:get_10101/features/wallet/wallet_theme.dart'; import 'package:get_10101/features/welcome/welcome_screen.dart'; import 'package:get_10101/ffi.dart' as rust; +import 'package:get_10101/util/coordinator_version.dart'; import 'package:get_10101/util/constants.dart'; import 'package:get_10101/util/environment.dart'; import 'package:get_10101/util/notifications.dart'; @@ -320,7 +321,7 @@ LogLevel mapLogLevel(String level) { /// Compare the version of the coordinator with the version of the app /// -/// - If the coordinator is newer, suggest to update the app.k +/// - If the coordinator is newer, suggest to update the app. /// - If the app is newer, log it. /// - If the coordinator cannot be reached, show a warning that the app may not function properly. Future compareCoordinatorVersion(bridge.Config config) async { @@ -331,17 +332,17 @@ Future compareCoordinatorVersion(bridge.Config config) async { ); final clientVersion = Version.parse(packageInfo.version); - final coordinatorVersion = Version.parse(jsonDecode(response.body)); - FLog.info(text: "Coordinator version: ${coordinatorVersion.toString()}"); + final coordinatorVersion = CoordinatorVersion.fromJson(jsonDecode(response.body)); + FLog.info(text: "Coordinator version: ${coordinatorVersion.version.toString()}"); - if (coordinatorVersion > clientVersion) { + if (coordinatorVersion.version > clientVersion) { FLog.warning(text: "Client out of date. Current version: ${clientVersion.toString()}"); showDialog( context: shellNavigatorKey.currentContext!, builder: (context) => AlertDialog( title: const Text("Update available"), content: Text("A new version of 10101 is available: " - "${coordinatorVersion.toString()}.\n\n" + "${coordinatorVersion.version.toString()}.\n\n" "Please note that if you do not update 10101, the app" " may not function properly."), actions: [ @@ -350,8 +351,8 @@ Future compareCoordinatorVersion(bridge.Config config) async { child: const Text('OK'), ), ])); - } else if (coordinatorVersion < clientVersion) { - FLog.warning(text: "10101 is newer than LSP: ${coordinatorVersion.toString()}"); + } else if (coordinatorVersion.version < clientVersion) { + FLog.warning(text: "10101 is newer than LSP: ${coordinatorVersion.version.toString()}"); } else { FLog.info(text: "Client is up to date: ${clientVersion.toString()}"); } diff --git a/mobile/lib/util/coordinator_version.dart b/mobile/lib/util/coordinator_version.dart new file mode 100644 index 000000000..c5002e30f --- /dev/null +++ b/mobile/lib/util/coordinator_version.dart @@ -0,0 +1,9 @@ +import 'package:version/version.dart'; + +class CoordinatorVersion { + final Version version; + + CoordinatorVersion(this.version); + + CoordinatorVersion.fromJson(Map json) : version = Version.parse(json['version']); +}