|
| 1 | +use crate::gather_all; |
| 2 | +use lazy_static::lazy_static; |
| 3 | +use regex::Regex; |
| 4 | +use reqwest::{ |
| 5 | + blocking::{Client, Response}, |
| 6 | + header, |
| 7 | +}; |
| 8 | +use serde::Deserialize; |
| 9 | +use std::env; |
| 10 | + |
| 11 | +lazy_static! { |
| 12 | + static ref NEXT_PAGE_RE: Regex = Regex::new(r#"<(?P<link>[^;]+)>;\srel="next""#).unwrap(); |
| 13 | +} |
| 14 | + |
| 15 | +#[derive(Debug, Deserialize)] |
| 16 | +struct Issue { |
| 17 | + title: String, |
| 18 | + number: u32, |
| 19 | + body: String, |
| 20 | + pull_request: Option<PR>, |
| 21 | +} |
| 22 | + |
| 23 | +#[derive(Debug, Deserialize)] |
| 24 | +struct PR {} |
| 25 | + |
| 26 | +enum Error { |
| 27 | + Reqwest(reqwest::Error), |
| 28 | + Env(std::env::VarError), |
| 29 | + Http(header::InvalidHeaderValue), |
| 30 | +} |
| 31 | + |
| 32 | +impl From<reqwest::Error> for Error { |
| 33 | + fn from(err: reqwest::Error) -> Self { |
| 34 | + Self::Reqwest(err) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +impl From<std::env::VarError> for Error { |
| 39 | + fn from(err: std::env::VarError) -> Self { |
| 40 | + Self::Env(err) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +impl From<header::InvalidHeaderValue> for Error { |
| 45 | + fn from(err: header::InvalidHeaderValue) -> Self { |
| 46 | + Self::Http(err) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +impl std::fmt::Display for Error { |
| 51 | + fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result { |
| 52 | + match self { |
| 53 | + Self::Reqwest(err) => write!(fmt, "reqwest: {}", err), |
| 54 | + Self::Env(err) => write!(fmt, "env: {}", err), |
| 55 | + Self::Http(err) => write!(fmt, "http: {}", err), |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +pub fn run(name: &str, filter: &[u32]) { |
| 61 | + match open_issues() { |
| 62 | + Ok(issues) => { |
| 63 | + for (i, issue) in filter_issues(&issues, name, filter).enumerate() { |
| 64 | + if i == 0 { |
| 65 | + println!("### `{}`\n", name); |
| 66 | + } |
| 67 | + println!("- [ ] #{} ({})", issue.number, issue.title) |
| 68 | + } |
| 69 | + }, |
| 70 | + Err(err) => eprintln!("{}", err), |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +pub fn run_all(filter: &[u32]) { |
| 75 | + match open_issues() { |
| 76 | + Ok(issues) => { |
| 77 | + let mut lint_names = gather_all().map(|lint| lint.name).collect::<Vec<_>>(); |
| 78 | + lint_names.sort(); |
| 79 | + for name in lint_names { |
| 80 | + let mut print_empty_line = false; |
| 81 | + for (i, issue) in filter_issues(&issues, &name, filter).enumerate() { |
| 82 | + if i == 0 { |
| 83 | + println!("### `{}`\n", name); |
| 84 | + print_empty_line = true; |
| 85 | + } |
| 86 | + println!("- [ ] #{} ({})", issue.number, issue.title) |
| 87 | + } |
| 88 | + if print_empty_line { |
| 89 | + println!(); |
| 90 | + } |
| 91 | + } |
| 92 | + }, |
| 93 | + Err(err) => eprintln!("{}", err), |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +fn open_issues() -> Result<Vec<Issue>, Error> { |
| 98 | + let github_token = env::var("GITHUB_TOKEN")?; |
| 99 | + |
| 100 | + let mut headers = header::HeaderMap::new(); |
| 101 | + headers.insert( |
| 102 | + header::AUTHORIZATION, |
| 103 | + header::HeaderValue::from_str(&format!("token {}", github_token))?, |
| 104 | + ); |
| 105 | + headers.insert(header::USER_AGENT, header::HeaderValue::from_static("ghost")); |
| 106 | + let client = Client::builder().default_headers(headers).build()?; |
| 107 | + |
| 108 | + let issues_base = "https://api.github.com/repos/rust-lang/rust-clippy/issues"; |
| 109 | + |
| 110 | + let mut issues = vec![]; |
| 111 | + let mut response = client |
| 112 | + .get(issues_base) |
| 113 | + .query(&[("per_page", "100"), ("state", "open"), ("direction", "asc")]) |
| 114 | + .send()?; |
| 115 | + while let Some(link) = next_link(&response) { |
| 116 | + issues.extend( |
| 117 | + response |
| 118 | + .json::<Vec<Issue>>()? |
| 119 | + .into_iter() |
| 120 | + .filter(|i| i.pull_request.is_none()), |
| 121 | + ); |
| 122 | + response = client.get(&link).send()?; |
| 123 | + } |
| 124 | + |
| 125 | + Ok(issues) |
| 126 | +} |
| 127 | + |
| 128 | +fn filter_issues<'a>(issues: &'a [Issue], name: &str, filter: &'a [u32]) -> impl Iterator<Item = &'a Issue> { |
| 129 | + let name = name.to_lowercase(); |
| 130 | + let separated_name = name.chars().map(|c| if c == '_' { ' ' } else { c }).collect::<String>(); |
| 131 | + let dash_separated_name = name.chars().map(|c| if c == '_' { '-' } else { c }).collect::<String>(); |
| 132 | + |
| 133 | + issues.iter().filter(move |i| { |
| 134 | + let title = i.title.to_lowercase(); |
| 135 | + let body = i.body.to_lowercase(); |
| 136 | + !filter.contains(&i.number) |
| 137 | + && (title.contains(&name) |
| 138 | + || title.contains(&separated_name) |
| 139 | + || title.contains(&dash_separated_name) |
| 140 | + || body.contains(&name) |
| 141 | + || body.contains(&separated_name) |
| 142 | + || body.contains(&dash_separated_name)) |
| 143 | + }) |
| 144 | +} |
| 145 | + |
| 146 | +fn next_link(response: &Response) -> Option<String> { |
| 147 | + if let Some(links) = response.headers().get("Link").and_then(|l| l.to_str().ok()) { |
| 148 | + if let Some(cap) = NEXT_PAGE_RE.captures_iter(links).next() { |
| 149 | + return Some(cap["link"].to_string()); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + None |
| 154 | +} |
0 commit comments