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

v2.3.X/dev issue#31 #57

Merged
merged 6 commits into from
Mar 5, 2024
Merged
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
19 changes: 19 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## PR

### CheckList(PR Assigners)

确认已完成以下操作:

- [ ] 已将分支与当前合入分支同步
- [ ] 已在关联 Issue 中补充「功能自测」部分内容
- [ ] 已更新关联 Issue 的状态 `backlog` -> `todo` -> `for test` -> `tested(需通过测试同学测试)` -> `done`
- [ ] 将 PR 添加上 `pr/reviewable`


### CheckList(PR Reviewers)

确认已完成以下操作:

- [ ] 检查关联 Issue 「功能自测」部分内容
- [ ] 必要时打回 `pr/reviewable`,要求重新修改
- [ ] 及时 Approve
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct Config {
pub remote_branch_name_template: String,
pub commit_custom_params: String,
pub git_flow: Vec<HashMap<String, String>>,
pub fork_remote_name: Option<String>,
}
#[cfg(target_os = "windows")]
pub const CONFIG_PATH: &str = "C:\\etc\\ncommit.toml";
Expand Down Expand Up @@ -87,6 +88,7 @@ pub fn load_config(config_path: &str) -> Result<Config, Box<dyn Error>> {
let current_path: String = get_current_path()?;
let project_path: PathBuf = PathBuf::from(project_path.to_string());
let current_path_buf: PathBuf = PathBuf::from(current_path.to_string());

if current_path_buf == project_path {
result_config = Some(project_config);
break;
Expand Down
45 changes: 37 additions & 8 deletions src/flow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,31 @@ pub trait GitCommand {
fn error_msg_transcate(&self, output: &str, msg: &str) {
use regex::Regex;
let label_nout_found = Regex::new(r"failed to update.* not found").unwrap();
let issue_id_not_found = Regex::new(r"GraphQL: Could not resolve to an issue or pull request with the number o.*").unwrap();
let issue_id_not_found = Regex::new(
r"GraphQL: Could not resolve to an issue or pull request with the number o.*",
)
.unwrap();
if label_nout_found.is_match(output) {
let log = format!("不存在的标签: {}", msg.to_string());
info!("{}", log);
return
return;
}
if issue_id_not_found.is_match(output) {
let log = format!("不存在这个 ISSUE ID");
info!("{}", log);
return;
}
let pr_exist =
Regex::new(r"a pull request for branch.* into branch .*already exists:").unwrap();
if pr_exist.is_match(output) {
let log = format!("已经存在这个 PR");
info!("{}", log);
}
let no_version_branch = Regex::new(r"could not compute title or body defaults: failed to run git.*unknown revision or path not in the working tree").unwrap();
if no_version_branch.is_match(output) {
let log = format!("下面这个报错应该是本地没有这个分支, 请先拉取远程分支: git fetch --all");
error!("{}", log);
}
error!("{}", output)
}
fn run_git_command_with_string(
Expand All @@ -60,7 +74,12 @@ pub trait GitCommand {
let commands = command.split(" ").collect::<Vec<&str>>();
let git_command = commands[0];
let git_args = &commands[1..];
Command::new(git_command).args(git_args).output()
let strip_args = git_args
.iter()
.map(|arg| arg.replace("\n", "").to_string()) // Change the type to String
.collect::<Vec<String>>(); // Change the type to Vec<String>
debug!("git command: {}, git args: {:#?}", git_command, strip_args);
Command::new(git_command).args(strip_args).output()
}
fn run_command_and_check_code(&self, command: &str) -> Vec<u8> {
if let Ok(output) = self.run_git_command_with_string(command) {
Expand Down Expand Up @@ -128,9 +147,19 @@ impl GitFlowCommandFunc for GitFlowCommand {
}
}
}
let in_config_labels = current_labels.iter().filter(|label| choice_labels.contains(label)).collect::<Vec<&String>>();
let covert_config_labels = in_config_labels.iter().map(|label| label.as_str()).collect::<Vec<&str>>().join(",");
let label_msg = format!("选择你想要切换到的标签, 当前 Issue Id: [{}] 当前 Labels: [{}]", self.id, covert_config_labels);
let in_config_labels = current_labels
.iter()
.filter(|label| choice_labels.contains(label))
.collect::<Vec<&String>>();
let covert_config_labels = in_config_labels
.iter()
.map(|label| label.as_str())
.collect::<Vec<&str>>()
.join(",");
let label_msg = format!(
"选择你想要切换到的标签, 当前 Issue Id: [{}] 当前 Labels: [{}]",
self.id, covert_config_labels
);
if let Ok(choice) = Select::new(&label_msg, choice_labels.clone()).prompt() {
info!("选择了 {}", choice);
self.run_command_and_check_code(
Expand Down Expand Up @@ -198,16 +227,16 @@ mod tests {
}
#[test]
fn test_branch_re() {
use regex::Regex;
use crate::config;
use regex::Regex;
let config = config::load_config(config::CONFIG_PATH).unwrap();
use crate::parse_branch_issue_id;
let re = Regex::new(r".*issue#?(\d+).*").unwrap();
let branch_name = "_V2.3.X/dev_issue#27".to_string();
if re.is_match(&branch_name) {
let caps = re.captures(&branch_name).unwrap();
println!("{:?}", caps.get(1).unwrap().as_str());
}else {
} else {
println!("no match");
std::process::exit(1);
}
Expand Down
89 changes: 74 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use clap::Parser;
use handlebars::Handlebars;
use inquire::Select;
use inquire::Text;
use log::error;
use regex::Regex;
use serde::{Deserialize, Serialize};
use serde_json::json;
Expand All @@ -12,14 +11,16 @@ use std::str;
use version_compare::Version;
mod config;
mod flow;
mod pr;
// mod tests;
mod view;
use anyhow::Result as Aresult;
use log::LevelFilter;
use log::{debug, LevelFilter, error};

#[macro_use]
extern crate colour;

#[derive(Parser, Debug)]
#[derive(Parser, Debug, Clone)]
#[clap(author, about, long_about = None)]
pub struct Args {
/// custom commit message
Expand Down Expand Up @@ -93,6 +94,27 @@ pub struct Args {
default_value = "false"
)]
debug: String,

#[clap(
long,
takes_value = false,
forbid_empty_values = false,
required = false,
default_missing_value = "true",
default_value = "false"
)]
pr: String,

#[clap(
short = 'f',
long,
takes_value = false,
forbid_empty_values = false,
required = false,
default_missing_value = "true",
default_value = "false"
)]
force: String,
}

#[derive(Serialize, Deserialize, Debug)]
Expand Down Expand Up @@ -157,6 +179,11 @@ fn custom_commit_params(custom_args: &str) -> String {
}

fn biggerst_version_number(version_list: Vec<String>) -> (String, usize) {
debug!("all version list -> {:#?}", version_list);
if version_list.len() == 0 {
error!("没有匹配到的版本号\n");
std::process::exit(1);
};
let mut version_numer: String = String::new();
let numer_version_list = version_list.clone();
for version in version_list {
Expand All @@ -166,10 +193,14 @@ fn biggerst_version_number(version_list: Vec<String>) -> (String, usize) {
version_numer = version.to_string();
}
}
debug!("biggerst version number -> {}", version_numer);
debug!("biggerst version number list -> {:#?}", numer_version_list);
let number_index = numer_version_list
.iter()
.position(|x| x == &version_numer)
.unwrap();
debug!("biggerst version number index -> {}", number_index);
debug!("biggerst version number -> {}", version_numer);
(version_numer, number_index)
}

Expand Down Expand Up @@ -215,14 +246,14 @@ fn branch_prefix_strip(branch_name: &str) -> String {
branch_name
}

#[warn(unused_assignments)]
fn get_latest_issue(
pub fn get_target_issue(
choice: bool,
version_re: &str,
auto_fetch: bool,
remote_name: &str,
new_branch_base_format: &str,
) -> (String, Vec<String>) {
skip_version_re: bool,
) -> (String, String, Vec<String>) {
let mut re_branchs: Vec<String> = Vec::new();
let mut all_branchs: Vec<String> = Vec::new();
let mut branch_pure_number: Vec<String> = Vec::new();
Expand All @@ -249,9 +280,11 @@ fn get_latest_issue(

let branch_vec = String::from_utf8_lossy(&branchs.stdout);
let branchs_vec: Vec<&str> = branch_vec.split("\n").collect();
debug!("branch list -> {:#?}", branchs_vec);

for branch in branchs_vec {
let branch_strip = branch_prefix_strip(branch);
debug!("branch_strip -> {}", branch_strip);
if target_remote_branch_re.is_match(&branch_strip) {
let remote_branch_name = target_remote_branch_re
.captures(&branch_strip)
Expand All @@ -263,21 +296,30 @@ fn get_latest_issue(
all_branchs.push(remote_branch_name);
}
// all_branchs.push(branch_strip.clone());
if nodeman_re.is_match(&branch_strip) {
debug!(
"all remote {} branch list -> {:#?}",
remote_name, &all_branchs
);
if nodeman_re.is_match(&branch_strip) && !skip_version_re {
let numer_re = nodeman_re.captures(&branch_strip).unwrap();
let version_numer: &str = numer_re.get(1).unwrap().as_str();
branch_pure_number.push(version_numer.to_string());
re_branchs.push(branch_strip);
}
}
debug!("branch_pure_number -> {:#?}", &branch_pure_number);
if skip_version_re {
re_branchs = all_branchs.clone();
};
if choice {
let choice_branch = choose_base_branch(&all_branchs, remote_name);
(format!("{}/{}", remote_name, choice_branch), all_branchs)
(remote_name.to_string(), choice_branch.to_string(), all_branchs)
} else {
blue!(
"match branch list -> {:?}, then choice biggerst version!\n",
re_branchs
);
debug!("all re branch list -> {:#?}", &re_branchs);
if re_branchs.is_empty() {
red!("no match branch by Regex: {}\n", version_re);
std::process::exit(1);
Expand All @@ -288,8 +330,9 @@ fn get_latest_issue(
let target_latest_branch_name =
render_branch_name_by_tmp(&_latest_re_branch, &new_branch_base_format.to_string());
(
format!("{}/{}", remote_name, target_latest_branch_name),
all_branchs,
remote_name.to_string(),
target_latest_branch_name.to_string(),
all_branchs
)
}
}
Expand Down Expand Up @@ -353,6 +396,7 @@ fn main() {
std::process::exit(1);
}
}
debug!("args -> {:#?}", args);
if args.web != "false" {
use view::View;
let web_handler = view::ViewHandler::new(args.web);
Expand All @@ -365,9 +409,20 @@ fn main() {
let id = id.parse::<i32>().unwrap();
parse_flow_command(id, "test".to_string())
}
let force = args.force == "true";
if args.pr == "true" {
use pr::{Pr, PrCommand};
let p = Pr::new(
args.chooise.clone(),
&yaml_config,
force,
args.chooise.clone(),
);
p.pr();
}
// "-m" show maximum number of issues to fetch
if let Ok(result) = Command::new("gh")
.args(["issue", "list", "--json", "number,title", "-L", "200"])
.args(["issue", "list", "--json", "number,title", "-L", "300"])
.output()
{
if let Ok(branchs) = str::from_utf8(&result.stdout) {
Expand Down Expand Up @@ -411,18 +466,21 @@ fn main() {
} else {
choice_switch = false;
}
let (latest_issue, all_branchs) = get_latest_issue(
let (remote_name, branch_name, all_branchs) = get_target_issue(
choice_switch,
&yaml_config.version_compare_re,
yaml_config.enable_auto_fetch,
&yaml_config.remote_name,
&yaml_config.remote_branch_name_template,
false,
);
let complate_brach_name = format!("{}/{}", remote_name, branch_name);

let new_branch = format!(
"{}{}",
&yaml_config.dev_issue_name_header, choice_issue_number
);
debug!("new branch name -> {}", new_branch);

if all_branchs.contains(&&new_branch) {
red!("branch {} already exist, checkout!!!\n", new_branch);
Expand All @@ -443,16 +501,17 @@ fn main() {
std::process::exit(1);
}

debug!("base branch command -> git checkout -b {} {}", &new_branch, complate_brach_name);
if let Ok(add_branch_result) = Command::new("git")
.args(["checkout", "-b", &new_branch, &latest_issue])
.args(["checkout", "-b", &new_branch, &complate_brach_name])
.output()
{
let code = add_branch_result.status.code();
if code == Some(0) {
green!(
"checkout branch by command -> git checkout -b {} {}\n",
new_branch,
latest_issue
complate_brach_name
);
} else {
let checkout_result =
Expand Down Expand Up @@ -493,7 +552,7 @@ fn main() {
red!(
"checkout branch to {} with base barnch -> {} filed! \n{}",
new_branch,
latest_issue,
complate_brach_name,
str::from_utf8(&add_branch_result.stderr).unwrap()
);
std::process::exit(1);
Expand Down
Loading
Loading