From 75ad38e3386b809c7dea44d46450af1aec464261 Mon Sep 17 00:00:00 2001 From: Andreas Coroiu Date: Thu, 24 Oct 2024 09:30:44 +0200 Subject: [PATCH] [PM-12989] Allow clients to get SDK version (#5) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 🎟ī¸ Tracking ## 📔 Objective Adds `client.function()` to wasm internal ## ⏰ Reminders before review - Contributor guidelines followed - All formatters and local linters executed and passed - Written new unit and / or integration tests where applicable - Protected functional changes with optionality (feature flags) - Used internationalization (i18n) for all UI strings - CI builds passed - Communicated to DevOps any deployment requirements - Updated any necessary documentation (Confluence, contributing docs) or informed the documentation team ## đŸĻŽ Reviewer guidelines - 👍 (`:+1:`) or similar for great changes - 📝 (`:memo:`) or ℹī¸ (`:information_source:`) for notes or general info - ❓ (`:question:`) for questions - 🤔 (`:thinking:`) or 💭 (`:thought_balloon:`) for more open inquiry that's not quite a confirmed issue and could potentially benefit from discussion - 🎨 (`:art:`) for suggestions / improvements - ❌ (`:x:`) or ⚠ī¸ (`:warning:`) for more significant problems or concerns needing attention - 🌱 (`:seedling:`) or â™ģī¸ (`:recycle:`) for future improvements or indications of technical debt - ⛏ (`:pick:`) for minor or nitpick changes --- .github/workflows/build-wasm-internal.yml | 17 +++++++ crates/bitwarden-wasm-internal/build.rs | 53 ++++++++++++++++++++ crates/bitwarden-wasm-internal/src/client.rs | 4 ++ 3 files changed, 74 insertions(+) create mode 100644 crates/bitwarden-wasm-internal/build.rs diff --git a/.github/workflows/build-wasm-internal.yml b/.github/workflows/build-wasm-internal.yml index 2262babc..8afcfaa2 100644 --- a/.github/workflows/build-wasm-internal.yml +++ b/.github/workflows/build-wasm-internal.yml @@ -23,6 +23,23 @@ jobs: - name: Checkout repo uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1 + - name: Set version (PR) + if: ${{ github.event_name == 'pull_request' }} + run: | + echo REF_NAME="${{ github.event.pull_request.head.ref }}" >> $GITHUB_ENV + echo SHA="${{ github.event.pull_request.head.sha }}" >> $GITHUB_ENV + + - name: Set env variables (Branch/Tag) + if: ${{ github.event_name == 'push' }} + run: | + echo REF_NAME="${GITHUB_REF_NAME}" >> $GITHUB_ENV + echo SHA="${GITHUB_SHA}" >> $GITHUB_ENV + + - name: Set version + run: | + echo SDK_VERSION="${REF_NAME} (${SHA:0:7})" >> $GITHUB_ENV + echo "SDK_VERSION=${SDK_VERSION}" + - name: Setup Node uses: actions/setup-node@0a44ba7841725637a19e28fa30b79a866c81b0a6 # v4.0.4 with: diff --git a/crates/bitwarden-wasm-internal/build.rs b/crates/bitwarden-wasm-internal/build.rs new file mode 100644 index 00000000..7df4fb42 --- /dev/null +++ b/crates/bitwarden-wasm-internal/build.rs @@ -0,0 +1,53 @@ +use std::{env, process::Command}; + +fn main() { + // Use the SDK_VERSION environment variable if it is set (e.g. by CI) or get it from Git + let sdk_version = env::var("SDK_VERSION") + .or_else(|_| version_from_git_info()) + .unwrap_or("unknown".to_string()); + + println!("cargo:rustc-env=SDK_VERSION={sdk_version}"); + println!("cargo:rustc-env=CARGO_PKG_VERSION={sdk_version}"); +} + +fn run(args: &[&str]) -> Result { + use std::io::{Error, ErrorKind}; + let out = Command::new(args[0]).args(&args[1..]).output()?; + if !out.status.success() { + return Err(Error::new(ErrorKind::Other, "Command not successful")); + } + Ok(String::from_utf8(out.stdout) + .map_err(|e| Error::new(ErrorKind::Other, e.to_string()))? + .trim() + .to_string()) +} + +/// This method reads info from Git, namely tags, branch, and revision +/// To access these values, use: +/// - `env!("GIT_EXACT_TAG")` +/// - `env!("GIT_BRANCH")` +/// - `env!("GIT_REV")` +fn version_from_git_info() -> Result { + // The exact tag for the current commit, can be empty when + // the current commit doesn't have an associated tag + let exact_tag = run(&["git", "describe", "--abbrev=0", "--tags", "--exact-match"]).ok(); + if let Some(ref exact) = exact_tag { + println!("cargo:rustc-env=GIT_EXACT_TAG={exact}"); + } + + // The current branch name + let branch = run(&["git", "rev-parse", "--abbrev-ref", "HEAD"])?; + println!("cargo:rustc-env=GIT_BRANCH={branch}"); + + // The current git commit hash + let rev = run(&["git", "rev-parse", "HEAD"])?; + let rev_short = rev.get(..8).unwrap_or_default(); + println!("cargo:rustc-env=GIT_REV={rev_short}"); + + // Combined version + if let Some(exact) = exact_tag { + Ok(exact) + } else { + Ok(format!("{branch} ({rev_short})")) + } +} diff --git a/crates/bitwarden-wasm-internal/src/client.rs b/crates/bitwarden-wasm-internal/src/client.rs index 0a5ce27d..a824b427 100644 --- a/crates/bitwarden-wasm-internal/src/client.rs +++ b/crates/bitwarden-wasm-internal/src/client.rs @@ -49,6 +49,10 @@ impl BitwardenClient { msg } + pub fn version(&self) -> String { + env!("SDK_VERSION").to_owned() + } + pub fn throw(&self, msg: String) -> Result<(), crate::error::GenericError> { Err(crate::error::GenericError(msg)) }