Skip to content

Commit

Permalink
Support shortened commit sha as input
Browse files Browse the repository at this point in the history
  • Loading branch information
Arrowana committed Oct 12, 2023
1 parent eceaa76 commit 518b390
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
29 changes: 27 additions & 2 deletions avm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,15 @@ pub enum InstallTarget {
Commit(String),
}

pub fn check_commit(commit: &str) -> Result<()> {
#[derive(Deserialize)]
struct GetCommitResponse {
sha: String,
}

/// The commit sha provided can be shortened,
///
/// returns the full commit sha3 for unique versioning downstream
pub fn check_and_get_full_commit(commit: &str) -> Result<String> {
let client = reqwest::blocking::Client::new();
let response = client
.get(format!(
Expand All @@ -103,7 +111,8 @@ pub fn check_commit(commit: &str) -> Result<()> {
response.text().unwrap()
));
};
Ok(())
let get_commit_response: GetCommitResponse = response.json().unwrap();
Ok(get_commit_response.sha)
}

fn get_anchor_version_from_commit(commit: &str) -> Version {
Expand Down Expand Up @@ -414,4 +423,20 @@ mod tests {
"0.28.0-e1afcbf71e0f2e10fae14525934a6a68479167b9"
)
}

#[test]
fn test_check_and_get_full_commit_when_full_commit() {
assert_eq!(
check_and_get_full_commit("e1afcbf71e0f2e10fae14525934a6a68479167b9").unwrap(),
"e1afcbf71e0f2e10fae14525934a6a68479167b9"
)
}

#[test]
fn test_check_and_get_full_commit_when_partial_commit() {
assert_eq!(
check_and_get_full_commit("e1afcbf").unwrap(),
"e1afcbf71e0f2e10fae14525934a6a68479167b9"
)
}
}
4 changes: 2 additions & 2 deletions avm/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ fn parse_install_target(version_or_commit: &str) -> Result<InstallTarget, Error>
parse_version(version_or_commit)
.map(InstallTarget::Version)
.or_else(|version_error| {
avm::check_commit(version_or_commit)
.map(|()| InstallTarget::Commit(version_or_commit.into()))
avm::check_and_get_full_commit(version_or_commit)
.map(InstallTarget::Commit)
.or_else(|commit_error| {
Err(anyhow!(
"Not a valid version or commit: {version_error}, {commit_error}"
Expand Down

0 comments on commit 518b390

Please sign in to comment.