github.rs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  1. use anyhow::Context;
  2. use chrono::{DateTime, FixedOffset, Utc};
  3. use futures::stream::{FuturesUnordered, StreamExt};
  4. use futures::{future::BoxFuture, FutureExt};
  5. use once_cell::sync::OnceCell;
  6. use reqwest::header::{AUTHORIZATION, USER_AGENT};
  7. use reqwest::{Client, Request, RequestBuilder, Response, StatusCode};
  8. use std::{
  9. fmt,
  10. time::{Duration, SystemTime},
  11. };
  12. #[derive(Debug, PartialEq, Eq, serde::Deserialize)]
  13. pub struct User {
  14. pub login: String,
  15. pub id: Option<i64>,
  16. }
  17. impl GithubClient {
  18. async fn _send_req(&self, req: RequestBuilder) -> Result<(Response, String), reqwest::Error> {
  19. const MAX_ATTEMPTS: usize = 2;
  20. log::debug!("_send_req with {:?}", req);
  21. let req = req.build()?;
  22. let req_dbg = format!("{:?}", req);
  23. let mut resp = self.client.execute(req.try_clone().unwrap()).await?;
  24. if let Some(sleep) = Self::needs_retry(&resp).await {
  25. resp = self.retry(req, sleep, MAX_ATTEMPTS).await?;
  26. }
  27. resp.error_for_status_ref()?;
  28. Ok((resp, req_dbg))
  29. }
  30. async fn needs_retry(resp: &Response) -> Option<Duration> {
  31. const REMAINING: &str = "X-RateLimit-Remaining";
  32. const RESET: &str = "X-RateLimit-Reset";
  33. if resp.status().is_success() {
  34. return None;
  35. }
  36. let headers = resp.headers();
  37. if !(headers.contains_key(REMAINING) && headers.contains_key(RESET)) {
  38. return None;
  39. }
  40. // Weird github api behavior. It asks us to retry but also has a remaining count above 1
  41. // Try again immediately and hope for the best...
  42. if headers[REMAINING] != "0" {
  43. return Some(Duration::from_secs(0));
  44. }
  45. let reset_time = headers[RESET].to_str().unwrap().parse::<u64>().unwrap();
  46. Some(Duration::from_secs(Self::calc_sleep(reset_time) + 10))
  47. }
  48. fn calc_sleep(reset_time: u64) -> u64 {
  49. let epoch_time = SystemTime::UNIX_EPOCH.elapsed().unwrap().as_secs();
  50. reset_time.saturating_sub(epoch_time)
  51. }
  52. fn retry(
  53. &self,
  54. req: Request,
  55. sleep: Duration,
  56. remaining_attempts: usize,
  57. ) -> BoxFuture<Result<Response, reqwest::Error>> {
  58. #[derive(Debug, serde::Deserialize)]
  59. struct RateLimit {
  60. pub limit: u64,
  61. pub remaining: u64,
  62. pub reset: u64,
  63. }
  64. #[derive(Debug, serde::Deserialize)]
  65. struct RateLimitResponse {
  66. pub resources: Resources,
  67. }
  68. #[derive(Debug, serde::Deserialize)]
  69. struct Resources {
  70. pub core: RateLimit,
  71. pub search: RateLimit,
  72. pub graphql: RateLimit,
  73. pub source_import: RateLimit,
  74. }
  75. log::warn!(
  76. "Retrying after {} seconds, remaining attepts {}",
  77. sleep.as_secs(),
  78. remaining_attempts,
  79. );
  80. async move {
  81. tokio::time::delay_for(sleep).await;
  82. // check rate limit
  83. let rate_resp = self
  84. .client
  85. .execute(
  86. self.client
  87. .get("https://api.github.com/rate_limit")
  88. .configure(self)
  89. .build()
  90. .unwrap(),
  91. )
  92. .await?;
  93. let rate_limit_response = rate_resp.json::<RateLimitResponse>().await?;
  94. // Check url for search path because github has different rate limits for the search api
  95. let rate_limit = if req
  96. .url()
  97. .path_segments()
  98. .map(|mut segments| matches!(segments.next(), Some("search")))
  99. .unwrap_or(false)
  100. {
  101. rate_limit_response.resources.search
  102. } else {
  103. rate_limit_response.resources.core
  104. };
  105. // If we still don't have any more remaining attempts, try sleeping for the remaining
  106. // period of time
  107. if rate_limit.remaining == 0 {
  108. let sleep = Self::calc_sleep(rate_limit.reset);
  109. if sleep > 0 {
  110. tokio::time::delay_for(Duration::from_secs(sleep)).await;
  111. }
  112. }
  113. let resp = self.client.execute(req.try_clone().unwrap()).await?;
  114. if let Some(sleep) = Self::needs_retry(&resp).await {
  115. if remaining_attempts > 0 {
  116. return self.retry(req, sleep, remaining_attempts - 1).await;
  117. }
  118. }
  119. Ok(resp)
  120. }
  121. .boxed()
  122. }
  123. async fn send_req(&self, req: RequestBuilder) -> anyhow::Result<Vec<u8>> {
  124. let (mut resp, req_dbg) = self._send_req(req).await?;
  125. let mut body = Vec::new();
  126. while let Some(chunk) = resp.chunk().await.transpose() {
  127. let chunk = chunk
  128. .context("reading stream failed")
  129. .map_err(anyhow::Error::from)
  130. .context(req_dbg.clone())?;
  131. body.extend_from_slice(&chunk);
  132. }
  133. Ok(body)
  134. }
  135. pub async fn json<T>(&self, req: RequestBuilder) -> anyhow::Result<T>
  136. where
  137. T: serde::de::DeserializeOwned,
  138. {
  139. let (resp, req_dbg) = self._send_req(req).await?;
  140. Ok(resp.json().await.context(req_dbg)?)
  141. }
  142. }
  143. impl User {
  144. pub async fn current(client: &GithubClient) -> anyhow::Result<Self> {
  145. client.json(client.get("https://api.github.com/user")).await
  146. }
  147. pub async fn is_team_member<'a>(&'a self, client: &'a GithubClient) -> anyhow::Result<bool> {
  148. let permission = crate::team_data::teams(client).await?;
  149. let map = permission.teams;
  150. let is_triager = map
  151. .get("wg-triage")
  152. .map_or(false, |w| w.members.iter().any(|g| g.github == self.login));
  153. let is_pri_member = map
  154. .get("wg-prioritization")
  155. .map_or(false, |w| w.members.iter().any(|g| g.github == self.login));
  156. Ok(
  157. map["all"].members.iter().any(|g| g.github == self.login)
  158. || is_triager
  159. || is_pri_member,
  160. )
  161. }
  162. // Returns the ID of the given user, if the user is in the `all` team.
  163. pub async fn get_id<'a>(&'a self, client: &'a GithubClient) -> anyhow::Result<Option<usize>> {
  164. let permission = crate::team_data::teams(client).await?;
  165. let map = permission.teams;
  166. Ok(map["all"]
  167. .members
  168. .iter()
  169. .find(|g| g.github == self.login)
  170. .map(|u| u.github_id))
  171. }
  172. }
  173. pub async fn get_team(
  174. client: &GithubClient,
  175. team: &str,
  176. ) -> anyhow::Result<Option<rust_team_data::v1::Team>> {
  177. let permission = crate::team_data::teams(client).await?;
  178. let mut map = permission.teams;
  179. Ok(map.swap_remove(team))
  180. }
  181. #[derive(PartialEq, Eq, Debug, Clone, serde::Deserialize)]
  182. pub struct Label {
  183. pub name: String,
  184. }
  185. impl Label {
  186. async fn exists<'a>(&'a self, repo_api_prefix: &'a str, client: &'a GithubClient) -> bool {
  187. #[allow(clippy::redundant_pattern_matching)]
  188. let url = format!("{}/labels/{}", repo_api_prefix, self.name);
  189. match client.send_req(client.get(&url)).await {
  190. Ok(_) => true,
  191. // XXX: Error handling if the request failed for reasons beyond 'label didn't exist'
  192. Err(_) => false,
  193. }
  194. }
  195. }
  196. #[derive(Debug, serde::Deserialize)]
  197. pub struct PullRequestDetails {
  198. // none for now
  199. }
  200. #[derive(Debug, serde::Deserialize)]
  201. pub struct Issue {
  202. pub number: u64,
  203. pub body: String,
  204. created_at: chrono::DateTime<Utc>,
  205. pub title: String,
  206. pub html_url: String,
  207. pub user: User,
  208. pub labels: Vec<Label>,
  209. pub assignees: Vec<User>,
  210. pub pull_request: Option<PullRequestDetails>,
  211. // API URL
  212. comments_url: String,
  213. #[serde(skip)]
  214. repository: OnceCell<IssueRepository>,
  215. }
  216. #[derive(Debug, serde::Deserialize)]
  217. pub struct Comment {
  218. #[serde(deserialize_with = "opt_string")]
  219. pub body: String,
  220. pub html_url: String,
  221. pub user: User,
  222. #[serde(alias = "submitted_at")] // for pull request reviews
  223. pub updated_at: chrono::DateTime<Utc>,
  224. }
  225. fn opt_string<'de, D>(deserializer: D) -> Result<String, D::Error>
  226. where
  227. D: serde::de::Deserializer<'de>,
  228. {
  229. use serde::de::Deserialize;
  230. match <Option<String>>::deserialize(deserializer) {
  231. Ok(v) => Ok(v.unwrap_or_default()),
  232. Err(e) => Err(e),
  233. }
  234. }
  235. #[derive(Debug)]
  236. pub enum AssignmentError {
  237. InvalidAssignee,
  238. Http(reqwest::Error),
  239. }
  240. #[derive(Debug)]
  241. pub enum Selection<'a, T> {
  242. All,
  243. One(&'a T),
  244. }
  245. impl fmt::Display for AssignmentError {
  246. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  247. match self {
  248. AssignmentError::InvalidAssignee => write!(f, "invalid assignee"),
  249. AssignmentError::Http(e) => write!(f, "cannot assign: {}", e),
  250. }
  251. }
  252. }
  253. impl std::error::Error for AssignmentError {}
  254. #[derive(Debug)]
  255. pub struct IssueRepository {
  256. pub organization: String,
  257. pub repository: String,
  258. }
  259. impl fmt::Display for IssueRepository {
  260. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  261. write!(f, "{}/{}", self.organization, self.repository)
  262. }
  263. }
  264. impl IssueRepository {
  265. fn url(&self) -> String {
  266. format!(
  267. "https://api.github.com/repos/{}/{}",
  268. self.organization, self.repository
  269. )
  270. }
  271. }
  272. impl Issue {
  273. pub fn zulip_topic_reference(&self) -> String {
  274. let repo = self.repository();
  275. if repo.organization == "rust-lang" {
  276. if repo.repository == "rust" {
  277. format!("#{}", self.number)
  278. } else {
  279. format!("{}#{}", repo.repository, self.number)
  280. }
  281. } else {
  282. format!("{}/{}#{}", repo.organization, repo.repository, self.number)
  283. }
  284. }
  285. pub fn repository(&self) -> &IssueRepository {
  286. self.repository.get_or_init(|| {
  287. // https://api.github.com/repos/rust-lang/rust/issues/69257/comments
  288. log::trace!("get repository for {}", self.comments_url);
  289. let url = url::Url::parse(&self.comments_url).unwrap();
  290. let mut segments = url.path_segments().unwrap();
  291. let _comments = segments.next_back().unwrap();
  292. let _number = segments.next_back().unwrap();
  293. let _issues_or_prs = segments.next_back().unwrap();
  294. let repository = segments.next_back().unwrap();
  295. let organization = segments.next_back().unwrap();
  296. IssueRepository {
  297. organization: organization.into(),
  298. repository: repository.into(),
  299. }
  300. })
  301. }
  302. pub fn global_id(&self) -> String {
  303. format!("{}#{}", self.repository(), self.number)
  304. }
  305. pub fn is_pr(&self) -> bool {
  306. self.pull_request.is_some()
  307. }
  308. pub async fn get_comment(&self, client: &GithubClient, id: usize) -> anyhow::Result<Comment> {
  309. let comment_url = format!("{}/issues/comments/{}", self.repository().url(), id);
  310. let comment = client.json(client.get(&comment_url)).await?;
  311. Ok(comment)
  312. }
  313. pub async fn edit_body(&self, client: &GithubClient, body: &str) -> anyhow::Result<()> {
  314. let edit_url = format!("{}/issues/{}", self.repository().url(), self.number);
  315. #[derive(serde::Serialize)]
  316. struct ChangedIssue<'a> {
  317. body: &'a str,
  318. }
  319. client
  320. ._send_req(client.patch(&edit_url).json(&ChangedIssue { body }))
  321. .await
  322. .context("failed to edit issue body")?;
  323. Ok(())
  324. }
  325. pub async fn edit_comment(
  326. &self,
  327. client: &GithubClient,
  328. id: usize,
  329. new_body: &str,
  330. ) -> anyhow::Result<()> {
  331. let comment_url = format!("{}/issues/comments/{}", self.repository().url(), id);
  332. #[derive(serde::Serialize)]
  333. struct NewComment<'a> {
  334. body: &'a str,
  335. }
  336. client
  337. ._send_req(
  338. client
  339. .patch(&comment_url)
  340. .json(&NewComment { body: new_body }),
  341. )
  342. .await
  343. .context("failed to edit comment")?;
  344. Ok(())
  345. }
  346. pub async fn post_comment(&self, client: &GithubClient, body: &str) -> anyhow::Result<()> {
  347. #[derive(serde::Serialize)]
  348. struct PostComment<'a> {
  349. body: &'a str,
  350. }
  351. client
  352. ._send_req(client.post(&self.comments_url).json(&PostComment { body }))
  353. .await
  354. .context("failed to post comment")?;
  355. Ok(())
  356. }
  357. pub async fn set_labels(
  358. &self,
  359. client: &GithubClient,
  360. labels: Vec<Label>,
  361. ) -> anyhow::Result<()> {
  362. log::info!("set_labels {} to {:?}", self.global_id(), labels);
  363. // PUT /repos/:owner/:repo/issues/:number/labels
  364. // repo_url = https://api.github.com/repos/Codertocat/Hello-World
  365. let url = format!(
  366. "{repo_url}/issues/{number}/labels",
  367. repo_url = self.repository().url(),
  368. number = self.number
  369. );
  370. let mut stream = labels
  371. .into_iter()
  372. .map(|label| async { (label.exists(&self.repository().url(), &client).await, label) })
  373. .collect::<FuturesUnordered<_>>();
  374. let mut labels = Vec::new();
  375. while let Some((true, label)) = stream.next().await {
  376. labels.push(label);
  377. }
  378. #[derive(serde::Serialize)]
  379. struct LabelsReq {
  380. labels: Vec<String>,
  381. }
  382. client
  383. ._send_req(client.put(&url).json(&LabelsReq {
  384. labels: labels.iter().map(|l| l.name.clone()).collect(),
  385. }))
  386. .await
  387. .context("failed to set labels")?;
  388. Ok(())
  389. }
  390. pub fn labels(&self) -> &[Label] {
  391. &self.labels
  392. }
  393. pub fn contain_assignee(&self, user: &str) -> bool {
  394. self.assignees.iter().any(|a| a.login == user)
  395. }
  396. pub async fn remove_assignees(
  397. &self,
  398. client: &GithubClient,
  399. selection: Selection<'_, String>,
  400. ) -> Result<(), AssignmentError> {
  401. log::info!("remove {:?} assignees for {}", selection, self.global_id());
  402. let url = format!(
  403. "{repo_url}/issues/{number}/assignees",
  404. repo_url = self.repository().url(),
  405. number = self.number
  406. );
  407. let assignees = match selection {
  408. Selection::All => self
  409. .assignees
  410. .iter()
  411. .map(|u| u.login.as_str())
  412. .collect::<Vec<_>>(),
  413. Selection::One(user) => vec![user.as_str()],
  414. };
  415. #[derive(serde::Serialize)]
  416. struct AssigneeReq<'a> {
  417. assignees: &'a [&'a str],
  418. }
  419. client
  420. ._send_req(client.delete(&url).json(&AssigneeReq {
  421. assignees: &assignees[..],
  422. }))
  423. .await
  424. .map_err(AssignmentError::Http)?;
  425. Ok(())
  426. }
  427. pub async fn set_assignee(
  428. &self,
  429. client: &GithubClient,
  430. user: &str,
  431. ) -> Result<(), AssignmentError> {
  432. log::info!("set_assignee for {} to {}", self.global_id(), user);
  433. let url = format!(
  434. "{repo_url}/issues/{number}/assignees",
  435. repo_url = self.repository().url(),
  436. number = self.number
  437. );
  438. let check_url = format!(
  439. "{repo_url}/assignees/{name}",
  440. repo_url = self.repository().url(),
  441. name = user,
  442. );
  443. match client._send_req(client.get(&check_url)).await {
  444. Ok((resp, _)) => {
  445. if resp.status() == reqwest::StatusCode::NO_CONTENT {
  446. // all okay
  447. log::debug!("set_assignee: assignee is valid");
  448. } else {
  449. log::error!(
  450. "unknown status for assignee check, assuming all okay: {:?}",
  451. resp
  452. );
  453. }
  454. }
  455. Err(e) => {
  456. if e.status() == Some(reqwest::StatusCode::NOT_FOUND) {
  457. log::debug!("set_assignee: assignee is invalid, returning");
  458. return Err(AssignmentError::InvalidAssignee);
  459. }
  460. log::debug!("set_assignee: get {} failed, {:?}", check_url, e);
  461. return Err(AssignmentError::Http(e));
  462. }
  463. }
  464. self.remove_assignees(client, Selection::All).await?;
  465. #[derive(serde::Serialize)]
  466. struct AssigneeReq<'a> {
  467. assignees: &'a [&'a str],
  468. }
  469. client
  470. ._send_req(client.post(&url).json(&AssigneeReq { assignees: &[user] }))
  471. .await
  472. .map_err(AssignmentError::Http)?;
  473. Ok(())
  474. }
  475. }
  476. #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
  477. #[serde(rename_all = "lowercase")]
  478. pub enum PullRequestReviewAction {
  479. Submitted,
  480. Edited,
  481. Dismissed,
  482. }
  483. #[derive(Debug, serde::Deserialize)]
  484. pub struct PullRequestReviewEvent {
  485. pub action: PullRequestReviewAction,
  486. pub pull_request: Issue,
  487. pub review: Comment,
  488. pub repository: Repository,
  489. }
  490. #[derive(Debug, serde::Deserialize)]
  491. pub struct PullRequestReviewComment {
  492. pub action: IssueCommentAction,
  493. #[serde(rename = "pull_request")]
  494. pub issue: Issue,
  495. pub comment: Comment,
  496. pub repository: Repository,
  497. }
  498. #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
  499. #[serde(rename_all = "lowercase")]
  500. pub enum IssueCommentAction {
  501. Created,
  502. Edited,
  503. Deleted,
  504. }
  505. #[derive(Debug, serde::Deserialize)]
  506. pub struct IssueCommentEvent {
  507. pub action: IssueCommentAction,
  508. pub issue: Issue,
  509. pub comment: Comment,
  510. pub repository: Repository,
  511. }
  512. #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
  513. #[serde(rename_all = "lowercase")]
  514. pub enum IssuesAction {
  515. Opened,
  516. Edited,
  517. Deleted,
  518. Transferred,
  519. Pinned,
  520. Unpinned,
  521. Closed,
  522. Reopened,
  523. Assigned,
  524. Unassigned,
  525. Labeled,
  526. Unlabeled,
  527. Locked,
  528. Unlocked,
  529. Milestoned,
  530. Demilestoned,
  531. ReviewRequested,
  532. ReviewRequestRemoved,
  533. ReadyForReview,
  534. Synchronize,
  535. }
  536. #[derive(Debug, serde::Deserialize)]
  537. pub struct IssuesEvent {
  538. pub action: IssuesAction,
  539. #[serde(alias = "pull_request")]
  540. pub issue: Issue,
  541. pub repository: Repository,
  542. /// Some if action is IssuesAction::Labeled, for example
  543. pub label: Option<Label>,
  544. }
  545. #[derive(Debug, serde::Deserialize)]
  546. pub struct IssueSearchResult {
  547. pub total_count: usize,
  548. pub incomplete_results: bool,
  549. pub items: Vec<Issue>,
  550. }
  551. #[derive(Debug, serde::Deserialize)]
  552. pub struct Repository {
  553. pub full_name: String,
  554. }
  555. impl Repository {
  556. const GITHUB_API_URL: &'static str = "https://api.github.com";
  557. pub async fn get_issues<'a>(
  558. &self,
  559. client: &GithubClient,
  560. query: &Query<'a>,
  561. ) -> anyhow::Result<Vec<Issue>> {
  562. let Query {
  563. filters,
  564. include_labels,
  565. exclude_labels,
  566. ..
  567. } = query;
  568. let use_issues = exclude_labels.is_empty()
  569. && filters
  570. .iter()
  571. .all(|&(key, _)| key != "no" && key != "closed-days-ago");
  572. // negating filters can only be handled by the search api
  573. let url = if use_issues {
  574. self.build_issues_url(filters, include_labels)
  575. } else {
  576. self.build_search_issues_url(filters, include_labels, exclude_labels)
  577. };
  578. let result = client.get(&url);
  579. if use_issues {
  580. client
  581. .json(result)
  582. .await
  583. .with_context(|| format!("failed to list issues from {}", url))
  584. } else {
  585. let result = client
  586. .json::<IssueSearchResult>(result)
  587. .await
  588. .with_context(|| format!("failed to list issues from {}", url))?;
  589. Ok(result.items)
  590. }
  591. }
  592. pub async fn get_issues_count<'a>(
  593. &self,
  594. client: &GithubClient,
  595. query: &Query<'a>,
  596. ) -> anyhow::Result<usize> {
  597. Ok(self.get_issues(client, query).await?.len())
  598. }
  599. fn build_issues_url(&self, filters: &Vec<(&str, &str)>, include_labels: &Vec<&str>) -> String {
  600. let filters = filters
  601. .iter()
  602. .map(|(key, val)| format!("{}={}", key, val))
  603. .chain(std::iter::once(format!(
  604. "labels={}",
  605. include_labels.join(",")
  606. )))
  607. .chain(std::iter::once("filter=all".to_owned()))
  608. .chain(std::iter::once(format!("sort=created")))
  609. .chain(std::iter::once(format!("direction=asc")))
  610. .chain(std::iter::once(format!("per_page=100")))
  611. .collect::<Vec<_>>()
  612. .join("&");
  613. format!(
  614. "{}/repos/{}/issues?{}",
  615. Repository::GITHUB_API_URL,
  616. self.full_name,
  617. filters
  618. )
  619. }
  620. fn build_search_issues_url(
  621. &self,
  622. filters: &Vec<(&str, &str)>,
  623. include_labels: &Vec<&str>,
  624. exclude_labels: &Vec<&str>,
  625. ) -> String {
  626. let filters = filters
  627. .iter()
  628. .map(|(key, val)| {
  629. if *key == "closed-days-ago" {
  630. let last_week =
  631. Utc::now() - chrono::Duration::days(val.parse::<i64>().unwrap());
  632. format!("closed:>={}", last_week.format("%Y-%m-%d"))
  633. } else {
  634. format!("{}:{}", key, val)
  635. }
  636. })
  637. .chain(
  638. include_labels
  639. .iter()
  640. .map(|label| format!("label:{}", label)),
  641. )
  642. .chain(
  643. exclude_labels
  644. .iter()
  645. .map(|label| format!("-label:{}", label)),
  646. )
  647. .chain(std::iter::once(format!("repo:{}", self.full_name)))
  648. .collect::<Vec<_>>()
  649. .join("+");
  650. format!(
  651. "{}/search/issues?q={}&sort=created&order=asc&per_page=100",
  652. Repository::GITHUB_API_URL,
  653. filters
  654. )
  655. }
  656. }
  657. pub struct Query<'a> {
  658. pub kind: QueryKind,
  659. // key/value filter
  660. pub filters: Vec<(&'a str, &'a str)>,
  661. pub include_labels: Vec<&'a str>,
  662. pub exclude_labels: Vec<&'a str>,
  663. }
  664. pub enum QueryKind {
  665. List,
  666. Count,
  667. }
  668. #[derive(Debug)]
  669. pub enum Event {
  670. IssueComment(IssueCommentEvent),
  671. Issue(IssuesEvent),
  672. }
  673. impl Event {
  674. pub fn repo_name(&self) -> &str {
  675. match self {
  676. Event::IssueComment(event) => &event.repository.full_name,
  677. Event::Issue(event) => &event.repository.full_name,
  678. }
  679. }
  680. pub fn issue(&self) -> Option<&Issue> {
  681. match self {
  682. Event::IssueComment(event) => Some(&event.issue),
  683. Event::Issue(event) => Some(&event.issue),
  684. }
  685. }
  686. /// This will both extract from IssueComment events but also Issue events
  687. pub fn comment_body(&self) -> Option<&str> {
  688. match self {
  689. Event::Issue(e) => Some(&e.issue.body),
  690. Event::IssueComment(e) => Some(&e.comment.body),
  691. }
  692. }
  693. pub fn html_url(&self) -> Option<&str> {
  694. match self {
  695. Event::Issue(e) => Some(&e.issue.html_url),
  696. Event::IssueComment(e) => Some(&e.comment.html_url),
  697. }
  698. }
  699. pub fn user(&self) -> &User {
  700. match self {
  701. Event::Issue(e) => &e.issue.user,
  702. Event::IssueComment(e) => &e.comment.user,
  703. }
  704. }
  705. pub fn time(&self) -> chrono::DateTime<FixedOffset> {
  706. match self {
  707. Event::Issue(e) => e.issue.created_at.into(),
  708. Event::IssueComment(e) => e.comment.updated_at.into(),
  709. }
  710. }
  711. }
  712. trait RequestSend: Sized {
  713. fn configure(self, g: &GithubClient) -> Self;
  714. }
  715. impl RequestSend for RequestBuilder {
  716. fn configure(self, g: &GithubClient) -> RequestBuilder {
  717. self.header(USER_AGENT, "rust-lang-triagebot")
  718. .header(AUTHORIZATION, format!("token {}", g.token))
  719. }
  720. }
  721. #[derive(Clone)]
  722. pub struct GithubClient {
  723. token: String,
  724. client: Client,
  725. }
  726. impl GithubClient {
  727. pub fn new(client: Client, token: String) -> Self {
  728. GithubClient { client, token }
  729. }
  730. pub fn raw(&self) -> &Client {
  731. &self.client
  732. }
  733. pub async fn raw_file(
  734. &self,
  735. repo: &str,
  736. branch: &str,
  737. path: &str,
  738. ) -> anyhow::Result<Option<Vec<u8>>> {
  739. let url = format!(
  740. "https://raw.githubusercontent.com/{}/{}/{}",
  741. repo, branch, path
  742. );
  743. let req = self.get(&url);
  744. let req_dbg = format!("{:?}", req);
  745. let req = req
  746. .build()
  747. .with_context(|| format!("failed to build request {:?}", req_dbg))?;
  748. let mut resp = self.client.execute(req).await.context(req_dbg.clone())?;
  749. let status = resp.status();
  750. match status {
  751. StatusCode::OK => {
  752. let mut buf = Vec::with_capacity(resp.content_length().unwrap_or(4) as usize);
  753. while let Some(chunk) = resp.chunk().await.transpose() {
  754. let chunk = chunk
  755. .context("reading stream failed")
  756. .map_err(anyhow::Error::from)
  757. .context(req_dbg.clone())?;
  758. buf.extend_from_slice(&chunk);
  759. }
  760. Ok(Some(buf))
  761. }
  762. StatusCode::NOT_FOUND => Ok(None),
  763. status => anyhow::bail!("failed to GET {}: {}", url, status),
  764. }
  765. }
  766. fn get(&self, url: &str) -> RequestBuilder {
  767. log::trace!("get {:?}", url);
  768. self.client.get(url).configure(self)
  769. }
  770. fn patch(&self, url: &str) -> RequestBuilder {
  771. log::trace!("patch {:?}", url);
  772. self.client.patch(url).configure(self)
  773. }
  774. fn delete(&self, url: &str) -> RequestBuilder {
  775. log::trace!("delete {:?}", url);
  776. self.client.delete(url).configure(self)
  777. }
  778. fn post(&self, url: &str) -> RequestBuilder {
  779. log::trace!("post {:?}", url);
  780. self.client.post(url).configure(self)
  781. }
  782. fn put(&self, url: &str) -> RequestBuilder {
  783. log::trace!("put {:?}", url);
  784. self.client.put(url).configure(self)
  785. }
  786. pub async fn rust_commit(&self, sha: &str) -> Option<GithubCommit> {
  787. let req = self.get(&format!(
  788. "https://api.github.com/repos/rust-lang/rust/commits/{}",
  789. sha
  790. ));
  791. match self.json(req).await {
  792. Ok(r) => Some(r),
  793. Err(e) => {
  794. log::error!("Failed to query commit {:?}: {:?}", sha, e);
  795. None
  796. }
  797. }
  798. }
  799. /// This does not retrieve all of them, only the last several.
  800. pub async fn bors_commits(&self) -> Vec<GithubCommit> {
  801. let req = self.get("https://api.github.com/repos/rust-lang/rust/commits?author=bors");
  802. match self.json(req).await {
  803. Ok(r) => r,
  804. Err(e) => {
  805. log::error!("Failed to query commit list: {:?}", e);
  806. Vec::new()
  807. }
  808. }
  809. }
  810. }
  811. #[derive(Debug, serde::Deserialize)]
  812. pub struct GithubCommit {
  813. pub sha: String,
  814. pub commit: GitCommit,
  815. pub parents: Vec<Parent>,
  816. }
  817. #[derive(Debug, serde::Deserialize)]
  818. pub struct GitCommit {
  819. pub author: GitUser,
  820. }
  821. #[derive(Debug, serde::Deserialize)]
  822. pub struct GitUser {
  823. pub date: DateTime<FixedOffset>,
  824. }
  825. #[derive(Debug, serde::Deserialize)]
  826. pub struct Parent {
  827. pub sha: String,
  828. }