Skip to content

Commit

Permalink
Why is the number field not present?
Browse files Browse the repository at this point in the history
  • Loading branch information
elegaanz committed Sep 25, 2024
1 parent bd10ce9 commit f43af1a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
21 changes: 19 additions & 2 deletions src/github/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use jwt_simple::{
claims::Claims,
reexports::coarsetime::Duration,
};
use reqwest::{RequestBuilder, StatusCode};
use reqwest::{RequestBuilder, Response, StatusCode};
use serde::Deserialize;
use tracing::{debug, warn};

Expand Down Expand Up @@ -81,7 +81,7 @@ impl GitHub {
.post(format!("app/installations/{installation}/access_tokens"))
.send()
.await?
.json()
.parse_json()
.await?;
self.jwt = installation_token.token;

Expand Down Expand Up @@ -252,3 +252,20 @@ pub struct Installation {
struct InstallationToken {
token: String,
}

trait JsonExt {
async fn parse_json<T: for<'a> Deserialize<'a>>(self) -> Result<T, ApiError>;
}

impl JsonExt for Response {
async fn parse_json<T: for<'a> Deserialize<'a>>(self) -> Result<T, ApiError> {
let bytes = self.bytes().await?;

debug!(
"Parsing JSON: {}",
std::str::from_utf8(&bytes).unwrap_or("[INVALID UTF8]")
);

Ok(serde_json::from_slice(&bytes)?)
}
}
30 changes: 14 additions & 16 deletions src/github/api/pr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde::{Deserialize, Serialize};

use super::{ApiError, GitHub, OwnerId, RepoId};
use super::{ApiError, GitHub, JsonExt, OwnerId, RepoId};

#[derive(Clone, Debug, Deserialize)]
pub struct MinimalPullRequest {
Expand All @@ -14,17 +14,16 @@ impl MinimalPullRequest {
owner: OwnerId,
repo: RepoId,
) -> Result<PullRequest, ApiError> {
Ok(api
.get(format!(
"repos/{owner}/{repo}/pulls/{pull_number}",
owner = owner,
repo = repo,
pull_number = self.number
))
.send()
.await?
.json()
.await?)
api.get(format!(
"repos/{owner}/{repo}/pulls/{pull_number}",
owner = owner,
repo = repo,
pull_number = self.number
))
.send()
.await?
.parse_json()
.await
}
}

Expand Down Expand Up @@ -74,12 +73,11 @@ impl GitHub {
pr: usize,
update: PullRequestUpdate,
) -> Result<PullRequest, ApiError> {
Ok(self
.patch(format!("{}/{}/pulls/{}", owner, repo, pr))
self.patch(format!("{}/{}/pulls/{}", owner, repo, pr))
.json(&update)
.send()
.await?
.json()
.await?)
.parse_json()
.await
}
}

0 comments on commit f43af1a

Please sign in to comment.