Catch no-branch cases for Travis

This commit is contained in:
Tom Parker-Shemilt 2020-02-12 23:38:26 +00:00
parent dfabc3b53e
commit 0a527ff6e3

View File

@ -33,6 +33,9 @@ enum CheckerError {
#[fail(display = "travis build is unknown")] #[fail(display = "travis build is unknown")]
TravisBuildUnknown, TravisBuildUnknown,
#[fail(display = "travis build image with no branch")]
TravisBuildNoBranch,
#[fail(display = "github actions image with no branch")] #[fail(display = "github actions image with no branch")]
GithubActionNoBranch, GithubActionNoBranch,
} }
@ -143,16 +146,21 @@ fn get_url(url: String) -> BoxFuture<'static, (String, Result<String, CheckerErr
} }
} }
lazy_static! { lazy_static! {
static ref TRAVIS_IMG_REGEX: Regex = Regex::new(r"https://api.travis-ci.(?:com|org)/[^/]+/.+\.svg").unwrap(); static ref TRAVIS_IMG_REGEX: Regex = Regex::new(r"https://api.travis-ci.(?:com|org)/[^/]+/.+\.svg(\?.+)?").unwrap();
static ref GITHUB_ACTIONS_REGEX: Regex = Regex::new(r"https://github.com/[^/]+/[^/]+/workflows/[^/]+/badge.svg(\?.+)?").unwrap(); static ref GITHUB_ACTIONS_REGEX: Regex = Regex::new(r"https://github.com/[^/]+/[^/]+/workflows/[^/]+/badge.svg(\?.+)?").unwrap();
} }
if TRAVIS_IMG_REGEX.is_match(&url) { if let Some(matches) = TRAVIS_IMG_REGEX.captures(&url) {
let content_dispostion = ok.headers().get(header::CONTENT_DISPOSITION).and_then(|h| h.to_str().ok()).unwrap_or(""); let content_dispostion = ok.headers().get(header::CONTENT_DISPOSITION).and_then(|h| h.to_str().ok()).unwrap_or("");
debug!("Content dispostion for {} is '{}'", content_dispostion, url); debug!("Content dispostion for {} is '{}'", content_dispostion, url);
if content_dispostion == "inline; filename=\"unknown.svg\"" { if content_dispostion == "inline; filename=\"unknown.svg\"" {
res = Err(CheckerError::TravisBuildUnknown); res = Err(CheckerError::TravisBuildUnknown);
break; break;
} }
let query = matches.get(1).map(|x| x.as_str()).unwrap_or("");
if !query.starts_with("?") || query.find("branch=").is_none() {
res = Err(CheckerError::TravisBuildNoBranch);
break;
}
} }
if let Some(matches) = GITHUB_ACTIONS_REGEX.captures(&url) { if let Some(matches) = GITHUB_ACTIONS_REGEX.captures(&url) {
debug!("Github actions match {:?}", matches); debug!("Github actions match {:?}", matches);
@ -266,6 +274,9 @@ async fn main() -> Result<(), Error> {
CheckerError::TravisBuildUnknown => { CheckerError::TravisBuildUnknown => {
format!("[Unknown travis build] {}", url) format!("[Unknown travis build] {}", url)
} }
CheckerError::TravisBuildNoBranch => {
format!("[Travis build image with no branch specified] {}", url)
}
CheckerError::GithubActionNoBranch => { CheckerError::GithubActionNoBranch => {
format!("[Github action image with no branch specified] {}", url) format!("[Github action image with no branch specified] {}", url)
} }