github.rs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. use anyhow::Context;
  2. use chrono::{FixedOffset, Utc};
  3. use futures::stream::{FuturesUnordered, StreamExt};
  4. use once_cell::sync::OnceCell;
  5. use reqwest::header::{AUTHORIZATION, USER_AGENT};
  6. use reqwest::{Client, RequestBuilder, Response, StatusCode};
  7. use std::fmt;
  8. #[derive(Debug, PartialEq, Eq, serde::Deserialize)]
  9. pub struct User {
  10. pub login: String,
  11. pub id: Option<i64>,
  12. }
  13. impl GithubClient {
  14. async fn _send_req(&self, req: RequestBuilder) -> Result<(Response, String), reqwest::Error> {
  15. log::debug!("_send_req with {:?}", req);
  16. let req = req.build()?;
  17. let req_dbg = format!("{:?}", req);
  18. let resp = self.client.execute(req).await?;
  19. resp.error_for_status_ref()?;
  20. Ok((resp, req_dbg))
  21. }
  22. async fn send_req(&self, req: RequestBuilder) -> anyhow::Result<Vec<u8>> {
  23. let (mut resp, req_dbg) = self._send_req(req).await?;
  24. let mut body = Vec::new();
  25. while let Some(chunk) = resp.chunk().await.transpose() {
  26. let chunk = chunk
  27. .context("reading stream failed")
  28. .map_err(anyhow::Error::from)
  29. .context(req_dbg.clone())?;
  30. body.extend_from_slice(&chunk);
  31. }
  32. Ok(body)
  33. }
  34. async fn json<T>(&self, req: RequestBuilder) -> anyhow::Result<T>
  35. where
  36. T: serde::de::DeserializeOwned,
  37. {
  38. let (resp, req_dbg) = self._send_req(req).await?;
  39. Ok(resp.json().await.context(req_dbg)?)
  40. }
  41. }
  42. impl User {
  43. pub async fn current(client: &GithubClient) -> anyhow::Result<Self> {
  44. client.json(client.get("https://api.github.com/user")).await
  45. }
  46. pub async fn is_team_member<'a>(&'a self, client: &'a GithubClient) -> anyhow::Result<bool> {
  47. let url = format!("{}/teams.json", rust_team_data::v1::BASE_URL);
  48. let permission: rust_team_data::v1::Teams = client
  49. .json(client.raw().get(&url))
  50. .await
  51. .context("could not get team data")?;
  52. let map = permission.teams;
  53. let is_triager = map
  54. .get("wg-triage")
  55. .map_or(false, |w| w.members.iter().any(|g| g.github == self.login));
  56. Ok(map["all"].members.iter().any(|g| g.github == self.login) || is_triager)
  57. }
  58. // Returns the ID of the given user, if the user is in the `all` team.
  59. pub async fn get_id<'a>(&'a self, client: &'a GithubClient) -> anyhow::Result<Option<usize>> {
  60. let url = format!("{}/teams.json", rust_team_data::v1::BASE_URL);
  61. let permission: rust_team_data::v1::Teams = client
  62. .json(client.raw().get(&url))
  63. .await
  64. .context("could not get team data")?;
  65. let map = permission.teams;
  66. Ok(map["all"]
  67. .members
  68. .iter()
  69. .find(|g| g.github == self.login)
  70. .map(|u| u.github_id))
  71. }
  72. }
  73. pub async fn get_team(
  74. client: &GithubClient,
  75. team: &str,
  76. ) -> anyhow::Result<Option<rust_team_data::v1::Team>> {
  77. let url = format!("{}/teams.json", rust_team_data::v1::BASE_URL);
  78. let permission: rust_team_data::v1::Teams = client
  79. .json(client.raw().get(&url))
  80. .await
  81. .context("could not get team data")?;
  82. let mut map = permission.teams;
  83. Ok(map.swap_remove(team))
  84. }
  85. #[derive(PartialEq, Eq, Debug, Clone, serde::Deserialize)]
  86. pub struct Label {
  87. pub name: String,
  88. }
  89. impl Label {
  90. async fn exists<'a>(&'a self, repo_api_prefix: &'a str, client: &'a GithubClient) -> bool {
  91. #[allow(clippy::redundant_pattern_matching)]
  92. let url = format!("{}/labels/{}", repo_api_prefix, self.name);
  93. match client.send_req(client.get(&url)).await {
  94. Ok(_) => true,
  95. // XXX: Error handling if the request failed for reasons beyond 'label didn't exist'
  96. Err(_) => false,
  97. }
  98. }
  99. }
  100. #[derive(Debug, serde::Deserialize)]
  101. pub struct PullRequestDetails {
  102. // none for now
  103. }
  104. #[derive(Debug, serde::Deserialize)]
  105. pub struct Issue {
  106. pub number: u64,
  107. pub body: String,
  108. created_at: chrono::DateTime<Utc>,
  109. title: String,
  110. html_url: String,
  111. pub user: User,
  112. labels: Vec<Label>,
  113. assignees: Vec<User>,
  114. pull_request: Option<PullRequestDetails>,
  115. // API URL
  116. repository_url: String,
  117. comments_url: String,
  118. #[serde(skip)]
  119. repository: OnceCell<IssueRepository>,
  120. }
  121. #[derive(Debug, serde::Deserialize)]
  122. pub struct Comment {
  123. pub body: String,
  124. pub html_url: String,
  125. pub user: User,
  126. pub updated_at: chrono::DateTime<Utc>,
  127. }
  128. #[derive(Debug)]
  129. pub enum AssignmentError {
  130. InvalidAssignee,
  131. Http(reqwest::Error),
  132. }
  133. #[derive(Debug)]
  134. pub enum Selection<'a, T> {
  135. All,
  136. One(&'a T),
  137. }
  138. impl fmt::Display for AssignmentError {
  139. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  140. match self {
  141. AssignmentError::InvalidAssignee => write!(f, "invalid assignee"),
  142. AssignmentError::Http(e) => write!(f, "cannot assign: {}", e),
  143. }
  144. }
  145. }
  146. impl std::error::Error for AssignmentError {}
  147. #[derive(Debug)]
  148. pub struct IssueRepository {
  149. pub organization: String,
  150. pub repository: String,
  151. }
  152. impl fmt::Display for IssueRepository {
  153. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  154. write!(f, "{}/{}", self.organization, self.repository)
  155. }
  156. }
  157. impl Issue {
  158. pub fn repository(&self) -> &IssueRepository {
  159. self.repository.get_or_init(|| {
  160. log::trace!("get repository for {}", self.repository_url);
  161. let url = url::Url::parse(&self.repository_url).unwrap();
  162. let mut segments = url.path_segments().unwrap();
  163. let repository = segments.nth_back(0).unwrap();
  164. let organization = segments.nth_back(1).unwrap();
  165. IssueRepository {
  166. organization: organization.into(),
  167. repository: repository.into(),
  168. }
  169. })
  170. }
  171. pub fn global_id(&self) -> String {
  172. format!("{}#{}", self.repository(), self.number)
  173. }
  174. pub fn is_pr(&self) -> bool {
  175. self.pull_request.is_some()
  176. }
  177. pub async fn get_comment(&self, client: &GithubClient, id: usize) -> anyhow::Result<Comment> {
  178. let comment_url = format!("{}/issues/comments/{}", self.repository_url, id);
  179. let comment = client.json(client.get(&comment_url)).await?;
  180. Ok(comment)
  181. }
  182. pub async fn edit_body(&self, client: &GithubClient, body: &str) -> anyhow::Result<()> {
  183. let edit_url = format!("{}/issues/{}", self.repository_url, self.number);
  184. #[derive(serde::Serialize)]
  185. struct ChangedIssue<'a> {
  186. body: &'a str,
  187. }
  188. client
  189. ._send_req(client.patch(&edit_url).json(&ChangedIssue { body }))
  190. .await
  191. .context("failed to edit issue body")?;
  192. Ok(())
  193. }
  194. pub async fn edit_comment(
  195. &self,
  196. client: &GithubClient,
  197. id: usize,
  198. new_body: &str,
  199. ) -> anyhow::Result<()> {
  200. let comment_url = format!("{}/issues/comments/{}", self.repository_url, id);
  201. #[derive(serde::Serialize)]
  202. struct NewComment<'a> {
  203. body: &'a str,
  204. }
  205. client
  206. ._send_req(
  207. client
  208. .patch(&comment_url)
  209. .json(&NewComment { body: new_body }),
  210. )
  211. .await
  212. .context("failed to edit comment")?;
  213. Ok(())
  214. }
  215. pub async fn post_comment(&self, client: &GithubClient, body: &str) -> anyhow::Result<()> {
  216. #[derive(serde::Serialize)]
  217. struct PostComment<'a> {
  218. body: &'a str,
  219. }
  220. client
  221. ._send_req(client.post(&self.comments_url).json(&PostComment { body }))
  222. .await
  223. .context("failed to post comment")?;
  224. Ok(())
  225. }
  226. pub async fn set_labels(
  227. &self,
  228. client: &GithubClient,
  229. labels: Vec<Label>,
  230. ) -> anyhow::Result<()> {
  231. log::info!("set_labels {} to {:?}", self.global_id(), labels);
  232. // PUT /repos/:owner/:repo/issues/:number/labels
  233. // repo_url = https://api.github.com/repos/Codertocat/Hello-World
  234. let url = format!(
  235. "{repo_url}/issues/{number}/labels",
  236. repo_url = self.repository_url,
  237. number = self.number
  238. );
  239. let mut stream = labels
  240. .into_iter()
  241. .map(|label| async { (label.exists(&self.repository_url, &client).await, label) })
  242. .collect::<FuturesUnordered<_>>();
  243. let mut labels = Vec::new();
  244. while let Some((true, label)) = stream.next().await {
  245. labels.push(label);
  246. }
  247. #[derive(serde::Serialize)]
  248. struct LabelsReq {
  249. labels: Vec<String>,
  250. }
  251. client
  252. ._send_req(client.put(&url).json(&LabelsReq {
  253. labels: labels.iter().map(|l| l.name.clone()).collect(),
  254. }))
  255. .await
  256. .context("failed to set labels")?;
  257. Ok(())
  258. }
  259. pub fn labels(&self) -> &[Label] {
  260. &self.labels
  261. }
  262. pub fn contain_assignee(&self, user: &User) -> bool {
  263. self.assignees.contains(user)
  264. }
  265. pub async fn remove_assignees(
  266. &self,
  267. client: &GithubClient,
  268. selection: Selection<'_, User>,
  269. ) -> Result<(), AssignmentError> {
  270. log::info!("remove {:?} assignees for {}", selection, self.global_id());
  271. let url = format!(
  272. "{repo_url}/issues/{number}/assignees",
  273. repo_url = self.repository_url,
  274. number = self.number
  275. );
  276. let assignees = match selection {
  277. Selection::All => self
  278. .assignees
  279. .iter()
  280. .map(|u| u.login.as_str())
  281. .collect::<Vec<_>>(),
  282. Selection::One(user) => vec![user.login.as_str()],
  283. };
  284. #[derive(serde::Serialize)]
  285. struct AssigneeReq<'a> {
  286. assignees: &'a [&'a str],
  287. }
  288. client
  289. ._send_req(client.delete(&url).json(&AssigneeReq {
  290. assignees: &assignees[..],
  291. }))
  292. .await
  293. .map_err(AssignmentError::Http)?;
  294. Ok(())
  295. }
  296. pub async fn set_assignee(
  297. &self,
  298. client: &GithubClient,
  299. user: &str,
  300. ) -> Result<(), AssignmentError> {
  301. log::info!("set_assignee for {} to {}", self.global_id(), user);
  302. let url = format!(
  303. "{repo_url}/issues/{number}/assignees",
  304. repo_url = self.repository_url,
  305. number = self.number
  306. );
  307. let check_url = format!(
  308. "{repo_url}/assignees/{name}",
  309. repo_url = self.repository_url,
  310. name = user,
  311. );
  312. match client._send_req(client.get(&check_url)).await {
  313. Ok((resp, _)) => {
  314. if resp.status() == reqwest::StatusCode::NO_CONTENT {
  315. // all okay
  316. log::debug!("set_assignee: assignee is valid");
  317. } else {
  318. log::error!(
  319. "unknown status for assignee check, assuming all okay: {:?}",
  320. resp
  321. );
  322. }
  323. }
  324. Err(e) => {
  325. if e.status() == Some(reqwest::StatusCode::NOT_FOUND) {
  326. log::debug!("set_assignee: assignee is invalid, returning");
  327. return Err(AssignmentError::InvalidAssignee);
  328. }
  329. log::debug!("set_assignee: get {} failed, {:?}", check_url, e);
  330. return Err(AssignmentError::Http(e));
  331. }
  332. }
  333. self.remove_assignees(client, Selection::All).await?;
  334. #[derive(serde::Serialize)]
  335. struct AssigneeReq<'a> {
  336. assignees: &'a [&'a str],
  337. }
  338. client
  339. ._send_req(client.post(&url).json(&AssigneeReq { assignees: &[user] }))
  340. .await
  341. .map_err(AssignmentError::Http)?;
  342. Ok(())
  343. }
  344. }
  345. #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
  346. #[serde(rename_all = "lowercase")]
  347. pub enum IssueCommentAction {
  348. Created,
  349. Edited,
  350. Deleted,
  351. }
  352. #[derive(Debug, serde::Deserialize)]
  353. pub struct IssueCommentEvent {
  354. pub action: IssueCommentAction,
  355. pub issue: Issue,
  356. pub comment: Comment,
  357. pub repository: Repository,
  358. }
  359. #[derive(PartialEq, Eq, Debug, serde::Deserialize)]
  360. #[serde(rename_all = "lowercase")]
  361. pub enum IssuesAction {
  362. Opened,
  363. Edited,
  364. Deleted,
  365. Transferred,
  366. Pinned,
  367. Unpinned,
  368. Closed,
  369. Reopened,
  370. Assigned,
  371. Unassigned,
  372. Labeled,
  373. Unlabeled,
  374. Locked,
  375. Unlocked,
  376. Milestoned,
  377. Demilestoned,
  378. }
  379. #[derive(Debug, serde::Deserialize)]
  380. pub struct IssuesEvent {
  381. pub action: IssuesAction,
  382. pub issue: Issue,
  383. pub repository: Repository,
  384. }
  385. #[derive(Debug, serde::Deserialize)]
  386. pub struct Repository {
  387. pub full_name: String,
  388. }
  389. #[derive(Debug)]
  390. pub enum Event {
  391. IssueComment(IssueCommentEvent),
  392. Issue(IssuesEvent),
  393. }
  394. impl Event {
  395. pub fn repo_name(&self) -> &str {
  396. match self {
  397. Event::IssueComment(event) => &event.repository.full_name,
  398. Event::Issue(event) => &event.repository.full_name,
  399. }
  400. }
  401. pub fn issue(&self) -> Option<&Issue> {
  402. match self {
  403. Event::IssueComment(event) => Some(&event.issue),
  404. Event::Issue(event) => Some(&event.issue),
  405. }
  406. }
  407. /// This will both extract from IssueComment events but also Issue events
  408. pub fn comment_body(&self) -> Option<&str> {
  409. match self {
  410. Event::Issue(e) => Some(&e.issue.body),
  411. Event::IssueComment(e) => Some(&e.comment.body),
  412. }
  413. }
  414. pub fn html_url(&self) -> Option<&str> {
  415. match self {
  416. Event::Issue(e) => Some(&e.issue.html_url),
  417. Event::IssueComment(e) => Some(&e.comment.html_url),
  418. }
  419. }
  420. pub fn user(&self) -> &User {
  421. match self {
  422. Event::Issue(e) => &e.issue.user,
  423. Event::IssueComment(e) => &e.comment.user,
  424. }
  425. }
  426. pub fn time(&self) -> chrono::DateTime<FixedOffset> {
  427. match self {
  428. Event::Issue(e) => e.issue.created_at.into(),
  429. Event::IssueComment(e) => e.comment.updated_at.into(),
  430. }
  431. }
  432. }
  433. trait RequestSend: Sized {
  434. fn configure(self, g: &GithubClient) -> Self;
  435. }
  436. impl RequestSend for RequestBuilder {
  437. fn configure(self, g: &GithubClient) -> RequestBuilder {
  438. self.header(USER_AGENT, "rust-lang-triagebot")
  439. .header(AUTHORIZATION, format!("token {}", g.token))
  440. }
  441. }
  442. #[derive(Clone)]
  443. pub struct GithubClient {
  444. token: String,
  445. client: Client,
  446. }
  447. impl GithubClient {
  448. pub fn new(client: Client, token: String) -> Self {
  449. GithubClient { client, token }
  450. }
  451. pub fn raw(&self) -> &Client {
  452. &self.client
  453. }
  454. pub async fn raw_file(
  455. &self,
  456. repo: &str,
  457. branch: &str,
  458. path: &str,
  459. ) -> anyhow::Result<Option<Vec<u8>>> {
  460. let url = format!(
  461. "https://raw.githubusercontent.com/{}/{}/{}",
  462. repo, branch, path
  463. );
  464. let req = self.get(&url);
  465. let req_dbg = format!("{:?}", req);
  466. let req = req
  467. .build()
  468. .with_context(|| format!("failed to build request {:?}", req_dbg))?;
  469. let mut resp = self.client.execute(req).await.context(req_dbg.clone())?;
  470. let status = resp.status();
  471. match status {
  472. StatusCode::OK => {
  473. let mut buf = Vec::with_capacity(resp.content_length().unwrap_or(4) as usize);
  474. while let Some(chunk) = resp.chunk().await.transpose() {
  475. let chunk = chunk
  476. .context("reading stream failed")
  477. .map_err(anyhow::Error::from)
  478. .context(req_dbg.clone())?;
  479. buf.extend_from_slice(&chunk);
  480. }
  481. Ok(Some(buf))
  482. }
  483. StatusCode::NOT_FOUND => Ok(None),
  484. status => anyhow::bail!("failed to GET {}: {}", url, status),
  485. }
  486. }
  487. fn get(&self, url: &str) -> RequestBuilder {
  488. log::trace!("get {:?}", url);
  489. self.client.get(url).configure(self)
  490. }
  491. fn patch(&self, url: &str) -> RequestBuilder {
  492. log::trace!("patch {:?}", url);
  493. self.client.patch(url).configure(self)
  494. }
  495. fn delete(&self, url: &str) -> RequestBuilder {
  496. log::trace!("delete {:?}", url);
  497. self.client.delete(url).configure(self)
  498. }
  499. fn post(&self, url: &str) -> RequestBuilder {
  500. log::trace!("post {:?}", url);
  501. self.client.post(url).configure(self)
  502. }
  503. fn put(&self, url: &str) -> RequestBuilder {
  504. log::trace!("put {:?}", url);
  505. self.client.put(url).configure(self)
  506. }
  507. }