Skip to content

Commit

Permalink
feat: inject git rev at build time and expose as zome call (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
8e8b2c authored Sep 2, 2024
1 parent 22334f5 commit 954d181
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 3 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions crates/version_coordinator/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "version_coordinator"
version = "0.0.1"
edition = "2021"

[lib]
crate-type = ["cdylib", "rlib"]
name = "version_coordinator"

[dependencies]
hdk = { workspace = true }
9 changes: 9 additions & 0 deletions crates/version_coordinator/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use std::process::Command;
fn main() {
let output = Command::new("git")
.args(&["rev-parse", "HEAD"])
.output()
.expect("Can get git revision");
let git_hash = String::from_utf8(output.stdout).unwrap();
println!("cargo:rustc-env=GIT_HASH={}", git_hash);
}
7 changes: 7 additions & 0 deletions crates/version_coordinator/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use hdk::prelude::*;

/// Returns the monorepo git commit revision that was injected at build time.
#[hdk_extern]
pub fn git_rev(_: ()) -> ExternResult<String> {
Ok(env!("GIT_HASH").to_string())
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"scripts": {
"build:dna": "scripts/build_dna.sh",
"test:tryorama": "npm run build:dna && npm run -w @holoom/types build && npm run -w @holoom/client -w @holoom/authority build && npm t -w @holoom/tryorama",
"test:tryorama": "npm run build:dna && npm run prepare-build:types && npm run -w @holoom/client -w @holoom/authority build && npm t -w @holoom/tryorama",
"test:dna": "npm run build:dna && cargo nextest run -j 1",
"prepare-build:types": "rimraf crates/holoom_types/bindings && cargo test --package holoom_types && npm run -w @holoom/types prepare:bindings && npm run -w @holoom/types build",
"build:client": "npm run build -w @holoom/client",
Expand Down
1 change: 0 additions & 1 deletion packages/tryorama/src/signer/sign.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { expect, test } from "vitest";
import { runScenario } from "@holochain/tryorama";

import { setupAliceOnly } from "../utils/setup-happ.js";
import { bindCoordinators } from "../utils/bindings.js";
import { sha512 } from "@noble/hashes/sha512";
import * as ed from "@noble/ed25519";
import { encode } from "@msgpack/msgpack";
Expand Down
7 changes: 6 additions & 1 deletion packages/tryorama/src/utils/bindings.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { AppClient } from "@holochain/client";
import { Player } from "@holochain/tryorama";
import { UsernameRegistryCoordinator, SignerCoordinator } from "@holoom/types";
import {
UsernameRegistryCoordinator,
SignerCoordinator,
VersionCoordinator,
} from "@holoom/types";

export function bindCoordinators(player: Player) {
const appClient = player.appWs as AppClient;
return {
signer: new SignerCoordinator(appClient),
usernameRegistry: new UsernameRegistryCoordinator(appClient),
version: new VersionCoordinator(appClient),
};
}
16 changes: 16 additions & 0 deletions packages/tryorama/src/version/git-rev.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { expect, test } from "vitest";
import { runScenario } from "@holochain/tryorama";
import { execSync } from "node:child_process";

import { setupAliceOnly } from "../utils/setup-happ.js";

test("Injected git revision matches monorepo's", async () => {
await runScenario(async (scenario) => {
const { aliceCoordinators } = await setupAliceOnly(scenario);

const revision = await execSync("git rev-parse HEAD").toString().trim();
expect(/^[a-f0-9]{40}/.test(revision)).toBe(true);

await expect(aliceCoordinators.version.gitRev()).resolves.toBe(revision);
});
});
18 changes: 18 additions & 0 deletions packages/types/src/zome-functions/VersionCoordinator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { AppClient } from "@holochain/client";

export class VersionCoordinator {
constructor(
private readonly client: AppClient,
private readonly roleName = "holoom",
private readonly zomeName = "version",
) {}

async gitRev(): Promise<string> {
return this.client.callZome({
role_name: this.roleName,
zome_name: this.zomeName,
fn_name: "git_rev",
payload: null,
});
}
}
1 change: 1 addition & 0 deletions packages/types/src/zome-functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from "./PingCoordinator";
export * from "./RecordsCoordinator";
export * from "./SignerCoordinator";
export * from "./UsernameRegistryCoordinator";
export * from "./VersionCoordinator";
3 changes: 3 additions & 0 deletions workdir/dna.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ coordinator:
- name: ping
hash: ~
bundled: "../target/wasm32-unknown-unknown/release/ping_coordinator.wasm"
- name: version
hash: ~
bundled: "../target/wasm32-unknown-unknown/release/version_coordinator.wasm"

0 comments on commit 954d181

Please sign in to comment.