1
0
Fork 0
mirror of https://github.com/rust-unofficial/awesome-rust.git synced 2024-11-27 11:44:52 -05:00

Add workaround for github actions

This commit is contained in:
Tom Parker-Shemilt 2020-01-13 22:13:00 +00:00
parent ff703bac03
commit c25072ec06
2 changed files with 45 additions and 32 deletions

View file

@ -16,3 +16,4 @@ lazy_static = "1"
env_logger = "0.7" env_logger = "0.7"
async-std = "1" async-std = "1"
log = "0.4" log = "0.4"
regex = "1"

View file

@ -1,7 +1,6 @@
use pulldown_cmark::{Parser, Event, Tag}; use pulldown_cmark::{Parser, Event, Tag};
use std::fs; use std::fs;
use futures::future::select_all; use futures::future::{select_all, BoxFuture, FutureExt};
use futures::future::FutureExt;
use std::collections::{BTreeSet, BTreeMap}; use std::collections::{BTreeSet, BTreeMap};
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
use anyhow::Result; use anyhow::Result;
@ -12,6 +11,7 @@ use std::time;
use log::{warn, debug}; use log::{warn, debug};
use std::io::Write; use std::io::Write;
use reqwest::{Client, redirect::Policy, StatusCode, header}; use reqwest::{Client, redirect::Policy, StatusCode, header};
use regex::Regex;
struct MaxHandles { struct MaxHandles {
remaining: AtomicU32 remaining: AtomicU32
@ -65,7 +65,8 @@ fn to_anyhow<T, E>(res: std::result::Result<T, E>) -> Result<T>
res.map_err(|x| Into::<anyhow::Error>::into(x)) res.map_err(|x| Into::<anyhow::Error>::into(x))
} }
async fn get_url(url: String) -> (String, Result<String>) { fn get_url(url: String) -> BoxFuture<'static, (String, Result<String>)> {
async move {
let _handle = HANDLES.get().await; let _handle = HANDLES.get().await;
let mut res = Err(anyhow::anyhow!("Should always try at least once..")); let mut res = Err(anyhow::anyhow!("Should always try at least once.."));
for _ in 0..5u8 { for _ in 0..5u8 {
@ -83,6 +84,16 @@ async fn get_url(url: String) -> (String, Result<String>) {
Ok(ref ok) => { Ok(ref ok) => {
let status = ok.status(); let status = ok.status();
if status != StatusCode::OK { if status != StatusCode::OK {
lazy_static! {
static ref ACTIONS_REGEX: Regex = Regex::new(r"https://github.com/(?P<org>[^/]+)/(?P<repo>[^/]+)/actions(?:\?workflow=.+)?").unwrap();
}
if status == StatusCode::NOT_FOUND && ACTIONS_REGEX.is_match(&url) {
let rewritten = ACTIONS_REGEX.replace_all(&url, "https://github.com/$org/$repo");
warn!("Got 404 with Github actions, so replacing {} with {}", url, rewritten);
let (_new_url, res) = get_url(rewritten.to_string()).await;
return (url, res);
}
warn!("Error while getting {}, retrying: {}", url, status); warn!("Error while getting {}, retrying: {}", url, status);
if status.is_redirection() { if status.is_redirection() {
res = Err(anyhow::anyhow!("Got status code {} redirecting to {}", status, ok.headers().get(header::LOCATION).and_then(|h| h.to_str().ok()).unwrap_or("<unknown>"))); res = Err(anyhow::anyhow!("Got status code {} redirecting to {}", status, ok.headers().get(header::LOCATION).and_then(|h| h.to_str().ok()).unwrap_or("<unknown>")));
@ -98,6 +109,7 @@ async fn get_url(url: String) -> (String, Result<String>) {
break; break;
} }
(url, res) (url, res)
}.boxed()
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]