1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
use url::Url; use regex::Regex; use serde::Deserialize; use reqwest::{Result, StatusCode}; use reqwest::blocking::{Client, Response}; #[derive(Debug, Eq, PartialEq)] pub enum GithubIssueType { Issue, PullRequest, } fn issue_type_from_string(str: &str) -> GithubIssueType { if str == "issues" { GithubIssueType::Issue } else { GithubIssueType::PullRequest } } #[derive(Debug, Eq, PartialEq)] pub enum Issue<'u> { Github(&'u str, &'u str, &'u str, GithubIssueType, &'u Url), Link(&'u Url), } pub fn issue_from_url(url: &Url) -> Issue { let github_regex = Regex::new(r"(?i)github.com/(.+?)/(.+?)/(issues|pull)/(\d+)$").unwrap(); return if let Some(capture) = github_regex.captures(url.as_str()) { let issue_type_string = capture.get(3).unwrap().as_str(); let issue_type = issue_type_from_string(issue_type_string); Issue::Github( capture.get(1).unwrap().as_str(), capture.get(2).unwrap().as_str(), capture.get(4).unwrap().as_str(), issue_type, url, ) } else { Issue::Link(url) }; } #[derive(Debug, Eq, PartialEq)] pub enum ValidationResult { NoLongerValid, StillValid, } #[derive(Deserialize, Debug, Eq, PartialEq)] struct IssueResult { state: String } pub trait IssueValidator { fn validate(&self, issue: &Issue) -> ValidationResult; } pub struct DefaultIssueValidator; impl IssueValidator for DefaultIssueValidator { fn validate(&self, issue: &Issue) -> ValidationResult { match issue { Issue::Github(owner, repo, number, issue_type, _url) => self.github_validation_result(owner, repo, number, issue_type), Issue::Link(url) => self.arbitrary_url_validation_result(url) } } } impl DefaultIssueValidator { fn github_validation_result(&self, owner: &str, repo: &str, number: &str, issue_type: &GithubIssueType) -> ValidationResult { let issue_kind = match issue_type { GithubIssueType::Issue => "issues", GithubIssueType::PullRequest => "pulls" }; let request_url = format!( "https://api.github.com/repos/{owner}/{repo}/{issue_kind}/{number}", owner = owner, repo = repo, issue_kind = issue_kind, number = number ); let client = Client::new(); let request = client.get(&request_url) .header("User-Agent", "younata/mdbook-section-validator"); let send_result: Result<Response> = request.send(); if let Result::Ok(response) = send_result { let json_result: Result<IssueResult> = response.json(); if let Result::Ok(issue) = json_result { if issue.state.as_str() == "open" { return ValidationResult::StillValid; } } else { eprintln!("Unable to unwrap json: {}", json_result.unwrap_err()); } } else { eprintln!("bad response: {}", send_result.unwrap_err()); } return ValidationResult::NoLongerValid; } fn arbitrary_url_validation_result(&self, url: &Url) -> ValidationResult { let client = Client::new(); let request = client.head(url.as_str()) .header("User-Agent", "younata/mdbook-section-validator"); let result: Result<Response> = request.send(); if let Result::Ok(response) = result { if response.status() == StatusCode::OK { return ValidationResult::StillValid; } } return ValidationResult::NoLongerValid; } } #[cfg(test)] mod tests { use reqwest::Url; use crate::issue_validator::{GithubIssueType, Issue, issue_from_url}; #[test] fn issue_from_url_github_pr() { let url = Url::parse("https://github.com/rust-lang/mdBook/pull/1539").unwrap(); assert_eq!(issue_from_url(&url), Issue::Github("rust-lang", "mdBook", "1539", GithubIssueType::PullRequest, &url)); } #[test] fn issue_from_url_github_issue() { let url = Url::parse("https://github.com/rust-lang/mdBook/issues/1538").unwrap(); assert_eq!(issue_from_url(&url), Issue::Github("rust-lang", "mdBook", "1538", GithubIssueType::Issue, &url)); } #[test] fn issue_from_url_arbitrary_link() { let url = Url::parse("https://example.com").unwrap(); assert_eq!(issue_from_url(&url), Issue::Link(&url)); } }