From f87ad1230864c271a4e60ad3b6ff3abea950568d Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sun, 19 Jul 2020 18:42:57 +0200 Subject: Fetch a GitHub PR suggestion comment Add a `Suggestion` struct to represent a GitHub PR suggestion comment. Use the 'github-rs' library to fetch a given comment from the site by its ID. Converted the 'github-rs' error into a string because its `github_rs::client::Error` type is private, so I can't define an error variant source with that type. --- src/lib.rs | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/lib.rs (limited to 'src') diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..c0aeb3b --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,67 @@ +#![warn(rust_2018_idioms)] + + +use github_rs::client::{Executor, Github}; +use serde_json::Value; +use thiserror::Error; + + +#[derive(Debug, Error)] +pub enum Error { + #[error("GitHub client error: {0}")] + Github(String), +} + + +pub struct Suggestion<'a> { + client: Github, + owner: &'a str, + repo: &'a str, +} + +impl<'a> Suggestion<'a> { + pub fn new(token: &str, owner: &'a str, repo: &'a str) -> Self { + let client = Github::new(&token).unwrap(); + + Suggestion { client, owner, repo } + } + + pub fn fetch(&self, id: &str) -> Result<(), Error> { + let comment = self.client + .get() + .repos() + .owner(self.owner) + .repo(self.repo) + .pulls() + .comments() + .id(id) + .execute::(); + + match comment { + Ok((_, _, json)) => { + println!("{:?}", json); + + Ok(()) + }, + Err(e) => Err(Error::Github(e.to_string())), + } + } + + pub fn patch(&self) { + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn suggestion_fetch_gets_pull_request_comment() { + let suggestion = Suggestion::new( + env!("GITHUB_TOKEN"), + "cli", + "cli", + ); + suggestion.fetch("438947607").unwrap(); + } +} -- cgit v1.2.3