Skip to content

Commit

Permalink
chore(term): add pat validation during github client construction
Browse files Browse the repository at this point in the history
  • Loading branch information
ymgyt committed Aug 31, 2024
1 parent 7ac2a21 commit f75ddf9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
12 changes: 8 additions & 4 deletions crates/synd_term/src/client/github/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
};

#[derive(Debug, Error)]
pub(crate) enum GithubError {
pub enum GithubError {
#[error("invalid credential. please make sure a valid PAT is set")]
BadCredential,
// https://docs.github.com/en/rest/using-the-rest-api/rate-limits-for-the-rest-api?apiVersion=2022-11-28#about-secondary-rate-limits
Expand Down Expand Up @@ -43,16 +43,20 @@ pub struct GithubClient {
}

impl GithubClient {
pub fn new(pat: impl Into<String>) -> Self {
// TODO: configure timeout
pub fn new(pat: impl Into<String>) -> Result<Self, GithubError> {
let pat = pat.into();
if pat.is_empty() {
return Err(GithubError::BadCredential);
}
let timeout = Some(config::github::CLIENT_TIMEOUT);
let octo = Octocrab::builder()
.personal_token(pat)
.set_connect_timeout(timeout)
.set_read_timeout(timeout)
.set_write_timeout(timeout)
.build()
.unwrap();
Self::with(octo)
Ok(Self::with(octo))
}

#[must_use]
Expand Down
4 changes: 3 additions & 1 deletion crates/synd_term/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ fn build_app(config: ConfigResolver, dry_run: bool) -> anyhow::Result<Applicatio
.dry_run(dry_run);

if config.is_github_enable() {
builder = builder.github_client(GithubClient::new(config.github_pat()));
builder = builder.github_client(
GithubClient::new(config.github_pat()).context("Failed to construct github client")?,
);
}

Ok(builder.build())
Expand Down

0 comments on commit f75ddf9

Please sign in to comment.