github.rs 15 KB

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