-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.rs
More file actions
93 lines (77 loc) · 2.51 KB
/
api.rs
File metadata and controls
93 lines (77 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
//! GitHub API operations
use super::client::GitHubClient;
use super::types::{PrOptions, PullRequestParams};
use crate::config::Repository;
use crate::constants::github::{DEFAULT_BRANCH_PREFIX, UUID_LENGTH};
use crate::git;
use anyhow::Result;
use colored::*;
use uuid::Uuid;
/// Create a pull request for a repository
pub async fn create_pull_request(repo: &Repository, options: &PrOptions) -> Result<()> {
let repo_path = repo.get_target_dir();
// Check if repository has changes
if !git::has_changes(&repo_path)? {
println!(
"{} | {}",
repo.name.cyan().bold(),
"No changes detected".yellow()
);
return Ok(());
}
// Generate branch name if not provided
let branch_name = options.branch_name.clone().unwrap_or_else(|| {
format!(
"{}-{}",
DEFAULT_BRANCH_PREFIX,
&Uuid::new_v4().simple().to_string()[..UUID_LENGTH]
)
});
// Create and checkout new branch
git::create_and_checkout_branch(&repo_path, &branch_name)?;
// Add all changes
git::add_all_changes(&repo_path)?;
// Commit changes
let commit_message = options
.commit_msg
.clone()
.unwrap_or_else(|| options.title.clone());
git::commit_changes(&repo_path, &commit_message)?;
if !options.create_only {
// Push branch
git::push_branch(&repo_path, &branch_name)?;
// Create PR via GitHub API
create_github_pr(repo, &branch_name, options).await?;
}
Ok(())
}
async fn create_github_pr(repo: &Repository, branch_name: &str, options: &PrOptions) -> Result<()> {
let client = GitHubClient::new(Some(options.token.clone()));
// Extract owner and repo name from URL
let (owner, repo_name) = client.parse_github_url(&repo.url)?;
// Determine base branch - get actual default branch if not specified
let base_branch = if let Some(ref base) = options.base_branch {
base.clone()
} else {
git::get_default_branch(&repo.get_target_dir())?
};
let result = client
.create_pull_request(PullRequestParams::new(
&owner,
&repo_name,
&options.title,
&options.body,
branch_name,
&base_branch,
options.draft,
))
.await?;
let pr_url = result["html_url"].as_str().unwrap_or("unknown");
println!(
"{} | {} {}",
repo.name.cyan().bold(),
"Pull request created:".green(),
pr_url
);
Ok(())
}