Skip to content

Commit

Permalink
feat: add support for TargetsMetadata
Browse files Browse the repository at this point in the history
The idea being we parse the target file into a struct called
`TargetsMetadata`. Will be used for populating the commit hashes
database.
  • Loading branch information
n-dusan committed Dec 6, 2024
1 parent 7da1c89 commit 116cd36
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 11 deletions.
43 changes: 33 additions & 10 deletions src/stelae/stele.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::path::{Path, PathBuf};

use super::types::repositories::Repository;
use super::types::{repositories::Repository, targets_metadata::TargetsMetadata};
use crate::{
stelae::types::{dependencies::Dependencies, repositories::Repositories},
utils::git::Repo,
Expand Down Expand Up @@ -87,16 +87,39 @@ impl Stele {
/// # Errors
/// Will error if unable to find or parse repositories file at `targets/repositories.json`
pub fn get_repositories(&mut self) -> anyhow::Result<Option<Repositories>> {
let blob = self
let Ok(blob) = self
.auth_repo
.get_bytes_at_path("HEAD", "targets/repositories.json");
if let Ok(repositories_blob) = blob {
let repositories_str = String::from_utf8(repositories_blob)?;
let repositories: Repositories = serde_json::from_str(&repositories_str)?;
self.repositories = Some(repositories.clone());
return Ok(Some(repositories));
}
Ok(None)
.get_bytes_at_path("HEAD", "targets/repositories.json")
else {
return Ok(None);
};
let repositories_str = String::from_utf8(blob)?;
let repositories: Repositories = serde_json::from_str(&repositories_str)?;
self.repositories = Some(repositories.clone());
Ok(Some(repositories))
}

/// Get Stele's targets metadata file at a specific committish and filename.
///
/// # Arguments
/// * `committish` - The committish to look for the targets metadata file.
/// * `path` - The path to the targets metadata file.
///
/// # Returns
/// Returns the targets metadata file if found, or None if not found.
pub fn get_targets_metadata_at_commit_and_filename(
&self,
committish: &str,
filename: &str,
) -> anyhow::Result<Option<TargetsMetadata>> {
let org = &self.auth_repo.org;
let file_path = format!("targets/{org}/{filename}");
let Ok(blob) = self.auth_repo.get_bytes_at_path(committish, &file_path) else {
return Ok(None);
};
let targets_metadata_str = String::from_utf8(blob)?;
let targets_metadata: TargetsMetadata = serde_json::from_str(&targets_metadata_str)?;
Ok(Some(targets_metadata))
}

/// Get Stele's qualified name.
Expand Down
3 changes: 2 additions & 1 deletion src/stelae/types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! The Types module contains data models for Stelae.
//! The `types` module contains data models for stelae.
pub mod dependencies;
pub mod repositories;
pub mod targets_metadata;
39 changes: 39 additions & 0 deletions src/stelae/types/targets_metadata.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//! Targets metadata types.
//! Represents the metadata in the `targets/<org>/<data-repo>` file.
use serde::Deserialize;
use serde_derive::Serialize;

/// An entry in the `targets/<org>/<data-repo>` file.
///
/// Example:
/// ```rust
/// use stelae::stelae::types::targets_metadata::TargetsMetadata;
/// use serde_json::json;
///
/// let data = r#"
/// {
/// "branch": "publication/2024-09-16",
/// "build-date": "2024-09-16",
/// "commit": "1b7334f58f41a53d6e4d9fc11fba6793cb22eb36",
/// "codified-date": "2024-10-03"
/// }
/// "#;
/// let targets_metadata: TargetsMetadata = serde_json::from_str(data).unwrap();
/// assert_eq!(targets_metadata.branch, "publication/2024-09-16");
/// assert_eq!(targets_metadata.build_date.unwrap(), "2024-09-16");
/// assert_eq!(targets_metadata.commit, "1b7334f58f41a53d6e4d9fc11fba6793cb22eb36");
/// assert_eq!(targets_metadata.codified_date.unwrap(), "2024-10-03");
/// ```
#[derive(Serialize, Deserialize, Debug)]
pub struct TargetsMetadata {
/// A git branch name.
pub branch: String,
/// The date the build was created.
#[serde(rename = "build-date")]
pub build_date: Option<String>,
/// The commit hash.
pub commit: String,
/// The date the code was codified.
#[serde(rename = "codified-date")]
pub codified_date: Option<String>,
}

0 comments on commit 116cd36

Please sign in to comment.