LINE1 10 20 30 40 50 60 70 80 901use anyhow::{bail, Result};2use dialoguer::{Confirm, Editor, FuzzySelect, Input};34use crate::cache::{Cache, Project, StatusOption};5use crate::config::Config;6use crate::github::GitHubClient;78pub struct IssueInput {9 pub repo_full_name: String,10 pub title: String,11 pub description: String,12 pub project: Option<Project>,13 pub status: Option<StatusOption>,14}1516pub fn collect_issue_input(config: &Config, cache: &Cache) -> Result<IssueInput> {17 18 let mut sorted_repos = cache.repos.clone();19 sorted_repos.sort_by(|a, b| {20 let count_a = cache.usage.repos.get(&a.full_name).copied().unwrap_or(0);21 let count_b = cache.usage.repos.get(&b.full_name).copied().unwrap_or(0);22 count_b.cmp(&count_a)23 });2425 let repo_names: Vec<String> = sorted_repos.iter().map(|r| r.full_name.clone()).collect();26 if repo_names.is_empty() {27 bail!("No repositories found in cache. Run `pike setup` first.");28 }2930 let repo_idx = FuzzySelect::new()31 .with_prompt("Select repository")32 .items(&repo_names)33 .interact()?;34 let repo_full_name = repo_names[repo_idx].clone();3536 let default_title = format!("unknown-{}", rand::random::<u16>());37 let title: String = Input::new()38 .with_prompt("Issue title")39 .default(default_title)40 .interact()?;4142 let description = if Confirm::new()43 .with_prompt("Add a description?")44 .default(false)45 .interact()?46 {47 Editor::new()48 .edit("")49 .ok()50 .flatten()51 .map(|t| t.trim().to_string())52 .unwrap_or_default()53 } else {54 String::new()55 };5657 let project = select_project(config, cache)?;5859 let status = if let Some(ref proj) = project {60 select_status(proj)?61 } else {62 None63 };6465 Ok(IssueInput {66 repo_full_name,67 title,68 description,69 project,70 status,71 })72}7374fn select_project(config: &Config, cache: &Cache) -> Result<Option<Project>> {75 if cache.projects.is_empty() {76 return Ok(None);77 }7879 80 let mut sorted_projects = cache.projects.clone();81 sorted_projects.sort_by(|a, b| {82 let count_a = cache.usage.projects.get(&a.id).copied().unwrap_or(0);83 let count_b = cache.usage.projects.get(&b.id).copied().unwrap_or(0);84 count_b.cmp(&count_a)85 });8687 let project_items: Vec<String> = sorted_projects.iter().map(|p| p.title.clone()).collect();88 let mut options = vec!["Skip (no project)".to_string()];89 options.extend(project_items.clone());9091 let default_idx = config92 .default_project93 .as_ref()94 .and_then(|dp| project_items.iter().position(|p| p == dp))95 .map(|i| i + 1)96 .unwrap_or(0);9798 let idx = FuzzySelect::new()99 .with_prompt("Select project")100 .items(&options)101 .default(default_idx)102 .interact()?;103104 if idx == 0 {105 Ok(None)106 } else {107 Ok(Some(sorted_projects[idx - 1].clone()))108 }109}110111fn select_status(project: &Project) -> Result<Option<StatusOption>> {112 if project.status_options.is_empty() {113 return Ok(None);114 }115116 let mut options = vec!["No status".to_string()];117 options.extend(project.status_options.iter().map(|s| s.name.clone()));118119 let idx = FuzzySelect::new()120 .with_prompt("Select status")121 .items(&options)122 .default(0)123 .interact()?;124125 if idx == 0 {126 Ok(None)127 } else {128 Ok(Some(project.status_options[idx - 1].clone()))129 }130}131132pub async fn run_setup(token: &str) -> Result<()> {133 println!(" GITHUB_TOKEN found");134135 let default_org: String = Input::new()136 .with_prompt("Default organization")137 .interact()?;138139 let config = Config {140 default_org: Some(default_org.clone()),141 default_project: None,142 favorite_repos: Vec::new(),143 };144 config.save()?;145 println!(" Config saved");146147 let client = GitHubClient::new(token);148149 println!("Fetching repositories...");150 let repos = client.list_org_repos(&default_org).await?;151 println!(" Fetched {} repositories", repos.len());152153 println!("Fetching projects...");154 let projects = client.list_org_projects(&default_org).await?;155 println!(" Fetched {} projects", projects.len());156157 let cache = Cache { repos, projects, ..Cache::default() };158 cache.save()?;159 println!(" Cache saved");160161 Ok(())162}