1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330 |
- use anyhow::Context;
- use async_trait::async_trait;
- use chrono::{DateTime, FixedOffset, Utc};
- use futures::stream::{FuturesUnordered, StreamExt};
- 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::{
- fmt,
- time::{Duration, SystemTime},
- };
- pub mod graphql;
- #[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<(Response, 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?;
- }
- resp.error_for_status_ref()?;
- Ok((resp, 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 {
- 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,
- pub graphql: RateLimit,
- 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("https://api.github.com/rate_limit")
- .configure(self)
- .build()
- .unwrap(),
- )
- .await?;
- 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()
- }
- async fn send_req(&self, req: RequestBuilder) -> anyhow::Result<Vec<u8>> {
- let (mut resp, req_dbg) = self._send_req(req).await?;
- let mut body = Vec::new();
- while let Some(chunk) = resp.chunk().await.transpose() {
- let chunk = chunk
- .context("reading stream failed")
- .map_err(anyhow::Error::from)
- .context(req_dbg.clone())?;
- body.extend_from_slice(&chunk);
- }
- Ok(body)
- }
- pub async fn json<T>(&self, req: RequestBuilder) -> anyhow::Result<T>
- where
- T: serde::de::DeserializeOwned,
- {
- let (resp, req_dbg) = self._send_req(req).await?;
- Ok(resp.json().await.context(req_dbg)?)
- }
- }
- impl User {
- pub async fn current(client: &GithubClient) -> anyhow::Result<Self> {
- client.json(client.get("https://api.github.com/user")).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-foundations")
- .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,
- }
- impl Label {
- async fn exists<'a>(&'a self, repo_api_prefix: &'a str, client: &'a GithubClient) -> bool {
- #[allow(clippy::redundant_pattern_matching)]
- let url = format!("{}/labels/{}", repo_api_prefix, self.name);
- match client.send_req(client.get(&url)).await {
- Ok(_) => true,
- // XXX: Error handling if the request failed for reasons beyond 'label didn't exist'
- Err(_) => false,
- }
- }
- }
- #[derive(Debug, serde::Deserialize)]
- pub struct PullRequestDetails {
- // none for now
- }
- #[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>,
- #[serde(default)]
- pub merge_commit_sha: Option<String>,
- pub title: String,
- pub html_url: String,
- pub user: User,
- pub labels: Vec<Label>,
- pub assignees: Vec<User>,
- pub pull_request: Option<PullRequestDetails>,
- #[serde(default)]
- pub merged: bool,
- // API URL
- comments_url: String,
- #[serde(skip)]
- repository: OnceCell<IssueRepository>,
- }
- /// 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 = "lowercase")]
- 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) -> String {
- format!(
- "https://api.github.com/repos/{}/{}",
- self.organization, self.repository
- )
- }
- }
- 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 async fn get_comment(&self, client: &GithubClient, id: usize) -> anyhow::Result<Comment> {
- let comment_url = format!("{}/issues/comments/{}", self.repository().url(), id);
- let comment = client.json(client.get(&comment_url)).await?;
- Ok(comment)
- }
- pub async fn edit_body(&self, client: &GithubClient, body: &str) -> anyhow::Result<()> {
- let edit_url = format!("{}/issues/{}", self.repository().url(), 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(), 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,
- }
- client
- ._send_req(client.post(&self.comments_url).json(&PostComment { body }))
- .await
- .context("failed to post comment")?;
- Ok(())
- }
- pub async fn set_labels(
- &self,
- client: &GithubClient,
- labels: Vec<Label>,
- ) -> anyhow::Result<()> {
- log::info!("set_labels {} to {:?}", self.global_id(), labels);
- // PUT /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(),
- number = self.number
- );
- let mut stream = labels
- .into_iter()
- .map(|label| async { (label.exists(&self.repository().url(), &client).await, label) })
- .collect::<FuturesUnordered<_>>();
- let mut labels = Vec::new();
- while let Some((true, label)) = stream.next().await {
- labels.push(label);
- }
- #[derive(serde::Serialize)]
- struct LabelsReq {
- labels: Vec<String>,
- }
- client
- ._send_req(client.put(&url).json(&LabelsReq {
- labels: labels.iter().map(|l| l.name.clone()).collect(),
- }))
- .await
- .context("failed to set labels")?;
- Ok(())
- }
- pub fn labels(&self) -> &[Label] {
- &self.labels
- }
- pub fn contain_assignee(&self, user: &str) -> bool {
- self.assignees.iter().any(|a| a.login == user)
- }
- 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(),
- 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 != user)
- .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(),
- 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() == user);
- 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(())
- }
- 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 create_url = format!("{}/milestones", self.repository().url());
- let resp = client
- .send_req(
- client
- .post(&create_url)
- .body(serde_json::to_vec(&MilestoneCreateBody { title }).unwrap()),
- )
- .await;
- // Explicitly do *not* try to return Err(...) if this fails -- that's
- // fine, it just means the milestone was already created.
- log::trace!("Created milestone: {:?}", resp);
- let list_url = format!("{}/milestones", self.repository().url());
- let milestone_list: Vec<Milestone> = client.json(client.get(&list_url)).await?;
- let milestone_no = if let Some(milestone) = milestone_list.iter().find(|v| v.title == title)
- {
- milestone.number
- } else {
- anyhow::bail!(
- "Despite just creating milestone {} on {}, it does not exist?",
- title,
- self.repository()
- )
- };
- #[derive(serde::Serialize)]
- struct SetMilestone {
- milestone: u64,
- }
- let url = format!("{}/issues/{}", self.repository().url(), self.number);
- client
- ._send_req(client.patch(&url).json(&SetMilestone {
- milestone: milestone_no,
- }))
- .await
- .context("failed to set milestone")?;
- Ok(())
- }
- pub async fn close(&self, client: &GithubClient) -> anyhow::Result<()> {
- let edit_url = format!("{}/issues/{}", self.repository().url(), 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(())
- }
- }
- #[derive(serde::Serialize)]
- struct MilestoneCreateBody<'a> {
- title: &'a str,
- }
- #[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,
- }
- #[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")]
- pub enum IssuesAction {
- Opened,
- Edited,
- Deleted,
- Transferred,
- Pinned,
- Unpinned,
- Closed,
- Reopened,
- Assigned,
- Unassigned,
- Labeled,
- Unlabeled,
- Locked,
- Unlocked,
- Milestoned,
- Demilestoned,
- ReviewRequested,
- ReviewRequestRemoved,
- ReadyForReview,
- Synchronize,
- ConvertedToDraft,
- }
- #[derive(Debug, serde::Deserialize)]
- pub struct IssuesEvent {
- pub action: IssuesAction,
- #[serde(alias = "pull_request")]
- pub issue: Issue,
- pub changes: Option<Changes>,
- pub repository: Repository,
- /// Some if action is IssuesAction::Labeled, for example
- pub label: Option<Label>,
- // These fields are the sha fields before/after a synchronize operation,
- // used to compute the diff between these two commits.
- #[serde(default)]
- before: Option<String>,
- #[serde(default)]
- after: Option<String>,
- #[serde(default)]
- pull_request: Option<PullRequestEventFields>,
- }
- #[derive(Debug, serde::Deserialize)]
- struct PullRequestEventFields {
- base: CommitBase,
- head: CommitBase,
- }
- #[derive(Default, Clone, Debug, serde::Deserialize)]
- pub struct CommitBase {
- sha: String,
- }
- impl IssuesEvent {
- /// Returns the diff in this event, for Open and Synchronize events for now.
- pub async fn diff_between(&self, client: &GithubClient) -> anyhow::Result<Option<String>> {
- let (before, after) = if self.action == IssuesAction::Synchronize {
- (
- self.before
- .clone()
- .expect("synchronize has before populated"),
- self.after.clone().expect("synchronize has after populated"),
- )
- } else if self.action == IssuesAction::Opened {
- if let Some(pr) = &self.pull_request {
- (pr.base.sha.clone(), pr.head.sha.clone())
- } else {
- return Ok(None);
- }
- } else {
- return Ok(None);
- };
- let mut req = client.get(&format!(
- "{}/compare/{}...{}",
- self.issue.repository().url(),
- before,
- after
- ));
- req = req.header("Accept", "application/vnd.github.v3.diff");
- let diff = client.send_req(req).await?;
- Ok(Some(String::from(String::from_utf8_lossy(&diff))))
- }
- }
- #[derive(Debug, serde::Deserialize)]
- pub struct IssueSearchResult {
- pub total_count: usize,
- pub incomplete_results: bool,
- pub items: Vec<Issue>,
- }
- #[derive(Debug, serde::Deserialize)]
- pub struct Repository {
- pub full_name: String,
- }
- struct Ordering<'a> {
- pub sort: &'a str,
- pub direction: &'a str,
- pub per_page: &'a str,
- }
- impl Repository {
- const GITHUB_API_URL: &'static str = "https://api.github.com";
- const GITHUB_GRAPHQL_API_URL: &'static str = "https://api.github.com/graphql";
- 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",
- };
- 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();
- let url = if use_search_api {
- self.build_search_issues_url(&filters, include_labels, exclude_labels, ordering)
- } else if is_pr {
- self.build_pulls_url(&filters, include_labels, ordering)
- } else {
- self.build_issues_url(&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))?;
- Ok(result.items)
- } else {
- client
- .json(result)
- .await
- .with_context(|| format!("failed to list issues from {}", url))
- }
- }
- fn build_issues_url(
- &self,
- filters: &Vec<(&str, &str)>,
- include_labels: &Vec<&str>,
- ordering: Ordering<'_>,
- ) -> String {
- self.build_endpoint_url("issues", filters, include_labels, ordering)
- }
- fn build_pulls_url(
- &self,
- filters: &Vec<(&str, &str)>,
- include_labels: &Vec<&str>,
- ordering: Ordering<'_>,
- ) -> String {
- self.build_endpoint_url("pulls", filters, include_labels, ordering)
- }
- fn build_endpoint_url(
- &self,
- 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/{}/{}?{}",
- Repository::GITHUB_API_URL,
- self.full_name,
- endpoint,
- filters
- )
- }
- fn build_search_issues_url(
- &self,
- 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={}",
- Repository::GITHUB_API_URL,
- filters,
- ordering.sort,
- ordering.direction,
- ordering.per_page,
- )
- }
- }
- 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>,
- }
- #[async_trait]
- impl<'q> IssuesQuery for Query<'q> {
- async fn query<'a>(
- &'a self,
- repo: &'a Repository,
- client: &'a GithubClient,
- ) -> anyhow::Result<Vec<crate::actions::IssueDecorator>> {
- let issues = repo
- .get_issues(&client, self)
- .await
- .with_context(|| "Unable to get issues.")?;
- let issues_decorator: Vec<_> = issues
- .iter()
- .map(|issue| 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: crate::actions::to_human(issue.updated_at),
- })
- .collect();
- 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,
- }
- #[derive(Debug)]
- pub enum Event {
- Create(CreateEvent),
- IssueComment(IssueCommentEvent),
- Issue(IssuesEvent),
- Push(PushEvent),
- }
- impl Event {
- pub fn repo_name(&self) -> String {
- match self {
- Event::Create(event) => event.repository.full_name.clone(),
- Event::IssueComment(event) => event.repository.full_name.clone(),
- Event::Issue(event) => event.repository.full_name.clone(),
- Event::Push(event) => event.repository.full_name.clone(),
- }
- }
- 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,
- }
- impl GithubClient {
- pub fn new(client: Client, token: String) -> Self {
- GithubClient { client, token }
- }
- pub fn new_with_default_token(client: Client) -> Self {
- Self::new(client, default_token_from_env())
- }
- pub fn raw(&self) -> &Client {
- &self.client
- }
- pub async fn raw_file(
- &self,
- repo: &str,
- branch: &str,
- path: &str,
- ) -> anyhow::Result<Option<Vec<u8>>> {
- let url = format!(
- "https://raw.githubusercontent.com/{}/{}/{}",
- repo, branch, path
- );
- let req = self.get(&url);
- let req_dbg = format!("{:?}", req);
- let req = req
- .build()
- .with_context(|| format!("failed to build request {:?}", req_dbg))?;
- let mut resp = self.client.execute(req).await.context(req_dbg.clone())?;
- let status = resp.status();
- match status {
- StatusCode::OK => {
- let mut buf = Vec::with_capacity(resp.content_length().unwrap_or(4) as usize);
- while let Some(chunk) = resp.chunk().await.transpose() {
- let chunk = chunk
- .context("reading stream failed")
- .map_err(anyhow::Error::from)
- .context(req_dbg.clone())?;
- buf.extend_from_slice(&chunk);
- }
- Ok(Some(buf))
- }
- 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)
- }
- 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!(
- "https://api.github.com/repos/rust-lang/rust/commits/{}",
- sha
- ));
- 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("https://api.github.com/repos/rust-lang/rust/commits?author=bors");
- match self.json(req).await {
- Ok(r) => r,
- Err(e) => {
- log::error!("Failed to query commit list: {:?}", e);
- Vec::new()
- }
- }
- }
- }
- #[derive(Debug, serde::Deserialize)]
- pub struct GithubCommit {
- pub sha: String,
- #[serde(default)]
- pub message: String,
- pub commit: GitCommit,
- pub parents: Vec<Parent>,
- }
- #[derive(Debug, serde::Deserialize)]
- pub struct GitCommit {
- pub author: GitUser,
- }
- #[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,
- client: &'a GithubClient,
- ) -> anyhow::Result<Vec<crate::actions::IssueDecorator>>;
- }
|