github.rs 32 KB

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