github.rs 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
  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. pull_request: Option<PullRequestEventFields>,
  679. }
  680. #[derive(Debug, serde::Deserialize)]
  681. struct PullRequestEventFields {
  682. base: CommitBase,
  683. head: CommitBase,
  684. }
  685. #[derive(Default, Clone, Debug, serde::Deserialize)]
  686. pub struct CommitBase {
  687. sha: String,
  688. }
  689. impl IssuesEvent {
  690. /// Returns the diff in this event, for Open and Synchronize events for now.
  691. pub async fn diff_between(&self, client: &GithubClient) -> anyhow::Result<Option<String>> {
  692. let (before, after) = if self.action == IssuesAction::Synchronize {
  693. (
  694. self.before
  695. .clone()
  696. .expect("synchronize has before populated"),
  697. self.after.clone().expect("synchronize has after populated"),
  698. )
  699. } else if self.action == IssuesAction::Opened {
  700. if let Some(pr) = &self.pull_request {
  701. (pr.base.sha.clone(), pr.head.sha.clone())
  702. } else {
  703. return Ok(None);
  704. }
  705. } else {
  706. return Ok(None);
  707. };
  708. let mut req = client.get(&format!(
  709. "{}/compare/{}...{}",
  710. self.issue.repository().url(),
  711. before,
  712. after
  713. ));
  714. req = req.header("Accept", "application/vnd.github.v3.diff");
  715. let diff = client.send_req(req).await?;
  716. Ok(Some(String::from(String::from_utf8_lossy(&diff))))
  717. }
  718. }
  719. #[derive(Debug, serde::Deserialize)]
  720. pub struct IssueSearchResult {
  721. pub total_count: usize,
  722. pub incomplete_results: bool,
  723. pub items: Vec<Issue>,
  724. }
  725. #[derive(Debug, serde::Deserialize)]
  726. pub struct Repository {
  727. pub full_name: String,
  728. }
  729. struct Ordering<'a> {
  730. pub sort: &'a str,
  731. pub direction: &'a str,
  732. pub per_page: &'a str,
  733. }
  734. impl Repository {
  735. const GITHUB_API_URL: &'static str = "https://api.github.com";
  736. const GITHUB_GRAPHQL_API_URL: &'static str = "https://api.github.com/graphql";
  737. pub fn owner(&self) -> &str {
  738. self.full_name.split_once('/').unwrap().0
  739. }
  740. pub fn name(&self) -> &str {
  741. self.full_name.split_once('/').unwrap().1
  742. }
  743. pub async fn get_issues<'a>(
  744. &self,
  745. client: &GithubClient,
  746. query: &Query<'a>,
  747. ) -> anyhow::Result<Vec<Issue>> {
  748. let Query {
  749. filters,
  750. include_labels,
  751. exclude_labels,
  752. } = query;
  753. let mut ordering = Ordering {
  754. sort: "created",
  755. direction: "asc",
  756. per_page: "100",
  757. };
  758. let filters: Vec<_> = filters
  759. .clone()
  760. .into_iter()
  761. .filter(|(key, val)| {
  762. match *key {
  763. "sort" => ordering.sort = val,
  764. "direction" => ordering.direction = val,
  765. "per_page" => ordering.per_page = val,
  766. _ => return true,
  767. };
  768. false
  769. })
  770. .collect();
  771. // `is: pull-request` indicates the query to retrieve PRs only
  772. let is_pr = filters
  773. .iter()
  774. .any(|&(key, value)| key == "is" && value == "pull-request");
  775. // There are some cases that can only be handled by the search API:
  776. // 1. When using negating label filters (exclude_labels)
  777. // 2. When there's a key parameter key=no
  778. // 3. When the query is to retrieve PRs only and there are label filters
  779. //
  780. // Check https://docs.github.com/en/rest/reference/search#search-issues-and-pull-requests
  781. // for more information
  782. let use_search_api = !exclude_labels.is_empty()
  783. || filters.iter().any(|&(key, _)| key == "no")
  784. || is_pr && !include_labels.is_empty();
  785. let url = if use_search_api {
  786. self.build_search_issues_url(&filters, include_labels, exclude_labels, ordering)
  787. } else if is_pr {
  788. self.build_pulls_url(&filters, include_labels, ordering)
  789. } else {
  790. self.build_issues_url(&filters, include_labels, ordering)
  791. };
  792. let result = client.get(&url);
  793. if use_search_api {
  794. let result = client
  795. .json::<IssueSearchResult>(result)
  796. .await
  797. .with_context(|| format!("failed to list issues from {}", url))?;
  798. Ok(result.items)
  799. } else {
  800. client
  801. .json(result)
  802. .await
  803. .with_context(|| format!("failed to list issues from {}", url))
  804. }
  805. }
  806. fn build_issues_url(
  807. &self,
  808. filters: &Vec<(&str, &str)>,
  809. include_labels: &Vec<&str>,
  810. ordering: Ordering<'_>,
  811. ) -> String {
  812. self.build_endpoint_url("issues", filters, include_labels, ordering)
  813. }
  814. fn build_pulls_url(
  815. &self,
  816. filters: &Vec<(&str, &str)>,
  817. include_labels: &Vec<&str>,
  818. ordering: Ordering<'_>,
  819. ) -> String {
  820. self.build_endpoint_url("pulls", filters, include_labels, ordering)
  821. }
  822. fn build_endpoint_url(
  823. &self,
  824. endpoint: &str,
  825. filters: &Vec<(&str, &str)>,
  826. include_labels: &Vec<&str>,
  827. ordering: Ordering<'_>,
  828. ) -> String {
  829. let filters = filters
  830. .iter()
  831. .map(|(key, val)| format!("{}={}", key, val))
  832. .chain(std::iter::once(format!(
  833. "labels={}",
  834. include_labels.join(",")
  835. )))
  836. .chain(std::iter::once("filter=all".to_owned()))
  837. .chain(std::iter::once(format!("sort={}", ordering.sort,)))
  838. .chain(std::iter::once(
  839. format!("direction={}", ordering.direction,),
  840. ))
  841. .chain(std::iter::once(format!("per_page={}", ordering.per_page,)))
  842. .collect::<Vec<_>>()
  843. .join("&");
  844. format!(
  845. "{}/repos/{}/{}?{}",
  846. Repository::GITHUB_API_URL,
  847. self.full_name,
  848. endpoint,
  849. filters
  850. )
  851. }
  852. fn build_search_issues_url(
  853. &self,
  854. filters: &Vec<(&str, &str)>,
  855. include_labels: &Vec<&str>,
  856. exclude_labels: &Vec<&str>,
  857. ordering: Ordering<'_>,
  858. ) -> String {
  859. let filters = filters
  860. .iter()
  861. .filter(|&&(key, val)| !(key == "state" && val == "all"))
  862. .map(|(key, val)| format!("{}:{}", key, val))
  863. .chain(
  864. include_labels
  865. .iter()
  866. .map(|label| format!("label:{}", label)),
  867. )
  868. .chain(
  869. exclude_labels
  870. .iter()
  871. .map(|label| format!("-label:{}", label)),
  872. )
  873. .chain(std::iter::once(format!("repo:{}", self.full_name)))
  874. .collect::<Vec<_>>()
  875. .join("+");
  876. format!(
  877. "{}/search/issues?q={}&sort={}&order={}&per_page={}",
  878. Repository::GITHUB_API_URL,
  879. filters,
  880. ordering.sort,
  881. ordering.direction,
  882. ordering.per_page,
  883. )
  884. }
  885. }
  886. pub struct Query<'a> {
  887. // key/value filter
  888. pub filters: Vec<(&'a str, &'a str)>,
  889. pub include_labels: Vec<&'a str>,
  890. pub exclude_labels: Vec<&'a str>,
  891. }
  892. #[async_trait]
  893. impl<'q> IssuesQuery for Query<'q> {
  894. async fn query<'a>(
  895. &'a self,
  896. repo: &'a Repository,
  897. client: &'a GithubClient,
  898. ) -> anyhow::Result<Vec<crate::actions::IssueDecorator>> {
  899. let issues = repo
  900. .get_issues(&client, self)
  901. .await
  902. .with_context(|| "Unable to get issues.")?;
  903. let issues_decorator: Vec<_> = issues
  904. .iter()
  905. .map(|issue| crate::actions::IssueDecorator {
  906. title: issue.title.clone(),
  907. number: issue.number,
  908. html_url: issue.html_url.clone(),
  909. repo_name: repo.name().to_owned(),
  910. labels: issue
  911. .labels
  912. .iter()
  913. .map(|l| l.name.as_ref())
  914. .collect::<Vec<_>>()
  915. .join(", "),
  916. assignees: issue
  917. .assignees
  918. .iter()
  919. .map(|u| u.login.as_ref())
  920. .collect::<Vec<_>>()
  921. .join(", "),
  922. updated_at: crate::actions::to_human(issue.updated_at),
  923. })
  924. .collect();
  925. Ok(issues_decorator)
  926. }
  927. }
  928. #[derive(Debug, serde::Deserialize)]
  929. #[serde(rename_all = "snake_case")]
  930. pub enum CreateKind {
  931. Branch,
  932. Tag,
  933. }
  934. #[derive(Debug, serde::Deserialize)]
  935. pub struct CreateEvent {
  936. pub ref_type: CreateKind,
  937. repository: Repository,
  938. sender: User,
  939. }
  940. #[derive(Debug, serde::Deserialize)]
  941. pub struct PushEvent {
  942. #[serde(rename = "ref")]
  943. pub git_ref: String,
  944. repository: Repository,
  945. sender: User,
  946. }
  947. #[derive(Debug)]
  948. pub enum Event {
  949. Create(CreateEvent),
  950. IssueComment(IssueCommentEvent),
  951. Issue(IssuesEvent),
  952. Push(PushEvent),
  953. }
  954. impl Event {
  955. pub fn repo_name(&self) -> String {
  956. match self {
  957. Event::Create(event) => event.repository.full_name.clone(),
  958. Event::IssueComment(event) => event.repository.full_name.clone(),
  959. Event::Issue(event) => event.repository.full_name.clone(),
  960. Event::Push(event) => event.repository.full_name.clone(),
  961. }
  962. }
  963. pub fn issue(&self) -> Option<&Issue> {
  964. match self {
  965. Event::Create(_) => None,
  966. Event::IssueComment(event) => Some(&event.issue),
  967. Event::Issue(event) => Some(&event.issue),
  968. Event::Push(_) => None,
  969. }
  970. }
  971. /// This will both extract from IssueComment events but also Issue events
  972. pub fn comment_body(&self) -> Option<&str> {
  973. match self {
  974. Event::Create(_) => None,
  975. Event::Issue(e) => Some(&e.issue.body),
  976. Event::IssueComment(e) => Some(&e.comment.body),
  977. Event::Push(_) => None,
  978. }
  979. }
  980. /// This will both extract from IssueComment events but also Issue events
  981. pub fn comment_from(&self) -> Option<&str> {
  982. match self {
  983. Event::Create(_) => None,
  984. Event::Issue(e) => Some(&e.changes.as_ref()?.body.as_ref()?.from),
  985. Event::IssueComment(e) => Some(&e.changes.as_ref()?.body.as_ref()?.from),
  986. Event::Push(_) => None,
  987. }
  988. }
  989. pub fn html_url(&self) -> Option<&str> {
  990. match self {
  991. Event::Create(_) => None,
  992. Event::Issue(e) => Some(&e.issue.html_url),
  993. Event::IssueComment(e) => Some(&e.comment.html_url),
  994. Event::Push(_) => None,
  995. }
  996. }
  997. pub fn user(&self) -> &User {
  998. match self {
  999. Event::Create(e) => &e.sender,
  1000. Event::Issue(e) => &e.issue.user,
  1001. Event::IssueComment(e) => &e.comment.user,
  1002. Event::Push(e) => &e.sender,
  1003. }
  1004. }
  1005. pub fn time(&self) -> Option<chrono::DateTime<FixedOffset>> {
  1006. match self {
  1007. Event::Create(_) => None,
  1008. Event::Issue(e) => Some(e.issue.created_at.into()),
  1009. Event::IssueComment(e) => Some(e.comment.updated_at.into()),
  1010. Event::Push(_) => None,
  1011. }
  1012. }
  1013. }
  1014. trait RequestSend: Sized {
  1015. fn configure(self, g: &GithubClient) -> Self;
  1016. }
  1017. impl RequestSend for RequestBuilder {
  1018. fn configure(self, g: &GithubClient) -> RequestBuilder {
  1019. let mut auth = HeaderValue::from_maybe_shared(format!("token {}", g.token)).unwrap();
  1020. auth.set_sensitive(true);
  1021. self.header(USER_AGENT, "rust-lang-triagebot")
  1022. .header(AUTHORIZATION, &auth)
  1023. }
  1024. }
  1025. /// Finds the token in the user's environment, panicking if no suitable token
  1026. /// can be found.
  1027. pub fn default_token_from_env() -> String {
  1028. match std::env::var("GITHUB_API_TOKEN") {
  1029. Ok(v) => return v,
  1030. Err(_) => (),
  1031. }
  1032. match get_token_from_git_config() {
  1033. Ok(v) => return v,
  1034. Err(_) => (),
  1035. }
  1036. panic!("could not find token in GITHUB_API_TOKEN or .gitconfig/github.oath-token")
  1037. }
  1038. fn get_token_from_git_config() -> anyhow::Result<String> {
  1039. let output = std::process::Command::new("git")
  1040. .arg("config")
  1041. .arg("--get")
  1042. .arg("github.oauth-token")
  1043. .output()?;
  1044. if !output.status.success() {
  1045. anyhow::bail!("error received executing `git`: {:?}", output.status);
  1046. }
  1047. let git_token = String::from_utf8(output.stdout)?.trim().to_string();
  1048. Ok(git_token)
  1049. }
  1050. #[derive(Clone)]
  1051. pub struct GithubClient {
  1052. token: String,
  1053. client: Client,
  1054. }
  1055. impl GithubClient {
  1056. pub fn new(client: Client, token: String) -> Self {
  1057. GithubClient { client, token }
  1058. }
  1059. pub fn new_with_default_token(client: Client) -> Self {
  1060. Self::new(client, default_token_from_env())
  1061. }
  1062. pub fn raw(&self) -> &Client {
  1063. &self.client
  1064. }
  1065. pub async fn raw_file(
  1066. &self,
  1067. repo: &str,
  1068. branch: &str,
  1069. path: &str,
  1070. ) -> anyhow::Result<Option<Vec<u8>>> {
  1071. let url = format!(
  1072. "https://raw.githubusercontent.com/{}/{}/{}",
  1073. repo, branch, path
  1074. );
  1075. let req = self.get(&url);
  1076. let req_dbg = format!("{:?}", req);
  1077. let req = req
  1078. .build()
  1079. .with_context(|| format!("failed to build request {:?}", req_dbg))?;
  1080. let mut resp = self.client.execute(req).await.context(req_dbg.clone())?;
  1081. let status = resp.status();
  1082. match status {
  1083. StatusCode::OK => {
  1084. let mut buf = Vec::with_capacity(resp.content_length().unwrap_or(4) as usize);
  1085. while let Some(chunk) = resp.chunk().await.transpose() {
  1086. let chunk = chunk
  1087. .context("reading stream failed")
  1088. .map_err(anyhow::Error::from)
  1089. .context(req_dbg.clone())?;
  1090. buf.extend_from_slice(&chunk);
  1091. }
  1092. Ok(Some(buf))
  1093. }
  1094. StatusCode::NOT_FOUND => Ok(None),
  1095. status => anyhow::bail!("failed to GET {}: {}", url, status),
  1096. }
  1097. }
  1098. /// Get the raw gist content from the URL of the HTML version of the gist:
  1099. ///
  1100. /// `html_url` looks like `https://gist.github.com/rust-play/7e80ca3b1ec7abe08f60c41aff91f060`.
  1101. ///
  1102. /// `filename` is the name of the file you want the content of.
  1103. pub async fn raw_gist_from_url(
  1104. &self,
  1105. html_url: &str,
  1106. filename: &str,
  1107. ) -> anyhow::Result<String> {
  1108. let url = html_url.replace("github.com", "githubusercontent.com") + "/raw/" + filename;
  1109. let response = self.raw().get(&url).send().await?;
  1110. response.text().await.context("raw gist from url")
  1111. }
  1112. fn get(&self, url: &str) -> RequestBuilder {
  1113. log::trace!("get {:?}", url);
  1114. self.client.get(url).configure(self)
  1115. }
  1116. fn patch(&self, url: &str) -> RequestBuilder {
  1117. log::trace!("patch {:?}", url);
  1118. self.client.patch(url).configure(self)
  1119. }
  1120. fn delete(&self, url: &str) -> RequestBuilder {
  1121. log::trace!("delete {:?}", url);
  1122. self.client.delete(url).configure(self)
  1123. }
  1124. fn post(&self, url: &str) -> RequestBuilder {
  1125. log::trace!("post {:?}", url);
  1126. self.client.post(url).configure(self)
  1127. }
  1128. fn put(&self, url: &str) -> RequestBuilder {
  1129. log::trace!("put {:?}", url);
  1130. self.client.put(url).configure(self)
  1131. }
  1132. pub async fn rust_commit(&self, sha: &str) -> Option<GithubCommit> {
  1133. let req = self.get(&format!(
  1134. "https://api.github.com/repos/rust-lang/rust/commits/{}",
  1135. sha
  1136. ));
  1137. match self.json(req).await {
  1138. Ok(r) => Some(r),
  1139. Err(e) => {
  1140. log::error!("Failed to query commit {:?}: {:?}", sha, e);
  1141. None
  1142. }
  1143. }
  1144. }
  1145. /// This does not retrieve all of them, only the last several.
  1146. pub async fn bors_commits(&self) -> Vec<GithubCommit> {
  1147. let req = self.get("https://api.github.com/repos/rust-lang/rust/commits?author=bors");
  1148. match self.json(req).await {
  1149. Ok(r) => r,
  1150. Err(e) => {
  1151. log::error!("Failed to query commit list: {:?}", e);
  1152. Vec::new()
  1153. }
  1154. }
  1155. }
  1156. }
  1157. #[derive(Debug, serde::Deserialize)]
  1158. pub struct GithubCommit {
  1159. pub sha: String,
  1160. #[serde(default)]
  1161. pub message: String,
  1162. pub commit: GitCommit,
  1163. pub parents: Vec<Parent>,
  1164. }
  1165. #[derive(Debug, serde::Deserialize)]
  1166. pub struct GitCommit {
  1167. pub author: GitUser,
  1168. }
  1169. #[derive(Debug, serde::Deserialize)]
  1170. pub struct GitUser {
  1171. pub date: DateTime<FixedOffset>,
  1172. }
  1173. #[derive(Debug, serde::Deserialize)]
  1174. pub struct Parent {
  1175. pub sha: String,
  1176. }
  1177. #[async_trait]
  1178. pub trait IssuesQuery {
  1179. async fn query<'a>(
  1180. &'a self,
  1181. repo: &'a Repository,
  1182. client: &'a GithubClient,
  1183. ) -> anyhow::Result<Vec<crate::actions::IssueDecorator>>;
  1184. }