github.rs 39 KB

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