Skip to content

Commit 75ad38e

Browse files
authored
[PM-12989] Allow clients to get SDK version (#5)
## 🎟️ Tracking <!-- Paste the link to the Jira or GitHub issue or otherwise describe / point to where this change is coming from. --> ## 📔 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 <!-- Suggested interactions but feel free to use (or not) as you desire! --> - 👍 (`:+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
1 parent 3800954 commit 75ad38e

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

.github/workflows/build-wasm-internal.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,23 @@ jobs:
2323
- name: Checkout repo
2424
uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
2525

26+
- name: Set version (PR)
27+
if: ${{ github.event_name == 'pull_request' }}
28+
run: |
29+
echo REF_NAME="${{ github.event.pull_request.head.ref }}" >> $GITHUB_ENV
30+
echo SHA="${{ github.event.pull_request.head.sha }}" >> $GITHUB_ENV
31+
32+
- name: Set env variables (Branch/Tag)
33+
if: ${{ github.event_name == 'push' }}
34+
run: |
35+
echo REF_NAME="${GITHUB_REF_NAME}" >> $GITHUB_ENV
36+
echo SHA="${GITHUB_SHA}" >> $GITHUB_ENV
37+
38+
- name: Set version
39+
run: |
40+
echo SDK_VERSION="${REF_NAME} (${SHA:0:7})" >> $GITHUB_ENV
41+
echo "SDK_VERSION=${SDK_VERSION}"
42+
2643
- name: Setup Node
2744
uses: actions/setup-node@0a44ba7841725637a19e28fa30b79a866c81b0a6 # v4.0.4
2845
with:
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use std::{env, process::Command};
2+
3+
fn main() {
4+
// Use the SDK_VERSION environment variable if it is set (e.g. by CI) or get it from Git
5+
let sdk_version = env::var("SDK_VERSION")
6+
.or_else(|_| version_from_git_info())
7+
.unwrap_or("unknown".to_string());
8+
9+
println!("cargo:rustc-env=SDK_VERSION={sdk_version}");
10+
println!("cargo:rustc-env=CARGO_PKG_VERSION={sdk_version}");
11+
}
12+
13+
fn run(args: &[&str]) -> Result<String, std::io::Error> {
14+
use std::io::{Error, ErrorKind};
15+
let out = Command::new(args[0]).args(&args[1..]).output()?;
16+
if !out.status.success() {
17+
return Err(Error::new(ErrorKind::Other, "Command not successful"));
18+
}
19+
Ok(String::from_utf8(out.stdout)
20+
.map_err(|e| Error::new(ErrorKind::Other, e.to_string()))?
21+
.trim()
22+
.to_string())
23+
}
24+
25+
/// This method reads info from Git, namely tags, branch, and revision
26+
/// To access these values, use:
27+
/// - `env!("GIT_EXACT_TAG")`
28+
/// - `env!("GIT_BRANCH")`
29+
/// - `env!("GIT_REV")`
30+
fn version_from_git_info() -> Result<String, std::io::Error> {
31+
// The exact tag for the current commit, can be empty when
32+
// the current commit doesn't have an associated tag
33+
let exact_tag = run(&["git", "describe", "--abbrev=0", "--tags", "--exact-match"]).ok();
34+
if let Some(ref exact) = exact_tag {
35+
println!("cargo:rustc-env=GIT_EXACT_TAG={exact}");
36+
}
37+
38+
// The current branch name
39+
let branch = run(&["git", "rev-parse", "--abbrev-ref", "HEAD"])?;
40+
println!("cargo:rustc-env=GIT_BRANCH={branch}");
41+
42+
// The current git commit hash
43+
let rev = run(&["git", "rev-parse", "HEAD"])?;
44+
let rev_short = rev.get(..8).unwrap_or_default();
45+
println!("cargo:rustc-env=GIT_REV={rev_short}");
46+
47+
// Combined version
48+
if let Some(exact) = exact_tag {
49+
Ok(exact)
50+
} else {
51+
Ok(format!("{branch} ({rev_short})"))
52+
}
53+
}

crates/bitwarden-wasm-internal/src/client.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ impl BitwardenClient {
4949
msg
5050
}
5151

52+
pub fn version(&self) -> String {
53+
env!("SDK_VERSION").to_owned()
54+
}
55+
5256
pub fn throw(&self, msg: String) -> Result<(), crate::error::GenericError> {
5357
Err(crate::error::GenericError(msg))
5458
}

0 commit comments

Comments
 (0)