github.rs 28 KB

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