Skip to content

Commit

Permalink
GitHubClient: Replace list filtering with get methods
Browse files Browse the repository at this point in the history
Remove my filtering experiment. Unfortunately, the name filter will only
do an exact match, so it is more like a "get" instead of filtering.
  • Loading branch information
bobbobbio committed Dec 4, 2024
1 parent 15bf68b commit d9152fe
Showing 1 changed file with 40 additions and 19 deletions.
59 changes: 40 additions & 19 deletions crates/maelstrom-github/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,7 @@ impl GitHubClient {
Ok(())
}

/// List all the given artifacts accessible to the current workflow run which match the given
/// name filter. If no filter is provided, they are all returned.
pub async fn list(
async fn list_internal(
&self,
name_filter: Option<String>,
id_filter: Option<DatabaseId>,
Expand All @@ -282,6 +280,35 @@ impl GitHubClient {
Ok(resp.artifacts)
}

/// List all the given artifacts accessible to the current workflow run.
pub async fn list(&self) -> Result<Vec<Artifact>> {
self.list_internal(None, None).await
}

/// Get the artifact represented by the given name if it exists.
pub async fn get(&self, name: &str) -> Result<Option<Artifact>> {
let mut artifacts = self.list_internal(Some(name.into()), None).await?;
if artifacts.is_empty() {
return Ok(None);
}
if artifacts.len() > 1 {
bail!("invalid filtered list response");
}
Ok(Some(artifacts.remove(0)))
}

/// Get the artifact represented by the given id if it exists.
pub async fn get_by_id(&self, id: DatabaseId) -> Result<Option<Artifact>> {
let mut artifacts = self.list_internal(None, Some(id)).await?;
if artifacts.is_empty() {
return Ok(None);
}
if artifacts.len() > 1 {
bail!("invalid filtered list response");
}
Ok(Some(artifacts.remove(0)))
}

/// Start a download of an artifact identified by the given name. The returned [`BlobClient`]
/// should be used to download all or part of the data.
///
Expand Down Expand Up @@ -380,16 +407,20 @@ mod tests {

client.upload("test_data", TEST_DATA).await.unwrap();

let listing = client.list(None, None).await.unwrap();
let listing = client.list().await.unwrap();
println!("got artifact listing {listing:?}");
assert!(listing.iter().find(|a| a.name == "test_data").is_some());

let name_listing = client.list(Some("test_data".into()), None).await.unwrap();
assert_eq!(name_listing.len(), 1, "{name_listing:?}");
let artifact = &name_listing[0];
let artifact = client.get("test_data").await.unwrap().unwrap();

let id_listing = client.list(None, Some(artifact.database_id)).await.unwrap();
assert_eq!(&name_listing, &id_listing);
let artifact2 = client
.get_by_id(artifact.database_id)
.await
.unwrap()
.unwrap();
assert_eq!(&artifact, &artifact2);

assert_eq!(client.get("this_does_not_exist").await.unwrap(), None);

let backend_ids = &artifact.backend_ids;
let mut download_stream = client
Expand All @@ -403,15 +434,5 @@ mod tests {
.unwrap();

assert_eq!(downloaded, TEST_DATA);

// discover how filtering works
client.upload("test_foo", TEST_DATA).await.unwrap();
for f in ["test_", "foo", "data", "^foo", "^test_"] {
let listing = client.list(Some(f.into()), None).await.unwrap();
println!(
"list with filter {f:?}: {:?}",
Vec::from_iter(listing.into_iter().map(|a| a.name))
);
}
}
}

0 comments on commit d9152fe

Please sign in to comment.