aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTeddy Wing2020-07-19 23:08:45 +0200
committerTeddy Wing2020-07-19 23:08:45 +0200
commitd65b1ab87e4d3dbd7bf7ade653dd64dc5b715368 (patch)
tree6debc9869ba2b3aa9808904f5f27fd25ebda6f1c /src
parentf87ad1230864c271a4e60ad3b6ff3abea950568d (diff)
downloadgit-suggestion-d65b1ab87e4d3dbd7bf7ade653dd64dc5b715368.tar.bz2
Extract diff hunk and comment body from GitHub response
Split the `Suggestion` struct into a new `Client` struct for the GitHub request, and `Suggestion` for the required response data.
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs41
1 files changed, 30 insertions, 11 deletions
diff --git a/src/lib.rs b/src/lib.rs
index c0aeb3b..9343b1f 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -2,6 +2,7 @@
use github_rs::client::{Executor, Github};
+use serde::Deserialize;
use serde_json::Value;
use thiserror::Error;
@@ -10,24 +11,27 @@ use thiserror::Error;
pub enum Error {
#[error("GitHub client error: {0}")]
Github(String),
+
+ #[error("Unable to deserialize")]
+ Deserialize(#[from] serde_json::error::Error),
}
-pub struct Suggestion<'a> {
+pub struct Client<'a> {
client: Github,
owner: &'a str,
repo: &'a str,
}
-impl<'a> Suggestion<'a> {
+impl<'a> Client<'a> {
pub fn new(token: &str, owner: &'a str, repo: &'a str) -> Self {
let client = Github::new(&token).unwrap();
- Suggestion { client, owner, repo }
+ Client { client, owner, repo }
}
- pub fn fetch(&self, id: &str) -> Result<(), Error> {
- let comment = self.client
+ pub fn fetch(&self, id: &str) -> Result<Suggestion, Error> {
+ let response = self.client
.get()
.repos()
.owner(self.owner)
@@ -37,16 +41,28 @@ impl<'a> Suggestion<'a> {
.id(id)
.execute::<Value>();
- match comment {
- Ok((_, _, json)) => {
- println!("{:?}", json);
+ match response {
+ Ok((_, _, Some(json))) => {
+ let suggestion = serde_json::from_value(json)?;
- Ok(())
+ Ok(suggestion)
},
+ Ok((_, _, None)) => Err(Error::Github("no response".to_owned())),
Err(e) => Err(Error::Github(e.to_string())),
}
}
+}
+#[derive(Debug, Deserialize)]
+pub struct Suggestion {
+ #[serde(rename = "diff_hunk")]
+ diff: String,
+
+ #[serde(rename = "body")]
+ suggestion: String,
+}
+
+impl Suggestion {
pub fn patch(&self) {
}
}
@@ -57,11 +73,14 @@ mod tests {
#[test]
fn suggestion_fetch_gets_pull_request_comment() {
- let suggestion = Suggestion::new(
+ let client = Client::new(
env!("GITHUB_TOKEN"),
"cli",
"cli",
);
- suggestion.fetch("438947607").unwrap();
+
+ let suggestion = client.fetch("438947607").unwrap();
+
+ println!("{:?}", suggestion);
}
}