1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//! Project and version metadata records.
//!
//! Builds `com.nahdig.release.projectMeta` records and the `metadata`/`target`
//! fragments embedded in `software.release.manifest` artifacts. Nothing here
//! performs I/O; callers write the records and index the resulting links.
//!
//! Same-DID and immutability preflight helpers live here so ingestion can
//! reject cross-repository references and overwrites of immutable
//! manifest/version records before a PDS write.
use anyhow::{Result, bail};
use serde_json::{Map, Value, json};
use crate::constants::{MANIFEST_COLLECTION, PROJECT_COLLECTION, VERSION_COLLECTION};
/// Collection for project metadata records; the record key is the project slug.
pub const PROJECT_META_COLLECTION: &str = "com.nahdig.release.projectMeta";
/// Project-level metadata. Every field is optional; absent fields are omitted
/// from the record rather than written empty.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct ProjectMeta {
pub description: Option<String>,
pub homepage: Option<String>,
pub license: Option<String>,
pub repository: Option<String>,
pub versioning_scheme: Option<String>,
}
/// Pinned Git source facts for an artifact, matching the
/// `com.nahdig.release.gitMeta` object.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct GitMeta {
pub repository: String,
pub revision: Option<String>,
pub commit: Option<String>,
}
/// Coarse runtime target, matching `software.release.defs#target`.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Target {
pub os: Option<String>,
pub architectures: Vec<String>,
}
/// Everything known about one published version.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct VersionMeta {
pub published_at: Option<String>,
pub raw_version: String,
pub target: Option<Target>,
pub git: Option<GitMeta>,
pub changelog_uri: Option<String>,
}
fn project_uri(repo_did: &str, slug: &str) -> String {
format!("at://{repo_did}/{PROJECT_COLLECTION}/{slug}")
}
fn insert_optional(object: &mut Map<String, Value>, key: &str, value: Option<&String>) {
if let Some(value) = value.filter(|value| !value.is_empty()) {
object.insert(key.to_owned(), Value::String(value.clone()));
}
}
/// Build a `com.nahdig.release.projectMeta` record keyed by the project slug.
///
/// `project` is the same-DID `software.release.project` AT-URI and `name` is
/// the slug, which is also the rkey.
pub fn project_meta_record(repo_did: &str, slug: &str, meta: &ProjectMeta) -> Value {
let mut record = Map::new();
record.insert("$type".to_owned(), json!(PROJECT_META_COLLECTION));
record.insert("project".to_owned(), json!(project_uri(repo_did, slug)));
record.insert("name".to_owned(), json!(slug));
insert_optional(&mut record, "description", meta.description.as_ref());
insert_optional(&mut record, "homepage", meta.homepage.as_ref());
insert_optional(&mut record, "license", meta.license.as_ref());
insert_optional(&mut record, "repository", meta.repository.as_ref());
insert_optional(
&mut record,
"versioningScheme",
meta.versioning_scheme.as_ref(),
);
Value::Object(record)
}
/// Git metadata fragment for `httpArtifact.metadata`/`blobArtifact.metadata`.
///
/// The fragment carries no `$type`: the artifact metadata union is open, so
/// the embedding lexicon does not require one.
pub fn git_metadata_json(git: &GitMeta) -> Value {
let mut fragment = Map::new();
fragment.insert("repository".to_owned(), json!(git.repository));
insert_optional(&mut fragment, "revision", git.revision.as_ref());
insert_optional(&mut fragment, "commit", git.commit.as_ref());
Value::Object(fragment)
}
fn target_json(target: &Target) -> Value {
let mut value = Map::new();
insert_optional(&mut value, "os", target.os.as_ref());
if !target.architectures.is_empty() {
value.insert("architectures".to_owned(), json!(target.architectures));
}
Value::Object(value)
}
/// Attach optional git metadata and runtime target to manifest artifacts.
///
/// Input fragments (`software.release.defs#httpArtifact` or `#blobArtifact`)
/// are cloned with their `$type` and core fields intact. A git fragment is
/// appended to any existing `metadata` array.
pub fn build_manifest_artifacts(
artifacts: &[Value],
git: Option<&GitMeta>,
target: Option<&Target>,
) -> Vec<Value> {
artifacts
.iter()
.map(|artifact| {
let mut artifact = artifact.clone();
if !artifact.is_object() {
return artifact;
}
if let Some(git) = git {
let fragment = git_metadata_json(git);
match artifact.get_mut("metadata") {
Some(Value::Array(items)) => items.push(fragment),
_ => {
artifact["metadata"] = Value::Array(vec![fragment]);
}
}
}
if let Some(target) = target {
artifact["target"] = target_json(target);
}
artifact
})
.collect()
}
/// Expose version facts for callers building `com.nahdig.cve.atprotoV1`
/// `versionLabels` entries. This never builds or edits a CVE record.
pub fn version_meta_json(meta: &VersionMeta) -> Value {
let mut value = Map::new();
if !meta.raw_version.is_empty() {
value.insert("versionLabels".to_owned(), json!([meta.raw_version]));
}
insert_optional(&mut value, "publishedAt", meta.published_at.as_ref());
insert_optional(&mut value, "changelog", meta.changelog_uri.as_ref());
if let Some(target) = &meta.target {
value.insert("target".to_owned(), target_json(target));
}
if let Some(git) = &meta.git {
value.insert("git".to_owned(), git_metadata_json(git));
}
Value::Object(value)
}
/// Reject any AT-URI that does not belong to `repo_did`.
pub fn assert_same_did(repo_did: &str, uri: &str) -> Result<()> {
let Some(rest) = uri.strip_prefix("at://") else {
bail!("not an at-uri: {uri:?}");
};
let Some((did, _)) = rest.split_once('/') else {
bail!("at-uri has no collection: {uri:?}");
};
if did != repo_did {
bail!("cross-DID reference {uri:?}: expected {repo_did}, got {did}");
}
Ok(())
}
/// Manifest and version records are write-once.
pub fn is_immutable(collection: &str) -> bool {
matches!(collection, MANIFEST_COLLECTION | VERSION_COLLECTION)
}
/// Preflight a put: refuse to overwrite an existing immutable record.
pub fn assert_put_allowed(collection: &str, exists: bool) -> Result<()> {
if exists && is_immutable(collection) {
bail!("{collection} is immutable: refusing to overwrite an existing record");
}
Ok(())
}