github.rs 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835
  1. use anyhow::Context;
  2. use async_trait::async_trait;
  3. use chrono::{DateTime, FixedOffset, Utc};
  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::collections::HashMap;
  10. use std::convert::TryInto;
  11. use std::{
  12. fmt,
  13. time::{Duration, SystemTime},
  14. };
  15. use tracing as log;
  16. #[derive(Debug, PartialEq, Eq, serde::Deserialize)]
  17. pub struct User {
  18. pub login: String,
  19. pub id: Option<i64>,
  20. }
  21. impl GithubClient {
  22. async fn _send_req(&self, req: RequestBuilder) -> anyhow::Result<(Response, String)> {
  23. const MAX_ATTEMPTS: usize = 2;
  24. log::debug!("_send_req with {:?}", req);
  25. let req_dbg = format!("{:?}", req);
  26. let req = req
  27. .build()
  28. .with_context(|| format!("building reqwest {}", req_dbg))?;
  29. let mut resp = self.client.execute(req.try_clone().unwrap()).await?;
  30. if let Some(sleep) = Self::needs_retry(&resp).await {
  31. resp = self.retry(req, sleep, MAX_ATTEMPTS).await?;
  32. }
  33. resp.error_for_status_ref()?;
  34. Ok((resp, req_dbg))
  35. }
  36. async fn needs_retry(resp: &Response) -> Option<Duration> {
  37. const REMAINING: &str = "X-RateLimit-Remaining";
  38. const RESET: &str = "X-RateLimit-Reset";
  39. if resp.status().is_success() {
  40. return None;
  41. }
  42. let headers = resp.headers();
  43. if !(headers.contains_key(REMAINING) && headers.contains_key(RESET)) {
  44. return None;
  45. }
  46. // Weird github api behavior. It asks us to retry but also has a remaining count above 1
  47. // Try again immediately and hope for the best...
  48. if headers[REMAINING] != "0" {
  49. return Some(Duration::from_secs(0));
  50. }
  51. let reset_time = headers[RESET].to_str().unwrap().parse::<u64>().unwrap();
  52. Some(Duration::from_secs(Self::calc_sleep(reset_time) + 10))
  53. }
  54. fn calc_sleep(reset_time: u64) -> u64 {
  55. let epoch_time = SystemTime::UNIX_EPOCH.elapsed().unwrap().as_secs();
  56. reset_time.saturating_sub(epoch_time)
  57. }
  58. fn retry(
  59. &self,
  60. req: Request,
  61. sleep: Duration,
  62. remaining_attempts: usize,
  63. ) -> BoxFuture<Result<Response, reqwest::Error>> {
  64. #[derive(Debug, serde::Deserialize)]
  65. struct RateLimit {
  66. #[allow(unused)]
  67. pub limit: u64,
  68. pub remaining: u64,
  69. pub reset: u64,
  70. }
  71. #[derive(Debug, serde::Deserialize)]
  72. struct RateLimitResponse {
  73. pub resources: Resources,
  74. }
  75. #[derive(Debug, serde::Deserialize)]
  76. struct Resources {
  77. pub core: RateLimit,
  78. pub search: RateLimit,
  79. #[allow(unused)]
  80. pub graphql: RateLimit,
  81. #[allow(unused)]
  82. pub source_import: RateLimit,
  83. }
  84. log::warn!(
  85. "Retrying after {} seconds, remaining attepts {}",
  86. sleep.as_secs(),
  87. remaining_attempts,
  88. );
  89. async move {
  90. tokio::time::sleep(sleep).await;
  91. // check rate limit
  92. let rate_resp = self
  93. .client
  94. .execute(
  95. self.client
  96. .get("https://api.github.com/rate_limit")
  97. .configure(self)
  98. .build()
  99. .unwrap(),
  100. )
  101. .await?;
  102. let rate_limit_response = rate_resp.json::<RateLimitResponse>().await?;
  103. // Check url for search path because github has different rate limits for the search api
  104. let rate_limit = if req
  105. .url()
  106. .path_segments()
  107. .map(|mut segments| matches!(segments.next(), Some("search")))
  108. .unwrap_or(false)
  109. {
  110. rate_limit_response.resources.search
  111. } else {
  112. rate_limit_response.resources.core
  113. };
  114. // If we still don't have any more remaining attempts, try sleeping for the remaining
  115. // period of time
  116. if rate_limit.remaining == 0 {
  117. let sleep = Self::calc_sleep(rate_limit.reset);
  118. if sleep > 0 {
  119. tokio::time::sleep(Duration::from_secs(sleep)).await;
  120. }
  121. }
  122. let resp = self.client.execute(req.try_clone().unwrap()).await?;
  123. if let Some(sleep) = Self::needs_retry(&resp).await {
  124. if remaining_attempts > 0 {
  125. return self.retry(req, sleep, remaining_attempts - 1).await;
  126. }
  127. }
  128. Ok(resp)
  129. }
  130. .boxed()
  131. }
  132. async fn send_req(&self, req: RequestBuilder) -> anyhow::Result<Vec<u8>> {
  133. let (mut resp, req_dbg) = self._send_req(req).await?;
  134. let mut body = Vec::new();
  135. while let Some(chunk) = resp.chunk().await.transpose() {
  136. let chunk = chunk
  137. .context("reading stream failed")
  138. .map_err(anyhow::Error::from)
  139. .context(req_dbg.clone())?;
  140. body.extend_from_slice(&chunk);
  141. }
  142. Ok(body)
  143. }
  144. pub async fn json<T>(&self, req: RequestBuilder) -> anyhow::Result<T>
  145. where
  146. T: serde::de::DeserializeOwned,
  147. {
  148. let (resp, req_dbg) = self._send_req(req).await?;
  149. Ok(resp.json().await.context(req_dbg)?)
  150. }
  151. }
  152. impl User {
  153. pub async fn current(client: &GithubClient) -> anyhow::Result<Self> {
  154. client.json(client.get("https://api.github.com/user")).await
  155. }
  156. pub async fn is_team_member<'a>(&'a self, client: &'a GithubClient) -> anyhow::Result<bool> {
  157. log::trace!("Getting team membership for {:?}", self.login);
  158. let permission = crate::team_data::teams(client).await?;
  159. let map = permission.teams;
  160. let is_triager = map
  161. .get("wg-triage")
  162. .map_or(false, |w| w.members.iter().any(|g| g.github == self.login));
  163. let is_pri_member = map
  164. .get("wg-prioritization")
  165. .map_or(false, |w| w.members.iter().any(|g| g.github == self.login));
  166. let is_async_member = map
  167. .get("wg-async")
  168. .map_or(false, |w| w.members.iter().any(|g| g.github == self.login));
  169. let in_all = map["all"].members.iter().any(|g| g.github == self.login);
  170. log::trace!(
  171. "{:?} is all?={:?}, triager?={:?}, prioritizer?={:?}, async?={:?}",
  172. self.login,
  173. in_all,
  174. is_triager,
  175. is_pri_member,
  176. is_async_member,
  177. );
  178. Ok(in_all || is_triager || is_pri_member || is_async_member)
  179. }
  180. // Returns the ID of the given user, if the user is in the `all` team.
  181. pub async fn get_id<'a>(&'a self, client: &'a GithubClient) -> anyhow::Result<Option<usize>> {
  182. let permission = crate::team_data::teams(client).await?;
  183. let map = permission.teams;
  184. Ok(map["all"]
  185. .members
  186. .iter()
  187. .find(|g| g.github == self.login)
  188. .map(|u| u.github_id))
  189. }
  190. }
  191. pub async fn get_team(
  192. client: &GithubClient,
  193. team: &str,
  194. ) -> anyhow::Result<Option<rust_team_data::v1::Team>> {
  195. let permission = crate::team_data::teams(client).await?;
  196. let mut map = permission.teams;
  197. Ok(map.swap_remove(team))
  198. }
  199. #[derive(PartialEq, Eq, Debug, Clone, serde::Deserialize)]
  200. pub struct Label {
  201. pub name: String,
  202. }
  203. /// An indicator used to differentiate between an issue and a pull request.
  204. ///
  205. /// Some webhook events include a `pull_request` field in the Issue object,
  206. /// and some don't. GitHub does include a few fields here, but they aren't
  207. /// needed at this time (merged_at, diff_url, html_url, patch_url, url).
  208. #[derive(Debug, serde::Deserialize)]
  209. pub struct PullRequestDetails {
  210. // none for now
  211. }
  212. /// An issue or pull request.
  213. ///
  214. /// For convenience, since issues and pull requests share most of their
  215. /// fields, this struct is used for both. The `pull_request` field can be used
  216. /// to determine which it is. Some fields are only available on pull requests
  217. /// (but not always, check the GitHub API for details).
  218. #[derive(Debug, serde::Deserialize)]
  219. pub struct Issue {
  220. pub number: u64,
  221. #[serde(deserialize_with = "opt_string")]
  222. pub body: String,
  223. created_at: chrono::DateTime<Utc>,
  224. pub updated_at: chrono::DateTime<Utc>,
  225. /// The SHA for a merge commit.
  226. ///
  227. /// This field is complicated, see the [Pull Request
  228. /// docs](https://docs.github.com/en/rest/pulls/pulls#get-a-pull-request)
  229. /// for details.
  230. #[serde(default)]
  231. pub merge_commit_sha: Option<String>,
  232. pub title: String,
  233. /// The common URL for viewing this issue or PR.
  234. ///
  235. /// Example: `https://github.com/octocat/Hello-World/pull/1347`
  236. pub html_url: String,
  237. pub user: User,
  238. pub labels: Vec<Label>,
  239. pub assignees: Vec<User>,
  240. /// Indicator if this is a pull request.
  241. ///
  242. /// This is `Some` if this is a PR (as opposed to an issue). Note that
  243. /// this does not always get filled in by GitHub, and must be manually
  244. /// populated (because some webhook events do not set it).
  245. pub pull_request: Option<PullRequestDetails>,
  246. /// Whether or not the pull request was merged.
  247. #[serde(default)]
  248. pub merged: bool,
  249. #[serde(default)]
  250. pub draft: bool,
  251. /// The API URL for discussion comments.
  252. ///
  253. /// Example: `https://api.github.com/repos/octocat/Hello-World/issues/1347/comments`
  254. comments_url: String,
  255. /// The repository for this issue.
  256. ///
  257. /// Note that this is constructed via the [`Issue::repository`] method.
  258. /// It is not deserialized from the GitHub API.
  259. #[serde(skip)]
  260. repository: OnceCell<IssueRepository>,
  261. /// The base commit for a PR (the branch of the destination repo).
  262. #[serde(default)]
  263. pub base: Option<CommitBase>,
  264. /// The head commit for a PR (the branch from the source repo).
  265. #[serde(default)]
  266. pub head: Option<CommitBase>,
  267. /// Whether it is open or closed.
  268. pub state: IssueState,
  269. }
  270. #[derive(Debug, serde::Deserialize, Eq, PartialEq)]
  271. #[serde(rename_all = "snake_case")]
  272. pub enum IssueState {
  273. Open,
  274. Closed,
  275. }
  276. /// Contains only the parts of `Issue` that are needed for turning the issue title into a Zulip
  277. /// topic.
  278. #[derive(Clone, Debug, PartialEq, Eq)]
  279. pub struct ZulipGitHubReference {
  280. pub number: u64,
  281. pub title: String,
  282. pub repository: IssueRepository,
  283. }
  284. impl ZulipGitHubReference {
  285. pub fn zulip_topic_reference(&self) -> String {
  286. let repo = &self.repository;
  287. if repo.organization == "rust-lang" {
  288. if repo.repository == "rust" {
  289. format!("#{}", self.number)
  290. } else {
  291. format!("{}#{}", repo.repository, self.number)
  292. }
  293. } else {
  294. format!("{}/{}#{}", repo.organization, repo.repository, self.number)
  295. }
  296. }
  297. }
  298. #[derive(Debug, serde::Deserialize)]
  299. pub struct Comment {
  300. #[serde(deserialize_with = "opt_string")]
  301. pub body: String,
  302. pub html_url: String,
  303. pub user: User,
  304. #[serde(alias = "submitted_at")] // for pull request reviews
  305. pub updated_at: chrono::DateTime<Utc>,
  306. #[serde(default, rename = "state")]
  307. pub pr_review_state: Option<PullRequestReviewState>,
  308. }
  309. #[derive(Debug, serde::Deserialize, Eq, PartialEq)]
  310. #[serde(rename_all = "snake_case")]
  311. pub enum PullRequestReviewState {
  312. Approved,
  313. ChangesRequested,
  314. Commented,
  315. Dismissed,
  316. Pending,
  317. }
  318. fn opt_string<'de, D>(deserializer: D) -> Result<String, D::Error>
  319. where
  320. D: serde::de::Deserializer<'de>,
  321. {
  322. use serde::de::Deserialize;
  323. match <Option<String>>::deserialize(deserializer) {
  324. Ok(v) => Ok(v.unwrap_or_default()),
  325. Err(e) => Err(e),
  326. }
  327. }
  328. #[derive(Debug)]
  329. pub enum AssignmentError {
  330. InvalidAssignee,
  331. Http(anyhow::Error),
  332. }
  333. #[derive(Debug)]
  334. pub enum Selection<'a, T: ?Sized> {
  335. All,
  336. One(&'a T),
  337. Except(&'a T),
  338. }
  339. impl fmt::Display for AssignmentError {
  340. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  341. match self {
  342. AssignmentError::InvalidAssignee => write!(f, "invalid assignee"),
  343. AssignmentError::Http(e) => write!(f, "cannot assign: {}", e),
  344. }
  345. }
  346. }
  347. impl std::error::Error for AssignmentError {}
  348. #[derive(Debug, Clone, PartialEq, Eq)]
  349. pub struct IssueRepository {
  350. pub organization: String,
  351. pub repository: String,
  352. }
  353. impl fmt::Display for IssueRepository {
  354. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  355. write!(f, "{}/{}", self.organization, self.repository)
  356. }
  357. }
  358. impl IssueRepository {
  359. fn url(&self) -> String {
  360. format!(
  361. "https://api.github.com/repos/{}/{}",
  362. self.organization, self.repository
  363. )
  364. }
  365. async fn has_label(&self, client: &GithubClient, label: &str) -> anyhow::Result<bool> {
  366. #[allow(clippy::redundant_pattern_matching)]
  367. let url = format!("{}/labels/{}", self.url(), label);
  368. match client._send_req(client.get(&url)).await {
  369. Ok((_, _)) => Ok(true),
  370. Err(e) => {
  371. if e.downcast_ref::<reqwest::Error>()
  372. .map_or(false, |e| e.status() == Some(StatusCode::NOT_FOUND))
  373. {
  374. Ok(false)
  375. } else {
  376. Err(e)
  377. }
  378. }
  379. }
  380. }
  381. }
  382. #[derive(Debug)]
  383. pub(crate) struct UnknownLabels {
  384. labels: Vec<String>,
  385. }
  386. // NOTE: This is used to post the Github comment; make sure it's valid markdown.
  387. impl fmt::Display for UnknownLabels {
  388. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  389. write!(f, "Unknown labels: {}", &self.labels.join(", "))
  390. }
  391. }
  392. impl std::error::Error for UnknownLabels {}
  393. impl Issue {
  394. pub fn to_zulip_github_reference(&self) -> ZulipGitHubReference {
  395. ZulipGitHubReference {
  396. number: self.number,
  397. title: self.title.clone(),
  398. repository: self.repository().clone(),
  399. }
  400. }
  401. pub fn repository(&self) -> &IssueRepository {
  402. self.repository.get_or_init(|| {
  403. // https://api.github.com/repos/rust-lang/rust/issues/69257/comments
  404. log::trace!("get repository for {}", self.comments_url);
  405. let url = url::Url::parse(&self.comments_url).unwrap();
  406. let mut segments = url.path_segments().unwrap();
  407. let _comments = segments.next_back().unwrap();
  408. let _number = segments.next_back().unwrap();
  409. let _issues_or_prs = segments.next_back().unwrap();
  410. let repository = segments.next_back().unwrap();
  411. let organization = segments.next_back().unwrap();
  412. IssueRepository {
  413. organization: organization.into(),
  414. repository: repository.into(),
  415. }
  416. })
  417. }
  418. pub fn global_id(&self) -> String {
  419. format!("{}#{}", self.repository(), self.number)
  420. }
  421. pub fn is_pr(&self) -> bool {
  422. self.pull_request.is_some()
  423. }
  424. pub fn is_open(&self) -> bool {
  425. self.state == IssueState::Open
  426. }
  427. pub async fn get_comment(&self, client: &GithubClient, id: usize) -> anyhow::Result<Comment> {
  428. let comment_url = format!("{}/issues/comments/{}", self.repository().url(), id);
  429. let comment = client.json(client.get(&comment_url)).await?;
  430. Ok(comment)
  431. }
  432. pub async fn edit_body(&self, client: &GithubClient, body: &str) -> anyhow::Result<()> {
  433. let edit_url = format!("{}/issues/{}", self.repository().url(), self.number);
  434. #[derive(serde::Serialize)]
  435. struct ChangedIssue<'a> {
  436. body: &'a str,
  437. }
  438. client
  439. ._send_req(client.patch(&edit_url).json(&ChangedIssue { body }))
  440. .await
  441. .context("failed to edit issue body")?;
  442. Ok(())
  443. }
  444. pub async fn edit_comment(
  445. &self,
  446. client: &GithubClient,
  447. id: usize,
  448. new_body: &str,
  449. ) -> anyhow::Result<()> {
  450. let comment_url = format!("{}/issues/comments/{}", self.repository().url(), id);
  451. #[derive(serde::Serialize)]
  452. struct NewComment<'a> {
  453. body: &'a str,
  454. }
  455. client
  456. ._send_req(
  457. client
  458. .patch(&comment_url)
  459. .json(&NewComment { body: new_body }),
  460. )
  461. .await
  462. .context("failed to edit comment")?;
  463. Ok(())
  464. }
  465. pub async fn post_comment(&self, client: &GithubClient, body: &str) -> anyhow::Result<()> {
  466. #[derive(serde::Serialize)]
  467. struct PostComment<'a> {
  468. body: &'a str,
  469. }
  470. client
  471. ._send_req(client.post(&self.comments_url).json(&PostComment { body }))
  472. .await
  473. .context("failed to post comment")?;
  474. Ok(())
  475. }
  476. pub async fn remove_label(&self, client: &GithubClient, label: &str) -> anyhow::Result<()> {
  477. log::info!("remove_label from {}: {:?}", self.global_id(), label);
  478. // DELETE /repos/:owner/:repo/issues/:number/labels/{name}
  479. let url = format!(
  480. "{repo_url}/issues/{number}/labels/{name}",
  481. repo_url = self.repository().url(),
  482. number = self.number,
  483. name = label,
  484. );
  485. if !self.labels().iter().any(|l| l.name == label) {
  486. log::info!(
  487. "remove_label from {}: {:?} already not present, skipping",
  488. self.global_id(),
  489. label
  490. );
  491. return Ok(());
  492. }
  493. client
  494. ._send_req(client.delete(&url))
  495. .await
  496. .context("failed to delete label")?;
  497. Ok(())
  498. }
  499. pub async fn add_labels(
  500. &self,
  501. client: &GithubClient,
  502. labels: Vec<Label>,
  503. ) -> anyhow::Result<()> {
  504. log::info!("add_labels: {} +{:?}", self.global_id(), labels);
  505. // POST /repos/:owner/:repo/issues/:number/labels
  506. // repo_url = https://api.github.com/repos/Codertocat/Hello-World
  507. let url = format!(
  508. "{repo_url}/issues/{number}/labels",
  509. repo_url = self.repository().url(),
  510. number = self.number
  511. );
  512. // Don't try to add labels already present on this issue.
  513. let labels = labels
  514. .into_iter()
  515. .filter(|l| !self.labels().contains(&l))
  516. .map(|l| l.name)
  517. .collect::<Vec<_>>();
  518. log::info!("add_labels: {} filtered to {:?}", self.global_id(), labels);
  519. if labels.is_empty() {
  520. return Ok(());
  521. }
  522. let mut unknown_labels = vec![];
  523. let mut known_labels = vec![];
  524. for label in labels {
  525. if !self.repository().has_label(client, &label).await? {
  526. unknown_labels.push(label);
  527. } else {
  528. known_labels.push(label);
  529. }
  530. }
  531. if !unknown_labels.is_empty() {
  532. return Err(UnknownLabels {
  533. labels: unknown_labels,
  534. }
  535. .into());
  536. }
  537. #[derive(serde::Serialize)]
  538. struct LabelsReq {
  539. labels: Vec<String>,
  540. }
  541. client
  542. ._send_req(client.post(&url).json(&LabelsReq {
  543. labels: known_labels,
  544. }))
  545. .await
  546. .context("failed to add labels")?;
  547. Ok(())
  548. }
  549. pub fn labels(&self) -> &[Label] {
  550. &self.labels
  551. }
  552. pub fn contain_assignee(&self, user: &str) -> bool {
  553. self.assignees
  554. .iter()
  555. .any(|a| a.login.to_lowercase() == user.to_lowercase())
  556. }
  557. pub async fn remove_assignees(
  558. &self,
  559. client: &GithubClient,
  560. selection: Selection<'_, str>,
  561. ) -> Result<(), AssignmentError> {
  562. log::info!("remove {:?} assignees for {}", selection, self.global_id());
  563. let url = format!(
  564. "{repo_url}/issues/{number}/assignees",
  565. repo_url = self.repository().url(),
  566. number = self.number
  567. );
  568. let assignees = match selection {
  569. Selection::All => self
  570. .assignees
  571. .iter()
  572. .map(|u| u.login.as_str())
  573. .collect::<Vec<_>>(),
  574. Selection::One(user) => vec![user],
  575. Selection::Except(user) => self
  576. .assignees
  577. .iter()
  578. .map(|u| u.login.as_str())
  579. .filter(|&u| u.to_lowercase() != user.to_lowercase())
  580. .collect::<Vec<_>>(),
  581. };
  582. #[derive(serde::Serialize)]
  583. struct AssigneeReq<'a> {
  584. assignees: &'a [&'a str],
  585. }
  586. client
  587. ._send_req(client.delete(&url).json(&AssigneeReq {
  588. assignees: &assignees[..],
  589. }))
  590. .await
  591. .map_err(AssignmentError::Http)?;
  592. Ok(())
  593. }
  594. pub async fn add_assignee(
  595. &self,
  596. client: &GithubClient,
  597. user: &str,
  598. ) -> Result<(), AssignmentError> {
  599. log::info!("add_assignee {} for {}", user, self.global_id());
  600. let url = format!(
  601. "{repo_url}/issues/{number}/assignees",
  602. repo_url = self.repository().url(),
  603. number = self.number
  604. );
  605. #[derive(serde::Serialize)]
  606. struct AssigneeReq<'a> {
  607. assignees: &'a [&'a str],
  608. }
  609. let result: Issue = client
  610. .json(client.post(&url).json(&AssigneeReq { assignees: &[user] }))
  611. .await
  612. .map_err(AssignmentError::Http)?;
  613. // Invalid assignees are silently ignored. We can just check if the user is now
  614. // contained in the assignees list.
  615. let success = result
  616. .assignees
  617. .iter()
  618. .any(|u| u.login.as_str().to_lowercase() == user.to_lowercase());
  619. if success {
  620. Ok(())
  621. } else {
  622. Err(AssignmentError::InvalidAssignee)
  623. }
  624. }
  625. pub async fn set_assignee(
  626. &self,
  627. client: &GithubClient,
  628. user: &str,
  629. ) -> Result<(), AssignmentError> {
  630. log::info!("set_assignee for {} to {}", self.global_id(), user);
  631. self.add_assignee(client, user).await?;
  632. self.remove_assignees(client, Selection::Except(user))
  633. .await?;
  634. Ok(())
  635. }
  636. pub async fn set_milestone(&self, client: &GithubClient, title: &str) -> anyhow::Result<()> {
  637. log::trace!(
  638. "Setting milestone for rust-lang/rust#{} to {}",
  639. self.number,
  640. title
  641. );
  642. let create_url = format!("{}/milestones", self.repository().url());
  643. let resp = client
  644. .send_req(
  645. client
  646. .post(&create_url)
  647. .body(serde_json::to_vec(&MilestoneCreateBody { title }).unwrap()),
  648. )
  649. .await;
  650. // Explicitly do *not* try to return Err(...) if this fails -- that's
  651. // fine, it just means the milestone was already created.
  652. log::trace!("Created milestone: {:?}", resp);
  653. let list_url = format!("{}/milestones", self.repository().url());
  654. let milestone_list: Vec<Milestone> = client.json(client.get(&list_url)).await?;
  655. let milestone_no = if let Some(milestone) = milestone_list.iter().find(|v| v.title == title)
  656. {
  657. milestone.number
  658. } else {
  659. anyhow::bail!(
  660. "Despite just creating milestone {} on {}, it does not exist?",
  661. title,
  662. self.repository()
  663. )
  664. };
  665. #[derive(serde::Serialize)]
  666. struct SetMilestone {
  667. milestone: u64,
  668. }
  669. let url = format!("{}/issues/{}", self.repository().url(), self.number);
  670. client
  671. ._send_req(client.patch(&url).json(&SetMilestone {
  672. milestone: milestone_no,
  673. }))
  674. .await
  675. .context("failed to set milestone")?;
  676. Ok(())
  677. }
  678. pub async fn close(&self, client: &GithubClient) -> anyhow::Result<()> {
  679. let edit_url = format!("{}/issues/{}", self.repository().url(), self.number);
  680. #[derive(serde::Serialize)]
  681. struct CloseIssue<'a> {
  682. state: &'a str,
  683. }
  684. client
  685. ._send_req(
  686. client
  687. .patch(&edit_url)
  688. .json(&CloseIssue { state: "closed" }),
  689. )
  690. .await
  691. .context("failed to close issue")?;
  692. Ok(())
  693. }
  694. /// Returns the diff in this event, for Open and Synchronize events for now.
  695. pub async fn diff(&self, client: &GithubClient) -> anyhow::Result<Option<String>> {
  696. let (before, after) = if let (Some(base), Some(head)) = (&self.base, &self.head) {
  697. (base.sha.clone(), head.sha.clone())
  698. } else {
  699. return Ok(None);
  700. };
  701. let mut req = client.get(&format!(
  702. "{}/compare/{}...{}",
  703. self.repository().url(),
  704. before,
  705. after
  706. ));
  707. req = req.header("Accept", "application/vnd.github.v3.diff");
  708. let diff = client.send_req(req).await?;
  709. Ok(Some(String::from(String::from_utf8_lossy(&diff))))
  710. }
  711. pub async fn files(&self, client: &GithubClient) -> anyhow::Result<Vec<PullRequestFile>> {
  712. if !self.is_pr() {
  713. return Ok(vec![]);
  714. }
  715. let req = client.get(&format!(
  716. "{}/pulls/{}/files",
  717. self.repository().url(),
  718. self.number
  719. ));
  720. Ok(client.json(req).await?)
  721. }
  722. }
  723. #[derive(Debug, serde::Deserialize)]
  724. pub struct PullRequestFile {
  725. pub sha: String,
  726. pub filename: String,
  727. pub blob_url: String,
  728. }
  729. #[derive(serde::Serialize)]
  730. struct MilestoneCreateBody<'a> {
  731. title: &'a str,
  732. }
  733. #[derive(Debug, serde::Deserialize)]
  734. pub struct Milestone {
  735. number: u64,
  736. title: String,
  737. }
  738. #[derive(Debug, serde::Deserialize)]
  739. pub struct ChangeInner {
  740. pub from: String,
  741. }
  742. #[derive(Debug, serde::Deserialize)]
  743. pub struct Changes {
  744. pub title: Option<ChangeInner>,
  745. pub body: Option<ChangeInner>,
  746. }
  747. #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
  748. #[serde(rename_all = "lowercase")]
  749. pub enum PullRequestReviewAction {
  750. Submitted,
  751. Edited,
  752. Dismissed,
  753. }
  754. /// A pull request review event.
  755. ///
  756. /// <https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#pull_request_review>
  757. #[derive(Debug, serde::Deserialize)]
  758. pub struct PullRequestReviewEvent {
  759. pub action: PullRequestReviewAction,
  760. pub pull_request: Issue,
  761. pub review: Comment,
  762. pub changes: Option<Changes>,
  763. pub repository: Repository,
  764. }
  765. #[derive(Debug, serde::Deserialize)]
  766. pub struct PullRequestReviewComment {
  767. pub action: IssueCommentAction,
  768. pub changes: Option<Changes>,
  769. #[serde(rename = "pull_request")]
  770. pub issue: Issue,
  771. pub comment: Comment,
  772. pub repository: Repository,
  773. }
  774. #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
  775. #[serde(rename_all = "lowercase")]
  776. pub enum IssueCommentAction {
  777. Created,
  778. Edited,
  779. Deleted,
  780. }
  781. #[derive(Debug, serde::Deserialize)]
  782. pub struct IssueCommentEvent {
  783. pub action: IssueCommentAction,
  784. pub changes: Option<Changes>,
  785. pub issue: Issue,
  786. pub comment: Comment,
  787. pub repository: Repository,
  788. }
  789. #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
  790. #[serde(rename_all = "snake_case")]
  791. pub enum IssuesAction {
  792. Opened,
  793. Edited,
  794. Deleted,
  795. Transferred,
  796. Pinned,
  797. Unpinned,
  798. Closed,
  799. Reopened,
  800. Assigned,
  801. Unassigned,
  802. Labeled,
  803. Unlabeled,
  804. Locked,
  805. Unlocked,
  806. Milestoned,
  807. Demilestoned,
  808. ReviewRequested,
  809. ReviewRequestRemoved,
  810. ReadyForReview,
  811. Synchronize,
  812. ConvertedToDraft,
  813. AutoMergeEnabled,
  814. AutoMergeDisabled,
  815. }
  816. #[derive(Debug, serde::Deserialize)]
  817. pub struct IssuesEvent {
  818. pub action: IssuesAction,
  819. #[serde(alias = "pull_request")]
  820. pub issue: Issue,
  821. pub changes: Option<Changes>,
  822. pub repository: Repository,
  823. /// Some if action is IssuesAction::Labeled, for example
  824. pub label: Option<Label>,
  825. }
  826. #[derive(Debug, serde::Deserialize)]
  827. struct PullRequestEventFields {}
  828. #[derive(Clone, Debug, serde::Deserialize)]
  829. pub struct CommitBase {
  830. sha: String,
  831. #[serde(rename = "ref")]
  832. pub git_ref: String,
  833. pub repo: Repository,
  834. }
  835. pub fn files_changed(diff: &str) -> Vec<&str> {
  836. let mut files = Vec::new();
  837. for line in diff.lines() {
  838. // mostly copied from highfive
  839. if line.starts_with("diff --git ") {
  840. files.push(
  841. line[line.find(" b/").unwrap()..]
  842. .strip_prefix(" b/")
  843. .unwrap(),
  844. );
  845. }
  846. }
  847. files
  848. }
  849. #[derive(Debug, serde::Deserialize)]
  850. pub struct IssueSearchResult {
  851. pub total_count: usize,
  852. pub incomplete_results: bool,
  853. pub items: Vec<Issue>,
  854. }
  855. #[derive(Clone, Debug, serde::Deserialize)]
  856. pub struct Repository {
  857. pub full_name: String,
  858. pub default_branch: String,
  859. #[serde(default)]
  860. pub fork: bool,
  861. }
  862. #[derive(Copy, Clone)]
  863. struct Ordering<'a> {
  864. pub sort: &'a str,
  865. pub direction: &'a str,
  866. pub per_page: &'a str,
  867. pub page: usize,
  868. }
  869. impl Repository {
  870. const GITHUB_API_URL: &'static str = "https://api.github.com";
  871. const GITHUB_GRAPHQL_API_URL: &'static str = "https://api.github.com/graphql";
  872. pub fn owner(&self) -> &str {
  873. self.full_name.split_once('/').unwrap().0
  874. }
  875. pub fn name(&self) -> &str {
  876. self.full_name.split_once('/').unwrap().1
  877. }
  878. pub async fn get_issues<'a>(
  879. &self,
  880. client: &GithubClient,
  881. query: &Query<'a>,
  882. ) -> anyhow::Result<Vec<Issue>> {
  883. let Query {
  884. filters,
  885. include_labels,
  886. exclude_labels,
  887. } = query;
  888. let mut ordering = Ordering {
  889. sort: "created",
  890. direction: "asc",
  891. per_page: "100",
  892. page: 1,
  893. };
  894. let filters: Vec<_> = filters
  895. .clone()
  896. .into_iter()
  897. .filter(|(key, val)| {
  898. match *key {
  899. "sort" => ordering.sort = val,
  900. "direction" => ordering.direction = val,
  901. "per_page" => ordering.per_page = val,
  902. _ => return true,
  903. };
  904. false
  905. })
  906. .collect();
  907. // `is: pull-request` indicates the query to retrieve PRs only
  908. let is_pr = filters
  909. .iter()
  910. .any(|&(key, value)| key == "is" && value == "pull-request");
  911. // There are some cases that can only be handled by the search API:
  912. // 1. When using negating label filters (exclude_labels)
  913. // 2. When there's a key parameter key=no
  914. // 3. When the query is to retrieve PRs only and there are label filters
  915. //
  916. // Check https://docs.github.com/en/rest/reference/search#search-issues-and-pull-requests
  917. // for more information
  918. let use_search_api = !exclude_labels.is_empty()
  919. || filters.iter().any(|&(key, _)| key == "no")
  920. || is_pr && !include_labels.is_empty();
  921. // If there are more than `per_page` of issues, we need to paginate
  922. let mut issues = vec![];
  923. loop {
  924. let url = if use_search_api {
  925. self.build_search_issues_url(&filters, include_labels, exclude_labels, ordering)
  926. } else if is_pr {
  927. self.build_pulls_url(&filters, include_labels, ordering)
  928. } else {
  929. self.build_issues_url(&filters, include_labels, ordering)
  930. };
  931. let result = client.get(&url);
  932. if use_search_api {
  933. let result = client
  934. .json::<IssueSearchResult>(result)
  935. .await
  936. .with_context(|| format!("failed to list issues from {}", url))?;
  937. issues.extend(result.items);
  938. if issues.len() < result.total_count {
  939. ordering.page += 1;
  940. continue;
  941. }
  942. } else {
  943. // FIXME: paginate with non-search
  944. issues = client
  945. .json(result)
  946. .await
  947. .with_context(|| format!("failed to list issues from {}", url))?
  948. }
  949. break;
  950. }
  951. Ok(issues)
  952. }
  953. fn build_issues_url(
  954. &self,
  955. filters: &Vec<(&str, &str)>,
  956. include_labels: &Vec<&str>,
  957. ordering: Ordering<'_>,
  958. ) -> String {
  959. self.build_endpoint_url("issues", filters, include_labels, ordering)
  960. }
  961. fn build_pulls_url(
  962. &self,
  963. filters: &Vec<(&str, &str)>,
  964. include_labels: &Vec<&str>,
  965. ordering: Ordering<'_>,
  966. ) -> String {
  967. self.build_endpoint_url("pulls", filters, include_labels, ordering)
  968. }
  969. fn build_endpoint_url(
  970. &self,
  971. endpoint: &str,
  972. filters: &Vec<(&str, &str)>,
  973. include_labels: &Vec<&str>,
  974. ordering: Ordering<'_>,
  975. ) -> String {
  976. let filters = filters
  977. .iter()
  978. .map(|(key, val)| format!("{}={}", key, val))
  979. .chain(std::iter::once(format!(
  980. "labels={}",
  981. include_labels.join(",")
  982. )))
  983. .chain(std::iter::once("filter=all".to_owned()))
  984. .chain(std::iter::once(format!("sort={}", ordering.sort,)))
  985. .chain(std::iter::once(
  986. format!("direction={}", ordering.direction,),
  987. ))
  988. .chain(std::iter::once(format!("per_page={}", ordering.per_page,)))
  989. .collect::<Vec<_>>()
  990. .join("&");
  991. format!(
  992. "{}/repos/{}/{}?{}",
  993. Repository::GITHUB_API_URL,
  994. self.full_name,
  995. endpoint,
  996. filters
  997. )
  998. }
  999. fn build_search_issues_url(
  1000. &self,
  1001. filters: &Vec<(&str, &str)>,
  1002. include_labels: &Vec<&str>,
  1003. exclude_labels: &Vec<&str>,
  1004. ordering: Ordering<'_>,
  1005. ) -> String {
  1006. let filters = filters
  1007. .iter()
  1008. .filter(|&&(key, val)| !(key == "state" && val == "all"))
  1009. .map(|(key, val)| format!("{}:{}", key, val))
  1010. .chain(
  1011. include_labels
  1012. .iter()
  1013. .map(|label| format!("label:{}", label)),
  1014. )
  1015. .chain(
  1016. exclude_labels
  1017. .iter()
  1018. .map(|label| format!("-label:{}", label)),
  1019. )
  1020. .chain(std::iter::once(format!("repo:{}", self.full_name)))
  1021. .collect::<Vec<_>>()
  1022. .join("+");
  1023. format!(
  1024. "{}/search/issues?q={}&sort={}&order={}&per_page={}&page={}",
  1025. Repository::GITHUB_API_URL,
  1026. filters,
  1027. ordering.sort,
  1028. ordering.direction,
  1029. ordering.per_page,
  1030. ordering.page,
  1031. )
  1032. }
  1033. }
  1034. pub struct Query<'a> {
  1035. // key/value filter
  1036. pub filters: Vec<(&'a str, &'a str)>,
  1037. pub include_labels: Vec<&'a str>,
  1038. pub exclude_labels: Vec<&'a str>,
  1039. }
  1040. fn quote_reply(markdown: &str) -> String {
  1041. if markdown.is_empty() {
  1042. String::from("*No content*")
  1043. } else {
  1044. format!("\n\t> {}", markdown.replace("\n", "\n\t> "))
  1045. }
  1046. }
  1047. #[async_trait]
  1048. impl<'q> IssuesQuery for Query<'q> {
  1049. async fn query<'a>(
  1050. &'a self,
  1051. repo: &'a Repository,
  1052. include_fcp_details: bool,
  1053. client: &'a GithubClient,
  1054. ) -> anyhow::Result<Vec<crate::actions::IssueDecorator>> {
  1055. let issues = repo
  1056. .get_issues(&client, self)
  1057. .await
  1058. .with_context(|| "Unable to get issues.")?;
  1059. let fcp_map = if include_fcp_details {
  1060. crate::rfcbot::get_all_fcps().await?
  1061. } else {
  1062. HashMap::new()
  1063. };
  1064. let mut issues_decorator = Vec::new();
  1065. for issue in issues {
  1066. let fcp_details = if include_fcp_details {
  1067. let repository_name = if let Some(repo) = issue.repository.get() {
  1068. repo.repository.clone()
  1069. } else {
  1070. let re = regex::Regex::new("https://github.com/rust-lang/|/").unwrap();
  1071. let split = re.split(&issue.html_url).collect::<Vec<&str>>();
  1072. split[1].to_string()
  1073. };
  1074. let key = format!(
  1075. "rust-lang/{}:{}:{}",
  1076. repository_name, issue.number, issue.title,
  1077. );
  1078. if let Some(fcp) = fcp_map.get(&key) {
  1079. let bot_tracking_comment_html_url = format!(
  1080. "{}#issuecomment-{}",
  1081. issue.html_url, fcp.fcp.fk_bot_tracking_comment
  1082. );
  1083. let bot_tracking_comment_content = quote_reply(&fcp.status_comment.body);
  1084. let fk_initiating_comment = fcp.fcp.fk_initiating_comment;
  1085. let init_comment = issue
  1086. .get_comment(&client, fk_initiating_comment.try_into()?)
  1087. .await?;
  1088. Some(crate::actions::FCPDetails {
  1089. bot_tracking_comment_html_url,
  1090. bot_tracking_comment_content,
  1091. initiating_comment_html_url: init_comment.html_url.clone(),
  1092. initiating_comment_content: quote_reply(&init_comment.body),
  1093. })
  1094. } else {
  1095. None
  1096. }
  1097. } else {
  1098. None
  1099. };
  1100. issues_decorator.push(crate::actions::IssueDecorator {
  1101. title: issue.title.clone(),
  1102. number: issue.number,
  1103. html_url: issue.html_url.clone(),
  1104. repo_name: repo.name().to_owned(),
  1105. labels: issue
  1106. .labels
  1107. .iter()
  1108. .map(|l| l.name.as_ref())
  1109. .collect::<Vec<_>>()
  1110. .join(", "),
  1111. assignees: issue
  1112. .assignees
  1113. .iter()
  1114. .map(|u| u.login.as_ref())
  1115. .collect::<Vec<_>>()
  1116. .join(", "),
  1117. updated_at_hts: crate::actions::to_human(issue.updated_at),
  1118. fcp_details,
  1119. });
  1120. }
  1121. Ok(issues_decorator)
  1122. }
  1123. }
  1124. #[derive(Debug, serde::Deserialize)]
  1125. #[serde(rename_all = "snake_case")]
  1126. pub enum CreateKind {
  1127. Branch,
  1128. Tag,
  1129. }
  1130. #[derive(Debug, serde::Deserialize)]
  1131. pub struct CreateEvent {
  1132. pub ref_type: CreateKind,
  1133. repository: Repository,
  1134. sender: User,
  1135. }
  1136. #[derive(Debug, serde::Deserialize)]
  1137. pub struct PushEvent {
  1138. #[serde(rename = "ref")]
  1139. pub git_ref: String,
  1140. repository: Repository,
  1141. sender: User,
  1142. }
  1143. /// An event triggered by a webhook.
  1144. #[derive(Debug)]
  1145. pub enum Event {
  1146. /// A Git branch or tag is created.
  1147. Create(CreateEvent),
  1148. /// A comment on an issue or PR.
  1149. ///
  1150. /// Can be:
  1151. /// - Regular comment on an issue or PR.
  1152. /// - A PR review.
  1153. /// - A comment on a PR review.
  1154. ///
  1155. /// These different scenarios are unified into the `IssueComment` variant
  1156. /// when triagebot receives the corresponding webhook event.
  1157. IssueComment(IssueCommentEvent),
  1158. /// Activity on an issue or PR.
  1159. Issue(IssuesEvent),
  1160. /// One or more commits are pushed to a repository branch or tag.
  1161. Push(PushEvent),
  1162. }
  1163. impl Event {
  1164. pub fn repo(&self) -> &Repository {
  1165. match self {
  1166. Event::Create(event) => &event.repository,
  1167. Event::IssueComment(event) => &event.repository,
  1168. Event::Issue(event) => &event.repository,
  1169. Event::Push(event) => &event.repository,
  1170. }
  1171. }
  1172. pub fn issue(&self) -> Option<&Issue> {
  1173. match self {
  1174. Event::Create(_) => None,
  1175. Event::IssueComment(event) => Some(&event.issue),
  1176. Event::Issue(event) => Some(&event.issue),
  1177. Event::Push(_) => None,
  1178. }
  1179. }
  1180. /// This will both extract from IssueComment events but also Issue events
  1181. pub fn comment_body(&self) -> Option<&str> {
  1182. match self {
  1183. Event::Create(_) => None,
  1184. Event::Issue(e) => Some(&e.issue.body),
  1185. Event::IssueComment(e) => Some(&e.comment.body),
  1186. Event::Push(_) => None,
  1187. }
  1188. }
  1189. /// This will both extract from IssueComment events but also Issue events
  1190. pub fn comment_from(&self) -> Option<&str> {
  1191. match self {
  1192. Event::Create(_) => None,
  1193. Event::Issue(e) => Some(&e.changes.as_ref()?.body.as_ref()?.from),
  1194. Event::IssueComment(e) => Some(&e.changes.as_ref()?.body.as_ref()?.from),
  1195. Event::Push(_) => None,
  1196. }
  1197. }
  1198. pub fn html_url(&self) -> Option<&str> {
  1199. match self {
  1200. Event::Create(_) => None,
  1201. Event::Issue(e) => Some(&e.issue.html_url),
  1202. Event::IssueComment(e) => Some(&e.comment.html_url),
  1203. Event::Push(_) => None,
  1204. }
  1205. }
  1206. pub fn user(&self) -> &User {
  1207. match self {
  1208. Event::Create(e) => &e.sender,
  1209. Event::Issue(e) => &e.issue.user,
  1210. Event::IssueComment(e) => &e.comment.user,
  1211. Event::Push(e) => &e.sender,
  1212. }
  1213. }
  1214. pub fn time(&self) -> Option<chrono::DateTime<FixedOffset>> {
  1215. match self {
  1216. Event::Create(_) => None,
  1217. Event::Issue(e) => Some(e.issue.created_at.into()),
  1218. Event::IssueComment(e) => Some(e.comment.updated_at.into()),
  1219. Event::Push(_) => None,
  1220. }
  1221. }
  1222. }
  1223. trait RequestSend: Sized {
  1224. fn configure(self, g: &GithubClient) -> Self;
  1225. }
  1226. impl RequestSend for RequestBuilder {
  1227. fn configure(self, g: &GithubClient) -> RequestBuilder {
  1228. let mut auth = HeaderValue::from_maybe_shared(format!("token {}", g.token)).unwrap();
  1229. auth.set_sensitive(true);
  1230. self.header(USER_AGENT, "rust-lang-triagebot")
  1231. .header(AUTHORIZATION, &auth)
  1232. }
  1233. }
  1234. /// Finds the token in the user's environment, panicking if no suitable token
  1235. /// can be found.
  1236. pub fn default_token_from_env() -> String {
  1237. match std::env::var("GITHUB_API_TOKEN") {
  1238. Ok(v) => return v,
  1239. Err(_) => (),
  1240. }
  1241. match get_token_from_git_config() {
  1242. Ok(v) => return v,
  1243. Err(_) => (),
  1244. }
  1245. panic!("could not find token in GITHUB_API_TOKEN or .gitconfig/github.oath-token")
  1246. }
  1247. fn get_token_from_git_config() -> anyhow::Result<String> {
  1248. let output = std::process::Command::new("git")
  1249. .arg("config")
  1250. .arg("--get")
  1251. .arg("github.oauth-token")
  1252. .output()?;
  1253. if !output.status.success() {
  1254. anyhow::bail!("error received executing `git`: {:?}", output.status);
  1255. }
  1256. let git_token = String::from_utf8(output.stdout)?.trim().to_string();
  1257. Ok(git_token)
  1258. }
  1259. #[derive(Clone)]
  1260. pub struct GithubClient {
  1261. token: String,
  1262. client: Client,
  1263. }
  1264. impl GithubClient {
  1265. pub fn new(client: Client, token: String) -> Self {
  1266. GithubClient { client, token }
  1267. }
  1268. pub fn new_with_default_token(client: Client) -> Self {
  1269. Self::new(client, default_token_from_env())
  1270. }
  1271. pub fn raw(&self) -> &Client {
  1272. &self.client
  1273. }
  1274. pub async fn raw_file(
  1275. &self,
  1276. repo: &str,
  1277. branch: &str,
  1278. path: &str,
  1279. ) -> anyhow::Result<Option<Vec<u8>>> {
  1280. let url = format!(
  1281. "https://raw.githubusercontent.com/{}/{}/{}",
  1282. repo, branch, path
  1283. );
  1284. let req = self.get(&url);
  1285. let req_dbg = format!("{:?}", req);
  1286. let req = req
  1287. .build()
  1288. .with_context(|| format!("failed to build request {:?}", req_dbg))?;
  1289. let mut resp = self.client.execute(req).await.context(req_dbg.clone())?;
  1290. let status = resp.status();
  1291. match status {
  1292. StatusCode::OK => {
  1293. let mut buf = Vec::with_capacity(resp.content_length().unwrap_or(4) as usize);
  1294. while let Some(chunk) = resp.chunk().await.transpose() {
  1295. let chunk = chunk
  1296. .context("reading stream failed")
  1297. .map_err(anyhow::Error::from)
  1298. .context(req_dbg.clone())?;
  1299. buf.extend_from_slice(&chunk);
  1300. }
  1301. Ok(Some(buf))
  1302. }
  1303. StatusCode::NOT_FOUND => Ok(None),
  1304. status => anyhow::bail!("failed to GET {}: {}", url, status),
  1305. }
  1306. }
  1307. /// Get the raw gist content from the URL of the HTML version of the gist:
  1308. ///
  1309. /// `html_url` looks like `https://gist.github.com/rust-play/7e80ca3b1ec7abe08f60c41aff91f060`.
  1310. ///
  1311. /// `filename` is the name of the file you want the content of.
  1312. pub async fn raw_gist_from_url(
  1313. &self,
  1314. html_url: &str,
  1315. filename: &str,
  1316. ) -> anyhow::Result<String> {
  1317. let url = html_url.replace("github.com", "githubusercontent.com") + "/raw/" + filename;
  1318. let response = self.raw().get(&url).send().await?;
  1319. response.text().await.context("raw gist from url")
  1320. }
  1321. fn get(&self, url: &str) -> RequestBuilder {
  1322. log::trace!("get {:?}", url);
  1323. self.client.get(url).configure(self)
  1324. }
  1325. fn patch(&self, url: &str) -> RequestBuilder {
  1326. log::trace!("patch {:?}", url);
  1327. self.client.patch(url).configure(self)
  1328. }
  1329. fn delete(&self, url: &str) -> RequestBuilder {
  1330. log::trace!("delete {:?}", url);
  1331. self.client.delete(url).configure(self)
  1332. }
  1333. fn post(&self, url: &str) -> RequestBuilder {
  1334. log::trace!("post {:?}", url);
  1335. self.client.post(url).configure(self)
  1336. }
  1337. #[allow(unused)]
  1338. fn put(&self, url: &str) -> RequestBuilder {
  1339. log::trace!("put {:?}", url);
  1340. self.client.put(url).configure(self)
  1341. }
  1342. pub async fn rust_commit(&self, sha: &str) -> Option<GithubCommit> {
  1343. let req = self.get(&format!(
  1344. "https://api.github.com/repos/rust-lang/rust/commits/{}",
  1345. sha
  1346. ));
  1347. match self.json(req).await {
  1348. Ok(r) => Some(r),
  1349. Err(e) => {
  1350. log::error!("Failed to query commit {:?}: {:?}", sha, e);
  1351. None
  1352. }
  1353. }
  1354. }
  1355. /// This does not retrieve all of them, only the last several.
  1356. pub async fn bors_commits(&self) -> Vec<GithubCommit> {
  1357. let req = self.get("https://api.github.com/repos/rust-lang/rust/commits?author=bors");
  1358. match self.json(req).await {
  1359. Ok(r) => r,
  1360. Err(e) => {
  1361. log::error!("Failed to query commit list: {:?}", e);
  1362. Vec::new()
  1363. }
  1364. }
  1365. }
  1366. /// Returns whether or not the given GitHub login has made any commits to
  1367. /// the given repo.
  1368. pub async fn is_new_contributor(&self, repo: &Repository, author: &str) -> bool {
  1369. let url = format!(
  1370. "{}/repos/{}/commits?author={}",
  1371. Repository::GITHUB_API_URL,
  1372. repo.full_name,
  1373. author,
  1374. );
  1375. let req = self.get(&url);
  1376. match self.json::<Vec<GithubCommit>>(req).await {
  1377. // Note: This only returns results for the default branch.
  1378. // That should be fine in most cases since I think it is rare for
  1379. // new users to make their first commit to a different branch.
  1380. Ok(res) => res.is_empty(),
  1381. Err(e) => {
  1382. log::warn!(
  1383. "failed to search for user commits in {} for author {author}: {e}",
  1384. repo.full_name
  1385. );
  1386. false
  1387. }
  1388. }
  1389. }
  1390. }
  1391. #[derive(Debug, serde::Deserialize)]
  1392. pub struct GithubCommit {
  1393. pub sha: String,
  1394. pub commit: GitCommit,
  1395. pub parents: Vec<Parent>,
  1396. }
  1397. #[derive(Debug, serde::Deserialize)]
  1398. pub struct GitCommit {
  1399. pub author: GitUser,
  1400. pub message: String,
  1401. }
  1402. #[derive(Debug, serde::Deserialize)]
  1403. pub struct GitUser {
  1404. pub date: DateTime<FixedOffset>,
  1405. }
  1406. #[derive(Debug, serde::Deserialize)]
  1407. pub struct Parent {
  1408. pub sha: String,
  1409. }
  1410. #[async_trait]
  1411. pub trait IssuesQuery {
  1412. async fn query<'a>(
  1413. &'a self,
  1414. repo: &'a Repository,
  1415. include_fcp_details: bool,
  1416. client: &'a GithubClient,
  1417. ) -> anyhow::Result<Vec<crate::actions::IssueDecorator>>;
  1418. }
  1419. pub struct LeastRecentlyReviewedPullRequests;
  1420. #[async_trait]
  1421. impl IssuesQuery for LeastRecentlyReviewedPullRequests {
  1422. async fn query<'a>(
  1423. &'a self,
  1424. repo: &'a Repository,
  1425. _include_fcp_details: bool,
  1426. client: &'a GithubClient,
  1427. ) -> anyhow::Result<Vec<crate::actions::IssueDecorator>> {
  1428. use cynic::QueryBuilder;
  1429. use github_graphql::queries;
  1430. let repository_owner = repo.owner().to_owned();
  1431. let repository_name = repo.name().to_owned();
  1432. let mut prs: Vec<Option<queries::PullRequest>> = vec![];
  1433. let mut args = queries::LeastRecentlyReviewedPullRequestsArguments {
  1434. repository_owner,
  1435. repository_name: repository_name.clone(),
  1436. after: None,
  1437. };
  1438. loop {
  1439. let query = queries::LeastRecentlyReviewedPullRequests::build(&args);
  1440. let req = client.post(Repository::GITHUB_GRAPHQL_API_URL);
  1441. let req = req.json(&query);
  1442. let (resp, req_dbg) = client._send_req(req).await?;
  1443. let response = resp.json().await.context(req_dbg)?;
  1444. let data: cynic::GraphQlResponse<queries::LeastRecentlyReviewedPullRequests> =
  1445. query.decode_response(response).with_context(|| {
  1446. format!("failed to parse response for `LeastRecentlyReviewedPullRequests`")
  1447. })?;
  1448. if let Some(errors) = data.errors {
  1449. anyhow::bail!("There were graphql errors. {:?}", errors);
  1450. }
  1451. let repository = data
  1452. .data
  1453. .ok_or_else(|| anyhow::anyhow!("No data returned."))?
  1454. .repository
  1455. .ok_or_else(|| anyhow::anyhow!("No repository."))?;
  1456. prs.extend(
  1457. repository
  1458. .pull_requests
  1459. .nodes
  1460. .unwrap_or_default()
  1461. .into_iter(),
  1462. );
  1463. let page_info = repository.pull_requests.page_info;
  1464. if !page_info.has_next_page || page_info.end_cursor.is_none() {
  1465. break;
  1466. }
  1467. args.after = page_info.end_cursor;
  1468. }
  1469. let mut prs: Vec<_> = prs
  1470. .into_iter()
  1471. .filter_map(|pr| pr)
  1472. .filter_map(|pr| {
  1473. if pr.is_draft {
  1474. return None;
  1475. }
  1476. let labels = pr.labels;
  1477. let labels = (|| -> Option<_> {
  1478. let labels = labels?;
  1479. let nodes = labels.nodes?;
  1480. let labels = nodes
  1481. .into_iter()
  1482. .filter_map(|node| node)
  1483. .map(|node| node.name)
  1484. .collect::<Vec<_>>();
  1485. Some(labels)
  1486. })()
  1487. .unwrap_or_default();
  1488. if !labels.iter().any(|label| label == "T-compiler") {
  1489. return None;
  1490. }
  1491. let labels = labels.join(", ");
  1492. let assignees: Vec<_> = pr
  1493. .assignees
  1494. .nodes
  1495. .unwrap_or_default()
  1496. .into_iter()
  1497. .filter_map(|user| user)
  1498. .map(|user| user.login)
  1499. .collect();
  1500. let latest_reviews = pr.latest_reviews;
  1501. let mut reviews = (|| -> Option<_> {
  1502. let reviews = latest_reviews?;
  1503. let nodes = reviews.nodes?;
  1504. let reviews = nodes
  1505. .into_iter()
  1506. .filter_map(|node| node)
  1507. .filter_map(|node| {
  1508. let created_at = node.created_at;
  1509. node.author.map(|author| (author, created_at))
  1510. })
  1511. .map(|(author, created_at)| (author.login, created_at))
  1512. .collect::<Vec<_>>();
  1513. Some(reviews)
  1514. })()
  1515. .unwrap_or_default();
  1516. reviews.sort_by_key(|r| r.1);
  1517. let comments = pr.comments;
  1518. let comments = (|| -> Option<_> {
  1519. let nodes = comments.nodes?;
  1520. let comments = nodes
  1521. .into_iter()
  1522. .filter_map(|node| node)
  1523. .filter_map(|node| {
  1524. let created_at = node.created_at;
  1525. node.author.map(|author| (author, created_at))
  1526. })
  1527. .map(|(author, created_at)| (author.login, created_at))
  1528. .collect::<Vec<_>>();
  1529. Some(comments)
  1530. })()
  1531. .unwrap_or_default();
  1532. let mut comments: Vec<_> = comments
  1533. .into_iter()
  1534. .filter(|comment| assignees.contains(&comment.0))
  1535. .collect();
  1536. comments.sort_by_key(|c| c.1);
  1537. let updated_at = std::cmp::max(
  1538. reviews.last().map(|t| t.1).unwrap_or(pr.created_at),
  1539. comments.last().map(|t| t.1).unwrap_or(pr.created_at),
  1540. );
  1541. let assignees = assignees.join(", ");
  1542. Some((
  1543. updated_at,
  1544. pr.number as u64,
  1545. pr.title,
  1546. pr.url.0,
  1547. repository_name.clone(),
  1548. labels,
  1549. assignees,
  1550. ))
  1551. })
  1552. .collect();
  1553. prs.sort_by_key(|pr| pr.0);
  1554. let prs: Vec<_> = prs
  1555. .into_iter()
  1556. .take(50)
  1557. .map(
  1558. |(updated_at, number, title, html_url, repo_name, labels, assignees)| {
  1559. let updated_at_hts = crate::actions::to_human(updated_at);
  1560. crate::actions::IssueDecorator {
  1561. number,
  1562. title,
  1563. html_url,
  1564. repo_name,
  1565. labels,
  1566. assignees,
  1567. updated_at_hts,
  1568. fcp_details: None,
  1569. }
  1570. },
  1571. )
  1572. .collect();
  1573. Ok(prs)
  1574. }
  1575. }
  1576. #[cfg(test)]
  1577. mod tests {
  1578. use super::*;
  1579. #[test]
  1580. fn display_labels() {
  1581. let x = UnknownLabels {
  1582. labels: vec!["A-bootstrap".into(), "xxx".into()],
  1583. };
  1584. assert_eq!(x.to_string(), "Unknown labels: A-bootstrap, xxx");
  1585. }
  1586. #[test]
  1587. fn extract_one_file() {
  1588. let input = r##"\
  1589. diff --git a/triagebot.toml b/triagebot.toml
  1590. index fb9cee43b2d..b484c25ea51 100644
  1591. --- a/triagebot.toml
  1592. +++ b/triagebot.toml
  1593. @@ -114,6 +114,15 @@ trigger_files = [
  1594. "src/tools/rustdoc-themes",
  1595. ]
  1596. +[autolabel."T-compiler"]
  1597. +trigger_files = [
  1598. + # Source code
  1599. + "compiler",
  1600. +
  1601. + # Tests
  1602. + "src/test/ui",
  1603. +]
  1604. +
  1605. [notify-zulip."I-prioritize"]
  1606. zulip_stream = 245100 # #t-compiler/wg-prioritization/alerts
  1607. topic = "#{number} {title}"
  1608. "##;
  1609. assert_eq!(files_changed(input), vec!["triagebot.toml".to_string()]);
  1610. }
  1611. #[test]
  1612. fn extract_several_files() {
  1613. let input = r##"\
  1614. diff --git a/library/stdarch b/library/stdarch
  1615. index b70ae88ef2a..cfba59fccd9 160000
  1616. --- a/library/stdarch
  1617. +++ b/library/stdarch
  1618. @@ -1 +1 @@
  1619. -Subproject commit b70ae88ef2a6c83acad0a1e83d5bd78f9655fd05
  1620. +Subproject commit cfba59fccd90b3b52a614120834320f764ab08d1
  1621. diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs
  1622. index 1fe4aa9023e..f0330f1e424 100644
  1623. --- a/src/librustdoc/clean/types.rs
  1624. +++ b/src/librustdoc/clean/types.rs
  1625. @@ -2322,3 +2322,4 @@ impl SubstParam {
  1626. if let Self::Lifetime(lt) = self { Some(lt) } else { None }
  1627. }
  1628. }
  1629. +
  1630. diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs
  1631. index c58310947d2..3b0854d4a9b 100644
  1632. --- a/src/librustdoc/core.rs
  1633. +++ b/src/librustdoc/core.rs
  1634. @@ -591,3 +591,4 @@ fn from(idx: u32) -> Self {
  1635. ImplTraitParam::ParamIndex(idx)
  1636. }
  1637. }
  1638. +
  1639. "##;
  1640. assert_eq!(
  1641. files_changed(input),
  1642. vec![
  1643. "library/stdarch".to_string(),
  1644. "src/librustdoc/clean/types.rs".to_string(),
  1645. "src/librustdoc/core.rs".to_string(),
  1646. ]
  1647. )
  1648. }
  1649. }