github.rs 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  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. /// Contains only the parts of `Issue` that are needed for turning the issue title into a Zulip
  229. /// topic.
  230. #[derive(Clone, Debug, PartialEq, Eq)]
  231. pub struct ZulipGitHubReference {
  232. pub number: u64,
  233. pub title: String,
  234. pub repository: IssueRepository,
  235. }
  236. impl ZulipGitHubReference {
  237. pub fn zulip_topic_reference(&self) -> String {
  238. let repo = &self.repository;
  239. if repo.organization == "rust-lang" {
  240. if repo.repository == "rust" {
  241. format!("#{}", self.number)
  242. } else {
  243. format!("{}#{}", repo.repository, self.number)
  244. }
  245. } else {
  246. format!("{}/{}#{}", repo.organization, repo.repository, self.number)
  247. }
  248. }
  249. }
  250. #[derive(Debug, serde::Deserialize)]
  251. pub struct Comment {
  252. #[serde(deserialize_with = "opt_string")]
  253. pub body: String,
  254. pub html_url: String,
  255. pub user: User,
  256. #[serde(alias = "submitted_at")] // for pull request reviews
  257. pub updated_at: chrono::DateTime<Utc>,
  258. }
  259. fn opt_string<'de, D>(deserializer: D) -> Result<String, D::Error>
  260. where
  261. D: serde::de::Deserializer<'de>,
  262. {
  263. use serde::de::Deserialize;
  264. match <Option<String>>::deserialize(deserializer) {
  265. Ok(v) => Ok(v.unwrap_or_default()),
  266. Err(e) => Err(e),
  267. }
  268. }
  269. #[derive(Debug)]
  270. pub enum AssignmentError {
  271. InvalidAssignee,
  272. Http(anyhow::Error),
  273. }
  274. #[derive(Debug)]
  275. pub enum Selection<'a, T: ?Sized> {
  276. All,
  277. One(&'a T),
  278. Except(&'a T),
  279. }
  280. impl fmt::Display for AssignmentError {
  281. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  282. match self {
  283. AssignmentError::InvalidAssignee => write!(f, "invalid assignee"),
  284. AssignmentError::Http(e) => write!(f, "cannot assign: {}", e),
  285. }
  286. }
  287. }
  288. impl std::error::Error for AssignmentError {}
  289. #[derive(Debug, Clone, PartialEq, Eq)]
  290. pub struct IssueRepository {
  291. pub organization: String,
  292. pub repository: String,
  293. }
  294. impl fmt::Display for IssueRepository {
  295. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  296. write!(f, "{}/{}", self.organization, self.repository)
  297. }
  298. }
  299. impl IssueRepository {
  300. fn url(&self) -> String {
  301. format!(
  302. "https://api.github.com/repos/{}/{}",
  303. self.organization, self.repository
  304. )
  305. }
  306. }
  307. impl Issue {
  308. pub fn to_zulip_github_reference(&self) -> ZulipGitHubReference {
  309. ZulipGitHubReference {
  310. number: self.number,
  311. title: self.title.clone(),
  312. repository: self.repository.get().unwrap().clone(),
  313. }
  314. }
  315. pub fn repository(&self) -> &IssueRepository {
  316. self.repository.get_or_init(|| {
  317. // https://api.github.com/repos/rust-lang/rust/issues/69257/comments
  318. log::trace!("get repository for {}", self.comments_url);
  319. let url = url::Url::parse(&self.comments_url).unwrap();
  320. let mut segments = url.path_segments().unwrap();
  321. let _comments = segments.next_back().unwrap();
  322. let _number = segments.next_back().unwrap();
  323. let _issues_or_prs = segments.next_back().unwrap();
  324. let repository = segments.next_back().unwrap();
  325. let organization = segments.next_back().unwrap();
  326. IssueRepository {
  327. organization: organization.into(),
  328. repository: repository.into(),
  329. }
  330. })
  331. }
  332. pub fn global_id(&self) -> String {
  333. format!("{}#{}", self.repository(), self.number)
  334. }
  335. pub fn is_pr(&self) -> bool {
  336. self.pull_request.is_some()
  337. }
  338. pub async fn get_comment(&self, client: &GithubClient, id: usize) -> anyhow::Result<Comment> {
  339. let comment_url = format!("{}/issues/comments/{}", self.repository().url(), id);
  340. let comment = client.json(client.get(&comment_url)).await?;
  341. Ok(comment)
  342. }
  343. pub async fn edit_body(&self, client: &GithubClient, body: &str) -> anyhow::Result<()> {
  344. let edit_url = format!("{}/issues/{}", self.repository().url(), self.number);
  345. #[derive(serde::Serialize)]
  346. struct ChangedIssue<'a> {
  347. body: &'a str,
  348. }
  349. client
  350. ._send_req(client.patch(&edit_url).json(&ChangedIssue { body }))
  351. .await
  352. .context("failed to edit issue body")?;
  353. Ok(())
  354. }
  355. pub async fn edit_comment(
  356. &self,
  357. client: &GithubClient,
  358. id: usize,
  359. new_body: &str,
  360. ) -> anyhow::Result<()> {
  361. let comment_url = format!("{}/issues/comments/{}", self.repository().url(), id);
  362. #[derive(serde::Serialize)]
  363. struct NewComment<'a> {
  364. body: &'a str,
  365. }
  366. client
  367. ._send_req(
  368. client
  369. .patch(&comment_url)
  370. .json(&NewComment { body: new_body }),
  371. )
  372. .await
  373. .context("failed to edit comment")?;
  374. Ok(())
  375. }
  376. pub async fn post_comment(&self, client: &GithubClient, body: &str) -> anyhow::Result<()> {
  377. #[derive(serde::Serialize)]
  378. struct PostComment<'a> {
  379. body: &'a str,
  380. }
  381. client
  382. ._send_req(client.post(&self.comments_url).json(&PostComment { body }))
  383. .await
  384. .context("failed to post comment")?;
  385. Ok(())
  386. }
  387. pub async fn set_labels(
  388. &self,
  389. client: &GithubClient,
  390. labels: Vec<Label>,
  391. ) -> anyhow::Result<()> {
  392. log::info!("set_labels {} to {:?}", self.global_id(), labels);
  393. // PUT /repos/:owner/:repo/issues/:number/labels
  394. // repo_url = https://api.github.com/repos/Codertocat/Hello-World
  395. let url = format!(
  396. "{repo_url}/issues/{number}/labels",
  397. repo_url = self.repository().url(),
  398. number = self.number
  399. );
  400. let mut stream = labels
  401. .into_iter()
  402. .map(|label| async { (label.exists(&self.repository().url(), &client).await, label) })
  403. .collect::<FuturesUnordered<_>>();
  404. let mut labels = Vec::new();
  405. while let Some((true, label)) = stream.next().await {
  406. labels.push(label);
  407. }
  408. #[derive(serde::Serialize)]
  409. struct LabelsReq {
  410. labels: Vec<String>,
  411. }
  412. client
  413. ._send_req(client.put(&url).json(&LabelsReq {
  414. labels: labels.iter().map(|l| l.name.clone()).collect(),
  415. }))
  416. .await
  417. .context("failed to set labels")?;
  418. Ok(())
  419. }
  420. pub fn labels(&self) -> &[Label] {
  421. &self.labels
  422. }
  423. pub fn contain_assignee(&self, user: &str) -> bool {
  424. self.assignees.iter().any(|a| a.login == user)
  425. }
  426. pub async fn remove_assignees(
  427. &self,
  428. client: &GithubClient,
  429. selection: Selection<'_, str>,
  430. ) -> Result<(), AssignmentError> {
  431. log::info!("remove {:?} assignees for {}", selection, self.global_id());
  432. let url = format!(
  433. "{repo_url}/issues/{number}/assignees",
  434. repo_url = self.repository().url(),
  435. number = self.number
  436. );
  437. let assignees = match selection {
  438. Selection::All => self
  439. .assignees
  440. .iter()
  441. .map(|u| u.login.as_str())
  442. .collect::<Vec<_>>(),
  443. Selection::One(user) => vec![user],
  444. Selection::Except(user) => self
  445. .assignees
  446. .iter()
  447. .map(|u| u.login.as_str())
  448. .filter(|&u| u != user)
  449. .collect::<Vec<_>>(),
  450. };
  451. #[derive(serde::Serialize)]
  452. struct AssigneeReq<'a> {
  453. assignees: &'a [&'a str],
  454. }
  455. client
  456. ._send_req(client.delete(&url).json(&AssigneeReq {
  457. assignees: &assignees[..],
  458. }))
  459. .await
  460. .map_err(AssignmentError::Http)?;
  461. Ok(())
  462. }
  463. pub async fn add_assignee(
  464. &self,
  465. client: &GithubClient,
  466. user: &str,
  467. ) -> Result<(), AssignmentError> {
  468. log::info!("add_assignee {} for {}", user, self.global_id());
  469. let url = format!(
  470. "{repo_url}/issues/{number}/assignees",
  471. repo_url = self.repository().url(),
  472. number = self.number
  473. );
  474. #[derive(serde::Serialize)]
  475. struct AssigneeReq<'a> {
  476. assignees: &'a [&'a str],
  477. }
  478. let result: Issue = client
  479. .json(client.post(&url).json(&AssigneeReq { assignees: &[user] }))
  480. .await
  481. .map_err(AssignmentError::Http)?;
  482. // Invalid assignees are silently ignored. We can just check if the user is now
  483. // contained in the assignees list.
  484. let success = result.assignees.iter().any(|u| u.login.as_str() == user);
  485. if success {
  486. Ok(())
  487. } else {
  488. Err(AssignmentError::InvalidAssignee)
  489. }
  490. }
  491. pub async fn set_assignee(
  492. &self,
  493. client: &GithubClient,
  494. user: &str,
  495. ) -> Result<(), AssignmentError> {
  496. log::info!("set_assignee for {} to {}", self.global_id(), user);
  497. self.add_assignee(client, user).await?;
  498. self.remove_assignees(client, Selection::Except(user))
  499. .await?;
  500. Ok(())
  501. }
  502. pub async fn set_milestone(&self, client: &GithubClient, title: &str) -> anyhow::Result<()> {
  503. log::trace!(
  504. "Setting milestone for rust-lang/rust#{} to {}",
  505. self.number,
  506. title
  507. );
  508. let create_url = format!("{}/milestones", self.repository().url());
  509. let resp = client
  510. .send_req(
  511. client
  512. .post(&create_url)
  513. .body(serde_json::to_vec(&MilestoneCreateBody { title }).unwrap()),
  514. )
  515. .await;
  516. // Explicitly do *not* try to return Err(...) if this fails -- that's
  517. // fine, it just means the milestone was already created.
  518. log::trace!("Created milestone: {:?}", resp);
  519. let list_url = format!("{}/milestones", self.repository().url());
  520. let milestone_list: Vec<Milestone> = client.json(client.get(&list_url)).await?;
  521. let milestone_no = if let Some(milestone) = milestone_list.iter().find(|v| v.title == title)
  522. {
  523. milestone.number
  524. } else {
  525. anyhow::bail!(
  526. "Despite just creating milestone {} on {}, it does not exist?",
  527. title,
  528. self.repository()
  529. )
  530. };
  531. #[derive(serde::Serialize)]
  532. struct SetMilestone {
  533. milestone: u64,
  534. }
  535. let url = format!("{}/issues/{}", self.repository().url(), self.number);
  536. client
  537. ._send_req(client.patch(&url).json(&SetMilestone {
  538. milestone: milestone_no,
  539. }))
  540. .await
  541. .context("failed to set milestone")?;
  542. Ok(())
  543. }
  544. pub async fn close(&self, client: &GithubClient) -> anyhow::Result<()> {
  545. let edit_url = format!("{}/issues/{}", self.repository().url(), self.number);
  546. #[derive(serde::Serialize)]
  547. struct CloseIssue<'a> {
  548. state: &'a str,
  549. }
  550. client
  551. ._send_req(
  552. client
  553. .patch(&edit_url)
  554. .json(&CloseIssue { state: "closed" }),
  555. )
  556. .await
  557. .context("failed to close issue")?;
  558. Ok(())
  559. }
  560. }
  561. #[derive(serde::Serialize)]
  562. struct MilestoneCreateBody<'a> {
  563. title: &'a str,
  564. }
  565. #[derive(Debug, serde::Deserialize)]
  566. pub struct Milestone {
  567. number: u64,
  568. title: String,
  569. }
  570. #[derive(Debug, serde::Deserialize)]
  571. pub struct ChangeInner {
  572. pub from: String,
  573. }
  574. #[derive(Debug, serde::Deserialize)]
  575. pub struct Changes {
  576. pub title: Option<ChangeInner>,
  577. pub body: Option<ChangeInner>,
  578. }
  579. #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
  580. #[serde(rename_all = "lowercase")]
  581. pub enum PullRequestReviewAction {
  582. Submitted,
  583. Edited,
  584. Dismissed,
  585. }
  586. #[derive(Debug, serde::Deserialize)]
  587. pub struct PullRequestReviewEvent {
  588. pub action: PullRequestReviewAction,
  589. pub pull_request: Issue,
  590. pub review: Comment,
  591. pub changes: Option<Changes>,
  592. pub repository: Repository,
  593. }
  594. #[derive(Debug, serde::Deserialize)]
  595. pub struct PullRequestReviewComment {
  596. pub action: IssueCommentAction,
  597. pub changes: Option<Changes>,
  598. #[serde(rename = "pull_request")]
  599. pub issue: Issue,
  600. pub comment: Comment,
  601. pub repository: Repository,
  602. }
  603. #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
  604. #[serde(rename_all = "lowercase")]
  605. pub enum IssueCommentAction {
  606. Created,
  607. Edited,
  608. Deleted,
  609. }
  610. #[derive(Debug, serde::Deserialize)]
  611. pub struct IssueCommentEvent {
  612. pub action: IssueCommentAction,
  613. pub changes: Option<Changes>,
  614. pub issue: Issue,
  615. pub comment: Comment,
  616. pub repository: Repository,
  617. }
  618. #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
  619. #[serde(rename_all = "snake_case")]
  620. pub enum IssuesAction {
  621. Opened,
  622. Edited,
  623. Deleted,
  624. Transferred,
  625. Pinned,
  626. Unpinned,
  627. Closed,
  628. Reopened,
  629. Assigned,
  630. Unassigned,
  631. Labeled,
  632. Unlabeled,
  633. Locked,
  634. Unlocked,
  635. Milestoned,
  636. Demilestoned,
  637. ReviewRequested,
  638. ReviewRequestRemoved,
  639. ReadyForReview,
  640. Synchronize,
  641. ConvertedToDraft,
  642. }
  643. #[derive(Debug, serde::Deserialize)]
  644. pub struct IssuesEvent {
  645. pub action: IssuesAction,
  646. #[serde(alias = "pull_request")]
  647. pub issue: Issue,
  648. pub changes: Option<Changes>,
  649. pub repository: Repository,
  650. /// Some if action is IssuesAction::Labeled, for example
  651. pub label: Option<Label>,
  652. }
  653. #[derive(Debug, serde::Deserialize)]
  654. pub struct IssueSearchResult {
  655. pub total_count: usize,
  656. pub incomplete_results: bool,
  657. pub items: Vec<Issue>,
  658. }
  659. #[derive(Debug, serde::Deserialize)]
  660. pub struct Repository {
  661. pub full_name: String,
  662. }
  663. impl Repository {
  664. const GITHUB_API_URL: &'static str = "https://api.github.com";
  665. pub async fn get_issues<'a>(
  666. &self,
  667. client: &GithubClient,
  668. query: &Query<'a>,
  669. ) -> anyhow::Result<Vec<Issue>> {
  670. let Query {
  671. filters,
  672. include_labels,
  673. exclude_labels,
  674. ..
  675. } = query;
  676. let use_issues = exclude_labels.is_empty() && filters.iter().all(|&(key, _)| key != "no");
  677. let is_pr = filters
  678. .iter()
  679. .any(|&(key, value)| key == "is" && value == "pr");
  680. // negating filters can only be handled by the search api
  681. let url = if use_issues {
  682. self.build_issues_url(filters, include_labels)
  683. } else if is_pr {
  684. self.build_pulls_url(filters, include_labels)
  685. } else {
  686. self.build_search_issues_url(filters, include_labels, exclude_labels)
  687. };
  688. let result = client.get(&url);
  689. if use_issues {
  690. client
  691. .json(result)
  692. .await
  693. .with_context(|| format!("failed to list issues from {}", url))
  694. } else {
  695. let result = client
  696. .json::<IssueSearchResult>(result)
  697. .await
  698. .with_context(|| format!("failed to list issues from {}", url))?;
  699. Ok(result.items)
  700. }
  701. }
  702. pub async fn get_issues_count<'a>(
  703. &self,
  704. client: &GithubClient,
  705. query: &Query<'a>,
  706. ) -> anyhow::Result<usize> {
  707. Ok(self.get_issues(client, query).await?.len())
  708. }
  709. fn build_issues_url(&self, filters: &Vec<(&str, &str)>, include_labels: &Vec<&str>) -> String {
  710. let filters = filters
  711. .iter()
  712. .map(|(key, val)| format!("{}={}", key, val))
  713. .chain(std::iter::once(format!(
  714. "labels={}",
  715. include_labels.join(",")
  716. )))
  717. .chain(std::iter::once("filter=all".to_owned()))
  718. .chain(std::iter::once(format!("sort=created")))
  719. .chain(std::iter::once(format!("direction=asc")))
  720. .chain(std::iter::once(format!("per_page=100")))
  721. .collect::<Vec<_>>()
  722. .join("&");
  723. format!(
  724. "{}/repos/{}/issues?{}",
  725. Repository::GITHUB_API_URL,
  726. self.full_name,
  727. filters
  728. )
  729. }
  730. fn build_pulls_url(&self, filters: &Vec<(&str, &str)>, include_labels: &Vec<&str>) -> String {
  731. let filters = filters
  732. .iter()
  733. .map(|(key, val)| format!("{}={}", key, val))
  734. .chain(std::iter::once(format!(
  735. "labels={}",
  736. include_labels.join(",")
  737. )))
  738. .chain(std::iter::once("filter=all".to_owned()))
  739. .chain(std::iter::once(format!("sort=created")))
  740. .chain(std::iter::once(format!("direction=asc")))
  741. .chain(std::iter::once(format!("per_page=100")))
  742. .collect::<Vec<_>>()
  743. .join("&");
  744. format!(
  745. "{}/repos/{}/pulls?{}",
  746. Repository::GITHUB_API_URL,
  747. self.full_name,
  748. filters
  749. )
  750. }
  751. fn build_search_issues_url(
  752. &self,
  753. filters: &Vec<(&str, &str)>,
  754. include_labels: &Vec<&str>,
  755. exclude_labels: &Vec<&str>,
  756. ) -> String {
  757. let filters = filters
  758. .iter()
  759. .map(|(key, val)| format!("{}:{}", key, val))
  760. .chain(
  761. include_labels
  762. .iter()
  763. .map(|label| format!("label:{}", label)),
  764. )
  765. .chain(
  766. exclude_labels
  767. .iter()
  768. .map(|label| format!("-label:{}", label)),
  769. )
  770. .chain(std::iter::once(format!("repo:{}", self.full_name)))
  771. .collect::<Vec<_>>()
  772. .join("+");
  773. format!(
  774. "{}/search/issues?q={}&sort=created&order=asc&per_page=100",
  775. Repository::GITHUB_API_URL,
  776. filters
  777. )
  778. }
  779. }
  780. pub struct Query<'a> {
  781. pub kind: QueryKind,
  782. // key/value filter
  783. pub filters: Vec<(&'a str, &'a str)>,
  784. pub include_labels: Vec<&'a str>,
  785. pub exclude_labels: Vec<&'a str>,
  786. }
  787. pub enum QueryKind {
  788. List,
  789. Count,
  790. }
  791. #[derive(Debug, serde::Deserialize)]
  792. #[serde(rename_all = "snake_case")]
  793. pub enum CreateKind {
  794. Branch,
  795. Tag,
  796. }
  797. #[derive(Debug, serde::Deserialize)]
  798. pub struct CreateEvent {
  799. pub ref_type: CreateKind,
  800. repository: Repository,
  801. sender: User,
  802. }
  803. #[derive(Debug, serde::Deserialize)]
  804. pub struct PushEvent {
  805. #[serde(rename = "ref")]
  806. pub git_ref: String,
  807. repository: Repository,
  808. sender: User,
  809. }
  810. #[derive(Debug)]
  811. pub enum Event {
  812. Create(CreateEvent),
  813. IssueComment(IssueCommentEvent),
  814. Issue(IssuesEvent),
  815. Push(PushEvent),
  816. }
  817. impl Event {
  818. pub fn repo_name(&self) -> &str {
  819. match self {
  820. Event::Create(event) => &event.repository.full_name,
  821. Event::IssueComment(event) => &event.repository.full_name,
  822. Event::Issue(event) => &event.repository.full_name,
  823. Event::Push(event) => &event.repository.full_name,
  824. }
  825. }
  826. pub fn issue(&self) -> Option<&Issue> {
  827. match self {
  828. Event::Create(_) => None,
  829. Event::IssueComment(event) => Some(&event.issue),
  830. Event::Issue(event) => Some(&event.issue),
  831. Event::Push(_) => None,
  832. }
  833. }
  834. /// This will both extract from IssueComment events but also Issue events
  835. pub fn comment_body(&self) -> Option<&str> {
  836. match self {
  837. Event::Create(_) => None,
  838. Event::Issue(e) => Some(&e.issue.body),
  839. Event::IssueComment(e) => Some(&e.comment.body),
  840. Event::Push(_) => None,
  841. }
  842. }
  843. /// This will both extract from IssueComment events but also Issue events
  844. pub fn comment_from(&self) -> Option<&str> {
  845. match self {
  846. Event::Create(_) => None,
  847. Event::Issue(e) => Some(&e.changes.as_ref()?.body.as_ref()?.from),
  848. Event::IssueComment(e) => Some(&e.changes.as_ref()?.body.as_ref()?.from),
  849. Event::Push(_) => None,
  850. }
  851. }
  852. pub fn html_url(&self) -> Option<&str> {
  853. match self {
  854. Event::Create(_) => None,
  855. Event::Issue(e) => Some(&e.issue.html_url),
  856. Event::IssueComment(e) => Some(&e.comment.html_url),
  857. Event::Push(_) => None,
  858. }
  859. }
  860. pub fn user(&self) -> &User {
  861. match self {
  862. Event::Create(e) => &e.sender,
  863. Event::Issue(e) => &e.issue.user,
  864. Event::IssueComment(e) => &e.comment.user,
  865. Event::Push(e) => &e.sender,
  866. }
  867. }
  868. pub fn time(&self) -> Option<chrono::DateTime<FixedOffset>> {
  869. match self {
  870. Event::Create(_) => None,
  871. Event::Issue(e) => Some(e.issue.created_at.into()),
  872. Event::IssueComment(e) => Some(e.comment.updated_at.into()),
  873. Event::Push(_) => None,
  874. }
  875. }
  876. }
  877. trait RequestSend: Sized {
  878. fn configure(self, g: &GithubClient) -> Self;
  879. }
  880. impl RequestSend for RequestBuilder {
  881. fn configure(self, g: &GithubClient) -> RequestBuilder {
  882. let mut auth = HeaderValue::from_maybe_shared(format!("token {}", g.token)).unwrap();
  883. auth.set_sensitive(true);
  884. self.header(USER_AGENT, "rust-lang-triagebot")
  885. .header(AUTHORIZATION, &auth)
  886. }
  887. }
  888. /// Finds the token in the user's environment, panicking if no suitable token
  889. /// can be found.
  890. pub fn default_token_from_env() -> String {
  891. match std::env::var("GITHUB_API_TOKEN") {
  892. Ok(v) => return v,
  893. Err(_) => (),
  894. }
  895. match get_token_from_git_config() {
  896. Ok(v) => return v,
  897. Err(_) => (),
  898. }
  899. panic!("could not find token in GITHUB_API_TOKEN or .gitconfig/github.oath-token")
  900. }
  901. fn get_token_from_git_config() -> anyhow::Result<String> {
  902. let output = std::process::Command::new("git")
  903. .arg("config")
  904. .arg("--get")
  905. .arg("github.oauth-token")
  906. .output()?;
  907. if !output.status.success() {
  908. anyhow::bail!("error received executing `git`: {:?}", output.status);
  909. }
  910. let git_token = String::from_utf8(output.stdout)?.trim().to_string();
  911. Ok(git_token)
  912. }
  913. #[derive(Clone)]
  914. pub struct GithubClient {
  915. token: String,
  916. client: Client,
  917. }
  918. impl GithubClient {
  919. pub fn new(client: Client, token: String) -> Self {
  920. GithubClient { client, token }
  921. }
  922. pub fn new_with_default_token(client: Client) -> Self {
  923. Self::new(client, default_token_from_env())
  924. }
  925. pub fn raw(&self) -> &Client {
  926. &self.client
  927. }
  928. pub async fn raw_file(
  929. &self,
  930. repo: &str,
  931. branch: &str,
  932. path: &str,
  933. ) -> anyhow::Result<Option<Vec<u8>>> {
  934. let url = format!(
  935. "https://raw.githubusercontent.com/{}/{}/{}",
  936. repo, branch, path
  937. );
  938. let req = self.get(&url);
  939. let req_dbg = format!("{:?}", req);
  940. let req = req
  941. .build()
  942. .with_context(|| format!("failed to build request {:?}", req_dbg))?;
  943. let mut resp = self.client.execute(req).await.context(req_dbg.clone())?;
  944. let status = resp.status();
  945. match status {
  946. StatusCode::OK => {
  947. let mut buf = Vec::with_capacity(resp.content_length().unwrap_or(4) as usize);
  948. while let Some(chunk) = resp.chunk().await.transpose() {
  949. let chunk = chunk
  950. .context("reading stream failed")
  951. .map_err(anyhow::Error::from)
  952. .context(req_dbg.clone())?;
  953. buf.extend_from_slice(&chunk);
  954. }
  955. Ok(Some(buf))
  956. }
  957. StatusCode::NOT_FOUND => Ok(None),
  958. status => anyhow::bail!("failed to GET {}: {}", url, status),
  959. }
  960. }
  961. /// Get the raw gist content from the URL of the HTML version of the gist:
  962. ///
  963. /// `html_url` looks like `https://gist.github.com/rust-play/7e80ca3b1ec7abe08f60c41aff91f060`.
  964. ///
  965. /// `filename` is the name of the file you want the content of.
  966. pub async fn raw_gist_from_url(
  967. &self,
  968. html_url: &str,
  969. filename: &str,
  970. ) -> anyhow::Result<String> {
  971. let url = html_url.replace("github.com", "githubusercontent.com") + "/raw/" + filename;
  972. let response = self.raw().get(&url).send().await?;
  973. response.text().await.context("raw gist from url")
  974. }
  975. fn get(&self, url: &str) -> RequestBuilder {
  976. log::trace!("get {:?}", url);
  977. self.client.get(url).configure(self)
  978. }
  979. fn patch(&self, url: &str) -> RequestBuilder {
  980. log::trace!("patch {:?}", url);
  981. self.client.patch(url).configure(self)
  982. }
  983. fn delete(&self, url: &str) -> RequestBuilder {
  984. log::trace!("delete {:?}", url);
  985. self.client.delete(url).configure(self)
  986. }
  987. fn post(&self, url: &str) -> RequestBuilder {
  988. log::trace!("post {:?}", url);
  989. self.client.post(url).configure(self)
  990. }
  991. fn put(&self, url: &str) -> RequestBuilder {
  992. log::trace!("put {:?}", url);
  993. self.client.put(url).configure(self)
  994. }
  995. pub async fn rust_commit(&self, sha: &str) -> Option<GithubCommit> {
  996. let req = self.get(&format!(
  997. "https://api.github.com/repos/rust-lang/rust/commits/{}",
  998. sha
  999. ));
  1000. match self.json(req).await {
  1001. Ok(r) => Some(r),
  1002. Err(e) => {
  1003. log::error!("Failed to query commit {:?}: {:?}", sha, e);
  1004. None
  1005. }
  1006. }
  1007. }
  1008. /// This does not retrieve all of them, only the last several.
  1009. pub async fn bors_commits(&self) -> Vec<GithubCommit> {
  1010. let req = self.get("https://api.github.com/repos/rust-lang/rust/commits?author=bors");
  1011. match self.json(req).await {
  1012. Ok(r) => r,
  1013. Err(e) => {
  1014. log::error!("Failed to query commit list: {:?}", e);
  1015. Vec::new()
  1016. }
  1017. }
  1018. }
  1019. }
  1020. #[derive(Debug, serde::Deserialize)]
  1021. pub struct GithubCommit {
  1022. pub sha: String,
  1023. pub commit: GitCommit,
  1024. pub parents: Vec<Parent>,
  1025. }
  1026. #[derive(Debug, serde::Deserialize)]
  1027. pub struct GitCommit {
  1028. pub author: GitUser,
  1029. }
  1030. #[derive(Debug, serde::Deserialize)]
  1031. pub struct GitUser {
  1032. pub date: DateTime<FixedOffset>,
  1033. }
  1034. #[derive(Debug, serde::Deserialize)]
  1035. pub struct Parent {
  1036. pub sha: String,
  1037. }