Skip to content

Commit edfc2b2

Browse files
committed
context_slug
1 parent 8f48c37 commit edfc2b2

File tree

6 files changed

+82
-77
lines changed

6 files changed

+82
-77
lines changed

lib/api_run/src/run.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use bencher_endpoint::{CorsResponse, Endpoint, Post, ResponseCreated};
2-
use bencher_json::{project, system::auth, JsonNewRun, JsonReport, NameIdKind, ResourceName};
2+
use bencher_json::{
3+
project, system::auth, JsonNewRun, JsonReport, NameIdKind, ReportContext, ResourceName,
4+
};
35
use bencher_rbac::project::Permission;
46
use bencher_schema::{
57
conn_lock,
@@ -67,7 +69,10 @@ async fn post_inner(
6769
.map_err(|e| forbidden_error(e.to_string()))?;
6870
(auth_user, query_project)
6971
},
70-
(Some(auth_user), Some(organization), None) => return Err(bad_request_error("todo")),
72+
(Some(_auth_user), Some(_organization), None) => {
73+
let _project_slug = json_run.context.as_ref().map(ReportContext::slug);
74+
return Err(bad_request_error("Not yet supported"));
75+
},
7176
_ => return Err(bad_request_error("Not yet supported")),
7277
};
7378

lib/bencher_context/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ publish = false
88

99
[features]
1010
client = ["dep:gix", "dep:uuid", "dep:windows"]
11-
server = ["bencher_valid/server"]
11+
server = ["dep:uuid", "bencher_valid/server"]
1212
schema = ["dep:schemars"]
1313

1414
[dependencies]
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use bencher_valid::BASE_36;
2+
use uuid::Uuid;
3+
4+
pub fn encode_uuid(uuid: Uuid) -> String {
5+
let base = BASE_36.len() as u128;
6+
let chars = BASE_36.chars().collect::<Vec<_>>();
7+
8+
let mut num = uuid.as_u128();
9+
let mut result = String::new();
10+
while num > 0 {
11+
let remainder = (num % base) as usize;
12+
if let Some(c) = chars.get(remainder) {
13+
result.push(*c);
14+
}
15+
num /= base;
16+
}
17+
18+
result.chars().rev().collect()
19+
}
20+
21+
#[cfg(test)]
22+
mod tests {
23+
use super::*;
24+
use uuid::Uuid;
25+
26+
#[test]
27+
fn test_encode_uuid() {
28+
let uuid = Uuid::parse_str("00000000-0000-0000-0000-000000000000").unwrap();
29+
assert_eq!(encode_uuid(uuid), "");
30+
31+
let uuid = Uuid::parse_str("ffffffff-ffff-ffff-ffff-ffffffffffff").unwrap();
32+
assert_eq!(encode_uuid(uuid), "f5lxx1zz5pnorynqglhzmsp33");
33+
34+
let uuid = Uuid::parse_str("12345678-1234-5678-1234-567812345678").unwrap();
35+
assert_eq!(encode_uuid(uuid), "12srddy53kndus0lmbgjgy7i0");
36+
}
37+
}

lib/bencher_context/src/server/base_36.rs

Lines changed: 0 additions & 63 deletions
This file was deleted.

lib/bencher_context/src/server/mod.rs

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
1-
use bencher_valid::Slug;
1+
use bencher_valid::{Slug, MAX_LEN};
2+
use uuid::Uuid;
23

34
use crate::{ContextPath, ReportContext};
45

5-
mod base_36;
6+
mod base36;
67

78
#[allow(clippy::multiple_inherent_impl)]
89
impl ReportContext {
910
pub fn slug(&self) -> Slug {
10-
let name = self.repo_name();
11-
let hash = self.repo_hash();
12-
13-
todo!()
11+
// + 42 chars
12+
let name = self.repo_name().map(truncate_name).unwrap_or_default();
13+
// + 1 char (-)
14+
// + 7 chars
15+
let hash = self.repo_hash().map(short_hash).unwrap_or_default();
16+
// + 1 char (-)
17+
// + 13 chars
18+
let fingerprint = self
19+
.testbed_fingerprint()
20+
.map(base36::encode_uuid)
21+
.as_deref()
22+
.map(short_fingerprint)
23+
.unwrap_or_default();
24+
// The spaces here are important,
25+
// in case any of the values are empty
26+
// they will essentially be ignored
27+
let slug = format!("{name} {hash} {fingerprint}");
28+
debug_assert!(slug.len() <= MAX_LEN, "Slug is too long: {slug}");
29+
Slug::new(slug)
1430
}
1531

1632
pub fn repo_name(&self) -> Option<&str> {
@@ -37,9 +53,19 @@ impl ReportContext {
3753
self.0.get(ContextPath::TESTBED_OS).map(String::as_str)
3854
}
3955

40-
pub fn testbed_fingerprint(&self) -> Option<&str> {
41-
self.0
42-
.get(ContextPath::TESTBED_FINGERPRINT)
43-
.map(String::as_str)
56+
pub fn testbed_fingerprint(&self) -> Option<Uuid> {
57+
self.0.get(ContextPath::TESTBED_FINGERPRINT)?.parse().ok()
4458
}
4559
}
60+
61+
fn truncate_name(name: &str) -> String {
62+
name.chars().take(42).collect()
63+
}
64+
65+
fn short_hash(hash: &str) -> String {
66+
hash.chars().take(7).collect()
67+
}
68+
69+
fn short_fingerprint(fingerprint: &str) -> String {
70+
fingerprint.chars().take(13).collect()
71+
}

lib/bencher_valid/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub use secret::Secret;
6060
pub use units::{Units, BYTES, NANOSECONDS, SECONDS};
6161
pub use user_name::UserName;
6262

63-
const MAX_LEN: usize = 64;
63+
pub const MAX_LEN: usize = 64;
6464

6565
#[cfg(feature = "wasm")]
6666
#[wasm_bindgen(start)]

0 commit comments

Comments
 (0)