Skip to content

Commit 9196d48

Browse files
authored
Merge pull request #6 from nickel-lang/index-package-subpaths
Bump nickel and support subpaths in index packages
2 parents aacde38 + f72a9c7 commit 9196d48

File tree

5 files changed

+74
-33
lines changed

5 files changed

+74
-33
lines changed

Cargo.lock

Lines changed: 7 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ clap = { version = "4.5.40", features = ["derive"] }
88
gitpatch = "0.7.1"
99
gix = { version = "0.70.0", features = ["blocking-http-transport-reqwest-rust-tls"]}
1010
miette = { version = "7.6.0", features = ["fancy"] }
11-
nickel-lang-core = "0.13.0"
12-
nickel-lang-git = "0.1.0"
13-
nickel-lang-package = "0.2.0"
11+
# TODO: once there's a nickel release that has support for index package subpaths, switch to that
12+
nickel-lang-core = { git = "https://github.com/tweag/nickel" }
13+
nickel-lang-git= { git = "https://github.com/tweag/nickel" }
14+
nickel-lang-package = { git = "https://github.com/tweag/nickel" }
1415
octocrab = "0.44.1"
1516
serde = "1.0.219"
1617
serde_json = "1.0.140"

flake.nix

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
{
3030
devShells.default = mkShell {
3131
nativeBuildInputs = [
32-
gosmee
3332
rust-toolchain
3433
];
3534
};

src/main.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,18 @@ impl PackageReport {
105105
index: &PackageIndex<Shared>,
106106
pkg: Package,
107107
) -> miette::Result<Self> {
108-
let PreciseId::Github { org, name, .. } = &pkg.id;
108+
let PreciseId::Github {
109+
org, name, path, ..
110+
} = &pkg.id;
109111
let permission =
110112
Permission::check(client, user.to_owned(), org.clone(), name.clone()).await?;
111113

112114
let temp_dir = tempdir().into_diagnostic()?;
113115
let status = if let Err(e) = package::fetch(&pkg, temp_dir.path()) {
114116
PackageStatus::FetchFailed(e.to_string())
115117
} else {
116-
match package::check_manifest(&pkg, temp_dir.path(), index) {
118+
let path = temp_dir.path().join(path);
119+
match package::check_manifest(&pkg, &path, index) {
117120
Ok(c) => PackageStatus::Manifest(Box::new(c)),
118121
Err(e) => PackageStatus::EvalFailed(e.to_string()),
119122
}
@@ -135,12 +138,14 @@ impl PackageReport {
135138
}
136139

137140
fn format(&self, f: &mut std::fmt::Formatter, indent: &str) -> std::fmt::Result {
138-
let PreciseId::Github { org, name, .. } = &self.pkg.id;
141+
let PreciseId::Github {
142+
org, name, path, ..
143+
} = &self.pkg.id;
139144
let perm = &self.permission;
140145
let indent_spaces = " ".repeat(indent.len());
141146
writeln!(
142147
f,
143-
"{}package {org}/{name}, version {}",
148+
"{}package {org}/{name}/{path}, version {}",
144149
indent, self.pkg.version
145150
)?;
146151
if perm.is_allowed {

src/package.rs

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ use std::path::Path;
22

33
use gitpatch::Patch;
44
use miette::{IntoDiagnostic as _, bail};
5-
use nickel_lang_core::error::report::report_as_str;
5+
use nickel_lang_core::error::report::{ColorOpt, report_as_str};
66
use nickel_lang_git::{Spec, Target};
77
use nickel_lang_package::{
88
IndexDependency, ManifestFile,
9-
index::{Package, PackageIndex, PreciseId, Shared, serialize::PackageFormat},
9+
index::{Id, Package, PackageIndex, PreciseId, Shared, serialize::PackageFormat},
1010
manifest::MANIFEST_NAME,
1111
version::SemVer,
1212
};
@@ -27,6 +27,8 @@ pub enum Error {
2727
Deserialize(#[from] serde_json::Error),
2828
#[error("org/name mismatch: path was \"{path}\", package was \"{package}\"")]
2929
OrgNameMismatch { path: String, package: String },
30+
#[error("path too deep: expected three components, got \"{path}\"")]
31+
PathToDeep { path: String },
3032
}
3133

3234
impl<'a> From<gitpatch::ParseError<'a>> for Error {
@@ -53,21 +55,24 @@ pub fn changed_packages(diff: &str) -> Result<Vec<Package>, Error> {
5355
let Some(path_name) = parts.next() else {
5456
return Err(Error::MissingRepo(path.into_owned()));
5557
};
58+
if parts.next().is_some() {
59+
return Err(Error::PathToDeep {
60+
path: path.into_owned(),
61+
});
62+
}
5663

5764
for line in patch.hunks.iter().flat_map(|h| h.lines.iter()) {
5865
match line {
5966
gitpatch::Line::Add(line) => {
6067
let package: PackageFormat = serde_json::from_str(line)?;
6168
let package = Package::from(package);
62-
match &package.id {
63-
PreciseId::Github { org, name, .. } => {
64-
if org != path_org || name != path_name {
65-
return Err(Error::OrgNameMismatch {
66-
path: format!("{path_org}/{path_name}"),
67-
package: format!("{org}/{name}"),
68-
});
69-
}
70-
}
69+
let id = Id::from(package.id.clone());
70+
let package_path = format!("github/{path_org}/{path_name}");
71+
if id.path().to_str() != Some(package_path.as_ref()) {
72+
return Err(Error::OrgNameMismatch {
73+
path: package_path,
74+
package: id.path().display().to_string(),
75+
});
7176
}
7277
ret.push(package);
7378
}
@@ -86,7 +91,12 @@ pub fn changed_packages(diff: &str) -> Result<Vec<Package>, Error> {
8691
/// This uses `nickel_lang_git`, with essentially the same code as nickel's package manager.
8792
/// In particular, this should catch any portability issues like illegal windows filenames.
8893
pub fn fetch(pkg: &Package, path: &Path) -> miette::Result<()> {
89-
let PreciseId::Github { org, name, commit } = &pkg.id;
94+
let PreciseId::Github {
95+
org,
96+
name,
97+
commit,
98+
path: _,
99+
} = &pkg.id;
90100
let url = format!("https://github.com/{org}/{name}.git");
91101
let spec = Spec {
92102
url: url.try_into().into_diagnostic()?,
@@ -182,7 +192,9 @@ impl<T> IntoDiagnostic<T> for Result<T, nickel_lang_package::error::Error> {
182192
Err(nickel_lang_package::error::Error::ManifestEval {
183193
mut files, error, ..
184194
}) => {
185-
bail!(report_as_str(&mut files, error, Default::default()))
195+
// ANSI codes don't seem to get rendered in github comments, so
196+
// turn off color.
197+
bail!(report_as_str(&mut files, *error, ColorOpt::Never))
186198
}
187199
Err(e) => bail!(e.to_string()),
188200
}
@@ -199,6 +211,7 @@ pub fn check_manifest(
199211
path.push(MANIFEST_NAME);
200212

201213
// TODO: report manifest eval errors better
214+
// FIXME: use the subdir here if necessary
202215
let manifest = ManifestFile::from_path(&path).into_diag()?;
203216

204217
let mut dependencies = Vec::new();
@@ -220,6 +233,9 @@ pub fn check_manifest(
220233

221234
#[cfg(test)]
222235
mod tests {
236+
use std::path::PathBuf;
237+
238+
use gix::ObjectId;
223239
use nickel_lang_package::version::SemVer;
224240

225241
use super::*;
@@ -232,6 +248,16 @@ index df1cd2a..2229806 100644
232248
@@ -1 +1,2 @@
233249
{"id":{"github":{"org":"nickel-lang","name":"nickel-schemastore","commit":"3ac728792d4a71f53897b185445b77029c3ce245"}},"version":{"major":0,"minor":1,"patch":0,"pre":""},"minimal_nickel_version":{"major":1,"minor":11,"patch":0,"pre":""},"dependencies":{},"authors":["Théophane Hufschmitt","Yann Hamdaoui <[email protected]>"],"description":"A nickel package containing contracts autogenerated from the Schemastore JSON Schema repository via json-schema-to-nickel.","keywords":["schemastore","schemas","json-schema","contracts"],"license":"MIT","v":0}
234250
+{"id":{"github":{"org":"nickel-lang","name":"nickel-schemastore","commit":"5b5edcba47eb5f957a34a6224b3d9b976a4fc911"}},"version":{"major":0,"minor":2,"patch":0,"pre":""},"minimal_nickel_version":{"major":1,"minor":11,"patch":0,"pre":""},"dependencies":{},"authors":["Théophane Hufschmitt","Yann Hamdaoui <[email protected]>"],"description":"Nickel contracts autogenerated from the Schemastore JSON Schema repository via json-schema-to-nickel","keywords":["schemastore","schemas","json-schema","contracts"],"license":"MIT","v":0}
251+
"#;
252+
253+
const SAMPLE_DIFF_WITH_SUBDIR: &str = r#"
254+
diff --git a/github/nickel-lang/json-schema-to-nickel%@lib b/github/nickel-lang/json-schema-to-nickel%@lib
255+
new file mode 100644
256+
index 0000000..17e1150
257+
--- /dev/null
258+
+++ b/github/nickel-lang/json-schema-to-nickel%@lib
259+
@@ -0,0 +1 @@
260+
+{"id":{"github":{"org":"nickel-lang","name":"json-schema-to-nickel","path":"lib","commit":"7d7c007c1de43aa448df633ddbcb33b54385d8a0"}},"version":{"major":0,"minor":1,"patch":0,"pre":""},"minimal_nickel_version":{"major":1,"minor":12,"patch":0,"pre":""},"dependencies":{},"authors":["The json-schema-to-nickel authors"],"description":"A library of predicates for JSON schema","keywords":[],"license":"","v":0}
235261
"#;
236262

237263
#[test]
@@ -240,4 +266,19 @@ index df1cd2a..2229806 100644
240266
assert_eq!(packages.len(), 1);
241267
assert_eq!(packages[0].version, SemVer::new(0, 2, 0));
242268
}
269+
270+
#[test]
271+
fn test_changed_packages_with_subdir() {
272+
let packages = changed_packages(SAMPLE_DIFF_WITH_SUBDIR).unwrap();
273+
assert_eq!(packages.len(), 1);
274+
assert_eq!(
275+
packages[0].id,
276+
PreciseId::Github {
277+
org: "nickel-lang".to_owned(),
278+
name: "json-schema-to-nickel".to_owned(),
279+
path: PathBuf::from("lib").try_into().unwrap(),
280+
commit: ObjectId::from_hex(b"7d7c007c1de43aa448df633ddbcb33b54385d8a0").unwrap(),
281+
}
282+
);
283+
}
243284
}

0 commit comments

Comments
 (0)