Skip to content

Commit

Permalink
fix(elan-utils/utils): go back to HTML parsing since lean-nightly doe…
Browse files Browse the repository at this point in the history
…s not have a latest (stable) release...
  • Loading branch information
Kha committed Mar 21, 2019
1 parent 9c56b05 commit 7e3b27a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# Unreleased

## Changed

- Fix release lookup once more with feeling

# 0.7.4 - 2019-03-20

## Changed
Expand Down
25 changes: 13 additions & 12 deletions src/elan-utils/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,32 +528,33 @@ pub fn toolchain_sort<T: AsRef<str>>(v: &mut Vec<T>) {
});
}

// fetch from HTTP redirect header instead of Github API to avoid rate limiting
// fetch from HTML page instead of Github API to avoid rate limit
pub fn fetch_latest_release_tag(repo_slug: &str) -> Result<String> {
use regex::Regex;

let latest_url = format!("https://github.com/{}/releases/latest", repo_slug);
let mut tag: Option<String> = None;
let re = Regex::new(r#"Location:.*/tag/(.+)"#).unwrap();

let mut data = Vec::new();
::download::curl::EASY.with(|handle| {
let mut handle = handle.borrow_mut();
handle.url(&latest_url).unwrap();
handle.follow_location(false).unwrap();
handle.follow_location(true).unwrap();
{
let mut transfer = handle.transfer();
transfer.header_function(|header| {
if tag.is_some() { return true }
if let Ok(header) = ::std::str::from_utf8(&header) {
let capture = re.captures(&header);
tag = capture.map(|cap| cap.get(1).unwrap().as_str().trim().to_string());
}
true
transfer.write_function(|new_data| {
data.extend_from_slice(new_data);
Ok(new_data.len())
}).unwrap();
transfer.perform().unwrap();
}
});
tag.ok_or("failed to parse latest release tag".into())
let redirect = ::std::str::from_utf8(&data).chain_err(|| "failed to decode release tag response")?;
let re = Regex::new(r#"/tag/([-a-z0-9.]+)"#).unwrap();
let capture = re.captures(&redirect);
match capture {
Some(cap) => Ok(cap.get(1).unwrap().as_str().to_string()),
None => Err("failed to parse latest release tag".into()),
}
}

#[cfg(test)]
Expand Down

0 comments on commit 7e3b27a

Please sign in to comment.