github.rs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  1. use anyhow::Context;
  2. use async_trait::async_trait;
  3. use chrono::{DateTime, FixedOffset, Utc};
  4. use futures::stream::{FuturesUnordered, StreamExt};
  5. use futures::{future::BoxFuture, FutureExt};
  6. use hyper::header::HeaderValue;
  7. use once_cell::sync::OnceCell;
  8. use reqwest::header::{AUTHORIZATION, USER_AGENT};
  9. use reqwest::{Client, Request, RequestBuilder, Response, StatusCode};
  10. use std::{
  11. fmt,
  12. time::{Duration, SystemTime},
  13. };
  14. pub mod graphql;
  15. #[derive(Debug, PartialEq, Eq, serde::Deserialize)]
  16. pub struct User {
  17. pub login: String,
  18. pub id: Option<i64>,
  19. }
  20. impl GithubClient {
  21. async fn _send_req(&self, req: RequestBuilder) -> anyhow::Result<(Response, String)> {
  22. const MAX_ATTEMPTS: usize = 2;
  23. log::debug!("_send_req with {:?}", req);
  24. let req_dbg = format!("{:?}", req);
  25. let req = req
  26. .build()
  27. .with_context(|| format!("building reqwest {}", req_dbg))?;
  28. let mut resp = self.client.execute(req.try_clone().unwrap()).await?;
  29. if let Some(sleep) = Self::needs_retry(&resp).await {
  30. resp = self.retry(req, sleep, MAX_ATTEMPTS).await?;
  31. }
  32. resp.error_for_status_ref()?;
  33. Ok((resp, req_dbg))
  34. }
  35. async fn needs_retry(resp: &Response) -> Option<Duration> {
  36. const REMAINING: &str = "X-RateLimit-Remaining";
  37. const RESET: &str = "X-RateLimit-Reset";
  38. if resp.status().is_success() {
  39. return None;
  40. }
  41. let headers = resp.headers();
  42. if !(headers.contains_key(REMAINING) && headers.contains_key(RESET)) {
  43. return None;
  44. }
  45. // Weird github api behavior. It asks us to retry but also has a remaining count above 1
  46. // Try again immediately and hope for the best...
  47. if headers[REMAINING] != "0" {
  48. return Some(Duration::from_secs(0));
  49. }
  50. let reset_time = headers[RESET].to_str().unwrap().parse::<u64>().unwrap();
  51. Some(Duration::from_secs(Self::calc_sleep(reset_time) + 10))
  52. }
  53. fn calc_sleep(reset_time: u64) -> u64 {
  54. let epoch_time = SystemTime::UNIX_EPOCH.elapsed().unwrap().as_secs();
  55. reset_time.saturating_sub(epoch_time)
  56. }
  57. fn retry(
  58. &self,
  59. req: Request,
  60. sleep: Duration,
  61. remaining_attempts: usize,
  62. ) -> BoxFuture<Result<Response, reqwest::Error>> {
  63. #[derive(Debug, serde::Deserialize)]
  64. struct RateLimit {
  65. pub limit: u64,
  66. pub remaining: u64,
  67. pub reset: u64,
  68. }
  69. #[derive(Debug, serde::Deserialize)]
  70. struct RateLimitResponse {
  71. pub resources: Resources,
  72. }
  73. #[derive(Debug, serde::Deserialize)]
  74. struct Resources {
  75. pub core: RateLimit,
  76. pub search: RateLimit,
  77. pub graphql: RateLimit,
  78. pub source_import: RateLimit,
  79. }
  80. log::warn!(
  81. "Retrying after {} seconds, remaining attepts {}",
  82. sleep.as_secs(),
  83. remaining_attempts,
  84. );
  85. async move {
  86. tokio::time::sleep(sleep).await;
  87. // check rate limit
  88. let rate_resp = self
  89. .client
  90. .execute(
  91. self.client
  92. .get("https://api.github.com/rate_limit")
  93. .configure(self)
  94. .build()
  95. .unwrap(),
  96. )
  97. .await?;
  98. let rate_limit_response = rate_resp.json::<RateLimitResponse>().await?;
  99. // Check url for search path because github has different rate limits for the search api
  100. let rate_limit = if req
  101. .url()
  102. .path_segments()
  103. .map(|mut segments| matches!(segments.next(), Some("search")))
  104. .unwrap_or(false)
  105. {
  106. rate_limit_response.resources.search
  107. } else {
  108. rate_limit_response.resources.core
  109. };
  110. // If we still don't have any more remaining attempts, try sleeping for the remaining
  111. // period of time
  112. if rate_limit.remaining == 0 {
  113. let sleep = Self::calc_sleep(rate_limit.reset);
  114. if sleep > 0 {
  115. tokio::time::sleep(Duration::from_secs(sleep)).await;
  116. }
  117. }
  118. let resp = self.client.execute(req.try_clone().unwrap()).await?;
  119. if let Some(sleep) = Self::needs_retry(&resp).await {
  120. if remaining_attempts > 0 {
  121. return self.retry(req, sleep, remaining_attempts - 1).await;
  122. }
  123. }
  124. Ok(resp)
  125. }
  126. .boxed()
  127. }
  128. async fn send_req(&self, req: RequestBuilder) -> anyhow::Result<Vec<u8>> {
  129. let (mut resp, req_dbg) = self._send_req(req).await?;
  130. let mut body = Vec::new();
  131. while let Some(chunk) = resp.chunk().await.transpose() {
  132. let chunk = chunk
  133. .context("reading stream failed")
  134. .map_err(anyhow::Error::from)
  135. .context(req_dbg.clone())?;
  136. body.extend_from_slice(&chunk);
  137. }
  138. Ok(body)
  139. }
  140. pub async fn json<T>(&self, req: RequestBuilder) -> anyhow::Result<T>
  141. where
  142. T: serde::de::DeserializeOwned,
  143. {
  144. let (resp, req_dbg) = self._send_req(req).await?;
  145. Ok(resp.json().await.context(req_dbg)?)
  146. }
  147. }
  148. impl User {
  149. pub async fn current(client: &GithubClient) -> anyhow::Result<Self> {
  150. client.json(client.get("https://api.github.com/user")).await
  151. }
  152. pub async fn is_team_member<'a>(&'a self, client: &'a GithubClient) -> anyhow::Result<bool> {
  153. log::trace!("Getting team membership for {:?}", self.login);
  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. let in_all = map["all"].members.iter().any(|g| g.github == self.login);
  163. log::trace!(
  164. "{:?} is all?={:?}, triager?={:?}, prioritizer?={:?}",
  165. self.login,
  166. in_all,
  167. is_triager,
  168. is_pri_member
  169. );
  170. Ok(in_all || is_triager || is_pri_member)
  171. }
  172. // Returns the ID of the given user, if the user is in the `all` team.
  173. pub async fn get_id<'a>(&'a self, client: &'a GithubClient) -> anyhow::Result<Option<usize>> {
  174. let permission = crate::team_data::teams(client).await?;
  175. let map = permission.teams;
  176. Ok(map["all"]
  177. .members
  178. .iter()
  179. .find(|g| g.github == self.login)
  180. .map(|u| u.github_id))
  181. }
  182. }
  183. pub async fn get_team(
  184. client: &GithubClient,
  185. team: &str,
  186. ) -> anyhow::Result<Option<rust_team_data::v1::Team>> {
  187. let permission = crate::team_data::teams(client).await?;
  188. let mut map = permission.teams;
  189. Ok(map.swap_remove(team))
  190. }
  191. #[derive(PartialEq, Eq, Debug, Clone, serde::Deserialize)]
  192. pub struct Label {
  193. pub name: String,
  194. }
  195. impl Label {
  196. async fn exists<'a>(&'a self, repo_api_prefix: &'a str, client: &'a GithubClient) -> bool {
  197. #[allow(clippy::redundant_pattern_matching)]
  198. let url = format!("{}/labels/{}", repo_api_prefix, self.name);
  199. match client.send_req(client.get(&url)).await {
  200. Ok(_) => true,
  201. // XXX: Error handling if the request failed for reasons beyond 'label didn't exist'
  202. Err(_) => false,
  203. }
  204. }
  205. }
  206. #[derive(Debug, serde::Deserialize)]
  207. pub struct PullRequestDetails {
  208. // none for now
  209. }
  210. #[derive(Debug, serde::Deserialize)]
  211. pub struct Issue {
  212. pub number: u64,
  213. pub body: String,
  214. created_at: chrono::DateTime<Utc>,
  215. pub updated_at: chrono::DateTime<Utc>,
  216. #[serde(default)]
  217. pub merge_commit_sha: Option<String>,
  218. pub title: String,
  219. pub html_url: String,
  220. pub user: User,
  221. pub labels: Vec<Label>,
  222. pub assignees: Vec<User>,
  223. pub pull_request: Option<PullRequestDetails>,
  224. #[serde(default)]
  225. pub merged: bool,
  226. // API URL
  227. comments_url: String,
  228. #[serde(skip)]
  229. repository: OnceCell<IssueRepository>,
  230. }
  231. /// Contains only the parts of `Issue` that are needed for turning the issue title into a Zulip
  232. /// topic.
  233. #[derive(Clone, Debug, PartialEq, Eq)]
  234. pub struct ZulipGitHubReference {
  235. pub number: u64,
  236. pub title: String,
  237. pub repository: IssueRepository,
  238. }
  239. impl ZulipGitHubReference {
  240. pub fn zulip_topic_reference(&self) -> String {
  241. let repo = &self.repository;
  242. if repo.organization == "rust-lang" {
  243. if repo.repository == "rust" {
  244. format!("#{}", self.number)
  245. } else {
  246. format!("{}#{}", repo.repository, self.number)
  247. }
  248. } else {
  249. format!("{}/{}#{}", repo.organization, repo.repository, self.number)
  250. }
  251. }
  252. }
  253. #[derive(Debug, serde::Deserialize)]
  254. pub struct Comment {
  255. #[serde(deserialize_with = "opt_string")]
  256. pub body: String,
  257. pub html_url: String,
  258. pub user: User,
  259. #[serde(alias = "submitted_at")] // for pull request reviews
  260. pub updated_at: chrono::DateTime<Utc>,
  261. #[serde(default, rename = "state")]
  262. pub pr_review_state: Option<PullRequestReviewState>,
  263. }
  264. #[derive(Debug, serde::Deserialize, Eq, PartialEq)]
  265. #[serde(rename_all = "SCREAMING_SNAKE_CASE")]
  266. pub enum PullRequestReviewState {
  267. Approved,
  268. ChangesRequested,
  269. Commented,
  270. Dismissed,
  271. Pending,
  272. }
  273. fn opt_string<'de, D>(deserializer: D) -> Result<String, D::Error>
  274. where
  275. D: serde::de::Deserializer<'de>,
  276. {
  277. use serde::de::Deserialize;
  278. match <Option<String>>::deserialize(deserializer) {
  279. Ok(v) => Ok(v.unwrap_or_default()),
  280. Err(e) => Err(e),
  281. }
  282. }
  283. #[derive(Debug)]
  284. pub enum AssignmentError {
  285. InvalidAssignee,
  286. Http(anyhow::Error),
  287. }
  288. #[derive(Debug)]
  289. pub enum Selection<'a, T: ?Sized> {
  290. All,
  291. One(&'a T),
  292. Except(&'a T),
  293. }
  294. impl fmt::Display for AssignmentError {
  295. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  296. match self {
  297. AssignmentError::InvalidAssignee => write!(f, "invalid assignee"),
  298. AssignmentError::Http(e) => write!(f, "cannot assign: {}", e),
  299. }
  300. }
  301. }
  302. impl std::error::Error for AssignmentError {}
  303. #[derive(Debug, Clone, PartialEq, Eq)]
  304. pub struct IssueRepository {
  305. pub organization: String,
  306. pub repository: String,
  307. }
  308. impl fmt::Display for IssueRepository {
  309. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  310. write!(f, "{}/{}", self.organization, self.repository)
  311. }
  312. }
  313. impl IssueRepository {
  314. fn url(&self) -> String {
  315. format!(
  316. "https://api.github.com/repos/{}/{}",
  317. self.organization, self.repository
  318. )
  319. }
  320. }
  321. impl Issue {
  322. pub fn to_zulip_github_reference(&self) -> ZulipGitHubReference {
  323. ZulipGitHubReference {
  324. number: self.number,
  325. title: self.title.clone(),
  326. repository: self.repository().clone(),
  327. }
  328. }
  329. pub fn repository(&self) -> &IssueRepository {
  330. self.repository.get_or_init(|| {
  331. // https://api.github.com/repos/rust-lang/rust/issues/69257/comments
  332. log::trace!("get repository for {}", self.comments_url);
  333. let url = url::Url::parse(&self.comments_url).unwrap();
  334. let mut segments = url.path_segments().unwrap();
  335. let _comments = segments.next_back().unwrap();
  336. let _number = segments.next_back().unwrap();
  337. let _issues_or_prs = segments.next_back().unwrap();
  338. let repository = segments.next_back().unwrap();
  339. let organization = segments.next_back().unwrap();
  340. IssueRepository {
  341. organization: organization.into(),
  342. repository: repository.into(),
  343. }
  344. })
  345. }
  346. pub fn global_id(&self) -> String {
  347. format!("{}#{}", self.repository(), self.number)
  348. }
  349. pub fn is_pr(&self) -> bool {
  350. self.pull_request.is_some()
  351. }
  352. pub async fn get_comment(&self, client: &GithubClient, id: usize) -> anyhow::Result<Comment> {
  353. let comment_url = format!("{}/issues/comments/{}", self.repository().url(), id);
  354. let comment = client.json(client.get(&comment_url)).await?;
  355. Ok(comment)
  356. }
  357. pub async fn edit_body(&self, client: &GithubClient, body: &str) -> anyhow::Result<()> {
  358. let edit_url = format!("{}/issues/{}", self.repository().url(), self.number);
  359. #[derive(serde::Serialize)]
  360. struct ChangedIssue<'a> {
  361. body: &'a str,
  362. }
  363. client
  364. ._send_req(client.patch(&edit_url).json(&ChangedIssue { body }))
  365. .await
  366. .context("failed to edit issue body")?;
  367. Ok(())
  368. }
  369. pub async fn edit_comment(
  370. &self,
  371. client: &GithubClient,
  372. id: usize,
  373. new_body: &str,
  374. ) -> anyhow::Result<()> {
  375. let comment_url = format!("{}/issues/comments/{}", self.repository().url(), id);
  376. #[derive(serde::Serialize)]
  377. struct NewComment<'a> {
  378. body: &'a str,
  379. }
  380. client
  381. ._send_req(
  382. client
  383. .patch(&comment_url)
  384. .json(&NewComment { body: new_body }),
  385. )
  386. .await
  387. .context("failed to edit comment")?;
  388. Ok(())
  389. }
  390. pub async fn post_comment(&self, client: &GithubClient, body: &str) -> anyhow::Result<()> {
  391. #[derive(serde::Serialize)]
  392. struct PostComment<'a> {
  393. body: &'a str,
  394. }
  395. client
  396. ._send_req(client.post(&self.comments_url).json(&PostComment { body }))
  397. .await
  398. .context("failed to post comment")?;
  399. Ok(())
  400. }
  401. pub async fn set_labels(
  402. &self,
  403. client: &GithubClient,
  404. labels: Vec<Label>,
  405. ) -> anyhow::Result<()> {
  406. log::info!("set_labels {} to {:?}", self.global_id(), labels);
  407. // PUT /repos/:owner/:repo/issues/:number/labels
  408. // repo_url = https://api.github.com/repos/Codertocat/Hello-World
  409. let url = format!(
  410. "{repo_url}/issues/{number}/labels",
  411. repo_url = self.repository().url(),
  412. number = self.number
  413. );
  414. let mut stream = labels
  415. .into_iter()
  416. .map(|label| async { (label.exists(&self.repository().url(), &client).await, label) })
  417. .collect::<FuturesUnordered<_>>();
  418. let mut labels = Vec::new();
  419. while let Some((true, label)) = stream.next().await {
  420. labels.push(label);
  421. }
  422. #[derive(serde::Serialize)]
  423. struct LabelsReq {
  424. labels: Vec<String>,
  425. }
  426. client
  427. ._send_req(client.put(&url).json(&LabelsReq {
  428. labels: labels.iter().map(|l| l.name.clone()).collect(),
  429. }))
  430. .await
  431. .context("failed to set labels")?;
  432. Ok(())
  433. }
  434. pub fn labels(&self) -> &[Label] {
  435. &self.labels
  436. }
  437. pub fn contain_assignee(&self, user: &str) -> bool {
  438. self.assignees.iter().any(|a| a.login == user)
  439. }
  440. pub async fn remove_assignees(
  441. &self,
  442. client: &GithubClient,
  443. selection: Selection<'_, str>,
  444. ) -> Result<(), AssignmentError> {
  445. log::info!("remove {:?} assignees for {}", selection, self.global_id());
  446. let url = format!(
  447. "{repo_url}/issues/{number}/assignees",
  448. repo_url = self.repository().url(),
  449. number = self.number
  450. );
  451. let assignees = match selection {
  452. Selection::All => self
  453. .assignees
  454. .iter()
  455. .map(|u| u.login.as_str())
  456. .collect::<Vec<_>>(),
  457. Selection::One(user) => vec![user],
  458. Selection::Except(user) => self
  459. .assignees
  460. .iter()
  461. .map(|u| u.login.as_str())
  462. .filter(|&u| u != user)
  463. .collect::<Vec<_>>(),
  464. };
  465. #[derive(serde::Serialize)]
  466. struct AssigneeReq<'a> {
  467. assignees: &'a [&'a str],
  468. }
  469. client
  470. ._send_req(client.delete(&url).json(&AssigneeReq {
  471. assignees: &assignees[..],
  472. }))
  473. .await
  474. .map_err(AssignmentError::Http)?;
  475. Ok(())
  476. }
  477. pub async fn add_assignee(
  478. &self,
  479. client: &GithubClient,
  480. user: &str,
  481. ) -> Result<(), AssignmentError> {
  482. log::info!("add_assignee {} for {}", user, self.global_id());
  483. let url = format!(
  484. "{repo_url}/issues/{number}/assignees",
  485. repo_url = self.repository().url(),
  486. number = self.number
  487. );
  488. #[derive(serde::Serialize)]
  489. struct AssigneeReq<'a> {
  490. assignees: &'a [&'a str],
  491. }
  492. let result: Issue = client
  493. .json(client.post(&url).json(&AssigneeReq { assignees: &[user] }))
  494. .await
  495. .map_err(AssignmentError::Http)?;
  496. // Invalid assignees are silently ignored. We can just check if the user is now
  497. // contained in the assignees list.
  498. let success = result.assignees.iter().any(|u| u.login.as_str() == user);
  499. if success {
  500. Ok(())
  501. } else {
  502. Err(AssignmentError::InvalidAssignee)
  503. }
  504. }
  505. pub async fn set_assignee(
  506. &self,
  507. client: &GithubClient,
  508. user: &str,
  509. ) -> Result<(), AssignmentError> {
  510. log::info!("set_assignee for {} to {}", self.global_id(), user);
  511. self.add_assignee(client, user).await?;
  512. self.remove_assignees(client, Selection::Except(user))
  513. .await?;
  514. Ok(())
  515. }
  516. pub async fn set_milestone(&self, client: &GithubClient, title: &str) -> anyhow::Result<()> {
  517. log::trace!(
  518. "Setting milestone for rust-lang/rust#{} to {}",
  519. self.number,
  520. title
  521. );
  522. let create_url = format!("{}/milestones", self.repository().url());
  523. let resp = client
  524. .send_req(
  525. client
  526. .post(&create_url)
  527. .body(serde_json::to_vec(&MilestoneCreateBody { title }).unwrap()),
  528. )
  529. .await;
  530. // Explicitly do *not* try to return Err(...) if this fails -- that's
  531. // fine, it just means the milestone was already created.
  532. log::trace!("Created milestone: {:?}", resp);
  533. let list_url = format!("{}/milestones", self.repository().url());
  534. let milestone_list: Vec<Milestone> = client.json(client.get(&list_url)).await?;
  535. let milestone_no = if let Some(milestone) = milestone_list.iter().find(|v| v.title == title)
  536. {
  537. milestone.number
  538. } else {
  539. anyhow::bail!(
  540. "Despite just creating milestone {} on {}, it does not exist?",
  541. title,
  542. self.repository()
  543. )
  544. };
  545. #[derive(serde::Serialize)]
  546. struct SetMilestone {
  547. milestone: u64,
  548. }
  549. let url = format!("{}/issues/{}", self.repository().url(), self.number);
  550. client
  551. ._send_req(client.patch(&url).json(&SetMilestone {
  552. milestone: milestone_no,
  553. }))
  554. .await
  555. .context("failed to set milestone")?;
  556. Ok(())
  557. }
  558. pub async fn close(&self, client: &GithubClient) -> anyhow::Result<()> {
  559. let edit_url = format!("{}/issues/{}", self.repository().url(), self.number);
  560. #[derive(serde::Serialize)]
  561. struct CloseIssue<'a> {
  562. state: &'a str,
  563. }
  564. client
  565. ._send_req(
  566. client
  567. .patch(&edit_url)
  568. .json(&CloseIssue { state: "closed" }),
  569. )
  570. .await
  571. .context("failed to close issue")?;
  572. Ok(())
  573. }
  574. }
  575. #[derive(serde::Serialize)]
  576. struct MilestoneCreateBody<'a> {
  577. title: &'a str,
  578. }
  579. #[derive(Debug, serde::Deserialize)]
  580. pub struct Milestone {
  581. number: u64,
  582. title: String,
  583. }
  584. #[derive(Debug, serde::Deserialize)]
  585. pub struct ChangeInner {
  586. pub from: String,
  587. }
  588. #[derive(Debug, serde::Deserialize)]
  589. pub struct Changes {
  590. pub title: Option<ChangeInner>,
  591. pub body: Option<ChangeInner>,
  592. }
  593. #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
  594. #[serde(rename_all = "lowercase")]
  595. pub enum PullRequestReviewAction {
  596. Submitted,
  597. Edited,
  598. Dismissed,
  599. }
  600. #[derive(Debug, serde::Deserialize)]
  601. pub struct PullRequestReviewEvent {
  602. pub action: PullRequestReviewAction,
  603. pub pull_request: Issue,
  604. pub review: Comment,
  605. pub changes: Option<Changes>,
  606. pub repository: Repository,
  607. }
  608. #[derive(Debug, serde::Deserialize)]
  609. pub struct PullRequestReviewComment {
  610. pub action: IssueCommentAction,
  611. pub changes: Option<Changes>,
  612. #[serde(rename = "pull_request")]
  613. pub issue: Issue,
  614. pub comment: Comment,
  615. pub repository: Repository,
  616. }
  617. #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
  618. #[serde(rename_all = "lowercase")]
  619. pub enum IssueCommentAction {
  620. Created,
  621. Edited,
  622. Deleted,
  623. }
  624. #[derive(Debug, serde::Deserialize)]
  625. pub struct IssueCommentEvent {
  626. pub action: IssueCommentAction,
  627. pub changes: Option<Changes>,
  628. pub issue: Issue,
  629. pub comment: Comment,
  630. pub repository: Repository,
  631. }
  632. #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
  633. #[serde(rename_all = "snake_case")]
  634. pub enum IssuesAction {
  635. Opened,
  636. Edited,
  637. Deleted,
  638. Transferred,
  639. Pinned,
  640. Unpinned,
  641. Closed,
  642. Reopened,
  643. Assigned,
  644. Unassigned,
  645. Labeled,
  646. Unlabeled,
  647. Locked,
  648. Unlocked,
  649. Milestoned,
  650. Demilestoned,
  651. ReviewRequested,
  652. ReviewRequestRemoved,
  653. ReadyForReview,
  654. Synchronize,
  655. ConvertedToDraft,
  656. }
  657. #[derive(Debug, serde::Deserialize)]
  658. pub struct IssuesEvent {
  659. pub action: IssuesAction,
  660. #[serde(alias = "pull_request")]
  661. pub issue: Issue,
  662. pub changes: Option<Changes>,
  663. pub repository: Repository,
  664. /// Some if action is IssuesAction::Labeled, for example
  665. pub label: Option<Label>,
  666. }
  667. #[derive(Debug, serde::Deserialize)]
  668. pub struct IssueSearchResult {
  669. pub total_count: usize,
  670. pub incomplete_results: bool,
  671. pub items: Vec<Issue>,
  672. }
  673. #[derive(Debug, serde::Deserialize)]
  674. pub struct Repository {
  675. pub full_name: String,
  676. }
  677. struct Ordering<'a> {
  678. pub sort: &'a str,
  679. pub direction: &'a str,
  680. pub per_page: &'a str,
  681. }
  682. impl Repository {
  683. const GITHUB_API_URL: &'static str = "https://api.github.com";
  684. const GITHUB_GRAPHQL_API_URL: &'static str = "https://api.github.com/graphql";
  685. pub fn owner(&self) -> &str {
  686. self.full_name.split_once('/').unwrap().0
  687. }
  688. pub fn name(&self) -> &str {
  689. self.full_name.split_once('/').unwrap().1
  690. }
  691. pub async fn get_issues<'a>(
  692. &self,
  693. client: &GithubClient,
  694. query: &Query<'a>,
  695. ) -> anyhow::Result<Vec<Issue>> {
  696. let Query {
  697. filters,
  698. include_labels,
  699. exclude_labels,
  700. } = query;
  701. let mut ordering = Ordering {
  702. sort: "created",
  703. direction: "asc",
  704. per_page: "100",
  705. };
  706. let filters: Vec<_> = filters
  707. .clone()
  708. .into_iter()
  709. .filter(|(key, val)| {
  710. match *key {
  711. "sort" => ordering.sort = val,
  712. "direction" => ordering.direction = val,
  713. "per_page" => ordering.per_page = val,
  714. _ => return true,
  715. };
  716. false
  717. })
  718. .collect();
  719. // `is: pull-request` indicates the query to retrieve PRs only
  720. let is_pr = filters
  721. .iter()
  722. .any(|&(key, value)| key == "is" && value == "pull-request");
  723. // There are some cases that can only be handled by the search API:
  724. // 1. When using negating label filters (exclude_labels)
  725. // 2. When there's a key parameter key=no
  726. // 3. When the query is to retrieve PRs only and there are label filters
  727. //
  728. // Check https://docs.github.com/en/rest/reference/search#search-issues-and-pull-requests
  729. // for more information
  730. let use_search_api = !exclude_labels.is_empty()
  731. || filters.iter().any(|&(key, _)| key == "no")
  732. || is_pr && !include_labels.is_empty();
  733. let url = if use_search_api {
  734. self.build_search_issues_url(&filters, include_labels, exclude_labels, ordering)
  735. } else if is_pr {
  736. self.build_pulls_url(&filters, include_labels, ordering)
  737. } else {
  738. self.build_issues_url(&filters, include_labels, ordering)
  739. };
  740. let result = client.get(&url);
  741. if use_search_api {
  742. let result = client
  743. .json::<IssueSearchResult>(result)
  744. .await
  745. .with_context(|| format!("failed to list issues from {}", url))?;
  746. Ok(result.items)
  747. } else {
  748. client
  749. .json(result)
  750. .await
  751. .with_context(|| format!("failed to list issues from {}", url))
  752. }
  753. }
  754. fn build_issues_url(
  755. &self,
  756. filters: &Vec<(&str, &str)>,
  757. include_labels: &Vec<&str>,
  758. ordering: Ordering<'_>,
  759. ) -> String {
  760. self.build_endpoint_url("issues", filters, include_labels, ordering)
  761. }
  762. fn build_pulls_url(
  763. &self,
  764. filters: &Vec<(&str, &str)>,
  765. include_labels: &Vec<&str>,
  766. ordering: Ordering<'_>,
  767. ) -> String {
  768. self.build_endpoint_url("pulls", filters, include_labels, ordering)
  769. }
  770. fn build_endpoint_url(
  771. &self,
  772. endpoint: &str,
  773. filters: &Vec<(&str, &str)>,
  774. include_labels: &Vec<&str>,
  775. ordering: Ordering<'_>,
  776. ) -> String {
  777. let filters = filters
  778. .iter()
  779. .map(|(key, val)| format!("{}={}", key, val))
  780. .chain(std::iter::once(format!(
  781. "labels={}",
  782. include_labels.join(",")
  783. )))
  784. .chain(std::iter::once("filter=all".to_owned()))
  785. .chain(std::iter::once(format!("sort={}", ordering.sort,)))
  786. .chain(std::iter::once(
  787. format!("direction={}", ordering.direction,),
  788. ))
  789. .chain(std::iter::once(format!("per_page={}", ordering.per_page,)))
  790. .collect::<Vec<_>>()
  791. .join("&");
  792. format!(
  793. "{}/repos/{}/{}?{}",
  794. Repository::GITHUB_API_URL,
  795. self.full_name,
  796. endpoint,
  797. filters
  798. )
  799. }
  800. fn build_search_issues_url(
  801. &self,
  802. filters: &Vec<(&str, &str)>,
  803. include_labels: &Vec<&str>,
  804. exclude_labels: &Vec<&str>,
  805. ordering: Ordering<'_>,
  806. ) -> String {
  807. let filters = filters
  808. .iter()
  809. .filter(|&&(key, val)| !(key == "state" && val == "all"))
  810. .map(|(key, val)| format!("{}:{}", key, val))
  811. .chain(
  812. include_labels
  813. .iter()
  814. .map(|label| format!("label:{}", label)),
  815. )
  816. .chain(
  817. exclude_labels
  818. .iter()
  819. .map(|label| format!("-label:{}", label)),
  820. )
  821. .chain(std::iter::once(format!("repo:{}", self.full_name)))
  822. .collect::<Vec<_>>()
  823. .join("+");
  824. format!(
  825. "{}/search/issues?q={}&sort={}&order={}&per_page={}",
  826. Repository::GITHUB_API_URL,
  827. filters,
  828. ordering.sort,
  829. ordering.direction,
  830. ordering.per_page,
  831. )
  832. }
  833. }
  834. pub struct Query<'a> {
  835. // key/value filter
  836. pub filters: Vec<(&'a str, &'a str)>,
  837. pub include_labels: Vec<&'a str>,
  838. pub exclude_labels: Vec<&'a str>,
  839. }
  840. #[async_trait]
  841. impl<'q> IssuesQuery for Query<'q> {
  842. async fn query<'a>(
  843. &'a self,
  844. repo: &'a Repository,
  845. client: &'a GithubClient,
  846. ) -> anyhow::Result<Vec<crate::actions::IssueDecorator>> {
  847. let issues = repo
  848. .get_issues(&client, self)
  849. .await
  850. .with_context(|| "Unable to get issues.")?;
  851. let issues_decorator: Vec<_> = issues
  852. .iter()
  853. .map(|issue| crate::actions::IssueDecorator {
  854. title: issue.title.clone(),
  855. number: issue.number,
  856. html_url: issue.html_url.clone(),
  857. repo_name: repo.name().to_owned(),
  858. labels: issue
  859. .labels
  860. .iter()
  861. .map(|l| l.name.as_ref())
  862. .collect::<Vec<_>>()
  863. .join(", "),
  864. assignees: issue
  865. .assignees
  866. .iter()
  867. .map(|u| u.login.as_ref())
  868. .collect::<Vec<_>>()
  869. .join(", "),
  870. updated_at: crate::actions::to_human(issue.updated_at),
  871. })
  872. .collect();
  873. Ok(issues_decorator)
  874. }
  875. }
  876. #[derive(Debug, serde::Deserialize)]
  877. #[serde(rename_all = "snake_case")]
  878. pub enum CreateKind {
  879. Branch,
  880. Tag,
  881. }
  882. #[derive(Debug, serde::Deserialize)]
  883. pub struct CreateEvent {
  884. pub ref_type: CreateKind,
  885. repository: Repository,
  886. sender: User,
  887. }
  888. #[derive(Debug, serde::Deserialize)]
  889. pub struct PushEvent {
  890. #[serde(rename = "ref")]
  891. pub git_ref: String,
  892. repository: Repository,
  893. sender: User,
  894. }
  895. #[derive(Debug)]
  896. pub enum Event {
  897. Create(CreateEvent),
  898. IssueComment(IssueCommentEvent),
  899. Issue(IssuesEvent),
  900. Push(PushEvent),
  901. }
  902. impl Event {
  903. pub fn repo_name(&self) -> String {
  904. match self {
  905. Event::Create(event) => event.repository.full_name.clone(),
  906. Event::IssueComment(event) => event.repository.full_name.clone(),
  907. Event::Issue(event) => event.repository.full_name.clone(),
  908. Event::Push(event) => event.repository.full_name.clone(),
  909. }
  910. }
  911. pub fn issue(&self) -> Option<&Issue> {
  912. match self {
  913. Event::Create(_) => None,
  914. Event::IssueComment(event) => Some(&event.issue),
  915. Event::Issue(event) => Some(&event.issue),
  916. Event::Push(_) => None,
  917. }
  918. }
  919. /// This will both extract from IssueComment events but also Issue events
  920. pub fn comment_body(&self) -> Option<&str> {
  921. match self {
  922. Event::Create(_) => None,
  923. Event::Issue(e) => Some(&e.issue.body),
  924. Event::IssueComment(e) => Some(&e.comment.body),
  925. Event::Push(_) => None,
  926. }
  927. }
  928. /// This will both extract from IssueComment events but also Issue events
  929. pub fn comment_from(&self) -> Option<&str> {
  930. match self {
  931. Event::Create(_) => None,
  932. Event::Issue(e) => Some(&e.changes.as_ref()?.body.as_ref()?.from),
  933. Event::IssueComment(e) => Some(&e.changes.as_ref()?.body.as_ref()?.from),
  934. Event::Push(_) => None,
  935. }
  936. }
  937. pub fn html_url(&self) -> Option<&str> {
  938. match self {
  939. Event::Create(_) => None,
  940. Event::Issue(e) => Some(&e.issue.html_url),
  941. Event::IssueComment(e) => Some(&e.comment.html_url),
  942. Event::Push(_) => None,
  943. }
  944. }
  945. pub fn user(&self) -> &User {
  946. match self {
  947. Event::Create(e) => &e.sender,
  948. Event::Issue(e) => &e.issue.user,
  949. Event::IssueComment(e) => &e.comment.user,
  950. Event::Push(e) => &e.sender,
  951. }
  952. }
  953. pub fn time(&self) -> Option<chrono::DateTime<FixedOffset>> {
  954. match self {
  955. Event::Create(_) => None,
  956. Event::Issue(e) => Some(e.issue.created_at.into()),
  957. Event::IssueComment(e) => Some(e.comment.updated_at.into()),
  958. Event::Push(_) => None,
  959. }
  960. }
  961. }
  962. trait RequestSend: Sized {
  963. fn configure(self, g: &GithubClient) -> Self;
  964. }
  965. impl RequestSend for RequestBuilder {
  966. fn configure(self, g: &GithubClient) -> RequestBuilder {
  967. let mut auth = HeaderValue::from_maybe_shared(format!("token {}", g.token)).unwrap();
  968. auth.set_sensitive(true);
  969. self.header(USER_AGENT, "rust-lang-triagebot")
  970. .header(AUTHORIZATION, &auth)
  971. }
  972. }
  973. /// Finds the token in the user's environment, panicking if no suitable token
  974. /// can be found.
  975. pub fn default_token_from_env() -> String {
  976. match std::env::var("GITHUB_API_TOKEN") {
  977. Ok(v) => return v,
  978. Err(_) => (),
  979. }
  980. match get_token_from_git_config() {
  981. Ok(v) => return v,
  982. Err(_) => (),
  983. }
  984. panic!("could not find token in GITHUB_API_TOKEN or .gitconfig/github.oath-token")
  985. }
  986. fn get_token_from_git_config() -> anyhow::Result<String> {
  987. let output = std::process::Command::new("git")
  988. .arg("config")
  989. .arg("--get")
  990. .arg("github.oauth-token")
  991. .output()?;
  992. if !output.status.success() {
  993. anyhow::bail!("error received executing `git`: {:?}", output.status);
  994. }
  995. let git_token = String::from_utf8(output.stdout)?.trim().to_string();
  996. Ok(git_token)
  997. }
  998. #[derive(Clone)]
  999. pub struct GithubClient {
  1000. token: String,
  1001. client: Client,
  1002. }
  1003. impl GithubClient {
  1004. pub fn new(client: Client, token: String) -> Self {
  1005. GithubClient { client, token }
  1006. }
  1007. pub fn new_with_default_token(client: Client) -> Self {
  1008. Self::new(client, default_token_from_env())
  1009. }
  1010. pub fn raw(&self) -> &Client {
  1011. &self.client
  1012. }
  1013. pub async fn raw_file(
  1014. &self,
  1015. repo: &str,
  1016. branch: &str,
  1017. path: &str,
  1018. ) -> anyhow::Result<Option<Vec<u8>>> {
  1019. let url = format!(
  1020. "https://raw.githubusercontent.com/{}/{}/{}",
  1021. repo, branch, path
  1022. );
  1023. let req = self.get(&url);
  1024. let req_dbg = format!("{:?}", req);
  1025. let req = req
  1026. .build()
  1027. .with_context(|| format!("failed to build request {:?}", req_dbg))?;
  1028. let mut resp = self.client.execute(req).await.context(req_dbg.clone())?;
  1029. let status = resp.status();
  1030. match status {
  1031. StatusCode::OK => {
  1032. let mut buf = Vec::with_capacity(resp.content_length().unwrap_or(4) as usize);
  1033. while let Some(chunk) = resp.chunk().await.transpose() {
  1034. let chunk = chunk
  1035. .context("reading stream failed")
  1036. .map_err(anyhow::Error::from)
  1037. .context(req_dbg.clone())?;
  1038. buf.extend_from_slice(&chunk);
  1039. }
  1040. Ok(Some(buf))
  1041. }
  1042. StatusCode::NOT_FOUND => Ok(None),
  1043. status => anyhow::bail!("failed to GET {}: {}", url, status),
  1044. }
  1045. }
  1046. /// Get the raw gist content from the URL of the HTML version of the gist:
  1047. ///
  1048. /// `html_url` looks like `https://gist.github.com/rust-play/7e80ca3b1ec7abe08f60c41aff91f060`.
  1049. ///
  1050. /// `filename` is the name of the file you want the content of.
  1051. pub async fn raw_gist_from_url(
  1052. &self,
  1053. html_url: &str,
  1054. filename: &str,
  1055. ) -> anyhow::Result<String> {
  1056. let url = html_url.replace("github.com", "githubusercontent.com") + "/raw/" + filename;
  1057. let response = self.raw().get(&url).send().await?;
  1058. response.text().await.context("raw gist from url")
  1059. }
  1060. fn get(&self, url: &str) -> RequestBuilder {
  1061. log::trace!("get {:?}", url);
  1062. self.client.get(url).configure(self)
  1063. }
  1064. fn patch(&self, url: &str) -> RequestBuilder {
  1065. log::trace!("patch {:?}", url);
  1066. self.client.patch(url).configure(self)
  1067. }
  1068. fn delete(&self, url: &str) -> RequestBuilder {
  1069. log::trace!("delete {:?}", url);
  1070. self.client.delete(url).configure(self)
  1071. }
  1072. fn post(&self, url: &str) -> RequestBuilder {
  1073. log::trace!("post {:?}", url);
  1074. self.client.post(url).configure(self)
  1075. }
  1076. fn put(&self, url: &str) -> RequestBuilder {
  1077. log::trace!("put {:?}", url);
  1078. self.client.put(url).configure(self)
  1079. }
  1080. pub async fn rust_commit(&self, sha: &str) -> Option<GithubCommit> {
  1081. let req = self.get(&format!(
  1082. "https://api.github.com/repos/rust-lang/rust/commits/{}",
  1083. sha
  1084. ));
  1085. match self.json(req).await {
  1086. Ok(r) => Some(r),
  1087. Err(e) => {
  1088. log::error!("Failed to query commit {:?}: {:?}", sha, e);
  1089. None
  1090. }
  1091. }
  1092. }
  1093. /// This does not retrieve all of them, only the last several.
  1094. pub async fn bors_commits(&self) -> Vec<GithubCommit> {
  1095. let req = self.get("https://api.github.com/repos/rust-lang/rust/commits?author=bors");
  1096. match self.json(req).await {
  1097. Ok(r) => r,
  1098. Err(e) => {
  1099. log::error!("Failed to query commit list: {:?}", e);
  1100. Vec::new()
  1101. }
  1102. }
  1103. }
  1104. }
  1105. #[derive(Debug, serde::Deserialize)]
  1106. pub struct GithubCommit {
  1107. pub sha: String,
  1108. #[serde(default)]
  1109. pub message: String,
  1110. pub commit: GitCommit,
  1111. pub parents: Vec<Parent>,
  1112. }
  1113. #[derive(Debug, serde::Deserialize)]
  1114. pub struct GitCommit {
  1115. pub author: GitUser,
  1116. }
  1117. #[derive(Debug, serde::Deserialize)]
  1118. pub struct GitUser {
  1119. pub date: DateTime<FixedOffset>,
  1120. }
  1121. #[derive(Debug, serde::Deserialize)]
  1122. pub struct Parent {
  1123. pub sha: String,
  1124. }
  1125. #[async_trait]
  1126. pub trait IssuesQuery {
  1127. async fn query<'a>(
  1128. &'a self,
  1129. repo: &'a Repository,
  1130. client: &'a GithubClient,
  1131. ) -> anyhow::Result<Vec<crate::actions::IssueDecorator>>;
  1132. }