diff options
| author | Teddy Wing | 2020-07-19 18:42:57 +0200 | 
|---|---|---|
| committer | Teddy Wing | 2020-07-19 18:42:57 +0200 | 
| commit | f87ad1230864c271a4e60ad3b6ff3abea950568d (patch) | |
| tree | f97fdb1090a73084268791ee4f608dcc0ff7193c /src | |
| parent | 9d78dbdb0be1e79a1e583bbfa991dac49aefa017 (diff) | |
| download | git-suggestion-f87ad1230864c271a4e60ad3b6ff3abea950568d.tar.bz2 | |
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.
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib.rs | 67 | 
1 files changed, 67 insertions, 0 deletions
| 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::<Value>(); + +        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(); +    } +} | 
