12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726 |
- use anyhow::{anyhow, Context};
- use async_trait::async_trait;
- use bytes::Bytes;
- use chrono::{DateTime, FixedOffset, Utc};
- use futures::{future::BoxFuture, FutureExt};
- use hyper::header::HeaderValue;
- use once_cell::sync::OnceCell;
- use reqwest::header::{AUTHORIZATION, USER_AGENT};
- use reqwest::{Client, Request, RequestBuilder, Response, StatusCode};
- use std::collections::{HashMap, HashSet};
- use std::convert::TryInto;
- use std::{
- fmt,
- time::{Duration, SystemTime},
- };
- use tracing as log;
- #[derive(Debug, PartialEq, Eq, serde::Deserialize)]
- pub struct User {
- pub login: String,
- pub id: Option<i64>,
- }
- impl GithubClient {
- async fn send_req(&self, req: RequestBuilder) -> anyhow::Result<(Bytes, String)> {
- const MAX_ATTEMPTS: usize = 2;
- log::debug!("send_req with {:?}", req);
- let req_dbg = format!("{:?}", req);
- let req = req
- .build()
- .with_context(|| format!("building reqwest {}", req_dbg))?;
- let mut resp = self.client.execute(req.try_clone().unwrap()).await?;
- if let Some(sleep) = Self::needs_retry(&resp).await {
- resp = self.retry(req, sleep, MAX_ATTEMPTS).await?;
- }
- let maybe_err = resp.error_for_status_ref().err();
- let body = resp
- .bytes()
- .await
- .with_context(|| format!("failed to read response body {req_dbg}"))?;
- if let Some(e) = maybe_err {
- return Err(anyhow::Error::new(e))
- .with_context(|| format!("response: {}", String::from_utf8_lossy(&body)));
- }
- Ok((body, req_dbg))
- }
- async fn needs_retry(resp: &Response) -> Option<Duration> {
- const REMAINING: &str = "X-RateLimit-Remaining";
- const RESET: &str = "X-RateLimit-Reset";
- if resp.status().is_success() {
- return None;
- }
- let headers = resp.headers();
- if !(headers.contains_key(REMAINING) && headers.contains_key(RESET)) {
- return None;
- }
- // Weird github api behavior. It asks us to retry but also has a remaining count above 1
- // Try again immediately and hope for the best...
- if headers[REMAINING] != "0" {
- return Some(Duration::from_secs(0));
- }
- let reset_time = headers[RESET].to_str().unwrap().parse::<u64>().unwrap();
- Some(Duration::from_secs(Self::calc_sleep(reset_time) + 10))
- }
- fn calc_sleep(reset_time: u64) -> u64 {
- let epoch_time = SystemTime::UNIX_EPOCH.elapsed().unwrap().as_secs();
- reset_time.saturating_sub(epoch_time)
- }
- fn retry(
- &self,
- req: Request,
- sleep: Duration,
- remaining_attempts: usize,
- ) -> BoxFuture<Result<Response, reqwest::Error>> {
- #[derive(Debug, serde::Deserialize)]
- struct RateLimit {
- #[allow(unused)]
- pub limit: u64,
- pub remaining: u64,
- pub reset: u64,
- }
- #[derive(Debug, serde::Deserialize)]
- struct RateLimitResponse {
- pub resources: Resources,
- }
- #[derive(Debug, serde::Deserialize)]
- struct Resources {
- pub core: RateLimit,
- pub search: RateLimit,
- #[allow(unused)]
- pub graphql: RateLimit,
- #[allow(unused)]
- pub source_import: RateLimit,
- }
- log::warn!(
- "Retrying after {} seconds, remaining attepts {}",
- sleep.as_secs(),
- remaining_attempts,
- );
- async move {
- tokio::time::sleep(sleep).await;
- // check rate limit
- let rate_resp = self
- .client
- .execute(
- self.client
- .get(&format!("{}/rate_limit", self.api_url))
- .configure(self)
- .build()
- .unwrap(),
- )
- .await?;
- rate_resp.error_for_status_ref()?;
- let rate_limit_response = rate_resp.json::<RateLimitResponse>().await?;
- // Check url for search path because github has different rate limits for the search api
- let rate_limit = if req
- .url()
- .path_segments()
- .map(|mut segments| matches!(segments.next(), Some("search")))
- .unwrap_or(false)
- {
- rate_limit_response.resources.search
- } else {
- rate_limit_response.resources.core
- };
- // If we still don't have any more remaining attempts, try sleeping for the remaining
- // period of time
- if rate_limit.remaining == 0 {
- let sleep = Self::calc_sleep(rate_limit.reset);
- if sleep > 0 {
- tokio::time::sleep(Duration::from_secs(sleep)).await;
- }
- }
- let resp = self.client.execute(req.try_clone().unwrap()).await?;
- if let Some(sleep) = Self::needs_retry(&resp).await {
- if remaining_attempts > 0 {
- return self.retry(req, sleep, remaining_attempts - 1).await;
- }
- }
- Ok(resp)
- }
- .boxed()
- }
- pub async fn json<T>(&self, req: RequestBuilder) -> anyhow::Result<T>
- where
- T: serde::de::DeserializeOwned,
- {
- let (body, _req_dbg) = self.send_req(req).await?;
- Ok(serde_json::from_slice(&body)?)
- }
- }
- impl User {
- pub async fn current(client: &GithubClient) -> anyhow::Result<Self> {
- client
- .json(client.get(&format!("{}/user", client.api_url)))
- .await
- }
- pub async fn is_team_member<'a>(&'a self, client: &'a GithubClient) -> anyhow::Result<bool> {
- log::trace!("Getting team membership for {:?}", self.login);
- let permission = crate::team_data::teams(client).await?;
- let map = permission.teams;
- let is_triager = map
- .get("wg-triage")
- .map_or(false, |w| w.members.iter().any(|g| g.github == self.login));
- let is_pri_member = map
- .get("wg-prioritization")
- .map_or(false, |w| w.members.iter().any(|g| g.github == self.login));
- let is_async_member = map
- .get("wg-async")
- .map_or(false, |w| w.members.iter().any(|g| g.github == self.login));
- let in_all = map["all"].members.iter().any(|g| g.github == self.login);
- log::trace!(
- "{:?} is all?={:?}, triager?={:?}, prioritizer?={:?}, async?={:?}",
- self.login,
- in_all,
- is_triager,
- is_pri_member,
- is_async_member,
- );
- Ok(in_all || is_triager || is_pri_member || is_async_member)
- }
- // Returns the ID of the given user, if the user is in the `all` team.
- pub async fn get_id<'a>(&'a self, client: &'a GithubClient) -> anyhow::Result<Option<usize>> {
- let permission = crate::team_data::teams(client).await?;
- let map = permission.teams;
- Ok(map["all"]
- .members
- .iter()
- .find(|g| g.github == self.login)
- .map(|u| u.github_id))
- }
- }
- pub async fn get_team(
- client: &GithubClient,
- team: &str,
- ) -> anyhow::Result<Option<rust_team_data::v1::Team>> {
- let permission = crate::team_data::teams(client).await?;
- let mut map = permission.teams;
- Ok(map.swap_remove(team))
- }
- #[derive(PartialEq, Eq, Debug, Clone, serde::Deserialize)]
- pub struct Label {
- pub name: String,
- }
- /// An indicator used to differentiate between an issue and a pull request.
- ///
- /// Some webhook events include a `pull_request` field in the Issue object,
- /// and some don't. GitHub does include a few fields here, but they aren't
- /// needed at this time (merged_at, diff_url, html_url, patch_url, url).
- #[derive(Debug, serde::Deserialize)]
- pub struct PullRequestDetails {
- /// This is a slot to hold the diff for a PR.
- ///
- /// This will be filled in only once as an optimization since multiple
- /// handlers want to see PR changes, and getting the diff can be
- /// expensive.
- #[serde(skip)]
- files_changed: tokio::sync::OnceCell<Vec<FileDiff>>,
- }
- /// Representation of a diff to a single file.
- #[derive(Debug)]
- pub struct FileDiff {
- /// The full path of the file.
- pub path: String,
- /// The diff for the file.
- pub diff: String,
- }
- impl PullRequestDetails {
- pub fn new() -> PullRequestDetails {
- PullRequestDetails {
- files_changed: tokio::sync::OnceCell::new(),
- }
- }
- }
- /// An issue or pull request.
- ///
- /// For convenience, since issues and pull requests share most of their
- /// fields, this struct is used for both. The `pull_request` field can be used
- /// to determine which it is. Some fields are only available on pull requests
- /// (but not always, check the GitHub API for details).
- #[derive(Debug, serde::Deserialize)]
- pub struct Issue {
- pub number: u64,
- #[serde(deserialize_with = "opt_string")]
- pub body: String,
- created_at: chrono::DateTime<Utc>,
- pub updated_at: chrono::DateTime<Utc>,
- /// The SHA for a merge commit.
- ///
- /// This field is complicated, see the [Pull Request
- /// docs](https://docs.github.com/en/rest/pulls/pulls#get-a-pull-request)
- /// for details.
- #[serde(default)]
- pub merge_commit_sha: Option<String>,
- pub title: String,
- /// The common URL for viewing this issue or PR.
- ///
- /// Example: `https://github.com/octocat/Hello-World/pull/1347`
- pub html_url: String,
- pub user: User,
- pub labels: Vec<Label>,
- pub assignees: Vec<User>,
- /// Indicator if this is a pull request.
- ///
- /// This is `Some` if this is a PR (as opposed to an issue). Note that
- /// this does not always get filled in by GitHub, and must be manually
- /// populated (because some webhook events do not set it).
- pub pull_request: Option<PullRequestDetails>,
- /// Whether or not the pull request was merged.
- #[serde(default)]
- pub merged: bool,
- #[serde(default)]
- pub draft: bool,
- /// The API URL for discussion comments.
- ///
- /// Example: `https://api.github.com/repos/octocat/Hello-World/issues/1347/comments`
- comments_url: String,
- /// The repository for this issue.
- ///
- /// Note that this is constructed via the [`Issue::repository`] method.
- /// It is not deserialized from the GitHub API.
- #[serde(skip)]
- repository: OnceCell<IssueRepository>,
- /// The base commit for a PR (the branch of the destination repo).
- #[serde(default)]
- pub base: Option<CommitBase>,
- /// The head commit for a PR (the branch from the source repo).
- #[serde(default)]
- pub head: Option<CommitBase>,
- /// Whether it is open or closed.
- pub state: IssueState,
- }
- #[derive(Debug, serde::Deserialize, Eq, PartialEq)]
- #[serde(rename_all = "snake_case")]
- pub enum IssueState {
- Open,
- Closed,
- }
- /// Contains only the parts of `Issue` that are needed for turning the issue title into a Zulip
- /// topic.
- #[derive(Clone, Debug, PartialEq, Eq)]
- pub struct ZulipGitHubReference {
- pub number: u64,
- pub title: String,
- pub repository: IssueRepository,
- }
- impl ZulipGitHubReference {
- pub fn zulip_topic_reference(&self) -> String {
- let repo = &self.repository;
- if repo.organization == "rust-lang" {
- if repo.repository == "rust" {
- format!("#{}", self.number)
- } else {
- format!("{}#{}", repo.repository, self.number)
- }
- } else {
- format!("{}/{}#{}", repo.organization, repo.repository, self.number)
- }
- }
- }
- #[derive(Debug, serde::Deserialize)]
- pub struct Comment {
- #[serde(deserialize_with = "opt_string")]
- pub body: String,
- pub html_url: String,
- pub user: User,
- #[serde(alias = "submitted_at")] // for pull request reviews
- pub updated_at: chrono::DateTime<Utc>,
- #[serde(default, rename = "state")]
- pub pr_review_state: Option<PullRequestReviewState>,
- }
- #[derive(Debug, serde::Deserialize, Eq, PartialEq)]
- #[serde(rename_all = "snake_case")]
- pub enum PullRequestReviewState {
- Approved,
- ChangesRequested,
- Commented,
- Dismissed,
- Pending,
- }
- fn opt_string<'de, D>(deserializer: D) -> Result<String, D::Error>
- where
- D: serde::de::Deserializer<'de>,
- {
- use serde::de::Deserialize;
- match <Option<String>>::deserialize(deserializer) {
- Ok(v) => Ok(v.unwrap_or_default()),
- Err(e) => Err(e),
- }
- }
- #[derive(Debug)]
- pub enum AssignmentError {
- InvalidAssignee,
- Http(anyhow::Error),
- }
- #[derive(Debug)]
- pub enum Selection<'a, T: ?Sized> {
- All,
- One(&'a T),
- Except(&'a T),
- }
- impl fmt::Display for AssignmentError {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- match self {
- AssignmentError::InvalidAssignee => write!(f, "invalid assignee"),
- AssignmentError::Http(e) => write!(f, "cannot assign: {}", e),
- }
- }
- }
- impl std::error::Error for AssignmentError {}
- #[derive(Debug, Clone, PartialEq, Eq)]
- pub struct IssueRepository {
- pub organization: String,
- pub repository: String,
- }
- impl fmt::Display for IssueRepository {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write!(f, "{}/{}", self.organization, self.repository)
- }
- }
- impl IssueRepository {
- fn url(&self, client: &GithubClient) -> String {
- format!(
- "{}/repos/{}/{}",
- client.api_url, self.organization, self.repository
- )
- }
- fn full_repo_name(&self) -> String {
- format!("{}/{}", self.organization, self.repository)
- }
- async fn has_label(&self, client: &GithubClient, label: &str) -> anyhow::Result<bool> {
- #[allow(clippy::redundant_pattern_matching)]
- let url = format!("{}/labels/{}", self.url(client), label);
- match client.send_req(client.get(&url)).await {
- Ok(_) => Ok(true),
- Err(e) => {
- if e.downcast_ref::<reqwest::Error>()
- .map_or(false, |e| e.status() == Some(StatusCode::NOT_FOUND))
- {
- Ok(false)
- } else {
- Err(e)
- }
- }
- }
- }
- }
- #[derive(Debug)]
- pub(crate) struct UnknownLabels {
- labels: Vec<String>,
- }
- // NOTE: This is used to post the Github comment; make sure it's valid markdown.
- impl fmt::Display for UnknownLabels {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- write!(f, "Unknown labels: {}", &self.labels.join(", "))
- }
- }
- impl std::error::Error for UnknownLabels {}
- impl Issue {
- pub fn to_zulip_github_reference(&self) -> ZulipGitHubReference {
- ZulipGitHubReference {
- number: self.number,
- title: self.title.clone(),
- repository: self.repository().clone(),
- }
- }
- pub fn repository(&self) -> &IssueRepository {
- self.repository.get_or_init(|| {
- // https://api.github.com/repos/rust-lang/rust/issues/69257/comments
- log::trace!("get repository for {}", self.comments_url);
- let url = url::Url::parse(&self.comments_url).unwrap();
- let mut segments = url.path_segments().unwrap();
- let _comments = segments.next_back().unwrap();
- let _number = segments.next_back().unwrap();
- let _issues_or_prs = segments.next_back().unwrap();
- let repository = segments.next_back().unwrap();
- let organization = segments.next_back().unwrap();
- IssueRepository {
- organization: organization.into(),
- repository: repository.into(),
- }
- })
- }
- pub fn global_id(&self) -> String {
- format!("{}#{}", self.repository(), self.number)
- }
- pub fn is_pr(&self) -> bool {
- self.pull_request.is_some()
- }
- pub fn is_open(&self) -> bool {
- self.state == IssueState::Open
- }
- pub async fn get_comment(&self, client: &GithubClient, id: usize) -> anyhow::Result<Comment> {
- let comment_url = format!("{}/issues/comments/{}", self.repository().url(client), id);
- let comment = client.json(client.get(&comment_url)).await?;
- Ok(comment)
- }
- // returns an array of one element
- pub async fn get_first_comment(&self, client: &GithubClient) -> anyhow::Result<Vec<Comment>> {
- let comment_url = format!(
- "{}/issues/{}/comments?page=1&per_page=1",
- self.repository().url(client),
- self.number,
- );
- Ok(client
- .json::<Vec<Comment>>(client.get(&comment_url))
- .await?)
- }
- // returns an array of one element
- pub async fn get_first100_comments(
- &self,
- client: &GithubClient,
- ) -> anyhow::Result<Vec<Comment>> {
- let comment_url = format!(
- "{}/issues/{}/comments?page=1&per_page=100",
- self.repository().url(client),
- self.number,
- );
- Ok(client
- .json::<Vec<Comment>>(client.get(&comment_url))
- .await?)
- }
- pub async fn edit_body(&self, client: &GithubClient, body: &str) -> anyhow::Result<()> {
- let edit_url = format!("{}/issues/{}", self.repository().url(client), self.number);
- #[derive(serde::Serialize)]
- struct ChangedIssue<'a> {
- body: &'a str,
- }
- client
- .send_req(client.patch(&edit_url).json(&ChangedIssue { body }))
- .await
- .context("failed to edit issue body")?;
- Ok(())
- }
- pub async fn edit_comment(
- &self,
- client: &GithubClient,
- id: usize,
- new_body: &str,
- ) -> anyhow::Result<()> {
- let comment_url = format!("{}/issues/comments/{}", self.repository().url(client), id);
- #[derive(serde::Serialize)]
- struct NewComment<'a> {
- body: &'a str,
- }
- client
- .send_req(
- client
- .patch(&comment_url)
- .json(&NewComment { body: new_body }),
- )
- .await
- .context("failed to edit comment")?;
- Ok(())
- }
- pub async fn post_comment(&self, client: &GithubClient, body: &str) -> anyhow::Result<()> {
- #[derive(serde::Serialize)]
- struct PostComment<'a> {
- body: &'a str,
- }
- let comments_path = self
- .comments_url
- .strip_prefix("https://api.github.com")
- .expect("expected api host");
- let comments_url = format!("{}{comments_path}", client.api_url);
- client
- .send_req(client.post(&comments_url).json(&PostComment { body }))
- .await
- .context("failed to post comment")?;
- Ok(())
- }
- pub async fn remove_label(&self, client: &GithubClient, label: &str) -> anyhow::Result<()> {
- log::info!("remove_label from {}: {:?}", self.global_id(), label);
- // DELETE /repos/:owner/:repo/issues/:number/labels/{name}
- let url = format!(
- "{repo_url}/issues/{number}/labels/{name}",
- repo_url = self.repository().url(client),
- number = self.number,
- name = label,
- );
- if !self.labels().iter().any(|l| l.name == label) {
- log::info!(
- "remove_label from {}: {:?} already not present, skipping",
- self.global_id(),
- label
- );
- return Ok(());
- }
- client
- .send_req(client.delete(&url))
- .await
- .context("failed to delete label")?;
- Ok(())
- }
- pub async fn add_labels(
- &self,
- client: &GithubClient,
- labels: Vec<Label>,
- ) -> anyhow::Result<()> {
- log::info!("add_labels: {} +{:?}", self.global_id(), labels);
- // POST /repos/:owner/:repo/issues/:number/labels
- // repo_url = https://api.github.com/repos/Codertocat/Hello-World
- let url = format!(
- "{repo_url}/issues/{number}/labels",
- repo_url = self.repository().url(client),
- number = self.number
- );
- // Don't try to add labels already present on this issue.
- let labels = labels
- .into_iter()
- .filter(|l| !self.labels().contains(&l))
- .map(|l| l.name)
- .collect::<Vec<_>>();
- log::info!("add_labels: {} filtered to {:?}", self.global_id(), labels);
- if labels.is_empty() {
- return Ok(());
- }
- let mut unknown_labels = vec![];
- let mut known_labels = vec![];
- for label in labels {
- if !self.repository().has_label(client, &label).await? {
- unknown_labels.push(label);
- } else {
- known_labels.push(label);
- }
- }
- if !unknown_labels.is_empty() {
- return Err(UnknownLabels {
- labels: unknown_labels,
- }
- .into());
- }
- #[derive(serde::Serialize)]
- struct LabelsReq {
- labels: Vec<String>,
- }
- client
- .send_req(client.post(&url).json(&LabelsReq {
- labels: known_labels,
- }))
- .await
- .context("failed to add labels")?;
- Ok(())
- }
- pub fn labels(&self) -> &[Label] {
- &self.labels
- }
- pub fn contain_assignee(&self, user: &str) -> bool {
- self.assignees
- .iter()
- .any(|a| a.login.to_lowercase() == user.to_lowercase())
- }
- pub async fn remove_assignees(
- &self,
- client: &GithubClient,
- selection: Selection<'_, str>,
- ) -> Result<(), AssignmentError> {
- log::info!("remove {:?} assignees for {}", selection, self.global_id());
- let url = format!(
- "{repo_url}/issues/{number}/assignees",
- repo_url = self.repository().url(client),
- number = self.number
- );
- let assignees = match selection {
- Selection::All => self
- .assignees
- .iter()
- .map(|u| u.login.as_str())
- .collect::<Vec<_>>(),
- Selection::One(user) => vec![user],
- Selection::Except(user) => self
- .assignees
- .iter()
- .map(|u| u.login.as_str())
- .filter(|&u| u.to_lowercase() != user.to_lowercase())
- .collect::<Vec<_>>(),
- };
- #[derive(serde::Serialize)]
- struct AssigneeReq<'a> {
- assignees: &'a [&'a str],
- }
- client
- .send_req(client.delete(&url).json(&AssigneeReq {
- assignees: &assignees[..],
- }))
- .await
- .map_err(AssignmentError::Http)?;
- Ok(())
- }
- pub async fn add_assignee(
- &self,
- client: &GithubClient,
- user: &str,
- ) -> Result<(), AssignmentError> {
- log::info!("add_assignee {} for {}", user, self.global_id());
- let url = format!(
- "{repo_url}/issues/{number}/assignees",
- repo_url = self.repository().url(client),
- number = self.number
- );
- #[derive(serde::Serialize)]
- struct AssigneeReq<'a> {
- assignees: &'a [&'a str],
- }
- let result: Issue = client
- .json(client.post(&url).json(&AssigneeReq { assignees: &[user] }))
- .await
- .map_err(AssignmentError::Http)?;
- // Invalid assignees are silently ignored. We can just check if the user is now
- // contained in the assignees list.
- let success = result
- .assignees
- .iter()
- .any(|u| u.login.as_str().to_lowercase() == user.to_lowercase());
- if success {
- Ok(())
- } else {
- Err(AssignmentError::InvalidAssignee)
- }
- }
- pub async fn set_assignee(
- &self,
- client: &GithubClient,
- user: &str,
- ) -> Result<(), AssignmentError> {
- log::info!("set_assignee for {} to {}", self.global_id(), user);
- self.add_assignee(client, user).await?;
- self.remove_assignees(client, Selection::Except(user))
- .await?;
- Ok(())
- }
- /// Sets the milestone of the issue or PR.
- ///
- /// This will create the milestone if it does not exist. The new milestone
- /// will start in the "open" state.
- pub async fn set_milestone(&self, client: &GithubClient, title: &str) -> anyhow::Result<()> {
- log::trace!(
- "Setting milestone for rust-lang/rust#{} to {}",
- self.number,
- title
- );
- let full_repo_name = self.repository().full_repo_name();
- let milestone = client
- .get_or_create_milestone(&full_repo_name, title, "open")
- .await?;
- client
- .set_milestone(&full_repo_name, &milestone, self.number)
- .await?;
- Ok(())
- }
- pub async fn close(&self, client: &GithubClient) -> anyhow::Result<()> {
- let edit_url = format!("{}/issues/{}", self.repository().url(client), self.number);
- #[derive(serde::Serialize)]
- struct CloseIssue<'a> {
- state: &'a str,
- }
- client
- .send_req(
- client
- .patch(&edit_url)
- .json(&CloseIssue { state: "closed" }),
- )
- .await
- .context("failed to close issue")?;
- Ok(())
- }
- /// Returns the diff in this event, for Open and Synchronize events for now.
- ///
- /// Returns `None` if the issue is not a PR.
- pub async fn diff(&self, client: &GithubClient) -> anyhow::Result<Option<&[FileDiff]>> {
- let Some(pr) = &self.pull_request else {
- return Ok(None);
- };
- let (before, after) = if let (Some(base), Some(head)) = (&self.base, &self.head) {
- (&base.sha, &head.sha)
- } else {
- return Ok(None);
- };
- let diff = pr
- .files_changed
- .get_or_try_init::<anyhow::Error, _, _>(|| async move {
- let url = format!(
- "{}/compare/{before}...{after}",
- self.repository().url(client)
- );
- let mut req = client.get(&url);
- req = req.header("Accept", "application/vnd.github.v3.diff");
- let (diff, _) = client
- .send_req(req)
- .await
- .with_context(|| format!("failed to fetch diff comparison for {url}"))?;
- let body = String::from_utf8_lossy(&diff);
- Ok(parse_diff(&body))
- })
- .await?;
- Ok(Some(diff))
- }
- /// Returns the commits from this pull request (no commits are returned if this `Issue` is not
- /// a pull request).
- pub async fn commits(&self, client: &GithubClient) -> anyhow::Result<Vec<GithubCommit>> {
- if !self.is_pr() {
- return Ok(vec![]);
- }
- let mut commits = Vec::new();
- let mut page = 1;
- loop {
- let req = client.get(&format!(
- "{}/pulls/{}/commits?page={page}&per_page=100",
- self.repository().url(client),
- self.number
- ));
- let new: Vec<_> = client.json(req).await?;
- if new.is_empty() {
- break;
- }
- commits.extend(new);
- page += 1;
- }
- Ok(commits)
- }
- pub async fn files(&self, client: &GithubClient) -> anyhow::Result<Vec<PullRequestFile>> {
- if !self.is_pr() {
- return Ok(vec![]);
- }
- let req = client.get(&format!(
- "{}/pulls/{}/files",
- self.repository().url(client),
- self.number
- ));
- Ok(client.json(req).await?)
- }
- }
- #[derive(Debug, serde::Deserialize)]
- pub struct PullRequestFile {
- pub sha: String,
- pub filename: String,
- pub blob_url: String,
- }
- #[derive(Debug, serde::Deserialize)]
- pub struct Milestone {
- number: u64,
- title: String,
- }
- #[derive(Debug, serde::Deserialize)]
- pub struct ChangeInner {
- pub from: String,
- }
- #[derive(Debug, serde::Deserialize)]
- pub struct Changes {
- pub title: Option<ChangeInner>,
- pub body: Option<ChangeInner>,
- }
- #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
- #[serde(rename_all = "lowercase")]
- pub enum PullRequestReviewAction {
- Submitted,
- Edited,
- Dismissed,
- }
- /// A pull request review event.
- ///
- /// <https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#pull_request_review>
- #[derive(Debug, serde::Deserialize)]
- pub struct PullRequestReviewEvent {
- pub action: PullRequestReviewAction,
- pub pull_request: Issue,
- pub review: Comment,
- pub changes: Option<Changes>,
- pub repository: Repository,
- }
- #[derive(Debug, serde::Deserialize)]
- pub struct PullRequestReviewComment {
- pub action: IssueCommentAction,
- pub changes: Option<Changes>,
- #[serde(rename = "pull_request")]
- pub issue: Issue,
- pub comment: Comment,
- pub repository: Repository,
- }
- #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
- #[serde(rename_all = "lowercase")]
- pub enum IssueCommentAction {
- Created,
- Edited,
- Deleted,
- }
- #[derive(Debug, serde::Deserialize)]
- pub struct IssueCommentEvent {
- pub action: IssueCommentAction,
- pub changes: Option<Changes>,
- pub issue: Issue,
- pub comment: Comment,
- pub repository: Repository,
- }
- #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
- #[serde(rename_all = "snake_case", tag = "action")]
- pub enum IssuesAction {
- Opened,
- Edited,
- Deleted,
- Transferred,
- Pinned,
- Unpinned,
- Closed,
- Reopened,
- Assigned,
- Unassigned,
- Labeled {
- /// The label added from the issue
- label: Label,
- },
- Unlabeled {
- /// The label removed from the issue
- label: Label,
- },
- Locked,
- Unlocked,
- Milestoned,
- Demilestoned,
- ReviewRequested {
- /// The person requested to review the pull request
- requested_reviewer: User,
- },
- ReviewRequestRemoved,
- ReadyForReview,
- Synchronize,
- ConvertedToDraft,
- AutoMergeEnabled,
- AutoMergeDisabled,
- }
- #[derive(Debug, serde::Deserialize)]
- pub struct IssuesEvent {
- #[serde(flatten)]
- pub action: IssuesAction,
- #[serde(alias = "pull_request")]
- pub issue: Issue,
- pub changes: Option<Changes>,
- pub repository: Repository,
- /// The GitHub user that triggered the event.
- pub sender: User,
- }
- #[derive(Debug, serde::Deserialize)]
- struct PullRequestEventFields {}
- #[derive(Clone, Debug, serde::Deserialize)]
- pub struct CommitBase {
- pub sha: String,
- #[serde(rename = "ref")]
- pub git_ref: String,
- pub repo: Repository,
- }
- pub fn parse_diff(diff: &str) -> Vec<FileDiff> {
- // This does not properly handle filenames with spaces.
- let re = regex::Regex::new("(?m)^diff --git .* b/(.*)").unwrap();
- let mut files: Vec<_> = re
- .captures_iter(diff)
- .map(|cap| {
- let start = cap.get(0).unwrap().start();
- let path = cap.get(1).unwrap().as_str().to_string();
- (start, path)
- })
- .collect();
- // Break the list up into (start, end) pairs starting from the "diff --git" line.
- files.push((diff.len(), String::new()));
- files
- .windows(2)
- .map(|w| {
- let (start, end) = (&w[0], &w[1]);
- FileDiff {
- path: start.1.clone(),
- diff: diff[start.0..end.0].to_string(),
- }
- })
- .collect()
- }
- #[derive(Debug, serde::Deserialize)]
- pub struct IssueSearchResult {
- pub total_count: usize,
- pub incomplete_results: bool,
- pub items: Vec<Issue>,
- }
- #[derive(Clone, Debug, serde::Deserialize)]
- pub struct Repository {
- pub full_name: String,
- pub default_branch: String,
- #[serde(default)]
- pub fork: bool,
- pub parent: Option<Box<Repository>>,
- }
- #[derive(Copy, Clone)]
- struct Ordering<'a> {
- pub sort: &'a str,
- pub direction: &'a str,
- pub per_page: &'a str,
- pub page: usize,
- }
- impl Repository {
- fn url(&self, client: &GithubClient) -> String {
- format!("{}/repos/{}", client.api_url, self.full_name)
- }
- pub fn owner(&self) -> &str {
- self.full_name.split_once('/').unwrap().0
- }
- pub fn name(&self) -> &str {
- self.full_name.split_once('/').unwrap().1
- }
- pub async fn get_issues<'a>(
- &self,
- client: &GithubClient,
- query: &Query<'a>,
- ) -> anyhow::Result<Vec<Issue>> {
- let Query {
- filters,
- include_labels,
- exclude_labels,
- } = query;
- let mut ordering = Ordering {
- sort: "created",
- direction: "asc",
- per_page: "100",
- page: 1,
- };
- let filters: Vec<_> = filters
- .clone()
- .into_iter()
- .filter(|(key, val)| {
- match *key {
- "sort" => ordering.sort = val,
- "direction" => ordering.direction = val,
- "per_page" => ordering.per_page = val,
- _ => return true,
- };
- false
- })
- .collect();
- // `is: pull-request` indicates the query to retrieve PRs only
- let is_pr = filters
- .iter()
- .any(|&(key, value)| key == "is" && value == "pull-request");
- // There are some cases that can only be handled by the search API:
- // 1. When using negating label filters (exclude_labels)
- // 2. When there's a key parameter key=no
- // 3. When the query is to retrieve PRs only and there are label filters
- //
- // Check https://docs.github.com/en/rest/reference/search#search-issues-and-pull-requests
- // for more information
- let use_search_api = !exclude_labels.is_empty()
- || filters.iter().any(|&(key, _)| key == "no")
- || is_pr && !include_labels.is_empty();
- // If there are more than `per_page` of issues, we need to paginate
- let mut issues = vec![];
- loop {
- let url = if use_search_api {
- self.build_search_issues_url(
- client,
- &filters,
- include_labels,
- exclude_labels,
- ordering,
- )
- } else if is_pr {
- self.build_pulls_url(client, &filters, include_labels, ordering)
- } else {
- self.build_issues_url(client, &filters, include_labels, ordering)
- };
- let result = client.get(&url);
- if use_search_api {
- let result = client
- .json::<IssueSearchResult>(result)
- .await
- .with_context(|| format!("failed to list issues from {}", url))?;
- issues.extend(result.items);
- if issues.len() < result.total_count {
- ordering.page += 1;
- continue;
- }
- } else {
- // FIXME: paginate with non-search
- issues = client
- .json(result)
- .await
- .with_context(|| format!("failed to list issues from {}", url))?
- }
- break;
- }
- Ok(issues)
- }
- fn build_issues_url(
- &self,
- client: &GithubClient,
- filters: &Vec<(&str, &str)>,
- include_labels: &Vec<&str>,
- ordering: Ordering<'_>,
- ) -> String {
- self.build_endpoint_url(client, "issues", filters, include_labels, ordering)
- }
- fn build_pulls_url(
- &self,
- client: &GithubClient,
- filters: &Vec<(&str, &str)>,
- include_labels: &Vec<&str>,
- ordering: Ordering<'_>,
- ) -> String {
- self.build_endpoint_url(client, "pulls", filters, include_labels, ordering)
- }
- fn build_endpoint_url(
- &self,
- client: &GithubClient,
- endpoint: &str,
- filters: &Vec<(&str, &str)>,
- include_labels: &Vec<&str>,
- ordering: Ordering<'_>,
- ) -> String {
- let filters = filters
- .iter()
- .map(|(key, val)| format!("{}={}", key, val))
- .chain(std::iter::once(format!(
- "labels={}",
- include_labels.join(",")
- )))
- .chain(std::iter::once("filter=all".to_owned()))
- .chain(std::iter::once(format!("sort={}", ordering.sort,)))
- .chain(std::iter::once(
- format!("direction={}", ordering.direction,),
- ))
- .chain(std::iter::once(format!("per_page={}", ordering.per_page,)))
- .collect::<Vec<_>>()
- .join("&");
- format!(
- "{}/repos/{}/{}?{}",
- client.api_url, self.full_name, endpoint, filters
- )
- }
- fn build_search_issues_url(
- &self,
- client: &GithubClient,
- filters: &Vec<(&str, &str)>,
- include_labels: &Vec<&str>,
- exclude_labels: &Vec<&str>,
- ordering: Ordering<'_>,
- ) -> String {
- let filters = filters
- .iter()
- .filter(|&&(key, val)| !(key == "state" && val == "all"))
- .map(|(key, val)| format!("{}:{}", key, val))
- .chain(
- include_labels
- .iter()
- .map(|label| format!("label:{}", label)),
- )
- .chain(
- exclude_labels
- .iter()
- .map(|label| format!("-label:{}", label)),
- )
- .chain(std::iter::once(format!("repo:{}", self.full_name)))
- .collect::<Vec<_>>()
- .join("+");
- format!(
- "{}/search/issues?q={}&sort={}&order={}&per_page={}&page={}",
- client.api_url,
- filters,
- ordering.sort,
- ordering.direction,
- ordering.per_page,
- ordering.page,
- )
- }
- /// Returns a list of commits between the SHA ranges of start (exclusive)
- /// and end (inclusive).
- pub async fn commits_in_range(
- &self,
- client: &GithubClient,
- start: &str,
- end: &str,
- ) -> anyhow::Result<Vec<GithubCommit>> {
- let mut commits = Vec::new();
- let mut page = 1;
- loop {
- let url = format!(
- "{}/commits?sha={end}&per_page=100&page={page}",
- self.url(client)
- );
- let mut this_page: Vec<GithubCommit> = client
- .json(client.get(&url))
- .await
- .with_context(|| format!("failed to fetch commits for {url}"))?;
- if let Some(idx) = this_page.iter().position(|commit| commit.sha == start) {
- this_page.truncate(idx);
- commits.extend(this_page);
- return Ok(commits);
- } else {
- commits.extend(this_page);
- }
- page += 1;
- }
- }
- /// Retrieves a git commit for the given SHA.
- pub async fn git_commit(&self, client: &GithubClient, sha: &str) -> anyhow::Result<GitCommit> {
- let url = format!("{}/git/commits/{sha}", self.url(client));
- client
- .json(client.get(&url))
- .await
- .with_context(|| format!("{} failed to get git commit {sha}", self.full_name))
- }
- /// Creates a new commit.
- pub async fn create_commit(
- &self,
- client: &GithubClient,
- message: &str,
- parents: &[&str],
- tree: &str,
- ) -> anyhow::Result<GitCommit> {
- let url = format!("{}/git/commits", self.url(client));
- client
- .json(client.post(&url).json(&serde_json::json!({
- "message": message,
- "parents": parents,
- "tree": tree,
- })))
- .await
- .with_context(|| format!("{} failed to create commit for tree {tree}", self.full_name))
- }
- /// Retrieves a git reference for the given refname.
- pub async fn get_reference(
- &self,
- client: &GithubClient,
- refname: &str,
- ) -> anyhow::Result<GitReference> {
- let url = format!("{}/git/ref/{}", self.url(client), refname);
- client
- .json(client.get(&url))
- .await
- .with_context(|| format!("{} failed to get git reference {refname}", self.full_name))
- }
- /// Updates an existing git reference to a new SHA.
- pub async fn update_reference(
- &self,
- client: &GithubClient,
- refname: &str,
- sha: &str,
- ) -> anyhow::Result<GitReference> {
- let url = format!("{}/git/refs/{}", self.url(client), refname);
- client
- .json(client.patch(&url).json(&serde_json::json!({
- "sha": sha,
- "force": true,
- })))
- .await
- .with_context(|| {
- format!(
- "{} failed to update reference {refname} to {sha}",
- self.full_name
- )
- })
- }
- /// Returns a list of recent commits on the given branch.
- ///
- /// Returns results in the OID range `oldest` (exclusive) to `newest`
- /// (inclusive).
- pub async fn recent_commits(
- &self,
- client: &GithubClient,
- branch: &str,
- oldest: &str,
- newest: &str,
- ) -> anyhow::Result<Vec<RecentCommit>> {
- // This is used to deduplicate the results (so that a PR with multiple
- // commits will only show up once).
- let mut prs_seen = HashSet::new();
- let mut recent_commits = Vec::new(); // This is the final result.
- use cynic::QueryBuilder;
- use github_graphql::docs_update_queries::{
- GitObject, RecentCommits, RecentCommitsArguments,
- };
- let mut args = RecentCommitsArguments {
- branch,
- name: self.name(),
- owner: self.owner(),
- after: None,
- };
- let mut found_newest = false;
- let mut found_oldest = false;
- // This simulates --first-parent. We only care about top-level commits.
- // Unfortunately the GitHub API doesn't provide anything like that.
- let mut next_first_parent = None;
- // Search for `oldest` within 3 pages (300 commits).
- for _ in 0..3 {
- let query = RecentCommits::build(args.clone());
- let data = client
- .json::<cynic::GraphQlResponse<RecentCommits>>(
- client.post(&client.graphql_url).json(&query),
- )
- .await
- .with_context(|| {
- format!(
- "{} failed to get recent commits branch={branch}",
- self.full_name
- )
- })?;
- if let Some(errors) = data.errors {
- anyhow::bail!("There were graphql errors. {:?}", errors);
- }
- let target = data
- .data
- .ok_or_else(|| anyhow::anyhow!("No data returned."))?
- .repository
- .ok_or_else(|| anyhow::anyhow!("No repository."))?
- .ref_
- .ok_or_else(|| anyhow::anyhow!("No ref."))?
- .target
- .ok_or_else(|| anyhow::anyhow!("No target."))?;
- let commit = match target {
- GitObject::Commit(commit) => commit,
- _ => anyhow::bail!("unexpected target type {target:?}"),
- };
- let commits = commit
- .history
- .nodes
- .into_iter()
- // Don't include anything newer than `newest`
- .skip_while(|node| {
- if found_newest || node.oid.0 == newest {
- found_newest = true;
- false
- } else {
- // This should only happen if there is a commit that arrives
- // between the time that `update_submodules` fetches the latest
- // ref, and this runs. This window should be a few seconds, so it
- // should be unlikely. This warning is here in case my assumptions
- // about how things work is not correct.
- tracing::warn!(
- "unexpected race with submodule history, newest oid={newest} skipping oid={}",
- node.oid.0
- );
- true
- }
- })
- // Skip nodes that aren't the first parent
- .filter(|node| {
- let this_first_parent = node.parents.nodes
- .first()
- .map(|parent| parent.oid.0.clone());
- match &next_first_parent {
- Some(first_parent) => {
- if first_parent == &node.oid.0 {
- // Found the next first parent, include it and
- // set next_first_parent to look for this
- // commit's first parent.
- next_first_parent = this_first_parent;
- true
- } else {
- // Still looking for the next first parent.
- false
- }
- }
- None => {
- // First commit.
- next_first_parent = this_first_parent;
- true
- }
- }
- })
- // Stop once reached the `oldest` commit
- .take_while(|node| {
- if node.oid.0 == oldest {
- found_oldest = true;
- false
- } else {
- true
- }
- })
- .filter_map(|node| {
- // Determine if this is associated with a PR or not.
- match node.associated_pull_requests
- // Get the first PR (we only care about one)
- .and_then(|mut pr| pr.nodes.pop()) {
- Some(pr) => {
- // Only include a PR once
- if prs_seen.insert(pr.number) {
- Some(RecentCommit {
- pr_num: Some(pr.number),
- title: pr.title,
- oid: node.oid.0.clone(),
- committed_date: node.committed_date,
- })
- } else {
- None
- }
- }
- None => {
- // This is an unassociated commit, possibly
- // created without a PR.
- Some(RecentCommit {
- pr_num: None,
- title: node.message_headline,
- oid: node.oid.0,
- committed_date: node.committed_date,
- })
- }
- }
- });
- recent_commits.extend(commits);
- let page_info = commit.history.page_info;
- if found_oldest || !page_info.has_next_page || page_info.end_cursor.is_none() {
- break;
- }
- args.after = page_info.end_cursor;
- }
- if !found_oldest {
- // This should probably do something more than log a warning, but
- // I don't think it is too important at this time (the log message
- // is only informational, and this should be unlikely to happen).
- tracing::warn!(
- "{} failed to find oldest commit sha={oldest} branch={branch}",
- self.full_name
- );
- }
- Ok(recent_commits)
- }
- /// Creates a new git tree based on another tree.
- pub async fn update_tree(
- &self,
- client: &GithubClient,
- base_tree: &str,
- tree: &[GitTreeEntry],
- ) -> anyhow::Result<GitTreeObject> {
- let url = format!("{}/git/trees", self.url(client));
- client
- .json(client.post(&url).json(&serde_json::json!({
- "base_tree": base_tree,
- "tree": tree,
- })))
- .await
- .with_context(|| {
- format!(
- "{} failed to update tree with base {base_tree}",
- self.full_name
- )
- })
- }
- /// Returns information about the git submodule at the given path.
- ///
- /// `refname` is the ref to use for fetching information. If `None`, will
- /// use the latest version on the default branch.
- pub async fn submodule(
- &self,
- client: &GithubClient,
- path: &str,
- refname: Option<&str>,
- ) -> anyhow::Result<Submodule> {
- let mut url = format!("{}/contents/{}", self.url(client), path);
- if let Some(refname) = refname {
- url.push_str("?ref=");
- url.push_str(refname);
- }
- client.json(client.get(&url)).await.with_context(|| {
- format!(
- "{} failed to get submodule path={path} refname={refname:?}",
- self.full_name
- )
- })
- }
- /// Creates a new PR.
- pub async fn new_pr(
- &self,
- client: &GithubClient,
- title: &str,
- head: &str,
- base: &str,
- body: &str,
- ) -> anyhow::Result<Issue> {
- let url = format!("{}/pulls", self.url(client));
- let mut issue: Issue = client
- .json(client.post(&url).json(&serde_json::json!({
- "title": title,
- "head": head,
- "base": base,
- "body": body,
- })))
- .await
- .with_context(|| {
- format!(
- "{} failed to create a new PR head={head} base={base} title={title}",
- self.full_name
- )
- })?;
- issue.pull_request = Some(PullRequestDetails::new());
- Ok(issue)
- }
- /// Synchronize a branch (in a forked repository) by pulling in its upstream contents.
- ///
- /// **Warning**: This will to a force update if there are conflicts.
- pub async fn merge_upstream(&self, client: &GithubClient, branch: &str) -> anyhow::Result<()> {
- let url = format!("{}/merge-upstream", self.url(client));
- let merge_error = match client
- .send_req(client.post(&url).json(&serde_json::json!({
- "branch": branch,
- })))
- .await
- {
- Ok(_) => return Ok(()),
- Err(e) => {
- if e.downcast_ref::<reqwest::Error>().map_or(false, |e| {
- matches!(
- e.status(),
- Some(StatusCode::UNPROCESSABLE_ENTITY | StatusCode::CONFLICT)
- )
- }) {
- e
- } else {
- return Err(e);
- }
- }
- };
- // 409 is a clear error that there is a merge conflict.
- // However, I don't understand how/why 422 might happen. The docs don't really say.
- // The gh cli falls back to trying to force a sync, so let's try that.
- log::info!(
- "{} failed to merge upstream branch {branch}, trying force sync: {merge_error:?}",
- self.full_name
- );
- let parent = self.parent.as_ref().ok_or_else(|| {
- anyhow::anyhow!(
- "{} failed to merge upstream branch {branch}, \
- force sync could not determine parent",
- self.full_name
- )
- })?;
- // Note: I'm not sure how to handle the case where the branch name
- // differs to the upstream. For example, if I create a branch off
- // master in my fork, somehow GitHub knows that my branch should push
- // to upstream/master (not upstream/my-branch-name). I can't find a
- // way to find that branch name. Perhaps GitHub assumes it is the
- // default branch if there is no matching branch name?
- let branch_ref = format!("heads/{branch}");
- let latest_parent_commit = parent
- .get_reference(client, &branch_ref)
- .await
- .with_context(|| {
- format!(
- "failed to get head branch {branch} when merging upstream to {}",
- self.full_name
- )
- })?;
- let sha = latest_parent_commit.object.sha;
- self.update_reference(client, &branch_ref, &sha)
- .await
- .with_context(|| {
- format!(
- "failed to force update {branch} to {sha} for {}",
- self.full_name
- )
- })?;
- Ok(())
- }
- /// Get or create a [`Milestone`].
- ///
- /// This will not change the state if it already exists.
- pub async fn get_or_create_milestone(
- &self,
- client: &GithubClient,
- title: &str,
- state: &str,
- ) -> anyhow::Result<Milestone> {
- client
- .get_or_create_milestone(&self.full_name, title, state)
- .await
- }
- /// Set the milestone of an issue or PR.
- pub async fn set_milestone(
- &self,
- client: &GithubClient,
- milestone: &Milestone,
- issue_num: u64,
- ) -> anyhow::Result<()> {
- client
- .set_milestone(&self.full_name, milestone, issue_num)
- .await
- }
- pub async fn get_issue(&self, client: &GithubClient, issue_num: u64) -> anyhow::Result<Issue> {
- let url = format!("{}/pulls/{issue_num}", self.url(client));
- client
- .json(client.get(&url))
- .await
- .with_context(|| format!("{} failed to get issue {issue_num}", self.full_name))
- }
- }
- pub struct Query<'a> {
- // key/value filter
- pub filters: Vec<(&'a str, &'a str)>,
- pub include_labels: Vec<&'a str>,
- pub exclude_labels: Vec<&'a str>,
- }
- fn quote_reply(markdown: &str) -> String {
- if markdown.is_empty() {
- String::from("*No content*")
- } else {
- format!("\n\t> {}", markdown.replace("\n", "\n\t> "))
- }
- }
- #[async_trait]
- impl<'q> IssuesQuery for Query<'q> {
- async fn query<'a>(
- &'a self,
- repo: &'a Repository,
- include_fcp_details: bool,
- include_mcp_details: bool,
- client: &'a GithubClient,
- ) -> anyhow::Result<Vec<crate::actions::IssueDecorator>> {
- let issues = repo
- .get_issues(&client, self)
- .await
- .with_context(|| "Unable to get issues.")?;
- let fcp_map = if include_fcp_details {
- crate::rfcbot::get_all_fcps().await?
- } else {
- HashMap::new()
- };
- let mut issues_decorator = Vec::new();
- let re = regex::Regex::new("https://github.com/rust-lang/|/").unwrap();
- let re_zulip_link = regex::Regex::new(r"\[stream\]:\s").unwrap();
- for issue in issues {
- let fcp_details = if include_fcp_details {
- let repository_name = if let Some(repo) = issue.repository.get() {
- repo.repository.clone()
- } else {
- let split = re.split(&issue.html_url).collect::<Vec<&str>>();
- split[1].to_string()
- };
- let key = format!(
- "rust-lang/{}:{}:{}",
- repository_name, issue.number, issue.title,
- );
- if let Some(fcp) = fcp_map.get(&key) {
- let bot_tracking_comment_html_url = format!(
- "{}#issuecomment-{}",
- issue.html_url, fcp.fcp.fk_bot_tracking_comment
- );
- let bot_tracking_comment_content = quote_reply(&fcp.status_comment.body);
- let fk_initiating_comment = fcp.fcp.fk_initiating_comment;
- let init_comment = issue
- .get_comment(&client, fk_initiating_comment.try_into()?)
- .await?;
- Some(crate::actions::FCPDetails {
- bot_tracking_comment_html_url,
- bot_tracking_comment_content,
- initiating_comment_html_url: init_comment.html_url.clone(),
- initiating_comment_content: quote_reply(&init_comment.body),
- })
- } else {
- None
- }
- } else {
- None
- };
- let mcp_details = if include_mcp_details {
- let first_comment = issue.get_first_comment(&client).await?;
- let split = re_zulip_link
- .split(&first_comment[0].body)
- .collect::<Vec<&str>>();
- let zulip_link = split.last().unwrap_or(&"#").to_string();
- Some(crate::actions::MCPDetails { zulip_link })
- } else {
- None
- };
- issues_decorator.push(crate::actions::IssueDecorator {
- title: issue.title.clone(),
- number: issue.number,
- html_url: issue.html_url.clone(),
- repo_name: repo.name().to_owned(),
- labels: issue
- .labels
- .iter()
- .map(|l| l.name.as_ref())
- .collect::<Vec<_>>()
- .join(", "),
- assignees: issue
- .assignees
- .iter()
- .map(|u| u.login.as_ref())
- .collect::<Vec<_>>()
- .join(", "),
- updated_at_hts: crate::actions::to_human(issue.updated_at),
- fcp_details,
- mcp_details,
- });
- }
- Ok(issues_decorator)
- }
- }
- #[derive(Debug, serde::Deserialize)]
- #[serde(rename_all = "snake_case")]
- pub enum CreateKind {
- Branch,
- Tag,
- }
- #[derive(Debug, serde::Deserialize)]
- pub struct CreateEvent {
- pub ref_type: CreateKind,
- repository: Repository,
- sender: User,
- }
- #[derive(Debug, serde::Deserialize)]
- pub struct PushEvent {
- #[serde(rename = "ref")]
- pub git_ref: String,
- repository: Repository,
- sender: User,
- }
- /// An event triggered by a webhook.
- #[derive(Debug)]
- pub enum Event {
- /// A Git branch or tag is created.
- Create(CreateEvent),
- /// A comment on an issue or PR.
- ///
- /// Can be:
- /// - Regular comment on an issue or PR.
- /// - A PR review.
- /// - A comment on a PR review.
- ///
- /// These different scenarios are unified into the `IssueComment` variant
- /// when triagebot receives the corresponding webhook event.
- IssueComment(IssueCommentEvent),
- /// Activity on an issue or PR.
- Issue(IssuesEvent),
- /// One or more commits are pushed to a repository branch or tag.
- Push(PushEvent),
- }
- impl Event {
- pub fn repo(&self) -> &Repository {
- match self {
- Event::Create(event) => &event.repository,
- Event::IssueComment(event) => &event.repository,
- Event::Issue(event) => &event.repository,
- Event::Push(event) => &event.repository,
- }
- }
- pub fn issue(&self) -> Option<&Issue> {
- match self {
- Event::Create(_) => None,
- Event::IssueComment(event) => Some(&event.issue),
- Event::Issue(event) => Some(&event.issue),
- Event::Push(_) => None,
- }
- }
- /// This will both extract from IssueComment events but also Issue events
- pub fn comment_body(&self) -> Option<&str> {
- match self {
- Event::Create(_) => None,
- Event::Issue(e) => Some(&e.issue.body),
- Event::IssueComment(e) => Some(&e.comment.body),
- Event::Push(_) => None,
- }
- }
- /// This will both extract from IssueComment events but also Issue events
- pub fn comment_from(&self) -> Option<&str> {
- match self {
- Event::Create(_) => None,
- Event::Issue(e) => Some(&e.changes.as_ref()?.body.as_ref()?.from),
- Event::IssueComment(e) => Some(&e.changes.as_ref()?.body.as_ref()?.from),
- Event::Push(_) => None,
- }
- }
- pub fn html_url(&self) -> Option<&str> {
- match self {
- Event::Create(_) => None,
- Event::Issue(e) => Some(&e.issue.html_url),
- Event::IssueComment(e) => Some(&e.comment.html_url),
- Event::Push(_) => None,
- }
- }
- pub fn user(&self) -> &User {
- match self {
- Event::Create(e) => &e.sender,
- Event::Issue(e) => &e.issue.user,
- Event::IssueComment(e) => &e.comment.user,
- Event::Push(e) => &e.sender,
- }
- }
- pub fn time(&self) -> Option<chrono::DateTime<FixedOffset>> {
- match self {
- Event::Create(_) => None,
- Event::Issue(e) => Some(e.issue.created_at.into()),
- Event::IssueComment(e) => Some(e.comment.updated_at.into()),
- Event::Push(_) => None,
- }
- }
- }
- trait RequestSend: Sized {
- fn configure(self, g: &GithubClient) -> Self;
- }
- impl RequestSend for RequestBuilder {
- fn configure(self, g: &GithubClient) -> RequestBuilder {
- let mut auth = HeaderValue::from_maybe_shared(format!("token {}", g.token)).unwrap();
- auth.set_sensitive(true);
- self.header(USER_AGENT, "rust-lang-triagebot")
- .header(AUTHORIZATION, &auth)
- }
- }
- /// Finds the token in the user's environment, panicking if no suitable token
- /// can be found.
- pub fn default_token_from_env() -> String {
- match std::env::var("GITHUB_API_TOKEN") {
- Ok(v) => return v,
- Err(_) => (),
- }
- match get_token_from_git_config() {
- Ok(v) => return v,
- Err(_) => (),
- }
- panic!("could not find token in GITHUB_API_TOKEN or .gitconfig/github.oath-token")
- }
- fn get_token_from_git_config() -> anyhow::Result<String> {
- let output = std::process::Command::new("git")
- .arg("config")
- .arg("--get")
- .arg("github.oauth-token")
- .output()?;
- if !output.status.success() {
- anyhow::bail!("error received executing `git`: {:?}", output.status);
- }
- let git_token = String::from_utf8(output.stdout)?.trim().to_string();
- Ok(git_token)
- }
- #[derive(Clone)]
- pub struct GithubClient {
- token: String,
- client: Client,
- api_url: String,
- graphql_url: String,
- raw_url: String,
- }
- impl GithubClient {
- pub fn new(token: String, api_url: String, graphql_url: String, raw_url: String) -> Self {
- GithubClient {
- client: Client::new(),
- token,
- api_url,
- graphql_url,
- raw_url,
- }
- }
- pub fn new_from_env() -> Self {
- Self::new(
- default_token_from_env(),
- std::env::var("GITHUB_API_URL")
- .unwrap_or_else(|_| "https://api.github.com".to_string()),
- std::env::var("GITHUB_GRAPHQL_API_URL")
- .unwrap_or_else(|_| "https://api.github.com/graphql".to_string()),
- std::env::var("GITHUB_RAW_URL")
- .unwrap_or_else(|_| "https://raw.githubusercontent.com".to_string()),
- )
- }
- pub fn raw(&self) -> &Client {
- &self.client
- }
- pub async fn raw_file(
- &self,
- repo: &str,
- branch: &str,
- path: &str,
- ) -> anyhow::Result<Option<Bytes>> {
- let url = format!("{}/{repo}/{branch}/{path}", self.raw_url);
- let req = self.get(&url);
- let req_dbg = format!("{:?}", req);
- let req = req
- .build()
- .with_context(|| format!("failed to build request {:?}", req_dbg))?;
- let resp = self.client.execute(req).await.context(req_dbg.clone())?;
- let status = resp.status();
- let body = resp
- .bytes()
- .await
- .with_context(|| format!("failed to read response body {req_dbg}"))?;
- match status {
- StatusCode::OK => Ok(Some(body)),
- StatusCode::NOT_FOUND => Ok(None),
- status => anyhow::bail!("failed to GET {}: {}", url, status),
- }
- }
- /// Get the raw gist content from the URL of the HTML version of the gist:
- ///
- /// `html_url` looks like `https://gist.github.com/rust-play/7e80ca3b1ec7abe08f60c41aff91f060`.
- ///
- /// `filename` is the name of the file you want the content of.
- pub async fn raw_gist_from_url(
- &self,
- html_url: &str,
- filename: &str,
- ) -> anyhow::Result<String> {
- let url = html_url.replace("github.com", "githubusercontent.com") + "/raw/" + filename;
- let response = self.raw().get(&url).send().await?;
- response.text().await.context("raw gist from url")
- }
- fn get(&self, url: &str) -> RequestBuilder {
- log::trace!("get {:?}", url);
- self.client.get(url).configure(self)
- }
- fn patch(&self, url: &str) -> RequestBuilder {
- log::trace!("patch {:?}", url);
- self.client.patch(url).configure(self)
- }
- fn delete(&self, url: &str) -> RequestBuilder {
- log::trace!("delete {:?}", url);
- self.client.delete(url).configure(self)
- }
- fn post(&self, url: &str) -> RequestBuilder {
- log::trace!("post {:?}", url);
- self.client.post(url).configure(self)
- }
- #[allow(unused)]
- fn put(&self, url: &str) -> RequestBuilder {
- log::trace!("put {:?}", url);
- self.client.put(url).configure(self)
- }
- pub async fn rust_commit(&self, sha: &str) -> Option<GithubCommit> {
- let req = self.get(&format!(
- "{}/repos/rust-lang/rust/commits/{sha}",
- self.api_url
- ));
- match self.json(req).await {
- Ok(r) => Some(r),
- Err(e) => {
- log::error!("Failed to query commit {:?}: {:?}", sha, e);
- None
- }
- }
- }
- /// This does not retrieve all of them, only the last several.
- pub async fn bors_commits(&self) -> Vec<GithubCommit> {
- let req = self.get(&format!(
- "{}/repos/rust-lang/rust/commits?author=bors",
- self.api_url
- ));
- match self.json(req).await {
- Ok(r) => r,
- Err(e) => {
- log::error!("Failed to query commit list: {:?}", e);
- Vec::new()
- }
- }
- }
- /// Issues an ad-hoc GraphQL query.
- pub async fn graphql_query<T: serde::de::DeserializeOwned>(
- &self,
- query: &str,
- vars: serde_json::Value,
- ) -> anyhow::Result<T> {
- self.json(self.post(&self.graphql_url).json(&serde_json::json!({
- "query": query,
- "variables": vars,
- })))
- .await
- }
- /// Returns the object ID of the given user.
- ///
- /// Returns `None` if the user doesn't exist.
- pub async fn user_object_id(&self, user: &str) -> anyhow::Result<Option<String>> {
- let user_info: serde_json::Value = self
- .graphql_query(
- "query($user:String!) {
- user(login:$user) {
- id
- }
- }",
- serde_json::json!({
- "user": user,
- }),
- )
- .await?;
- if let Some(id) = user_info["data"]["user"]["id"].as_str() {
- return Ok(Some(id.to_string()));
- }
- if let Some(errors) = user_info["errors"].as_array() {
- if errors
- .iter()
- .any(|err| err["type"].as_str().unwrap_or_default() == "NOT_FOUND")
- {
- return Ok(None);
- }
- let messages: Vec<_> = errors
- .iter()
- .map(|err| err["message"].as_str().unwrap_or_default())
- .collect();
- anyhow::bail!("failed to query user: {}", messages.join("\n"));
- }
- anyhow::bail!("query for user {user} failed, no error message? {user_info:?}");
- }
- /// Returns whether or not the given GitHub login has made any commits to
- /// the given repo.
- pub async fn is_new_contributor(&self, repo: &Repository, author: &str) -> bool {
- let user_id = match self.user_object_id(author).await {
- Ok(None) => return true,
- Ok(Some(id)) => id,
- Err(e) => {
- log::warn!("failed to query user: {e:?}");
- return true;
- }
- };
- // Note: This only returns results for the default branch. That should
- // be fine in most cases since I think it is rare for new users to
- // make their first commit to a different branch.
- //
- // Note: This is using GraphQL because the
- // `/repos/ORG/REPO/commits?author=AUTHOR` API was having problems not
- // finding users (https://github.com/rust-lang/triagebot/issues/1689).
- // The other possibility is the `/search/commits?q=repo:{}+author:{}`
- // API, but that endpoint has a very limited rate limit, and doesn't
- // work on forks. This GraphQL query seems to work fairly reliably,
- // and seems to cost only 1 point.
- match self
- .graphql_query::<serde_json::Value>(
- "query($repository_owner:String!, $repository_name:String!, $user_id:ID!) {
- repository(owner: $repository_owner, name: $repository_name) {
- defaultBranchRef {
- target {
- ... on Commit {
- history(author: {id: $user_id}) {
- totalCount
- }
- }
- }
- }
- }
- }",
- serde_json::json!({
- "repository_owner": repo.owner(),
- "repository_name": repo.name(),
- "user_id": user_id
- }),
- )
- .await
- {
- Ok(c) => {
- if let Some(c) = c["data"]["repository"]["defaultBranchRef"]["target"]["history"]
- ["totalCount"]
- .as_i64()
- {
- return c == 0;
- }
- log::warn!("new user query failed: {c:?}");
- false
- }
- Err(e) => {
- log::warn!(
- "failed to search for user commits in {} for author {author}: {e:?}",
- repo.full_name
- );
- // Using `false` since if there is some underlying problem, we
- // don't need to spam everyone with the "new user" welcome
- // message.
- false
- }
- }
- }
- /// Returns information about a repository.
- ///
- /// The `full_name` should be something like `rust-lang/rust`.
- pub async fn repository(&self, full_name: &str) -> anyhow::Result<Repository> {
- let req = self.get(&format!("{}/repos/{full_name}", self.api_url));
- self.json(req)
- .await
- .with_context(|| format!("{} failed to get repo", full_name))
- }
- /// Get or create a [`Milestone`].
- ///
- /// This will not change the state if it already exists.
- async fn get_or_create_milestone(
- &self,
- full_repo_name: &str,
- title: &str,
- state: &str,
- ) -> anyhow::Result<Milestone> {
- let url = format!("{}/repos/{full_repo_name}/milestones", self.api_url);
- let resp = self
- .send_req(self.post(&url).json(&serde_json::json!({
- "title": title,
- "state": state,
- })))
- .await;
- match resp {
- Ok((body, _dbg)) => {
- let milestone = serde_json::from_slice(&body)?;
- log::trace!("Created milestone: {milestone:?}");
- return Ok(milestone);
- }
- Err(e) => {
- if e.downcast_ref::<reqwest::Error>().map_or(false, |e| {
- matches!(e.status(), Some(StatusCode::UNPROCESSABLE_ENTITY))
- }) {
- // fall-through, it already exists
- } else {
- return Err(e.context(format!(
- "failed to create milestone {url} with title {title}"
- )));
- }
- }
- }
- // In the case where it already exists, we need to search for its number.
- let mut page = 1;
- loop {
- let url = format!(
- "{}/repos/{full_repo_name}/milestones?page={page}&state=all",
- self.api_url
- );
- let milestones: Vec<Milestone> = self
- .json(self.get(&url))
- .await
- .with_context(|| format!("failed to get milestones {url} searching for {title}"))?;
- if milestones.is_empty() {
- anyhow::bail!("expected to find milestone with title {title}");
- }
- if let Some(milestone) = milestones.into_iter().find(|m| m.title == title) {
- return Ok(milestone);
- }
- page += 1;
- }
- }
- /// Set the milestone of an issue or PR.
- async fn set_milestone(
- &self,
- full_repo_name: &str,
- milestone: &Milestone,
- issue_num: u64,
- ) -> anyhow::Result<()> {
- let url = format!("{}/repos/{full_repo_name}/issues/{issue_num}", self.api_url);
- self.send_req(self.patch(&url).json(&serde_json::json!({
- "milestone": milestone.number
- })))
- .await
- .with_context(|| format!("failed to set milestone for {url} to milestone {milestone:?}"))?;
- Ok(())
- }
- }
- #[derive(Debug, serde::Deserialize)]
- pub struct GithubCommit {
- pub sha: String,
- pub commit: GithubCommitCommitField,
- pub parents: Vec<Parent>,
- }
- #[derive(Debug, serde::Deserialize)]
- pub struct GithubCommitCommitField {
- pub author: GitUser,
- pub message: String,
- pub tree: GitCommitTree,
- }
- #[derive(Debug, serde::Deserialize)]
- pub struct GitCommit {
- pub sha: String,
- pub author: GitUser,
- pub message: String,
- pub tree: GitCommitTree,
- }
- #[derive(Debug, serde::Deserialize)]
- pub struct GitCommitTree {
- pub sha: String,
- }
- #[derive(Debug, serde::Deserialize)]
- pub struct GitTreeObject {
- pub sha: String,
- }
- #[derive(Debug, serde::Serialize, serde::Deserialize)]
- pub struct GitTreeEntry {
- pub path: String,
- pub mode: String,
- #[serde(rename = "type")]
- pub object_type: String,
- pub sha: String,
- }
- pub struct RecentCommit {
- pub title: String,
- pub pr_num: Option<i32>,
- pub oid: String,
- pub committed_date: DateTime<Utc>,
- }
- #[derive(Debug, serde::Deserialize)]
- pub struct GitUser {
- pub date: DateTime<FixedOffset>,
- }
- #[derive(Debug, serde::Deserialize)]
- pub struct Parent {
- pub sha: String,
- }
- #[async_trait]
- pub trait IssuesQuery {
- async fn query<'a>(
- &'a self,
- repo: &'a Repository,
- include_fcp_details: bool,
- include_mcp_details: bool,
- client: &'a GithubClient,
- ) -> anyhow::Result<Vec<crate::actions::IssueDecorator>>;
- }
- pub struct LeastRecentlyReviewedPullRequests;
- #[async_trait]
- impl IssuesQuery for LeastRecentlyReviewedPullRequests {
- async fn query<'a>(
- &'a self,
- repo: &'a Repository,
- _include_fcp_details: bool,
- _include_mcp_details: bool,
- client: &'a GithubClient,
- ) -> anyhow::Result<Vec<crate::actions::IssueDecorator>> {
- use cynic::QueryBuilder;
- use github_graphql::queries;
- let repository_owner = repo.owner();
- let repository_name = repo.name();
- let mut prs: Vec<queries::PullRequest> = vec![];
- let mut args = queries::LeastRecentlyReviewedPullRequestsArguments {
- repository_owner,
- repository_name,
- after: None,
- };
- loop {
- let query = queries::LeastRecentlyReviewedPullRequests::build(args.clone());
- let req = client.post(&client.graphql_url);
- let req = req.json(&query);
- let data: cynic::GraphQlResponse<queries::LeastRecentlyReviewedPullRequests> =
- client.json(req).await?;
- if let Some(errors) = data.errors {
- anyhow::bail!("There were graphql errors. {:?}", errors);
- }
- let repository = data
- .data
- .ok_or_else(|| anyhow::anyhow!("No data returned."))?
- .repository
- .ok_or_else(|| anyhow::anyhow!("No repository."))?;
- prs.extend(repository.pull_requests.nodes);
- let page_info = repository.pull_requests.page_info;
- if !page_info.has_next_page || page_info.end_cursor.is_none() {
- break;
- }
- args.after = page_info.end_cursor;
- }
- let mut prs: Vec<_> = prs
- .into_iter()
- .filter_map(|pr| {
- if pr.is_draft {
- return None;
- }
- let labels = pr
- .labels
- .map(|l| l.nodes)
- .unwrap_or_default()
- .into_iter()
- .map(|node| node.name)
- .collect::<Vec<_>>();
- if !labels.iter().any(|label| label == "T-compiler") {
- return None;
- }
- let labels = labels.join(", ");
- let assignees: Vec<_> = pr
- .assignees
- .nodes
- .into_iter()
- .map(|user| user.login)
- .collect();
- let mut reviews = pr
- .latest_reviews
- .map(|connection| connection.nodes)
- .unwrap_or_default()
- .into_iter()
- .filter_map(|node| {
- let created_at = node.created_at;
- node.author.map(|author| (author, created_at))
- })
- .map(|(author, created_at)| (author.login, created_at))
- .collect::<Vec<_>>();
- reviews.sort_by_key(|r| r.1);
- let mut comments = pr
- .comments
- .nodes
- .into_iter()
- .filter_map(|node| {
- let created_at = node.created_at;
- node.author.map(|author| (author, created_at))
- })
- .map(|(author, created_at)| (author.login, created_at))
- .filter(|comment| assignees.contains(&comment.0))
- .collect::<Vec<_>>();
- comments.sort_by_key(|c| c.1);
- let updated_at = std::cmp::max(
- reviews.last().map(|t| t.1).unwrap_or(pr.created_at),
- comments.last().map(|t| t.1).unwrap_or(pr.created_at),
- );
- let assignees = assignees.join(", ");
- Some((
- updated_at,
- pr.number as u64,
- pr.title,
- pr.url.0,
- repository_name,
- labels,
- assignees,
- ))
- })
- .collect();
- prs.sort_by_key(|pr| pr.0);
- let prs: Vec<_> = prs
- .into_iter()
- .take(50)
- .map(
- |(updated_at, number, title, html_url, repo_name, labels, assignees)| {
- let updated_at_hts = crate::actions::to_human(updated_at);
- crate::actions::IssueDecorator {
- number,
- title,
- html_url,
- repo_name: repo_name.to_string(),
- labels,
- assignees,
- updated_at_hts,
- fcp_details: None,
- mcp_details: None,
- }
- },
- )
- .collect();
- Ok(prs)
- }
- }
- async fn project_items_by_status(
- client: &GithubClient,
- status_filter: impl Fn(Option<&str>) -> bool,
- ) -> anyhow::Result<Vec<github_graphql::project_items::ProjectV2Item>> {
- use cynic::QueryBuilder;
- use github_graphql::project_items;
- const DESIGN_MEETING_PROJECT: i32 = 31;
- let mut args = project_items::Arguments {
- project_number: DESIGN_MEETING_PROJECT,
- after: None,
- };
- let mut all_items = vec![];
- loop {
- let query = project_items::Query::build(args.clone());
- let req = client.post(&client.graphql_url);
- let req = req.json(&query);
- let data: cynic::GraphQlResponse<project_items::Query> = client.json(req).await?;
- if let Some(errors) = data.errors {
- anyhow::bail!("There were graphql errors. {:?}", errors);
- }
- let items = data
- .data
- .ok_or_else(|| anyhow!("No data returned."))?
- .organization
- .ok_or_else(|| anyhow!("Organization not found."))?
- .project_v2
- .ok_or_else(|| anyhow!("Project not found."))?
- .items;
- let filtered = items
- .nodes
- .ok_or_else(|| anyhow!("Malformed response."))?
- .into_iter()
- .flatten()
- .filter(|item| status_filter(item.status()));
- all_items.extend(filtered);
- let page_info = items.page_info;
- if !page_info.has_next_page || page_info.end_cursor.is_none() {
- break;
- }
- args.after = page_info.end_cursor;
- }
- all_items.sort_by_key(|item| item.date());
- Ok(all_items)
- }
- pub enum DesignMeetingStatus {
- Proposed,
- Scheduled,
- Done,
- Empty,
- }
- impl DesignMeetingStatus {
- fn query_str(&self) -> Option<&str> {
- match self {
- DesignMeetingStatus::Proposed => Some("Needs triage"),
- DesignMeetingStatus::Scheduled => Some("Scheduled"),
- DesignMeetingStatus::Done => Some("Done"),
- DesignMeetingStatus::Empty => None,
- }
- }
- }
- pub struct DesignMeetings {
- pub with_status: DesignMeetingStatus,
- }
- #[async_trait]
- impl IssuesQuery for DesignMeetings {
- async fn query<'a>(
- &'a self,
- _repo: &'a Repository,
- _include_fcp_details: bool,
- _include_mcp_details: bool,
- client: &'a GithubClient,
- ) -> anyhow::Result<Vec<crate::actions::IssueDecorator>> {
- use github_graphql::project_items::ProjectV2ItemContent;
- let items =
- project_items_by_status(client, |status| status == self.with_status.query_str())
- .await?;
- Ok(items
- .into_iter()
- .flat_map(|item| match item.content {
- Some(ProjectV2ItemContent::Issue(issue)) => Some(crate::actions::IssueDecorator {
- assignees: String::new(),
- number: issue.number.try_into().unwrap(),
- fcp_details: None,
- mcp_details: None,
- html_url: issue.url.0,
- title: issue.title,
- repo_name: String::new(),
- labels: String::new(),
- updated_at_hts: String::new(),
- }),
- _ => None,
- })
- .collect())
- }
- }
- #[derive(Debug, serde::Deserialize)]
- pub struct GitReference {
- #[serde(rename = "ref")]
- pub refname: String,
- pub object: GitObject,
- }
- #[derive(Debug, serde::Deserialize)]
- pub struct GitObject {
- #[serde(rename = "type")]
- pub object_type: String,
- pub sha: String,
- pub url: String,
- }
- #[derive(Debug, serde::Deserialize)]
- pub struct Submodule {
- pub name: String,
- pub path: String,
- pub sha: String,
- pub submodule_git_url: String,
- }
- impl Submodule {
- /// Returns the `Repository` this submodule points to.
- ///
- /// This assumes that the submodule is on GitHub.
- pub async fn repository(&self, client: &GithubClient) -> anyhow::Result<Repository> {
- let fullname = self
- .submodule_git_url
- .strip_prefix("https://github.com/")
- .ok_or_else(|| {
- anyhow::anyhow!(
- "only github submodules supported, got {}",
- self.submodule_git_url
- )
- })?
- .strip_suffix(".git")
- .ok_or_else(|| {
- anyhow::anyhow!("expected .git suffix, got {}", self.submodule_git_url)
- })?;
- client.repository(fullname).await
- }
- }
- #[cfg(test)]
- mod tests {
- use super::*;
- #[test]
- fn display_labels() {
- let x = UnknownLabels {
- labels: vec!["A-bootstrap".into(), "xxx".into()],
- };
- assert_eq!(x.to_string(), "Unknown labels: A-bootstrap, xxx");
- }
- #[test]
- fn extract_one_file() {
- let input = r##"\
- diff --git a/triagebot.toml b/triagebot.toml
- index fb9cee43b2d..b484c25ea51 100644
- --- a/triagebot.toml
- +++ b/triagebot.toml
- @@ -114,6 +114,15 @@ trigger_files = [
- "src/tools/rustdoc-themes",
- ]
- +[autolabel."T-compiler"]
- +trigger_files = [
- + # Source code
- + "compiler",
- +
- + # Tests
- + "src/test/ui",
- +]
- +
- [notify-zulip."I-prioritize"]
- zulip_stream = 245100 # #t-compiler/wg-prioritization/alerts
- topic = "#{number} {title}"
- "##;
- let files: Vec<_> = parse_diff(input).into_iter().map(|d| d.path).collect();
- assert_eq!(files, vec!["triagebot.toml".to_string()]);
- }
- #[test]
- fn extract_several_files() {
- let input = r##"\
- diff --git a/library/stdarch b/library/stdarch
- index b70ae88ef2a..cfba59fccd9 160000
- --- a/library/stdarch
- +++ b/library/stdarch
- @@ -1 +1 @@
- -Subproject commit b70ae88ef2a6c83acad0a1e83d5bd78f9655fd05
- +Subproject commit cfba59fccd90b3b52a614120834320f764ab08d1
- diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs
- index 1fe4aa9023e..f0330f1e424 100644
- --- a/src/librustdoc/clean/types.rs
- +++ b/src/librustdoc/clean/types.rs
- @@ -2322,3 +2322,4 @@ impl SubstParam {
- if let Self::Lifetime(lt) = self { Some(lt) } else { None }
- }
- }
- +
- diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs
- index c58310947d2..3b0854d4a9b 100644
- --- a/src/librustdoc/core.rs
- +++ b/src/librustdoc/core.rs
- @@ -591,3 +591,4 @@ fn from(idx: u32) -> Self {
- ImplTraitParam::ParamIndex(idx)
- }
- }
- +
- "##;
- let files: Vec<_> = parse_diff(input).into_iter().map(|d| d.path).collect();
- assert_eq!(
- files,
- vec![
- "library/stdarch".to_string(),
- "src/librustdoc/clean/types.rs".to_string(),
- "src/librustdoc/core.rs".to_string(),
- ]
- )
- }
- }
|