github.rs 56 KB

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