Skip to content

Commit

Permalink
feat: toml support and add PAT
Browse files Browse the repository at this point in the history
  • Loading branch information
djego committed Sep 30, 2024
1 parent 5864879 commit 78ab4ff
Show file tree
Hide file tree
Showing 9 changed files with 227 additions and 68 deletions.
128 changes: 103 additions & 25 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ crossterm = "0.28.1"
dotenv = "0.15.0"
octocrab = "0.39.0"
ratatui = "0.28.1"
serde = { version = "1.0", features = ["derive"] }
thiserror = "1.0.64"
tokio = { version = "1.40.0", features = ["full"] }
toml = "0.8.19"
19 changes: 10 additions & 9 deletions src/core/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ pub struct App {
pub pull_request: PullRequest,
pub input_mode: InputMode,
pub current_field: usize,
pub show_popup: bool,
pub show_confirm_popup: bool,
pub show_pat_popup: bool,
pub repo_owner: String,
pub repo_name: String,
pub default_target_branch: String,
pub config_pat: String,
}

impl App {
Expand All @@ -28,9 +30,11 @@ impl App {
pull_request: PullRequest::new(current_branch.clone()),
input_mode: InputMode::Normal,
current_field: 0,
show_popup: false,
show_confirm_popup: false,
show_pat_popup: false,
error_message: None,
success_message: None,
config_pat: String::new(),
repo_owner,
repo_name,
default_target_branch: std::env::var("GITHUB_DEFAULT_TARGET_BRANCH")
Expand All @@ -40,12 +44,9 @@ impl App {

pub async fn create_github_pull_request(
&self,
pat: String,
) -> Result<OctocrabPullRequest, PullRequestError> {
let github_token = std::env::var("GITHUB_TOKEN")
.map_err(|_| PullRequestError::PATNotSet("Github PAT not set".to_string()))?;

let octocrab = Octocrab::builder().personal_token(github_token).build()?;

let octocrab = Octocrab::builder().personal_token(pat).build()?;
if self.pull_request.source_branch.is_empty() {
return Err(PullRequestError::InvalidInput(
"Source branch is empty".to_string(),
Expand Down Expand Up @@ -103,14 +104,14 @@ impl App {

pub fn confirm_pull_request(&mut self) {
self.input_mode = InputMode::Creating;
self.show_popup = true;
self.show_confirm_popup = true;
}

pub fn reset(&mut self) {
self.pull_request = PullRequest::new(self.pull_request.source_branch.clone());
self.input_mode = InputMode::Normal;
self.current_field = 0;
self.show_popup = false;
self.show_confirm_popup = false;
self.error_message = None;
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/app_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ mod tests {
assert!(app.error_message.is_none());
assert_eq!(app.input_mode, InputMode::Normal);
assert_eq!(app.current_field, 0);
assert!(!app.show_popup);
assert!(!app.show_confirm_popup);
}

#[test]
Expand Down
35 changes: 35 additions & 0 deletions src/core/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use serde::{Deserialize, Serialize};
use std::fs::{self, File};
use std::io::{self, Write};

#[derive(Deserialize, Serialize)]
pub struct Config {
pub github: GitHubConfig,
}

#[derive(Deserialize, Serialize)]
pub struct GitHubConfig {
pub pat: String,
}

pub fn load_config() -> Option<Config> {
if let Ok(config_content) = fs::read_to_string("config.toml") {
let config: Config =
toml::from_str(&config_content).expect("Failed to parse configuration file");
Some(config)
} else {
None // No existe el archivo
}
}

pub fn save_config(pat: &str) -> Result<(), io::Error> {
let config = Config {
github: GitHubConfig {
pat: pat.to_string(),
},
};
let toml_str = toml::to_string(&config).expect("Failed to serialize configuration");
let mut file = File::create("config.toml")?;
file.write_all(toml_str.as_bytes())?;
Ok(())
}
Loading

0 comments on commit 78ab4ff

Please sign in to comment.