github.rs 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701
  1. use anyhow::{anyhow, Context};
  2. use async_trait::async_trait;
  3. use bytes::Bytes;
  4. use chrono::{DateTime, FixedOffset, Utc};
  5. use futures::{future::BoxFuture, FutureExt};
  6. use hyper::header::HeaderValue;
  7. use once_cell::sync::OnceCell;
  8. use reqwest::header::{AUTHORIZATION, USER_AGENT};
  9. use reqwest::{Client, Request, RequestBuilder, Response, StatusCode};
  10. use std::collections::{HashMap, HashSet};
  11. use std::convert::TryInto;
  12. use std::{
  13. fmt,
  14. time::{Duration, SystemTime},
  15. };
  16. use tracing as log;
  17. #[derive(Debug, PartialEq, Eq, serde::Deserialize)]
  18. pub struct User {
  19. pub login: String,
  20. pub id: Option<i64>,
  21. }
  22. impl GithubClient {
  23. async fn send_req(&self, req: RequestBuilder) -> anyhow::Result<(Bytes, String)> {
  24. const MAX_ATTEMPTS: usize = 2;
  25. log::debug!("send_req with {:?}", req);
  26. let req_dbg = format!("{:?}", req);
  27. let req = req
  28. .build()
  29. .with_context(|| format!("building reqwest {}", req_dbg))?;
  30. let mut resp = self.client.execute(req.try_clone().unwrap()).await?;
  31. if let Some(sleep) = Self::needs_retry(&resp).await {
  32. resp = self.retry(req, sleep, MAX_ATTEMPTS).await?;
  33. }
  34. let maybe_err = resp.error_for_status_ref().err();
  35. let body = resp
  36. .bytes()
  37. .await
  38. .with_context(|| format!("failed to read response body {req_dbg}"))?;
  39. if let Some(e) = maybe_err {
  40. return Err(anyhow::Error::new(e))
  41. .with_context(|| format!("response: {}", String::from_utf8_lossy(&body)));
  42. }
  43. Ok((body, req_dbg))
  44. }
  45. async fn needs_retry(resp: &Response) -> Option<Duration> {
  46. const REMAINING: &str = "X-RateLimit-Remaining";
  47. const RESET: &str = "X-RateLimit-Reset";
  48. if resp.status().is_success() {
  49. return None;
  50. }
  51. let headers = resp.headers();
  52. if !(headers.contains_key(REMAINING) && headers.contains_key(RESET)) {
  53. return None;
  54. }
  55. // Weird github api behavior. It asks us to retry but also has a remaining count above 1
  56. // Try again immediately and hope for the best...
  57. if headers[REMAINING] != "0" {
  58. return Some(Duration::from_secs(0));
  59. }
  60. let reset_time = headers[RESET].to_str().unwrap().parse::<u64>().unwrap();
  61. Some(Duration::from_secs(Self::calc_sleep(reset_time) + 10))
  62. }
  63. fn calc_sleep(reset_time: u64) -> u64 {
  64. let epoch_time = SystemTime::UNIX_EPOCH.elapsed().unwrap().as_secs();
  65. reset_time.saturating_sub(epoch_time)
  66. }
  67. fn retry(
  68. &self,
  69. req: Request,
  70. sleep: Duration,
  71. remaining_attempts: usize,
  72. ) -> BoxFuture<Result<Response, reqwest::Error>> {
  73. #[derive(Debug, serde::Deserialize)]
  74. struct RateLimit {
  75. #[allow(unused)]
  76. pub limit: u64,
  77. pub remaining: u64,
  78. pub reset: u64,
  79. }
  80. #[derive(Debug, serde::Deserialize)]
  81. struct RateLimitResponse {
  82. pub resources: Resources,
  83. }
  84. #[derive(Debug, serde::Deserialize)]
  85. struct Resources {
  86. pub core: RateLimit,
  87. pub search: RateLimit,
  88. #[allow(unused)]
  89. pub graphql: RateLimit,
  90. #[allow(unused)]
  91. pub source_import: RateLimit,
  92. }
  93. log::warn!(
  94. "Retrying after {} seconds, remaining attepts {}",
  95. sleep.as_secs(),
  96. remaining_attempts,
  97. );
  98. async move {
  99. tokio::time::sleep(sleep).await;
  100. // check rate limit
  101. let rate_resp = self
  102. .client
  103. .execute(
  104. self.client
  105. .get("https://api.github.com/rate_limit")
  106. .configure(self)
  107. .build()
  108. .unwrap(),
  109. )
  110. .await?;
  111. rate_resp.error_for_status_ref()?;
  112. let rate_limit_response = rate_resp.json::<RateLimitResponse>().await?;
  113. // Check url for search path because github has different rate limits for the search api
  114. let rate_limit = if req
  115. .url()
  116. .path_segments()
  117. .map(|mut segments| matches!(segments.next(), Some("search")))
  118. .unwrap_or(false)
  119. {
  120. rate_limit_response.resources.search
  121. } else {
  122. rate_limit_response.resources.core
  123. };
  124. // If we still don't have any more remaining attempts, try sleeping for the remaining
  125. // period of time
  126. if rate_limit.remaining == 0 {
  127. let sleep = Self::calc_sleep(rate_limit.reset);
  128. if sleep > 0 {
  129. tokio::time::sleep(Duration::from_secs(sleep)).await;
  130. }
  131. }
  132. let resp = self.client.execute(req.try_clone().unwrap()).await?;
  133. if let Some(sleep) = Self::needs_retry(&resp).await {
  134. if remaining_attempts > 0 {
  135. return self.retry(req, sleep, remaining_attempts - 1).await;
  136. }
  137. }
  138. Ok(resp)
  139. }
  140. .boxed()
  141. }
  142. pub async fn json<T>(&self, req: RequestBuilder) -> anyhow::Result<T>
  143. where
  144. T: serde::de::DeserializeOwned,
  145. {
  146. let (body, _req_dbg) = self.send_req(req).await?;
  147. Ok(serde_json::from_slice(&body)?)
  148. }
  149. }
  150. impl User {
  151. pub async fn current(client: &GithubClient) -> anyhow::Result<Self> {
  152. client.json(client.get("https://api.github.com/user")).await
  153. }
  154. pub async fn is_team_member<'a>(&'a self, client: &'a GithubClient) -> anyhow::Result<bool> {
  155. log::trace!("Getting team membership for {:?}", self.login);
  156. let permission = crate::team_data::teams(client).await?;
  157. let map = permission.teams;
  158. let is_triager = map
  159. .get("wg-triage")
  160. .map_or(false, |w| w.members.iter().any(|g| g.github == self.login));
  161. let is_pri_member = map
  162. .get("wg-prioritization")
  163. .map_or(false, |w| w.members.iter().any(|g| g.github == self.login));
  164. let is_async_member = map
  165. .get("wg-async")
  166. .map_or(false, |w| w.members.iter().any(|g| g.github == self.login));
  167. let in_all = map["all"].members.iter().any(|g| g.github == self.login);
  168. log::trace!(
  169. "{:?} is all?={:?}, triager?={:?}, prioritizer?={:?}, async?={:?}",
  170. self.login,
  171. in_all,
  172. is_triager,
  173. is_pri_member,
  174. is_async_member,
  175. );
  176. Ok(in_all || is_triager || is_pri_member || is_async_member)
  177. }
  178. // Returns the ID of the given user, if the user is in the `all` team.
  179. pub async fn get_id<'a>(&'a self, client: &'a GithubClient) -> anyhow::Result<Option<usize>> {
  180. let permission = crate::team_data::teams(client).await?;
  181. let map = permission.teams;
  182. Ok(map["all"]
  183. .members
  184. .iter()
  185. .find(|g| g.github == self.login)
  186. .map(|u| u.github_id))
  187. }
  188. }
  189. pub async fn get_team(
  190. client: &GithubClient,
  191. team: &str,
  192. ) -> anyhow::Result<Option<rust_team_data::v1::Team>> {
  193. let permission = crate::team_data::teams(client).await?;
  194. let mut map = permission.teams;
  195. Ok(map.swap_remove(team))
  196. }
  197. #[derive(PartialEq, Eq, Debug, Clone, serde::Deserialize)]
  198. pub struct Label {
  199. pub name: String,
  200. }
  201. /// An indicator used to differentiate between an issue and a pull request.
  202. ///
  203. /// Some webhook events include a `pull_request` field in the Issue object,
  204. /// and some don't. GitHub does include a few fields here, but they aren't
  205. /// needed at this time (merged_at, diff_url, html_url, patch_url, url).
  206. #[derive(Debug, serde::Deserialize)]
  207. pub struct PullRequestDetails {
  208. /// This is a slot to hold the diff for a PR.
  209. ///
  210. /// This will be filled in only once as an optimization since multiple
  211. /// handlers want to see PR changes, and getting the diff can be
  212. /// expensive.
  213. #[serde(skip)]
  214. files_changed: tokio::sync::OnceCell<Vec<FileDiff>>,
  215. }
  216. /// Representation of a diff to a single file.
  217. #[derive(Debug)]
  218. pub struct FileDiff {
  219. /// The full path of the file.
  220. pub path: String,
  221. /// The diff for the file.
  222. pub diff: String,
  223. }
  224. impl PullRequestDetails {
  225. pub fn new() -> PullRequestDetails {
  226. PullRequestDetails {
  227. files_changed: tokio::sync::OnceCell::new(),
  228. }
  229. }
  230. }
  231. /// An issue or pull request.
  232. ///
  233. /// For convenience, since issues and pull requests share most of their
  234. /// fields, this struct is used for both. The `pull_request` field can be used
  235. /// to determine which it is. Some fields are only available on pull requests
  236. /// (but not always, check the GitHub API for details).
  237. #[derive(Debug, serde::Deserialize)]
  238. pub struct Issue {
  239. pub number: u64,
  240. #[serde(deserialize_with = "opt_string")]
  241. pub body: String,
  242. created_at: chrono::DateTime<Utc>,
  243. pub updated_at: chrono::DateTime<Utc>,
  244. /// The SHA for a merge commit.
  245. ///
  246. /// This field is complicated, see the [Pull Request
  247. /// docs](https://docs.github.com/en/rest/pulls/pulls#get-a-pull-request)
  248. /// for details.
  249. #[serde(default)]
  250. pub merge_commit_sha: Option<String>,
  251. pub title: String,
  252. /// The common URL for viewing this issue or PR.
  253. ///
  254. /// Example: `https://github.com/octocat/Hello-World/pull/1347`
  255. pub html_url: String,
  256. pub user: User,
  257. pub labels: Vec<Label>,
  258. pub assignees: Vec<User>,
  259. /// Indicator if this is a pull request.
  260. ///
  261. /// This is `Some` if this is a PR (as opposed to an issue). Note that
  262. /// this does not always get filled in by GitHub, and must be manually
  263. /// populated (because some webhook events do not set it).
  264. pub pull_request: Option<PullRequestDetails>,
  265. /// Whether or not the pull request was merged.
  266. #[serde(default)]
  267. pub merged: bool,
  268. #[serde(default)]
  269. pub draft: bool,
  270. /// The API URL for discussion comments.
  271. ///
  272. /// Example: `https://api.github.com/repos/octocat/Hello-World/issues/1347/comments`
  273. comments_url: String,
  274. /// The repository for this issue.
  275. ///
  276. /// Note that this is constructed via the [`Issue::repository`] method.
  277. /// It is not deserialized from the GitHub API.
  278. #[serde(skip)]
  279. repository: OnceCell<IssueRepository>,
  280. /// The base commit for a PR (the branch of the destination repo).
  281. #[serde(default)]
  282. pub base: Option<CommitBase>,
  283. /// The head commit for a PR (the branch from the source repo).
  284. #[serde(default)]
  285. pub head: Option<CommitBase>,
  286. /// Whether it is open or closed.
  287. pub state: IssueState,
  288. }
  289. #[derive(Debug, serde::Deserialize, Eq, PartialEq)]
  290. #[serde(rename_all = "snake_case")]
  291. pub enum IssueState {
  292. Open,
  293. Closed,
  294. }
  295. /// Contains only the parts of `Issue` that are needed for turning the issue title into a Zulip
  296. /// topic.
  297. #[derive(Clone, Debug, PartialEq, Eq)]
  298. pub struct ZulipGitHubReference {
  299. pub number: u64,
  300. pub title: String,
  301. pub repository: IssueRepository,
  302. }
  303. impl ZulipGitHubReference {
  304. pub fn zulip_topic_reference(&self) -> String {
  305. let repo = &self.repository;
  306. if repo.organization == "rust-lang" {
  307. if repo.repository == "rust" {
  308. format!("#{}", self.number)
  309. } else {
  310. format!("{}#{}", repo.repository, self.number)
  311. }
  312. } else {
  313. format!("{}/{}#{}", repo.organization, repo.repository, self.number)
  314. }
  315. }
  316. }
  317. #[derive(Debug, serde::Deserialize)]
  318. pub struct Comment {
  319. #[serde(deserialize_with = "opt_string")]
  320. pub body: String,
  321. pub html_url: String,
  322. pub user: User,
  323. #[serde(alias = "submitted_at")] // for pull request reviews
  324. pub updated_at: chrono::DateTime<Utc>,
  325. #[serde(default, rename = "state")]
  326. pub pr_review_state: Option<PullRequestReviewState>,
  327. }
  328. #[derive(Debug, serde::Deserialize, Eq, PartialEq)]
  329. #[serde(rename_all = "snake_case")]
  330. pub enum PullRequestReviewState {
  331. Approved,
  332. ChangesRequested,
  333. Commented,
  334. Dismissed,
  335. Pending,
  336. }
  337. fn opt_string<'de, D>(deserializer: D) -> Result<String, D::Error>
  338. where
  339. D: serde::de::Deserializer<'de>,
  340. {
  341. use serde::de::Deserialize;
  342. match <Option<String>>::deserialize(deserializer) {
  343. Ok(v) => Ok(v.unwrap_or_default()),
  344. Err(e) => Err(e),
  345. }
  346. }
  347. #[derive(Debug)]
  348. pub enum AssignmentError {
  349. InvalidAssignee,
  350. Http(anyhow::Error),
  351. }
  352. #[derive(Debug)]
  353. pub enum Selection<'a, T: ?Sized> {
  354. All,
  355. One(&'a T),
  356. Except(&'a T),
  357. }
  358. impl fmt::Display for AssignmentError {
  359. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  360. match self {
  361. AssignmentError::InvalidAssignee => write!(f, "invalid assignee"),
  362. AssignmentError::Http(e) => write!(f, "cannot assign: {}", e),
  363. }
  364. }
  365. }
  366. impl std::error::Error for AssignmentError {}
  367. #[derive(Debug, Clone, PartialEq, Eq)]
  368. pub struct IssueRepository {
  369. pub organization: String,
  370. pub repository: String,
  371. }
  372. impl fmt::Display for IssueRepository {
  373. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  374. write!(f, "{}/{}", self.organization, self.repository)
  375. }
  376. }
  377. impl IssueRepository {
  378. fn url(&self) -> String {
  379. format!(
  380. "https://api.github.com/repos/{}/{}",
  381. self.organization, self.repository
  382. )
  383. }
  384. fn full_repo_name(&self) -> String {
  385. format!("{}/{}", self.organization, self.repository)
  386. }
  387. async fn has_label(&self, client: &GithubClient, label: &str) -> anyhow::Result<bool> {
  388. #[allow(clippy::redundant_pattern_matching)]
  389. let url = format!("{}/labels/{}", self.url(), label);
  390. match client.send_req(client.get(&url)).await {
  391. Ok(_) => Ok(true),
  392. Err(e) => {
  393. if e.downcast_ref::<reqwest::Error>()
  394. .map_or(false, |e| e.status() == Some(StatusCode::NOT_FOUND))
  395. {
  396. Ok(false)
  397. } else {
  398. Err(e)
  399. }
  400. }
  401. }
  402. }
  403. }
  404. #[derive(Debug)]
  405. pub(crate) struct UnknownLabels {
  406. labels: Vec<String>,
  407. }
  408. // NOTE: This is used to post the Github comment; make sure it's valid markdown.
  409. impl fmt::Display for UnknownLabels {
  410. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  411. write!(f, "Unknown labels: {}", &self.labels.join(", "))
  412. }
  413. }
  414. impl std::error::Error for UnknownLabels {}
  415. impl Issue {
  416. pub fn to_zulip_github_reference(&self) -> ZulipGitHubReference {
  417. ZulipGitHubReference {
  418. number: self.number,
  419. title: self.title.clone(),
  420. repository: self.repository().clone(),
  421. }
  422. }
  423. pub fn repository(&self) -> &IssueRepository {
  424. self.repository.get_or_init(|| {
  425. // https://api.github.com/repos/rust-lang/rust/issues/69257/comments
  426. log::trace!("get repository for {}", self.comments_url);
  427. let url = url::Url::parse(&self.comments_url).unwrap();
  428. let mut segments = url.path_segments().unwrap();
  429. let _comments = segments.next_back().unwrap();
  430. let _number = segments.next_back().unwrap();
  431. let _issues_or_prs = segments.next_back().unwrap();
  432. let repository = segments.next_back().unwrap();
  433. let organization = segments.next_back().unwrap();
  434. IssueRepository {
  435. organization: organization.into(),
  436. repository: repository.into(),
  437. }
  438. })
  439. }
  440. pub fn global_id(&self) -> String {
  441. format!("{}#{}", self.repository(), self.number)
  442. }
  443. pub fn is_pr(&self) -> bool {
  444. self.pull_request.is_some()
  445. }
  446. pub fn is_open(&self) -> bool {
  447. self.state == IssueState::Open
  448. }
  449. pub async fn get_comment(&self, client: &GithubClient, id: usize) -> anyhow::Result<Comment> {
  450. let comment_url = format!("{}/issues/comments/{}", self.repository().url(), id);
  451. let comment = client.json(client.get(&comment_url)).await?;
  452. Ok(comment)
  453. }
  454. // returns an array of one element
  455. pub async fn get_first_comment(&self, client: &GithubClient) -> anyhow::Result<Vec<Comment>> {
  456. let comment_url = format!(
  457. "{}/issues/{}/comments?page=1&per_page=1",
  458. self.repository().url(),
  459. self.number,
  460. );
  461. Ok(client
  462. .json::<Vec<Comment>>(client.get(&comment_url))
  463. .await?)
  464. }
  465. // returns an array of one element
  466. pub async fn get_first100_comments(
  467. &self,
  468. client: &GithubClient,
  469. ) -> anyhow::Result<Vec<Comment>> {
  470. let comment_url = format!(
  471. "{}/issues/{}/comments?page=1&per_page=100",
  472. self.repository().url(),
  473. self.number,
  474. );
  475. Ok(client
  476. .json::<Vec<Comment>>(client.get(&comment_url))
  477. .await?)
  478. }
  479. pub async fn edit_body(&self, client: &GithubClient, body: &str) -> anyhow::Result<()> {
  480. let edit_url = format!("{}/issues/{}", self.repository().url(), self.number);
  481. #[derive(serde::Serialize)]
  482. struct ChangedIssue<'a> {
  483. body: &'a str,
  484. }
  485. client
  486. .send_req(client.patch(&edit_url).json(&ChangedIssue { body }))
  487. .await
  488. .context("failed to edit issue body")?;
  489. Ok(())
  490. }
  491. pub async fn edit_comment(
  492. &self,
  493. client: &GithubClient,
  494. id: usize,
  495. new_body: &str,
  496. ) -> anyhow::Result<()> {
  497. let comment_url = format!("{}/issues/comments/{}", self.repository().url(), id);
  498. #[derive(serde::Serialize)]
  499. struct NewComment<'a> {
  500. body: &'a str,
  501. }
  502. client
  503. .send_req(
  504. client
  505. .patch(&comment_url)
  506. .json(&NewComment { body: new_body }),
  507. )
  508. .await
  509. .context("failed to edit comment")?;
  510. Ok(())
  511. }
  512. pub async fn post_comment(&self, client: &GithubClient, body: &str) -> anyhow::Result<()> {
  513. #[derive(serde::Serialize)]
  514. struct PostComment<'a> {
  515. body: &'a str,
  516. }
  517. client
  518. .send_req(client.post(&self.comments_url).json(&PostComment { body }))
  519. .await
  520. .context("failed to post comment")?;
  521. Ok(())
  522. }
  523. pub async fn remove_label(&self, client: &GithubClient, label: &str) -> anyhow::Result<()> {
  524. log::info!("remove_label from {}: {:?}", self.global_id(), label);
  525. // DELETE /repos/:owner/:repo/issues/:number/labels/{name}
  526. let url = format!(
  527. "{repo_url}/issues/{number}/labels/{name}",
  528. repo_url = self.repository().url(),
  529. number = self.number,
  530. name = label,
  531. );
  532. if !self.labels().iter().any(|l| l.name == label) {
  533. log::info!(
  534. "remove_label from {}: {:?} already not present, skipping",
  535. self.global_id(),
  536. label
  537. );
  538. return Ok(());
  539. }
  540. client
  541. .send_req(client.delete(&url))
  542. .await
  543. .context("failed to delete label")?;
  544. Ok(())
  545. }
  546. pub async fn add_labels(
  547. &self,
  548. client: &GithubClient,
  549. labels: Vec<Label>,
  550. ) -> anyhow::Result<()> {
  551. log::info!("add_labels: {} +{:?}", self.global_id(), labels);
  552. // POST /repos/:owner/:repo/issues/:number/labels
  553. // repo_url = https://api.github.com/repos/Codertocat/Hello-World
  554. let url = format!(
  555. "{repo_url}/issues/{number}/labels",
  556. repo_url = self.repository().url(),
  557. number = self.number
  558. );
  559. // Don't try to add labels already present on this issue.
  560. let labels = labels
  561. .into_iter()
  562. .filter(|l| !self.labels().contains(&l))
  563. .map(|l| l.name)
  564. .collect::<Vec<_>>();
  565. log::info!("add_labels: {} filtered to {:?}", self.global_id(), labels);
  566. if labels.is_empty() {
  567. return Ok(());
  568. }
  569. let mut unknown_labels = vec![];
  570. let mut known_labels = vec![];
  571. for label in labels {
  572. if !self.repository().has_label(client, &label).await? {
  573. unknown_labels.push(label);
  574. } else {
  575. known_labels.push(label);
  576. }
  577. }
  578. if !unknown_labels.is_empty() {
  579. return Err(UnknownLabels {
  580. labels: unknown_labels,
  581. }
  582. .into());
  583. }
  584. #[derive(serde::Serialize)]
  585. struct LabelsReq {
  586. labels: Vec<String>,
  587. }
  588. client
  589. .send_req(client.post(&url).json(&LabelsReq {
  590. labels: known_labels,
  591. }))
  592. .await
  593. .context("failed to add labels")?;
  594. Ok(())
  595. }
  596. pub fn labels(&self) -> &[Label] {
  597. &self.labels
  598. }
  599. pub fn contain_assignee(&self, user: &str) -> bool {
  600. self.assignees
  601. .iter()
  602. .any(|a| a.login.to_lowercase() == user.to_lowercase())
  603. }
  604. pub async fn remove_assignees(
  605. &self,
  606. client: &GithubClient,
  607. selection: Selection<'_, str>,
  608. ) -> Result<(), AssignmentError> {
  609. log::info!("remove {:?} assignees for {}", selection, self.global_id());
  610. let url = format!(
  611. "{repo_url}/issues/{number}/assignees",
  612. repo_url = self.repository().url(),
  613. number = self.number
  614. );
  615. let assignees = match selection {
  616. Selection::All => self
  617. .assignees
  618. .iter()
  619. .map(|u| u.login.as_str())
  620. .collect::<Vec<_>>(),
  621. Selection::One(user) => vec![user],
  622. Selection::Except(user) => self
  623. .assignees
  624. .iter()
  625. .map(|u| u.login.as_str())
  626. .filter(|&u| u.to_lowercase() != user.to_lowercase())
  627. .collect::<Vec<_>>(),
  628. };
  629. #[derive(serde::Serialize)]
  630. struct AssigneeReq<'a> {
  631. assignees: &'a [&'a str],
  632. }
  633. client
  634. .send_req(client.delete(&url).json(&AssigneeReq {
  635. assignees: &assignees[..],
  636. }))
  637. .await
  638. .map_err(AssignmentError::Http)?;
  639. Ok(())
  640. }
  641. pub async fn add_assignee(
  642. &self,
  643. client: &GithubClient,
  644. user: &str,
  645. ) -> Result<(), AssignmentError> {
  646. log::info!("add_assignee {} for {}", user, self.global_id());
  647. let url = format!(
  648. "{repo_url}/issues/{number}/assignees",
  649. repo_url = self.repository().url(),
  650. number = self.number
  651. );
  652. #[derive(serde::Serialize)]
  653. struct AssigneeReq<'a> {
  654. assignees: &'a [&'a str],
  655. }
  656. let result: Issue = client
  657. .json(client.post(&url).json(&AssigneeReq { assignees: &[user] }))
  658. .await
  659. .map_err(AssignmentError::Http)?;
  660. // Invalid assignees are silently ignored. We can just check if the user is now
  661. // contained in the assignees list.
  662. let success = result
  663. .assignees
  664. .iter()
  665. .any(|u| u.login.as_str().to_lowercase() == user.to_lowercase());
  666. if success {
  667. Ok(())
  668. } else {
  669. Err(AssignmentError::InvalidAssignee)
  670. }
  671. }
  672. pub async fn set_assignee(
  673. &self,
  674. client: &GithubClient,
  675. user: &str,
  676. ) -> Result<(), AssignmentError> {
  677. log::info!("set_assignee for {} to {}", self.global_id(), user);
  678. self.add_assignee(client, user).await?;
  679. self.remove_assignees(client, Selection::Except(user))
  680. .await?;
  681. Ok(())
  682. }
  683. /// Sets the milestone of the issue or PR.
  684. ///
  685. /// This will create the milestone if it does not exist. The new milestone
  686. /// will start in the "open" state.
  687. pub async fn set_milestone(&self, client: &GithubClient, title: &str) -> anyhow::Result<()> {
  688. log::trace!(
  689. "Setting milestone for rust-lang/rust#{} to {}",
  690. self.number,
  691. title
  692. );
  693. let full_repo_name = self.repository().full_repo_name();
  694. let milestone = client
  695. .get_or_create_milestone(&full_repo_name, title, "open")
  696. .await?;
  697. client
  698. .set_milestone(&full_repo_name, &milestone, self.number)
  699. .await?;
  700. Ok(())
  701. }
  702. pub async fn close(&self, client: &GithubClient) -> anyhow::Result<()> {
  703. let edit_url = format!("{}/issues/{}", self.repository().url(), self.number);
  704. #[derive(serde::Serialize)]
  705. struct CloseIssue<'a> {
  706. state: &'a str,
  707. }
  708. client
  709. .send_req(
  710. client
  711. .patch(&edit_url)
  712. .json(&CloseIssue { state: "closed" }),
  713. )
  714. .await
  715. .context("failed to close issue")?;
  716. Ok(())
  717. }
  718. /// Returns the diff in this event, for Open and Synchronize events for now.
  719. ///
  720. /// Returns `None` if the issue is not a PR.
  721. pub async fn diff(&self, client: &GithubClient) -> anyhow::Result<Option<&[FileDiff]>> {
  722. let Some(pr) = &self.pull_request else {
  723. return Ok(None);
  724. };
  725. let (before, after) = if let (Some(base), Some(head)) = (&self.base, &self.head) {
  726. (&base.sha, &head.sha)
  727. } else {
  728. return Ok(None);
  729. };
  730. let diff = pr
  731. .files_changed
  732. .get_or_try_init::<anyhow::Error, _, _>(|| async move {
  733. let url = format!("{}/compare/{before}...{after}", self.repository().url());
  734. let mut req = client.get(&url);
  735. req = req.header("Accept", "application/vnd.github.v3.diff");
  736. let (diff, _) = client
  737. .send_req(req)
  738. .await
  739. .with_context(|| format!("failed to fetch diff comparison for {url}"))?;
  740. let body = String::from_utf8_lossy(&diff);
  741. Ok(parse_diff(&body))
  742. })
  743. .await?;
  744. Ok(Some(diff))
  745. }
  746. /// Returns the commits from this pull request (no commits are returned if this `Issue` is not
  747. /// a pull request).
  748. pub async fn commits(&self, client: &GithubClient) -> anyhow::Result<Vec<GithubCommit>> {
  749. if !self.is_pr() {
  750. return Ok(vec![]);
  751. }
  752. let mut commits = Vec::new();
  753. let mut page = 1;
  754. loop {
  755. let req = client.get(&format!(
  756. "{}/pulls/{}/commits?page={page}&per_page=100",
  757. self.repository().url(),
  758. self.number
  759. ));
  760. let new: Vec<_> = client.json(req).await?;
  761. if new.is_empty() {
  762. break;
  763. }
  764. commits.extend(new);
  765. page += 1;
  766. }
  767. Ok(commits)
  768. }
  769. pub async fn files(&self, client: &GithubClient) -> anyhow::Result<Vec<PullRequestFile>> {
  770. if !self.is_pr() {
  771. return Ok(vec![]);
  772. }
  773. let req = client.get(&format!(
  774. "{}/pulls/{}/files",
  775. self.repository().url(),
  776. self.number
  777. ));
  778. Ok(client.json(req).await?)
  779. }
  780. }
  781. #[derive(Debug, serde::Deserialize)]
  782. pub struct PullRequestFile {
  783. pub sha: String,
  784. pub filename: String,
  785. pub blob_url: String,
  786. }
  787. #[derive(Debug, serde::Deserialize)]
  788. pub struct Milestone {
  789. number: u64,
  790. title: String,
  791. }
  792. #[derive(Debug, serde::Deserialize)]
  793. pub struct ChangeInner {
  794. pub from: String,
  795. }
  796. #[derive(Debug, serde::Deserialize)]
  797. pub struct Changes {
  798. pub title: Option<ChangeInner>,
  799. pub body: Option<ChangeInner>,
  800. }
  801. #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
  802. #[serde(rename_all = "lowercase")]
  803. pub enum PullRequestReviewAction {
  804. Submitted,
  805. Edited,
  806. Dismissed,
  807. }
  808. /// A pull request review event.
  809. ///
  810. /// <https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#pull_request_review>
  811. #[derive(Debug, serde::Deserialize)]
  812. pub struct PullRequestReviewEvent {
  813. pub action: PullRequestReviewAction,
  814. pub pull_request: Issue,
  815. pub review: Comment,
  816. pub changes: Option<Changes>,
  817. pub repository: Repository,
  818. }
  819. #[derive(Debug, serde::Deserialize)]
  820. pub struct PullRequestReviewComment {
  821. pub action: IssueCommentAction,
  822. pub changes: Option<Changes>,
  823. #[serde(rename = "pull_request")]
  824. pub issue: Issue,
  825. pub comment: Comment,
  826. pub repository: Repository,
  827. }
  828. #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
  829. #[serde(rename_all = "lowercase")]
  830. pub enum IssueCommentAction {
  831. Created,
  832. Edited,
  833. Deleted,
  834. }
  835. #[derive(Debug, serde::Deserialize)]
  836. pub struct IssueCommentEvent {
  837. pub action: IssueCommentAction,
  838. pub changes: Option<Changes>,
  839. pub issue: Issue,
  840. pub comment: Comment,
  841. pub repository: Repository,
  842. }
  843. #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
  844. #[serde(rename_all = "snake_case", tag = "action")]
  845. pub enum IssuesAction {
  846. Opened,
  847. Edited,
  848. Deleted,
  849. Transferred,
  850. Pinned,
  851. Unpinned,
  852. Closed,
  853. Reopened,
  854. Assigned,
  855. Unassigned,
  856. Labeled {
  857. /// The label added from the issue
  858. label: Label,
  859. },
  860. Unlabeled {
  861. /// The label removed from the issue
  862. label: Label,
  863. },
  864. Locked,
  865. Unlocked,
  866. Milestoned,
  867. Demilestoned,
  868. ReviewRequested {
  869. /// The person requested to review the pull request
  870. requested_reviewer: User,
  871. },
  872. ReviewRequestRemoved,
  873. ReadyForReview,
  874. Synchronize,
  875. ConvertedToDraft,
  876. AutoMergeEnabled,
  877. AutoMergeDisabled,
  878. }
  879. #[derive(Debug, serde::Deserialize)]
  880. pub struct IssuesEvent {
  881. #[serde(flatten)]
  882. pub action: IssuesAction,
  883. #[serde(alias = "pull_request")]
  884. pub issue: Issue,
  885. pub changes: Option<Changes>,
  886. pub repository: Repository,
  887. /// The GitHub user that triggered the event.
  888. pub sender: User,
  889. }
  890. #[derive(Debug, serde::Deserialize)]
  891. struct PullRequestEventFields {}
  892. #[derive(Clone, Debug, serde::Deserialize)]
  893. pub struct CommitBase {
  894. sha: String,
  895. #[serde(rename = "ref")]
  896. pub git_ref: String,
  897. pub repo: Repository,
  898. }
  899. pub fn parse_diff(diff: &str) -> Vec<FileDiff> {
  900. // This does not properly handle filenames with spaces.
  901. let re = regex::Regex::new("(?m)^diff --git .* b/(.*)").unwrap();
  902. let mut files: Vec<_> = re
  903. .captures_iter(diff)
  904. .map(|cap| {
  905. let start = cap.get(0).unwrap().start();
  906. let path = cap.get(1).unwrap().as_str().to_string();
  907. (start, path)
  908. })
  909. .collect();
  910. // Break the list up into (start, end) pairs starting from the "diff --git" line.
  911. files.push((diff.len(), String::new()));
  912. files
  913. .windows(2)
  914. .map(|w| {
  915. let (start, end) = (&w[0], &w[1]);
  916. FileDiff {
  917. path: start.1.clone(),
  918. diff: diff[start.0..end.0].to_string(),
  919. }
  920. })
  921. .collect()
  922. }
  923. #[derive(Debug, serde::Deserialize)]
  924. pub struct IssueSearchResult {
  925. pub total_count: usize,
  926. pub incomplete_results: bool,
  927. pub items: Vec<Issue>,
  928. }
  929. #[derive(Clone, Debug, serde::Deserialize)]
  930. pub struct Repository {
  931. pub full_name: String,
  932. pub default_branch: String,
  933. #[serde(default)]
  934. pub fork: bool,
  935. pub parent: Option<Box<Repository>>,
  936. }
  937. #[derive(Copy, Clone)]
  938. struct Ordering<'a> {
  939. pub sort: &'a str,
  940. pub direction: &'a str,
  941. pub per_page: &'a str,
  942. pub page: usize,
  943. }
  944. impl Repository {
  945. const GITHUB_API_URL: &'static str = "https://api.github.com";
  946. const GITHUB_GRAPHQL_API_URL: &'static str = "https://api.github.com/graphql";
  947. fn url(&self) -> String {
  948. format!("{}/repos/{}", Repository::GITHUB_API_URL, self.full_name)
  949. }
  950. pub fn owner(&self) -> &str {
  951. self.full_name.split_once('/').unwrap().0
  952. }
  953. pub fn name(&self) -> &str {
  954. self.full_name.split_once('/').unwrap().1
  955. }
  956. pub async fn get_issues<'a>(
  957. &self,
  958. client: &GithubClient,
  959. query: &Query<'a>,
  960. ) -> anyhow::Result<Vec<Issue>> {
  961. let Query {
  962. filters,
  963. include_labels,
  964. exclude_labels,
  965. } = query;
  966. let mut ordering = Ordering {
  967. sort: "created",
  968. direction: "asc",
  969. per_page: "100",
  970. page: 1,
  971. };
  972. let filters: Vec<_> = filters
  973. .clone()
  974. .into_iter()
  975. .filter(|(key, val)| {
  976. match *key {
  977. "sort" => ordering.sort = val,
  978. "direction" => ordering.direction = val,
  979. "per_page" => ordering.per_page = val,
  980. _ => return true,
  981. };
  982. false
  983. })
  984. .collect();
  985. // `is: pull-request` indicates the query to retrieve PRs only
  986. let is_pr = filters
  987. .iter()
  988. .any(|&(key, value)| key == "is" && value == "pull-request");
  989. // There are some cases that can only be handled by the search API:
  990. // 1. When using negating label filters (exclude_labels)
  991. // 2. When there's a key parameter key=no
  992. // 3. When the query is to retrieve PRs only and there are label filters
  993. //
  994. // Check https://docs.github.com/en/rest/reference/search#search-issues-and-pull-requests
  995. // for more information
  996. let use_search_api = !exclude_labels.is_empty()
  997. || filters.iter().any(|&(key, _)| key == "no")
  998. || is_pr && !include_labels.is_empty();
  999. // If there are more than `per_page` of issues, we need to paginate
  1000. let mut issues = vec![];
  1001. loop {
  1002. let url = if use_search_api {
  1003. self.build_search_issues_url(&filters, include_labels, exclude_labels, ordering)
  1004. } else if is_pr {
  1005. self.build_pulls_url(&filters, include_labels, ordering)
  1006. } else {
  1007. self.build_issues_url(&filters, include_labels, ordering)
  1008. };
  1009. let result = client.get(&url);
  1010. if use_search_api {
  1011. let result = client
  1012. .json::<IssueSearchResult>(result)
  1013. .await
  1014. .with_context(|| format!("failed to list issues from {}", url))?;
  1015. issues.extend(result.items);
  1016. if issues.len() < result.total_count {
  1017. ordering.page += 1;
  1018. continue;
  1019. }
  1020. } else {
  1021. // FIXME: paginate with non-search
  1022. issues = client
  1023. .json(result)
  1024. .await
  1025. .with_context(|| format!("failed to list issues from {}", url))?
  1026. }
  1027. break;
  1028. }
  1029. Ok(issues)
  1030. }
  1031. fn build_issues_url(
  1032. &self,
  1033. filters: &Vec<(&str, &str)>,
  1034. include_labels: &Vec<&str>,
  1035. ordering: Ordering<'_>,
  1036. ) -> String {
  1037. self.build_endpoint_url("issues", filters, include_labels, ordering)
  1038. }
  1039. fn build_pulls_url(
  1040. &self,
  1041. filters: &Vec<(&str, &str)>,
  1042. include_labels: &Vec<&str>,
  1043. ordering: Ordering<'_>,
  1044. ) -> String {
  1045. self.build_endpoint_url("pulls", filters, include_labels, ordering)
  1046. }
  1047. fn build_endpoint_url(
  1048. &self,
  1049. endpoint: &str,
  1050. filters: &Vec<(&str, &str)>,
  1051. include_labels: &Vec<&str>,
  1052. ordering: Ordering<'_>,
  1053. ) -> String {
  1054. let filters = filters
  1055. .iter()
  1056. .map(|(key, val)| format!("{}={}", key, val))
  1057. .chain(std::iter::once(format!(
  1058. "labels={}",
  1059. include_labels.join(",")
  1060. )))
  1061. .chain(std::iter::once("filter=all".to_owned()))
  1062. .chain(std::iter::once(format!("sort={}", ordering.sort,)))
  1063. .chain(std::iter::once(
  1064. format!("direction={}", ordering.direction,),
  1065. ))
  1066. .chain(std::iter::once(format!("per_page={}", ordering.per_page,)))
  1067. .collect::<Vec<_>>()
  1068. .join("&");
  1069. format!(
  1070. "{}/repos/{}/{}?{}",
  1071. Repository::GITHUB_API_URL,
  1072. self.full_name,
  1073. endpoint,
  1074. filters
  1075. )
  1076. }
  1077. fn build_search_issues_url(
  1078. &self,
  1079. filters: &Vec<(&str, &str)>,
  1080. include_labels: &Vec<&str>,
  1081. exclude_labels: &Vec<&str>,
  1082. ordering: Ordering<'_>,
  1083. ) -> String {
  1084. let filters = filters
  1085. .iter()
  1086. .filter(|&&(key, val)| !(key == "state" && val == "all"))
  1087. .map(|(key, val)| format!("{}:{}", key, val))
  1088. .chain(
  1089. include_labels
  1090. .iter()
  1091. .map(|label| format!("label:{}", label)),
  1092. )
  1093. .chain(
  1094. exclude_labels
  1095. .iter()
  1096. .map(|label| format!("-label:{}", label)),
  1097. )
  1098. .chain(std::iter::once(format!("repo:{}", self.full_name)))
  1099. .collect::<Vec<_>>()
  1100. .join("+");
  1101. format!(
  1102. "{}/search/issues?q={}&sort={}&order={}&per_page={}&page={}",
  1103. Repository::GITHUB_API_URL,
  1104. filters,
  1105. ordering.sort,
  1106. ordering.direction,
  1107. ordering.per_page,
  1108. ordering.page,
  1109. )
  1110. }
  1111. /// Returns a list of commits between the SHA ranges of start (exclusive)
  1112. /// and end (inclusive).
  1113. pub async fn commits_in_range(
  1114. &self,
  1115. client: &GithubClient,
  1116. start: &str,
  1117. end: &str,
  1118. ) -> anyhow::Result<Vec<GithubCommit>> {
  1119. let mut commits = Vec::new();
  1120. let mut page = 1;
  1121. loop {
  1122. let url = format!("{}/commits?sha={end}&per_page=100&page={page}", self.url());
  1123. let mut this_page: Vec<GithubCommit> = client
  1124. .json(client.get(&url))
  1125. .await
  1126. .with_context(|| format!("failed to fetch commits for {url}"))?;
  1127. if let Some(idx) = this_page.iter().position(|commit| commit.sha == start) {
  1128. this_page.truncate(idx);
  1129. commits.extend(this_page);
  1130. return Ok(commits);
  1131. } else {
  1132. commits.extend(this_page);
  1133. }
  1134. page += 1;
  1135. }
  1136. }
  1137. /// Retrieves a git commit for the given SHA.
  1138. pub async fn git_commit(&self, client: &GithubClient, sha: &str) -> anyhow::Result<GitCommit> {
  1139. let url = format!("{}/git/commits/{sha}", self.url());
  1140. client
  1141. .json(client.get(&url))
  1142. .await
  1143. .with_context(|| format!("{} failed to get git commit {sha}", self.full_name))
  1144. }
  1145. /// Creates a new commit.
  1146. pub async fn create_commit(
  1147. &self,
  1148. client: &GithubClient,
  1149. message: &str,
  1150. parents: &[&str],
  1151. tree: &str,
  1152. ) -> anyhow::Result<GitCommit> {
  1153. let url = format!("{}/git/commits", self.url());
  1154. client
  1155. .json(client.post(&url).json(&serde_json::json!({
  1156. "message": message,
  1157. "parents": parents,
  1158. "tree": tree,
  1159. })))
  1160. .await
  1161. .with_context(|| format!("{} failed to create commit for tree {tree}", self.full_name))
  1162. }
  1163. /// Retrieves a git reference for the given refname.
  1164. pub async fn get_reference(
  1165. &self,
  1166. client: &GithubClient,
  1167. refname: &str,
  1168. ) -> anyhow::Result<GitReference> {
  1169. let url = format!("{}/git/ref/{}", self.url(), refname);
  1170. client
  1171. .json(client.get(&url))
  1172. .await
  1173. .with_context(|| format!("{} failed to get git reference {refname}", self.full_name))
  1174. }
  1175. /// Updates an existing git reference to a new SHA.
  1176. pub async fn update_reference(
  1177. &self,
  1178. client: &GithubClient,
  1179. refname: &str,
  1180. sha: &str,
  1181. ) -> anyhow::Result<GitReference> {
  1182. let url = format!("{}/git/refs/{}", self.url(), refname);
  1183. client
  1184. .json(client.patch(&url).json(&serde_json::json!({
  1185. "sha": sha,
  1186. "force": true,
  1187. })))
  1188. .await
  1189. .with_context(|| {
  1190. format!(
  1191. "{} failed to update reference {refname} to {sha}",
  1192. self.full_name
  1193. )
  1194. })
  1195. }
  1196. /// Returns a list of recent commits on the given branch.
  1197. ///
  1198. /// Returns results in the OID range `oldest` (exclusive) to `newest`
  1199. /// (inclusive).
  1200. pub async fn recent_commits(
  1201. &self,
  1202. client: &GithubClient,
  1203. branch: &str,
  1204. oldest: &str,
  1205. newest: &str,
  1206. ) -> anyhow::Result<Vec<RecentCommit>> {
  1207. // This is used to deduplicate the results (so that a PR with multiple
  1208. // commits will only show up once).
  1209. let mut prs_seen = HashSet::new();
  1210. let mut recent_commits = Vec::new(); // This is the final result.
  1211. use cynic::QueryBuilder;
  1212. use github_graphql::docs_update_queries::{
  1213. GitObject, RecentCommits, RecentCommitsArguments,
  1214. };
  1215. let mut args = RecentCommitsArguments {
  1216. branch,
  1217. name: self.name(),
  1218. owner: self.owner(),
  1219. after: None,
  1220. };
  1221. let mut found_newest = false;
  1222. let mut found_oldest = false;
  1223. // This simulates --first-parent. We only care about top-level commits.
  1224. // Unfortunately the GitHub API doesn't provide anything like that.
  1225. let mut next_first_parent = None;
  1226. // Search for `oldest` within 3 pages (300 commits).
  1227. for _ in 0..3 {
  1228. let query = RecentCommits::build(args.clone());
  1229. let data = client
  1230. .json::<cynic::GraphQlResponse<RecentCommits>>(
  1231. client.post(Repository::GITHUB_GRAPHQL_API_URL).json(&query),
  1232. )
  1233. .await
  1234. .with_context(|| {
  1235. format!(
  1236. "{} failed to get recent commits branch={branch}",
  1237. self.full_name
  1238. )
  1239. })?;
  1240. if let Some(errors) = data.errors {
  1241. anyhow::bail!("There were graphql errors. {:?}", errors);
  1242. }
  1243. let target = data
  1244. .data
  1245. .ok_or_else(|| anyhow::anyhow!("No data returned."))?
  1246. .repository
  1247. .ok_or_else(|| anyhow::anyhow!("No repository."))?
  1248. .ref_
  1249. .ok_or_else(|| anyhow::anyhow!("No ref."))?
  1250. .target
  1251. .ok_or_else(|| anyhow::anyhow!("No target."))?;
  1252. let commit = match target {
  1253. GitObject::Commit(commit) => commit,
  1254. _ => anyhow::bail!("unexpected target type {target:?}"),
  1255. };
  1256. let commits = commit
  1257. .history
  1258. .nodes
  1259. .into_iter()
  1260. // Don't include anything newer than `newest`
  1261. .skip_while(|node| {
  1262. if found_newest || node.oid.0 == newest {
  1263. found_newest = true;
  1264. false
  1265. } else {
  1266. // This should only happen if there is a commit that arrives
  1267. // between the time that `update_submodules` fetches the latest
  1268. // ref, and this runs. This window should be a few seconds, so it
  1269. // should be unlikely. This warning is here in case my assumptions
  1270. // about how things work is not correct.
  1271. tracing::warn!(
  1272. "unexpected race with submodule history, newest oid={newest} skipping oid={}",
  1273. node.oid.0
  1274. );
  1275. true
  1276. }
  1277. })
  1278. // Skip nodes that aren't the first parent
  1279. .filter(|node| {
  1280. let this_first_parent = node.parents.nodes
  1281. .first()
  1282. .map(|parent| parent.oid.0.clone());
  1283. match &next_first_parent {
  1284. Some(first_parent) => {
  1285. if first_parent == &node.oid.0 {
  1286. // Found the next first parent, include it and
  1287. // set next_first_parent to look for this
  1288. // commit's first parent.
  1289. next_first_parent = this_first_parent;
  1290. true
  1291. } else {
  1292. // Still looking for the next first parent.
  1293. false
  1294. }
  1295. }
  1296. None => {
  1297. // First commit.
  1298. next_first_parent = this_first_parent;
  1299. true
  1300. }
  1301. }
  1302. })
  1303. // Stop once reached the `oldest` commit
  1304. .take_while(|node| {
  1305. if node.oid.0 == oldest {
  1306. found_oldest = true;
  1307. false
  1308. } else {
  1309. true
  1310. }
  1311. })
  1312. .filter_map(|node| {
  1313. // Determine if this is associated with a PR or not.
  1314. match node.associated_pull_requests
  1315. // Get the first PR (we only care about one)
  1316. .and_then(|mut pr| pr.nodes.pop()) {
  1317. Some(pr) => {
  1318. // Only include a PR once
  1319. if prs_seen.insert(pr.number) {
  1320. Some(RecentCommit {
  1321. pr_num: Some(pr.number),
  1322. title: pr.title,
  1323. oid: node.oid.0.clone(),
  1324. committed_date: node.committed_date,
  1325. })
  1326. } else {
  1327. None
  1328. }
  1329. }
  1330. None => {
  1331. // This is an unassociated commit, possibly
  1332. // created without a PR.
  1333. Some(RecentCommit {
  1334. pr_num: None,
  1335. title: node.message_headline,
  1336. oid: node.oid.0,
  1337. committed_date: node.committed_date,
  1338. })
  1339. }
  1340. }
  1341. });
  1342. recent_commits.extend(commits);
  1343. let page_info = commit.history.page_info;
  1344. if found_oldest || !page_info.has_next_page || page_info.end_cursor.is_none() {
  1345. break;
  1346. }
  1347. args.after = page_info.end_cursor;
  1348. }
  1349. if !found_oldest {
  1350. // This should probably do something more than log a warning, but
  1351. // I don't think it is too important at this time (the log message
  1352. // is only informational, and this should be unlikely to happen).
  1353. tracing::warn!(
  1354. "{} failed to find oldest commit sha={oldest} branch={branch}",
  1355. self.full_name
  1356. );
  1357. }
  1358. Ok(recent_commits)
  1359. }
  1360. /// Creates a new git tree based on another tree.
  1361. pub async fn update_tree(
  1362. &self,
  1363. client: &GithubClient,
  1364. base_tree: &str,
  1365. tree: &[GitTreeEntry],
  1366. ) -> anyhow::Result<GitTreeObject> {
  1367. let url = format!("{}/git/trees", self.url());
  1368. client
  1369. .json(client.post(&url).json(&serde_json::json!({
  1370. "base_tree": base_tree,
  1371. "tree": tree,
  1372. })))
  1373. .await
  1374. .with_context(|| {
  1375. format!(
  1376. "{} failed to update tree with base {base_tree}",
  1377. self.full_name
  1378. )
  1379. })
  1380. }
  1381. /// Returns information about the git submodule at the given path.
  1382. ///
  1383. /// `refname` is the ref to use for fetching information. If `None`, will
  1384. /// use the latest version on the default branch.
  1385. pub async fn submodule(
  1386. &self,
  1387. client: &GithubClient,
  1388. path: &str,
  1389. refname: Option<&str>,
  1390. ) -> anyhow::Result<Submodule> {
  1391. let mut url = format!("{}/contents/{}", self.url(), path);
  1392. if let Some(refname) = refname {
  1393. url.push_str("?ref=");
  1394. url.push_str(refname);
  1395. }
  1396. client.json(client.get(&url)).await.with_context(|| {
  1397. format!(
  1398. "{} failed to get submodule path={path} refname={refname:?}",
  1399. self.full_name
  1400. )
  1401. })
  1402. }
  1403. /// Creates a new PR.
  1404. pub async fn new_pr(
  1405. &self,
  1406. client: &GithubClient,
  1407. title: &str,
  1408. head: &str,
  1409. base: &str,
  1410. body: &str,
  1411. ) -> anyhow::Result<Issue> {
  1412. let url = format!("{}/pulls", self.url());
  1413. let mut issue: Issue = client
  1414. .json(client.post(&url).json(&serde_json::json!({
  1415. "title": title,
  1416. "head": head,
  1417. "base": base,
  1418. "body": body,
  1419. })))
  1420. .await
  1421. .with_context(|| {
  1422. format!(
  1423. "{} failed to create a new PR head={head} base={base} title={title}",
  1424. self.full_name
  1425. )
  1426. })?;
  1427. issue.pull_request = Some(PullRequestDetails::new());
  1428. Ok(issue)
  1429. }
  1430. /// Synchronize a branch (in a forked repository) by pulling in its upstream contents.
  1431. ///
  1432. /// **Warning**: This will to a force update if there are conflicts.
  1433. pub async fn merge_upstream(&self, client: &GithubClient, branch: &str) -> anyhow::Result<()> {
  1434. let url = format!("{}/merge-upstream", self.url());
  1435. let merge_error = match client
  1436. .send_req(client.post(&url).json(&serde_json::json!({
  1437. "branch": branch,
  1438. })))
  1439. .await
  1440. {
  1441. Ok(_) => return Ok(()),
  1442. Err(e) => {
  1443. if e.downcast_ref::<reqwest::Error>().map_or(false, |e| {
  1444. matches!(
  1445. e.status(),
  1446. Some(StatusCode::UNPROCESSABLE_ENTITY | StatusCode::CONFLICT)
  1447. )
  1448. }) {
  1449. e
  1450. } else {
  1451. return Err(e);
  1452. }
  1453. }
  1454. };
  1455. // 409 is a clear error that there is a merge conflict.
  1456. // However, I don't understand how/why 422 might happen. The docs don't really say.
  1457. // The gh cli falls back to trying to force a sync, so let's try that.
  1458. log::info!(
  1459. "{} failed to merge upstream branch {branch}, trying force sync: {merge_error:?}",
  1460. self.full_name
  1461. );
  1462. let parent = self.parent.as_ref().ok_or_else(|| {
  1463. anyhow::anyhow!(
  1464. "{} failed to merge upstream branch {branch}, \
  1465. force sync could not determine parent",
  1466. self.full_name
  1467. )
  1468. })?;
  1469. // Note: I'm not sure how to handle the case where the branch name
  1470. // differs to the upstream. For example, if I create a branch off
  1471. // master in my fork, somehow GitHub knows that my branch should push
  1472. // to upstream/master (not upstream/my-branch-name). I can't find a
  1473. // way to find that branch name. Perhaps GitHub assumes it is the
  1474. // default branch if there is no matching branch name?
  1475. let branch_ref = format!("heads/{branch}");
  1476. let latest_parent_commit = parent
  1477. .get_reference(client, &branch_ref)
  1478. .await
  1479. .with_context(|| {
  1480. format!(
  1481. "failed to get head branch {branch} when merging upstream to {}",
  1482. self.full_name
  1483. )
  1484. })?;
  1485. let sha = latest_parent_commit.object.sha;
  1486. self.update_reference(client, &branch_ref, &sha)
  1487. .await
  1488. .with_context(|| {
  1489. format!(
  1490. "failed to force update {branch} to {sha} for {}",
  1491. self.full_name
  1492. )
  1493. })?;
  1494. Ok(())
  1495. }
  1496. /// Get or create a [`Milestone`].
  1497. ///
  1498. /// This will not change the state if it already exists.
  1499. pub async fn get_or_create_milestone(
  1500. &self,
  1501. client: &GithubClient,
  1502. title: &str,
  1503. state: &str,
  1504. ) -> anyhow::Result<Milestone> {
  1505. client
  1506. .get_or_create_milestone(&self.full_name, title, state)
  1507. .await
  1508. }
  1509. /// Set the milestone of an issue or PR.
  1510. pub async fn set_milestone(
  1511. &self,
  1512. client: &GithubClient,
  1513. milestone: &Milestone,
  1514. issue_num: u64,
  1515. ) -> anyhow::Result<()> {
  1516. client
  1517. .set_milestone(&self.full_name, milestone, issue_num)
  1518. .await
  1519. }
  1520. pub async fn get_issue(&self, client: &GithubClient, issue_num: u64) -> anyhow::Result<Issue> {
  1521. let url = format!("{}/pulls/{issue_num}", self.url());
  1522. client
  1523. .json(client.get(&url))
  1524. .await
  1525. .with_context(|| format!("{} failed to get issue {issue_num}", self.full_name))
  1526. }
  1527. }
  1528. pub struct Query<'a> {
  1529. // key/value filter
  1530. pub filters: Vec<(&'a str, &'a str)>,
  1531. pub include_labels: Vec<&'a str>,
  1532. pub exclude_labels: Vec<&'a str>,
  1533. }
  1534. fn quote_reply(markdown: &str) -> String {
  1535. if markdown.is_empty() {
  1536. String::from("*No content*")
  1537. } else {
  1538. format!("\n\t> {}", markdown.replace("\n", "\n\t> "))
  1539. }
  1540. }
  1541. #[async_trait]
  1542. impl<'q> IssuesQuery for Query<'q> {
  1543. async fn query<'a>(
  1544. &'a self,
  1545. repo: &'a Repository,
  1546. include_fcp_details: bool,
  1547. include_mcp_details: bool,
  1548. client: &'a GithubClient,
  1549. ) -> anyhow::Result<Vec<crate::actions::IssueDecorator>> {
  1550. let issues = repo
  1551. .get_issues(&client, self)
  1552. .await
  1553. .with_context(|| "Unable to get issues.")?;
  1554. let fcp_map = if include_fcp_details {
  1555. crate::rfcbot::get_all_fcps().await?
  1556. } else {
  1557. HashMap::new()
  1558. };
  1559. let mut issues_decorator = Vec::new();
  1560. let re = regex::Regex::new("https://github.com/rust-lang/|/").unwrap();
  1561. let re_zulip_link = regex::Regex::new(r"\[stream\]:\s").unwrap();
  1562. for issue in issues {
  1563. let fcp_details = if include_fcp_details {
  1564. let repository_name = if let Some(repo) = issue.repository.get() {
  1565. repo.repository.clone()
  1566. } else {
  1567. let split = re.split(&issue.html_url).collect::<Vec<&str>>();
  1568. split[1].to_string()
  1569. };
  1570. let key = format!(
  1571. "rust-lang/{}:{}:{}",
  1572. repository_name, issue.number, issue.title,
  1573. );
  1574. if let Some(fcp) = fcp_map.get(&key) {
  1575. let bot_tracking_comment_html_url = format!(
  1576. "{}#issuecomment-{}",
  1577. issue.html_url, fcp.fcp.fk_bot_tracking_comment
  1578. );
  1579. let bot_tracking_comment_content = quote_reply(&fcp.status_comment.body);
  1580. let fk_initiating_comment = fcp.fcp.fk_initiating_comment;
  1581. let init_comment = issue
  1582. .get_comment(&client, fk_initiating_comment.try_into()?)
  1583. .await?;
  1584. Some(crate::actions::FCPDetails {
  1585. bot_tracking_comment_html_url,
  1586. bot_tracking_comment_content,
  1587. initiating_comment_html_url: init_comment.html_url.clone(),
  1588. initiating_comment_content: quote_reply(&init_comment.body),
  1589. })
  1590. } else {
  1591. None
  1592. }
  1593. } else {
  1594. None
  1595. };
  1596. let mcp_details = if include_mcp_details {
  1597. let first_comment = issue.get_first_comment(&client).await?;
  1598. let split = re_zulip_link
  1599. .split(&first_comment[0].body)
  1600. .collect::<Vec<&str>>();
  1601. let zulip_link = split.last().unwrap_or(&"#").to_string();
  1602. Some(crate::actions::MCPDetails { zulip_link })
  1603. } else {
  1604. None
  1605. };
  1606. issues_decorator.push(crate::actions::IssueDecorator {
  1607. title: issue.title.clone(),
  1608. number: issue.number,
  1609. html_url: issue.html_url.clone(),
  1610. repo_name: repo.name().to_owned(),
  1611. labels: issue
  1612. .labels
  1613. .iter()
  1614. .map(|l| l.name.as_ref())
  1615. .collect::<Vec<_>>()
  1616. .join(", "),
  1617. assignees: issue
  1618. .assignees
  1619. .iter()
  1620. .map(|u| u.login.as_ref())
  1621. .collect::<Vec<_>>()
  1622. .join(", "),
  1623. updated_at_hts: crate::actions::to_human(issue.updated_at),
  1624. fcp_details,
  1625. mcp_details,
  1626. });
  1627. }
  1628. Ok(issues_decorator)
  1629. }
  1630. }
  1631. #[derive(Debug, serde::Deserialize)]
  1632. #[serde(rename_all = "snake_case")]
  1633. pub enum CreateKind {
  1634. Branch,
  1635. Tag,
  1636. }
  1637. #[derive(Debug, serde::Deserialize)]
  1638. pub struct CreateEvent {
  1639. pub ref_type: CreateKind,
  1640. repository: Repository,
  1641. sender: User,
  1642. }
  1643. #[derive(Debug, serde::Deserialize)]
  1644. pub struct PushEvent {
  1645. #[serde(rename = "ref")]
  1646. pub git_ref: String,
  1647. repository: Repository,
  1648. sender: User,
  1649. }
  1650. /// An event triggered by a webhook.
  1651. #[derive(Debug)]
  1652. pub enum Event {
  1653. /// A Git branch or tag is created.
  1654. Create(CreateEvent),
  1655. /// A comment on an issue or PR.
  1656. ///
  1657. /// Can be:
  1658. /// - Regular comment on an issue or PR.
  1659. /// - A PR review.
  1660. /// - A comment on a PR review.
  1661. ///
  1662. /// These different scenarios are unified into the `IssueComment` variant
  1663. /// when triagebot receives the corresponding webhook event.
  1664. IssueComment(IssueCommentEvent),
  1665. /// Activity on an issue or PR.
  1666. Issue(IssuesEvent),
  1667. /// One or more commits are pushed to a repository branch or tag.
  1668. Push(PushEvent),
  1669. }
  1670. impl Event {
  1671. pub fn repo(&self) -> &Repository {
  1672. match self {
  1673. Event::Create(event) => &event.repository,
  1674. Event::IssueComment(event) => &event.repository,
  1675. Event::Issue(event) => &event.repository,
  1676. Event::Push(event) => &event.repository,
  1677. }
  1678. }
  1679. pub fn issue(&self) -> Option<&Issue> {
  1680. match self {
  1681. Event::Create(_) => None,
  1682. Event::IssueComment(event) => Some(&event.issue),
  1683. Event::Issue(event) => Some(&event.issue),
  1684. Event::Push(_) => None,
  1685. }
  1686. }
  1687. /// This will both extract from IssueComment events but also Issue events
  1688. pub fn comment_body(&self) -> Option<&str> {
  1689. match self {
  1690. Event::Create(_) => None,
  1691. Event::Issue(e) => Some(&e.issue.body),
  1692. Event::IssueComment(e) => Some(&e.comment.body),
  1693. Event::Push(_) => None,
  1694. }
  1695. }
  1696. /// This will both extract from IssueComment events but also Issue events
  1697. pub fn comment_from(&self) -> Option<&str> {
  1698. match self {
  1699. Event::Create(_) => None,
  1700. Event::Issue(e) => Some(&e.changes.as_ref()?.body.as_ref()?.from),
  1701. Event::IssueComment(e) => Some(&e.changes.as_ref()?.body.as_ref()?.from),
  1702. Event::Push(_) => None,
  1703. }
  1704. }
  1705. pub fn html_url(&self) -> Option<&str> {
  1706. match self {
  1707. Event::Create(_) => None,
  1708. Event::Issue(e) => Some(&e.issue.html_url),
  1709. Event::IssueComment(e) => Some(&e.comment.html_url),
  1710. Event::Push(_) => None,
  1711. }
  1712. }
  1713. pub fn user(&self) -> &User {
  1714. match self {
  1715. Event::Create(e) => &e.sender,
  1716. Event::Issue(e) => &e.issue.user,
  1717. Event::IssueComment(e) => &e.comment.user,
  1718. Event::Push(e) => &e.sender,
  1719. }
  1720. }
  1721. pub fn time(&self) -> Option<chrono::DateTime<FixedOffset>> {
  1722. match self {
  1723. Event::Create(_) => None,
  1724. Event::Issue(e) => Some(e.issue.created_at.into()),
  1725. Event::IssueComment(e) => Some(e.comment.updated_at.into()),
  1726. Event::Push(_) => None,
  1727. }
  1728. }
  1729. }
  1730. trait RequestSend: Sized {
  1731. fn configure(self, g: &GithubClient) -> Self;
  1732. }
  1733. impl RequestSend for RequestBuilder {
  1734. fn configure(self, g: &GithubClient) -> RequestBuilder {
  1735. let mut auth = HeaderValue::from_maybe_shared(format!("token {}", g.token)).unwrap();
  1736. auth.set_sensitive(true);
  1737. self.header(USER_AGENT, "rust-lang-triagebot")
  1738. .header(AUTHORIZATION, &auth)
  1739. }
  1740. }
  1741. /// Finds the token in the user's environment, panicking if no suitable token
  1742. /// can be found.
  1743. pub fn default_token_from_env() -> String {
  1744. match std::env::var("GITHUB_API_TOKEN") {
  1745. Ok(v) => return v,
  1746. Err(_) => (),
  1747. }
  1748. match get_token_from_git_config() {
  1749. Ok(v) => return v,
  1750. Err(_) => (),
  1751. }
  1752. panic!("could not find token in GITHUB_API_TOKEN or .gitconfig/github.oath-token")
  1753. }
  1754. fn get_token_from_git_config() -> anyhow::Result<String> {
  1755. let output = std::process::Command::new("git")
  1756. .arg("config")
  1757. .arg("--get")
  1758. .arg("github.oauth-token")
  1759. .output()?;
  1760. if !output.status.success() {
  1761. anyhow::bail!("error received executing `git`: {:?}", output.status);
  1762. }
  1763. let git_token = String::from_utf8(output.stdout)?.trim().to_string();
  1764. Ok(git_token)
  1765. }
  1766. #[derive(Clone)]
  1767. pub struct GithubClient {
  1768. token: String,
  1769. client: Client,
  1770. }
  1771. impl GithubClient {
  1772. pub fn new(client: Client, token: String) -> Self {
  1773. GithubClient { client, token }
  1774. }
  1775. pub fn new_with_default_token(client: Client) -> Self {
  1776. Self::new(client, default_token_from_env())
  1777. }
  1778. pub fn raw(&self) -> &Client {
  1779. &self.client
  1780. }
  1781. pub async fn raw_file(
  1782. &self,
  1783. repo: &str,
  1784. branch: &str,
  1785. path: &str,
  1786. ) -> anyhow::Result<Option<Bytes>> {
  1787. let url = format!(
  1788. "https://raw.githubusercontent.com/{}/{}/{}",
  1789. repo, branch, path
  1790. );
  1791. let req = self.get(&url);
  1792. let req_dbg = format!("{:?}", req);
  1793. let req = req
  1794. .build()
  1795. .with_context(|| format!("failed to build request {:?}", req_dbg))?;
  1796. let resp = self.client.execute(req).await.context(req_dbg.clone())?;
  1797. let status = resp.status();
  1798. let body = resp
  1799. .bytes()
  1800. .await
  1801. .with_context(|| format!("failed to read response body {req_dbg}"))?;
  1802. match status {
  1803. StatusCode::OK => Ok(Some(body)),
  1804. StatusCode::NOT_FOUND => Ok(None),
  1805. status => anyhow::bail!("failed to GET {}: {}", url, status),
  1806. }
  1807. }
  1808. /// Get the raw gist content from the URL of the HTML version of the gist:
  1809. ///
  1810. /// `html_url` looks like `https://gist.github.com/rust-play/7e80ca3b1ec7abe08f60c41aff91f060`.
  1811. ///
  1812. /// `filename` is the name of the file you want the content of.
  1813. pub async fn raw_gist_from_url(
  1814. &self,
  1815. html_url: &str,
  1816. filename: &str,
  1817. ) -> anyhow::Result<String> {
  1818. let url = html_url.replace("github.com", "githubusercontent.com") + "/raw/" + filename;
  1819. let response = self.raw().get(&url).send().await?;
  1820. response.text().await.context("raw gist from url")
  1821. }
  1822. fn get(&self, url: &str) -> RequestBuilder {
  1823. log::trace!("get {:?}", url);
  1824. self.client.get(url).configure(self)
  1825. }
  1826. fn patch(&self, url: &str) -> RequestBuilder {
  1827. log::trace!("patch {:?}", url);
  1828. self.client.patch(url).configure(self)
  1829. }
  1830. fn delete(&self, url: &str) -> RequestBuilder {
  1831. log::trace!("delete {:?}", url);
  1832. self.client.delete(url).configure(self)
  1833. }
  1834. fn post(&self, url: &str) -> RequestBuilder {
  1835. log::trace!("post {:?}", url);
  1836. self.client.post(url).configure(self)
  1837. }
  1838. #[allow(unused)]
  1839. fn put(&self, url: &str) -> RequestBuilder {
  1840. log::trace!("put {:?}", url);
  1841. self.client.put(url).configure(self)
  1842. }
  1843. pub async fn rust_commit(&self, sha: &str) -> Option<GithubCommit> {
  1844. let req = self.get(&format!(
  1845. "https://api.github.com/repos/rust-lang/rust/commits/{}",
  1846. sha
  1847. ));
  1848. match self.json(req).await {
  1849. Ok(r) => Some(r),
  1850. Err(e) => {
  1851. log::error!("Failed to query commit {:?}: {:?}", sha, e);
  1852. None
  1853. }
  1854. }
  1855. }
  1856. /// This does not retrieve all of them, only the last several.
  1857. pub async fn bors_commits(&self) -> Vec<GithubCommit> {
  1858. let req = self.get("https://api.github.com/repos/rust-lang/rust/commits?author=bors");
  1859. match self.json(req).await {
  1860. Ok(r) => r,
  1861. Err(e) => {
  1862. log::error!("Failed to query commit list: {:?}", e);
  1863. Vec::new()
  1864. }
  1865. }
  1866. }
  1867. /// Issues an ad-hoc GraphQL query.
  1868. pub async fn graphql_query<T: serde::de::DeserializeOwned>(
  1869. &self,
  1870. query: &str,
  1871. vars: serde_json::Value,
  1872. ) -> anyhow::Result<T> {
  1873. self.json(
  1874. self.post(Repository::GITHUB_GRAPHQL_API_URL)
  1875. .json(&serde_json::json!({
  1876. "query": query,
  1877. "variables": vars,
  1878. })),
  1879. )
  1880. .await
  1881. }
  1882. /// Returns the object ID of the given user.
  1883. ///
  1884. /// Returns `None` if the user doesn't exist.
  1885. pub async fn user_object_id(&self, user: &str) -> anyhow::Result<Option<String>> {
  1886. let user_info: serde_json::Value = self
  1887. .graphql_query(
  1888. "query($user:String!) {
  1889. user(login:$user) {
  1890. id
  1891. }
  1892. }",
  1893. serde_json::json!({
  1894. "user": user,
  1895. }),
  1896. )
  1897. .await?;
  1898. if let Some(id) = user_info["data"]["user"]["id"].as_str() {
  1899. return Ok(Some(id.to_string()));
  1900. }
  1901. if let Some(errors) = user_info["errors"].as_array() {
  1902. if errors
  1903. .iter()
  1904. .any(|err| err["type"].as_str().unwrap_or_default() == "NOT_FOUND")
  1905. {
  1906. return Ok(None);
  1907. }
  1908. let messages: Vec<_> = errors
  1909. .iter()
  1910. .map(|err| err["message"].as_str().unwrap_or_default())
  1911. .collect();
  1912. anyhow::bail!("failed to query user: {}", messages.join("\n"));
  1913. }
  1914. anyhow::bail!("query for user {user} failed, no error message? {user_info:?}");
  1915. }
  1916. /// Returns whether or not the given GitHub login has made any commits to
  1917. /// the given repo.
  1918. pub async fn is_new_contributor(&self, repo: &Repository, author: &str) -> bool {
  1919. let user_id = match self.user_object_id(author).await {
  1920. Ok(None) => return true,
  1921. Ok(Some(id)) => id,
  1922. Err(e) => {
  1923. log::warn!("failed to query user: {e:?}");
  1924. return true;
  1925. }
  1926. };
  1927. // Note: This only returns results for the default branch. That should
  1928. // be fine in most cases since I think it is rare for new users to
  1929. // make their first commit to a different branch.
  1930. //
  1931. // Note: This is using GraphQL because the
  1932. // `/repos/ORG/REPO/commits?author=AUTHOR` API was having problems not
  1933. // finding users (https://github.com/rust-lang/triagebot/issues/1689).
  1934. // The other possibility is the `/search/commits?q=repo:{}+author:{}`
  1935. // API, but that endpoint has a very limited rate limit, and doesn't
  1936. // work on forks. This GraphQL query seems to work fairly reliably,
  1937. // and seems to cost only 1 point.
  1938. match self
  1939. .graphql_query::<serde_json::Value>(
  1940. "query($repository_owner:String!, $repository_name:String!, $user_id:ID!) {
  1941. repository(owner: $repository_owner, name: $repository_name) {
  1942. defaultBranchRef {
  1943. target {
  1944. ... on Commit {
  1945. history(author: {id: $user_id}) {
  1946. totalCount
  1947. }
  1948. }
  1949. }
  1950. }
  1951. }
  1952. }",
  1953. serde_json::json!({
  1954. "repository_owner": repo.owner(),
  1955. "repository_name": repo.name(),
  1956. "user_id": user_id
  1957. }),
  1958. )
  1959. .await
  1960. {
  1961. Ok(c) => {
  1962. if let Some(c) = c["data"]["repository"]["defaultBranchRef"]["target"]["history"]
  1963. ["totalCount"]
  1964. .as_i64()
  1965. {
  1966. return c == 0;
  1967. }
  1968. log::warn!("new user query failed: {c:?}");
  1969. false
  1970. }
  1971. Err(e) => {
  1972. log::warn!(
  1973. "failed to search for user commits in {} for author {author}: {e:?}",
  1974. repo.full_name
  1975. );
  1976. // Using `false` since if there is some underlying problem, we
  1977. // don't need to spam everyone with the "new user" welcome
  1978. // message.
  1979. false
  1980. }
  1981. }
  1982. }
  1983. /// Returns information about a repository.
  1984. ///
  1985. /// The `full_name` should be something like `rust-lang/rust`.
  1986. pub async fn repository(&self, full_name: &str) -> anyhow::Result<Repository> {
  1987. let req = self.get(&format!("{}/repos/{full_name}", Repository::GITHUB_API_URL));
  1988. self.json(req)
  1989. .await
  1990. .with_context(|| format!("{} failed to get repo", full_name))
  1991. }
  1992. /// Get or create a [`Milestone`].
  1993. ///
  1994. /// This will not change the state if it already exists.
  1995. async fn get_or_create_milestone(
  1996. &self,
  1997. full_repo_name: &str,
  1998. title: &str,
  1999. state: &str,
  2000. ) -> anyhow::Result<Milestone> {
  2001. let url = format!(
  2002. "{}/repos/{full_repo_name}/milestones",
  2003. Repository::GITHUB_API_URL
  2004. );
  2005. let resp = self
  2006. .send_req(self.post(&url).json(&serde_json::json!({
  2007. "title": title,
  2008. "state": state,
  2009. })))
  2010. .await;
  2011. match resp {
  2012. Ok((body, _dbg)) => {
  2013. let milestone = serde_json::from_slice(&body)?;
  2014. log::trace!("Created milestone: {milestone:?}");
  2015. return Ok(milestone);
  2016. }
  2017. Err(e) => {
  2018. if e.downcast_ref::<reqwest::Error>().map_or(false, |e| {
  2019. matches!(e.status(), Some(StatusCode::UNPROCESSABLE_ENTITY))
  2020. }) {
  2021. // fall-through, it already exists
  2022. } else {
  2023. return Err(e.context(format!(
  2024. "failed to create milestone {url} with title {title}"
  2025. )));
  2026. }
  2027. }
  2028. }
  2029. // In the case where it already exists, we need to search for its number.
  2030. let mut page = 1;
  2031. loop {
  2032. let url = format!(
  2033. "{}/repos/{full_repo_name}/milestones?page={page}&state=all",
  2034. Repository::GITHUB_API_URL
  2035. );
  2036. let milestones: Vec<Milestone> = self
  2037. .json(self.get(&url))
  2038. .await
  2039. .with_context(|| format!("failed to get milestones {url} searching for {title}"))?;
  2040. if milestones.is_empty() {
  2041. anyhow::bail!("expected to find milestone with title {title}");
  2042. }
  2043. if let Some(milestone) = milestones.into_iter().find(|m| m.title == title) {
  2044. return Ok(milestone);
  2045. }
  2046. page += 1;
  2047. }
  2048. }
  2049. /// Set the milestone of an issue or PR.
  2050. async fn set_milestone(
  2051. &self,
  2052. full_repo_name: &str,
  2053. milestone: &Milestone,
  2054. issue_num: u64,
  2055. ) -> anyhow::Result<()> {
  2056. let url = format!(
  2057. "{}/repos/{full_repo_name}/issues/{issue_num}",
  2058. Repository::GITHUB_API_URL
  2059. );
  2060. self.send_req(self.patch(&url).json(&serde_json::json!({
  2061. "milestone": milestone.number
  2062. })))
  2063. .await
  2064. .with_context(|| format!("failed to set milestone for {url} to milestone {milestone:?}"))?;
  2065. Ok(())
  2066. }
  2067. }
  2068. #[derive(Debug, serde::Deserialize)]
  2069. pub struct GithubCommit {
  2070. pub sha: String,
  2071. pub commit: GithubCommitCommitField,
  2072. pub parents: Vec<Parent>,
  2073. }
  2074. #[derive(Debug, serde::Deserialize)]
  2075. pub struct GithubCommitCommitField {
  2076. pub author: GitUser,
  2077. pub message: String,
  2078. pub tree: GitCommitTree,
  2079. }
  2080. #[derive(Debug, serde::Deserialize)]
  2081. pub struct GitCommit {
  2082. pub sha: String,
  2083. pub author: GitUser,
  2084. pub message: String,
  2085. pub tree: GitCommitTree,
  2086. }
  2087. #[derive(Debug, serde::Deserialize)]
  2088. pub struct GitCommitTree {
  2089. pub sha: String,
  2090. }
  2091. #[derive(Debug, serde::Deserialize)]
  2092. pub struct GitTreeObject {
  2093. pub sha: String,
  2094. }
  2095. #[derive(Debug, serde::Serialize, serde::Deserialize)]
  2096. pub struct GitTreeEntry {
  2097. pub path: String,
  2098. pub mode: String,
  2099. #[serde(rename = "type")]
  2100. pub object_type: String,
  2101. pub sha: String,
  2102. }
  2103. pub struct RecentCommit {
  2104. pub title: String,
  2105. pub pr_num: Option<i32>,
  2106. pub oid: String,
  2107. pub committed_date: DateTime<Utc>,
  2108. }
  2109. #[derive(Debug, serde::Deserialize)]
  2110. pub struct GitUser {
  2111. pub date: DateTime<FixedOffset>,
  2112. }
  2113. #[derive(Debug, serde::Deserialize)]
  2114. pub struct Parent {
  2115. pub sha: String,
  2116. }
  2117. #[async_trait]
  2118. pub trait IssuesQuery {
  2119. async fn query<'a>(
  2120. &'a self,
  2121. repo: &'a Repository,
  2122. include_fcp_details: bool,
  2123. include_mcp_details: bool,
  2124. client: &'a GithubClient,
  2125. ) -> anyhow::Result<Vec<crate::actions::IssueDecorator>>;
  2126. }
  2127. pub struct LeastRecentlyReviewedPullRequests;
  2128. #[async_trait]
  2129. impl IssuesQuery for LeastRecentlyReviewedPullRequests {
  2130. async fn query<'a>(
  2131. &'a self,
  2132. repo: &'a Repository,
  2133. _include_fcp_details: bool,
  2134. _include_mcp_details: bool,
  2135. client: &'a GithubClient,
  2136. ) -> anyhow::Result<Vec<crate::actions::IssueDecorator>> {
  2137. use cynic::QueryBuilder;
  2138. use github_graphql::queries;
  2139. let repository_owner = repo.owner();
  2140. let repository_name = repo.name();
  2141. let mut prs: Vec<queries::PullRequest> = vec![];
  2142. let mut args = queries::LeastRecentlyReviewedPullRequestsArguments {
  2143. repository_owner,
  2144. repository_name,
  2145. after: None,
  2146. };
  2147. loop {
  2148. let query = queries::LeastRecentlyReviewedPullRequests::build(args.clone());
  2149. let req = client.post(Repository::GITHUB_GRAPHQL_API_URL);
  2150. let req = req.json(&query);
  2151. let data: cynic::GraphQlResponse<queries::LeastRecentlyReviewedPullRequests> =
  2152. client.json(req).await?;
  2153. if let Some(errors) = data.errors {
  2154. anyhow::bail!("There were graphql errors. {:?}", errors);
  2155. }
  2156. let repository = data
  2157. .data
  2158. .ok_or_else(|| anyhow::anyhow!("No data returned."))?
  2159. .repository
  2160. .ok_or_else(|| anyhow::anyhow!("No repository."))?;
  2161. prs.extend(repository.pull_requests.nodes);
  2162. let page_info = repository.pull_requests.page_info;
  2163. if !page_info.has_next_page || page_info.end_cursor.is_none() {
  2164. break;
  2165. }
  2166. args.after = page_info.end_cursor;
  2167. }
  2168. let mut prs: Vec<_> = prs
  2169. .into_iter()
  2170. .filter_map(|pr| {
  2171. if pr.is_draft {
  2172. return None;
  2173. }
  2174. let labels = pr
  2175. .labels
  2176. .map(|l| l.nodes)
  2177. .unwrap_or_default()
  2178. .into_iter()
  2179. .map(|node| node.name)
  2180. .collect::<Vec<_>>();
  2181. if !labels.iter().any(|label| label == "T-compiler") {
  2182. return None;
  2183. }
  2184. let labels = labels.join(", ");
  2185. let assignees: Vec<_> = pr
  2186. .assignees
  2187. .nodes
  2188. .into_iter()
  2189. .map(|user| user.login)
  2190. .collect();
  2191. let mut reviews = pr
  2192. .latest_reviews
  2193. .map(|connection| connection.nodes)
  2194. .unwrap_or_default()
  2195. .into_iter()
  2196. .filter_map(|node| {
  2197. let created_at = node.created_at;
  2198. node.author.map(|author| (author, created_at))
  2199. })
  2200. .map(|(author, created_at)| (author.login, created_at))
  2201. .collect::<Vec<_>>();
  2202. reviews.sort_by_key(|r| r.1);
  2203. let mut comments = pr
  2204. .comments
  2205. .nodes
  2206. .into_iter()
  2207. .filter_map(|node| {
  2208. let created_at = node.created_at;
  2209. node.author.map(|author| (author, created_at))
  2210. })
  2211. .map(|(author, created_at)| (author.login, created_at))
  2212. .filter(|comment| assignees.contains(&comment.0))
  2213. .collect::<Vec<_>>();
  2214. comments.sort_by_key(|c| c.1);
  2215. let updated_at = std::cmp::max(
  2216. reviews.last().map(|t| t.1).unwrap_or(pr.created_at),
  2217. comments.last().map(|t| t.1).unwrap_or(pr.created_at),
  2218. );
  2219. let assignees = assignees.join(", ");
  2220. Some((
  2221. updated_at,
  2222. pr.number as u64,
  2223. pr.title,
  2224. pr.url.0,
  2225. repository_name,
  2226. labels,
  2227. assignees,
  2228. ))
  2229. })
  2230. .collect();
  2231. prs.sort_by_key(|pr| pr.0);
  2232. let prs: Vec<_> = prs
  2233. .into_iter()
  2234. .take(50)
  2235. .map(
  2236. |(updated_at, number, title, html_url, repo_name, labels, assignees)| {
  2237. let updated_at_hts = crate::actions::to_human(updated_at);
  2238. crate::actions::IssueDecorator {
  2239. number,
  2240. title,
  2241. html_url,
  2242. repo_name: repo_name.to_string(),
  2243. labels,
  2244. assignees,
  2245. updated_at_hts,
  2246. fcp_details: None,
  2247. mcp_details: None,
  2248. }
  2249. },
  2250. )
  2251. .collect();
  2252. Ok(prs)
  2253. }
  2254. }
  2255. async fn project_items_by_status(
  2256. client: &GithubClient,
  2257. status_filter: impl Fn(Option<&str>) -> bool,
  2258. ) -> anyhow::Result<Vec<github_graphql::project_items::ProjectV2Item>> {
  2259. use cynic::QueryBuilder;
  2260. use github_graphql::project_items;
  2261. const DESIGN_MEETING_PROJECT: i32 = 31;
  2262. let mut args = project_items::Arguments {
  2263. project_number: DESIGN_MEETING_PROJECT,
  2264. after: None,
  2265. };
  2266. let mut all_items = vec![];
  2267. loop {
  2268. let query = project_items::Query::build(args.clone());
  2269. let req = client.post(Repository::GITHUB_GRAPHQL_API_URL);
  2270. let req = req.json(&query);
  2271. let data: cynic::GraphQlResponse<project_items::Query> = client.json(req).await?;
  2272. if let Some(errors) = data.errors {
  2273. anyhow::bail!("There were graphql errors. {:?}", errors);
  2274. }
  2275. let items = data
  2276. .data
  2277. .ok_or_else(|| anyhow!("No data returned."))?
  2278. .organization
  2279. .ok_or_else(|| anyhow!("Organization not found."))?
  2280. .project_v2
  2281. .ok_or_else(|| anyhow!("Project not found."))?
  2282. .items;
  2283. let filtered = items
  2284. .nodes
  2285. .ok_or_else(|| anyhow!("Malformed response."))?
  2286. .into_iter()
  2287. .flatten()
  2288. .filter(|item| status_filter(item.status()));
  2289. all_items.extend(filtered);
  2290. let page_info = items.page_info;
  2291. if !page_info.has_next_page || page_info.end_cursor.is_none() {
  2292. break;
  2293. }
  2294. args.after = page_info.end_cursor;
  2295. }
  2296. all_items.sort_by_key(|item| item.date());
  2297. Ok(all_items)
  2298. }
  2299. pub enum DesignMeetingStatus {
  2300. Proposed,
  2301. Scheduled,
  2302. Done,
  2303. Empty,
  2304. }
  2305. impl DesignMeetingStatus {
  2306. fn query_str(&self) -> Option<&str> {
  2307. match self {
  2308. DesignMeetingStatus::Proposed => Some("Needs triage"),
  2309. DesignMeetingStatus::Scheduled => Some("Scheduled"),
  2310. DesignMeetingStatus::Done => Some("Done"),
  2311. DesignMeetingStatus::Empty => None,
  2312. }
  2313. }
  2314. }
  2315. pub struct DesignMeetings {
  2316. pub with_status: DesignMeetingStatus,
  2317. }
  2318. #[async_trait]
  2319. impl IssuesQuery for DesignMeetings {
  2320. async fn query<'a>(
  2321. &'a self,
  2322. _repo: &'a Repository,
  2323. _include_fcp_details: bool,
  2324. _include_mcp_details: bool,
  2325. client: &'a GithubClient,
  2326. ) -> anyhow::Result<Vec<crate::actions::IssueDecorator>> {
  2327. use github_graphql::project_items::ProjectV2ItemContent;
  2328. let items =
  2329. project_items_by_status(client, |status| status == self.with_status.query_str())
  2330. .await?;
  2331. Ok(items
  2332. .into_iter()
  2333. .flat_map(|item| match item.content {
  2334. Some(ProjectV2ItemContent::Issue(issue)) => Some(crate::actions::IssueDecorator {
  2335. assignees: String::new(),
  2336. number: issue.number.try_into().unwrap(),
  2337. fcp_details: None,
  2338. mcp_details: None,
  2339. html_url: issue.url.0,
  2340. title: issue.title,
  2341. repo_name: String::new(),
  2342. labels: String::new(),
  2343. updated_at_hts: String::new(),
  2344. }),
  2345. _ => None,
  2346. })
  2347. .collect())
  2348. }
  2349. }
  2350. #[derive(Debug, serde::Deserialize)]
  2351. pub struct GitReference {
  2352. #[serde(rename = "ref")]
  2353. pub refname: String,
  2354. pub object: GitObject,
  2355. }
  2356. #[derive(Debug, serde::Deserialize)]
  2357. pub struct GitObject {
  2358. #[serde(rename = "type")]
  2359. pub object_type: String,
  2360. pub sha: String,
  2361. pub url: String,
  2362. }
  2363. #[derive(Debug, serde::Deserialize)]
  2364. pub struct Submodule {
  2365. pub name: String,
  2366. pub path: String,
  2367. pub sha: String,
  2368. pub submodule_git_url: String,
  2369. }
  2370. impl Submodule {
  2371. /// Returns the `Repository` this submodule points to.
  2372. ///
  2373. /// This assumes that the submodule is on GitHub.
  2374. pub async fn repository(&self, client: &GithubClient) -> anyhow::Result<Repository> {
  2375. let fullname = self
  2376. .submodule_git_url
  2377. .strip_prefix("https://github.com/")
  2378. .ok_or_else(|| {
  2379. anyhow::anyhow!(
  2380. "only github submodules supported, got {}",
  2381. self.submodule_git_url
  2382. )
  2383. })?
  2384. .strip_suffix(".git")
  2385. .ok_or_else(|| {
  2386. anyhow::anyhow!("expected .git suffix, got {}", self.submodule_git_url)
  2387. })?;
  2388. client.repository(fullname).await
  2389. }
  2390. }
  2391. #[cfg(test)]
  2392. mod tests {
  2393. use super::*;
  2394. #[test]
  2395. fn display_labels() {
  2396. let x = UnknownLabels {
  2397. labels: vec!["A-bootstrap".into(), "xxx".into()],
  2398. };
  2399. assert_eq!(x.to_string(), "Unknown labels: A-bootstrap, xxx");
  2400. }
  2401. #[test]
  2402. fn extract_one_file() {
  2403. let input = r##"\
  2404. diff --git a/triagebot.toml b/triagebot.toml
  2405. index fb9cee43b2d..b484c25ea51 100644
  2406. --- a/triagebot.toml
  2407. +++ b/triagebot.toml
  2408. @@ -114,6 +114,15 @@ trigger_files = [
  2409. "src/tools/rustdoc-themes",
  2410. ]
  2411. +[autolabel."T-compiler"]
  2412. +trigger_files = [
  2413. + # Source code
  2414. + "compiler",
  2415. +
  2416. + # Tests
  2417. + "src/test/ui",
  2418. +]
  2419. +
  2420. [notify-zulip."I-prioritize"]
  2421. zulip_stream = 245100 # #t-compiler/wg-prioritization/alerts
  2422. topic = "#{number} {title}"
  2423. "##;
  2424. let files: Vec<_> = parse_diff(input).into_iter().map(|d| d.path).collect();
  2425. assert_eq!(files, vec!["triagebot.toml".to_string()]);
  2426. }
  2427. #[test]
  2428. fn extract_several_files() {
  2429. let input = r##"\
  2430. diff --git a/library/stdarch b/library/stdarch
  2431. index b70ae88ef2a..cfba59fccd9 160000
  2432. --- a/library/stdarch
  2433. +++ b/library/stdarch
  2434. @@ -1 +1 @@
  2435. -Subproject commit b70ae88ef2a6c83acad0a1e83d5bd78f9655fd05
  2436. +Subproject commit cfba59fccd90b3b52a614120834320f764ab08d1
  2437. diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs
  2438. index 1fe4aa9023e..f0330f1e424 100644
  2439. --- a/src/librustdoc/clean/types.rs
  2440. +++ b/src/librustdoc/clean/types.rs
  2441. @@ -2322,3 +2322,4 @@ impl SubstParam {
  2442. if let Self::Lifetime(lt) = self { Some(lt) } else { None }
  2443. }
  2444. }
  2445. +
  2446. diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs
  2447. index c58310947d2..3b0854d4a9b 100644
  2448. --- a/src/librustdoc/core.rs
  2449. +++ b/src/librustdoc/core.rs
  2450. @@ -591,3 +591,4 @@ fn from(idx: u32) -> Self {
  2451. ImplTraitParam::ParamIndex(idx)
  2452. }
  2453. }
  2454. +
  2455. "##;
  2456. let files: Vec<_> = parse_diff(input).into_iter().map(|d| d.path).collect();
  2457. assert_eq!(
  2458. files,
  2459. vec![
  2460. "library/stdarch".to_string(),
  2461. "src/librustdoc/clean/types.rs".to_string(),
  2462. "src/librustdoc/core.rs".to_string(),
  2463. ]
  2464. )
  2465. }
  2466. }