Skip to content

Commit

Permalink
feat: set current repo (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
djego authored Sep 29, 2024
1 parent 801dcb6 commit 7c0da3d
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 19 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ cd prt
Then, create a `.env` file with the following content:
````bash
GITHUB_TOKEN=your_github_token
GITHUB_OWNER=your_github_owner
GITHUB_REPO_NAME=your_github_repo
GITHUB_DEFAULT_BRANCH=your_github_default_branch # if not set, it will be 'main'
````
Finally, use the following commands:
Expand All @@ -26,7 +24,7 @@ cargo updatae
cargo run
````

## Demo
## Demo
[![asciicast](https://asciinema.org/a/677701.svg)](https://asciinema.org/a/677701)

Happy coding!
11 changes: 8 additions & 3 deletions src/core/app.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::core::errors::PullRequestError;
use crate::core::git::get_repo_info;
use crate::core::input_mode::InputMode;
use crate::core::pull_request::PullRequest;
use octocrab::models::pulls::PullRequest as OctocrabPullRequest;
Expand All @@ -18,15 +19,19 @@ pub struct App {

impl App {
pub fn new() -> App {
let (repo_owner, repo_name) = match get_repo_info() {
Some((owner, repo)) => (owner, repo),
None => ("-".to_string(), "-".to_string()),
};
App {
pull_request: PullRequest::new(),
input_mode: InputMode::Normal,
current_field: 0,
show_popup: false,
error_message: None,
success_message: None,
repo_owner: std::env::var("GITHUB_OWNER").unwrap_or_else(|_| "owner".to_string()),
repo_name: std::env::var("GITHUB_REPO_NAME").unwrap_or_else(|_| "repo".to_string()),
repo_owner,
repo_name,
default_branch: std::env::var("GITHUB_DEFAULT_BRANCH")
.unwrap_or_else(|_| "main".to_string()),
}
Expand Down Expand Up @@ -96,7 +101,7 @@ impl App {
self.current_field = index;
}

pub fn preview_pull_request(&mut self) {
pub fn confirm_pull_request(&mut self) {
self.input_mode = InputMode::Creating;
self.show_popup = true;
}
Expand Down
11 changes: 3 additions & 8 deletions src/core/app_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,12 @@ mod tests {

#[test]
fn test_app_initialization() {
// Configura las variables de entorno necesarias para la prueba
env::set_var("GITHUB_OWNER", "test_owner");
env::set_var("GITHUB_REPO_NAME", "test_repo");
//let (repo_owner, repo_name) = mock_get_repo_info().unwrap();
env::set_var("GITHUB_DEFAULT_BRANCH", "main");

// Crea una instancia de App
let app = App::new();

// Verifica que la inicialización sea correcta
assert_eq!(app.repo_owner, "test_owner");
assert_eq!(app.repo_name, "test_repo");
//assert_eq!(app.repo_owner, repo_owner);
//assert_eq!(app.repo_name, repo_name);
assert_eq!(app.default_branch, "main");
assert!(app.pull_request.title.is_empty()); // asumiendo que title se inicializa vacío
assert!(app.pull_request.description.is_empty());
Expand Down
32 changes: 32 additions & 0 deletions src/core/git.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use std::process::Command;
use std::str;

pub fn get_repo_info() -> Option<(String, String)> {
let output = Command::new("git")
.arg("config")
.arg("--get")
.arg("remote.origin.url")
.output()
.expect("Failed to execute git command");

if output.status.success() {
let url = str::from_utf8(&output.stdout).unwrap().trim();
if let Some((owner, repo)) = parse_git_url(url) {
return Some((owner.to_string(), repo.to_string()));
}
}

None
}

fn parse_git_url(url: &str) -> Option<(&str, &str)> {
if url.starts_with("https://") || url.starts_with("git@") {
let parts: Vec<&str> = url.rsplitn(2, '/').collect();
if parts.len() == 2 {
let repo = parts[0].trim_end_matches(".git");
let owner = parts[1].rsplitn(2, ':').collect::<Vec<&str>>()[0];
return Some((owner, repo));
}
}
None
}
1 change: 1 addition & 0 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ pub mod input_mode;
pub mod pull_request;

pub mod app_test;
pub mod git;
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
mod core;
mod ui;

use crate::core::app::App;
use crate::core::input_mode::InputMode;
use crate::ui::layout::ui;
use core::app::App;
use crossterm::{
event::{self, Event, KeyCode},
execute,
Expand All @@ -15,12 +15,12 @@ use std::io;

fn main() -> Result<(), io::Error> {
dotenv().ok();

enable_raw_mode()?;
let mut stdout = io::stdout();
execute!(stdout, EnterAlternateScreen)?;
let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;

let mut app = App::new();
let runtime = tokio::runtime::Runtime::new().unwrap();
loop {
Expand Down Expand Up @@ -64,7 +64,7 @@ fn main() -> Result<(), io::Error> {
if current_field_index == 1 {
current_field.push('\n');
} else {
app.preview_pull_request();
app.confirm_pull_request();
}
}
KeyCode::Tab => {
Expand Down
4 changes: 2 additions & 2 deletions src/ui/layout.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::ui::util::{centered_rect, inner_area}; // Importa las funciones utilitarias
use crate::App; // Importa la estructura App
use crate::ui::util::{centered_rect, inner_area};
use crate::App;
use crate::InputMode;
use ratatui::layout::{Constraint, Direction, Layout};
use ratatui::style::Modifier;
Expand Down

0 comments on commit 7c0da3d

Please sign in to comment.