Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use octocrab for GraphQL queries #187

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions spr/src/commands/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use crate::error::Error;
use crate::error::Result;
use graphql_client::{GraphQLQuery, Response};
use reqwest;

#[allow(clippy::upper_case_acronyms)]
type URI = String;
Expand All @@ -21,7 +20,6 @@ type URI = String;
pub struct SearchQuery;

pub async fn list(
graphql_client: reqwest::Client,
config: &crate::config::Config,
) -> Result<()> {
let variables = search_query::Variables {
Expand All @@ -31,13 +29,9 @@ pub async fn list(
),
};
let request_body = SearchQuery::build_query(variables);
let res = graphql_client
.post("https://api.github.com/graphql")
.json(&request_body)
.send()
let response_body: Response<search_query::ResponseData> = octocrab::instance()
.post("graphql",Some(&request_body))
.await?;
let response_body: Response<search_query::ResponseData> =
res.json().await?;

print_pr_info(response_body).ok_or_else(|| Error::new("unexpected error"))
}
Expand Down
25 changes: 6 additions & 19 deletions spr/src/github.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use std::collections::{HashMap, HashSet};
pub struct GitHub {
config: crate::config::Config,
git: crate::git::Git,
graphql_client: reqwest::Client,
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -134,12 +133,10 @@ impl GitHub {
pub fn new(
config: crate::config::Config,
git: crate::git::Git,
graphql_client: reqwest::Client,
) -> Self {
Self {
config,
git,
graphql_client,
}
}

Expand All @@ -165,7 +162,6 @@ impl GitHub {
let GitHub {
config,
git,
graphql_client,
} = self;

let variables = pull_request_query::Variables {
Expand All @@ -174,13 +170,9 @@ impl GitHub {
number: number as i64,
};
let request_body = PullRequestQuery::build_query(variables);
let res = graphql_client
.post("https://api.github.com/graphql")
.json(&request_body)
.send()
let response_body: Response<pull_request_query::ResponseData> = octocrab::instance()
.post("graphql", Some(&request_body))
.await?;
let response_body: Response<pull_request_query::ResponseData> =
res.json().await?;

if let Some(errors) = response_body.errors {
let error =
Expand Down Expand Up @@ -395,15 +387,10 @@ impl GitHub {
number: number as i64,
};
let request_body = PullRequestMergeabilityQuery::build_query(variables);
let res = self
.graphql_client
.post("https://api.github.com/graphql")
.json(&request_body)
.send()
.await?;
let response_body: Response<
pull_request_mergeability_query::ResponseData,
> = res.json().await?;
let response_body: Response<pull_request_mergeability_query::ResponseData> =
octocrab::instance()
.post("graphql", Some(&request_body))
.await?;

if let Some(errors) = response_body.errors {
let error = Err(Error::new(format!(
Expand Down
7 changes: 1 addition & 6 deletions spr/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,9 @@ pub async fn spr() -> Result<()> {
format!("Bearer {}", github_auth_token).parse()?,
);

let graphql_client = reqwest::Client::builder()
.default_headers(headers)
.build()?;

let mut gh = spr::github::GitHub::new(
config.clone(),
git.clone(),
graphql_client.clone(),
);

match cli.command {
Expand All @@ -192,7 +187,7 @@ pub async fn spr() -> Result<()> {
Commands::Amend(opts) => {
commands::amend::amend(opts, &git, &mut gh, &config).await?
}
Commands::List => commands::list::list(graphql_client, &config).await?,
Commands::List => commands::list::list(&config).await?,
Commands::Patch(opts) => {
commands::patch::patch(opts, &git, &mut gh, &config).await?
}
Expand Down