skillshub 0.1.9

A package manager for AI coding agent skills - like homebrew for skills
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
use anyhow::{Context, Result};
use flate2::read::GzDecoder;
use reqwest::blocking::{Client, RequestBuilder};
use serde::Deserialize;
use std::collections::HashMap;
use std::fs;
use std::io::Cursor;
use std::path::Path;
use tar::Archive;

use super::models::{GitHubUrl, SkillEntry, TapRegistry};
use crate::skill::SkillMetadata;

/// User agent for API requests
const USER_AGENT: &str = "skillshub";

/// Build an HTTP client with GitHub token if available
fn build_client() -> Result<Client> {
    Client::builder()
        .user_agent(USER_AGENT)
        .build()
        .context("Failed to build HTTP client")
}

/// Add GitHub token authentication to a request if GITHUB_TOKEN is set
fn with_auth(request: RequestBuilder) -> RequestBuilder {
    if let Ok(token) = std::env::var("GITHUB_TOKEN") {
        request.bearer_auth(token)
    } else {
        request
    }
}

/// GitHub Tree API response
#[derive(Debug, Deserialize)]
struct TreeResponse {
    tree: Vec<TreeEntry>,
}

/// Entry in GitHub Tree API response
#[derive(Debug, Deserialize)]
struct TreeEntry {
    path: String,
    #[serde(rename = "type")]
    entry_type: String,
}

/// GitHub Repository API response (partial)
#[derive(Debug, Deserialize)]
struct RepoInfo {
    default_branch: String,
}

/// Get the default branch for a repository from GitHub API
pub fn get_default_branch(owner: &str, repo: &str) -> Result<String> {
    let client = build_client()?;
    let api_base = std::env::var("SKILLSHUB_GITHUB_API_BASE").unwrap_or_else(|_| "https://api.github.com".to_string());
    let url = format!("{}/repos/{}/{}", api_base, owner, repo);

    let response = with_auth(client.get(&url))
        .send()
        .with_context(|| format!("Failed to fetch repo info from {}", url))?;

    let status = response.status();
    if !status.is_success() {
        if status == reqwest::StatusCode::NOT_FOUND {
            anyhow::bail!(
                "Repository not found on GitHub: {}/{}\n\
                 Please check that:\n\
                 - The repository exists and is spelled correctly\n\
                 - The repository is public (or GITHUB_TOKEN is set for private repos)",
                owner,
                repo
            );
        }
        anyhow::bail!("Failed to fetch repo info: HTTP {}", status);
    }

    let info: RepoInfo = response
        .json()
        .with_context(|| "Failed to parse repository info response")?;
    Ok(info.default_branch)
}

/// Parse a GitHub URL or repository identifier into components
///
/// Supports formats:
/// - owner/repo (short format, uses repo's default branch)
/// - https://github.com/owner/repo (uses repo's default branch)
/// - https://github.com/owner/repo/tree/branch
/// - https://github.com/owner/repo/tree/branch/path/to/folder
///
/// When no branch is specified in the URL, `branch` will be `None`,
/// indicating that the repository's default branch should be used.
pub fn parse_github_url(url: &str) -> Result<GitHubUrl> {
    let url = url.trim_end_matches('/');

    // Try to strip protocol prefixes
    let path = url
        .strip_prefix("https://github.com/")
        .or_else(|| url.strip_prefix("http://github.com/"))
        .or_else(|| url.strip_prefix("github.com/"));

    // If no prefix was stripped, check if it's a valid owner/repo format
    let path = match path {
        Some(p) => p,
        None => {
            // Check if it looks like owner/repo (no protocol, no dots in the first segment)
            if is_valid_repo_id(url) {
                url
            } else {
                anyhow::bail!(
                    "Invalid GitHub URL or repository ID: {}\n\
                     Expected formats:\n\
                     - owner/repo\n\
                     - https://github.com/owner/repo",
                    url
                );
            }
        }
    };

    let parts: Vec<&str> = path.split('/').collect();

    if parts.len() < 2 {
        anyhow::bail!("Invalid repository ID: must be in 'owner/repo' format");
    }

    let owner = parts[0].to_string();
    let repo = parts[1].to_string();

    // Check for /tree/branch/path format
    let (branch, subpath) = if parts.len() > 3 && parts[2] == "tree" {
        let branch = Some(parts[3].to_string());
        let subpath = if parts.len() > 4 {
            Some(parts[4..].join("/"))
        } else {
            None
        };
        (branch, subpath)
    } else {
        // No branch specified - use None to indicate "use default branch"
        (None, None)
    };

    Ok(GitHubUrl {
        owner,
        repo,
        branch,
        path: subpath,
    })
}

/// Check if a string looks like a valid owner/repo identifier
/// Valid: "owner/repo", "my-org/my-repo", "user123/repo_name"
/// Invalid: "https://...", "gitlab.com/...", "just-one-part"
fn is_valid_repo_id(s: &str) -> bool {
    let parts: Vec<&str> = s.split('/').collect();

    // Must have exactly 2 parts for owner/repo
    if parts.len() != 2 {
        return false;
    }

    let owner = parts[0];
    let repo = parts[1];

    // Both parts must be non-empty
    if owner.is_empty() || repo.is_empty() {
        return false;
    }

    // Owner and repo should only contain valid GitHub username/repo characters
    // GitHub allows alphanumeric, hyphens, underscores, and dots
    let is_valid_part = |part: &str| {
        !part.is_empty()
            && part
                .chars()
                .all(|c| c.is_alphanumeric() || c == '-' || c == '_' || c == '.')
            && !part.starts_with('-')
            && !part.starts_with('.')
    };

    is_valid_part(owner) && is_valid_part(repo)
}

/// Discover skills from a GitHub repository by scanning for SKILL.md files
///
/// Uses the GitHub Tree API to recursively find all SKILL.md files in the repo,
/// then fetches each one to extract metadata.
/// Set GITHUB_TOKEN environment variable to avoid rate limiting.
pub fn discover_skills_from_repo(github_url: &GitHubUrl, tap_name: &str) -> Result<TapRegistry> {
    let client = build_client()?;

    // Resolve branch: use specified branch or fetch the repository's default branch
    let branch = match &github_url.branch {
        Some(b) => b.clone(),
        None => get_default_branch(&github_url.owner, &github_url.repo)?,
    };

    // Fetch the full repo tree with recursive=1
    let tree_url = format!("{}/git/trees/{}?recursive=1", github_url.api_url(), branch);

    let response = with_auth(client.get(&tree_url))
        .send()
        .with_context(|| format!("Failed to fetch repo tree from {}", tree_url))?;

    if !response.status().is_success() {
        let status = response.status();
        if status == reqwest::StatusCode::NOT_FOUND {
            anyhow::bail!(
                "Branch '{}' not found in repository {}/{}\n\
                 Please check that the branch exists.",
                branch,
                github_url.owner,
                github_url.repo
            );
        }
        anyhow::bail!("Failed to fetch repo tree: HTTP {} from {}", status, tree_url);
    }

    let tree_response: TreeResponse = response.json().with_context(|| "Failed to parse tree response")?;

    // Find all SKILL.md files
    // A SKILL.md can be at the root (path == "SKILL.md") or in subdirectories (path ends with "/SKILL.md")
    let skill_paths = extract_skill_paths(&tree_response.tree);

    if skill_paths.is_empty() {
        anyhow::bail!("No skills found in repository (no SKILL.md files detected)");
    }

    // Fetch metadata for each skill
    let mut skills = HashMap::new();
    for skill_path in &skill_paths {
        let skill_md_url = if skill_path.is_empty() {
            // Root-level SKILL.md
            github_url.raw_url("SKILL.md", &branch)
        } else {
            github_url.raw_url(&format!("{}/SKILL.md", skill_path), &branch)
        };

        // Note: raw.githubusercontent.com doesn't need auth, but we add it anyway
        match with_auth(client.get(&skill_md_url)).send() {
            Ok(resp) if resp.status().is_success() => {
                if let Ok(content) = resp.text() {
                    if let Some((name, description)) = parse_skill_md_content(&content) {
                        skills.insert(
                            name.clone(),
                            SkillEntry {
                                path: skill_path.clone(),
                                description,
                                homepage: None,
                            },
                        );
                    }
                }
            }
            _ => {
                // If we can't fetch metadata, use directory name as skill name
                // For root-level skills, use the repo name
                let skill_name = if skill_path.is_empty() {
                    &github_url.repo
                } else {
                    skill_path.rsplit('/').next().unwrap_or(skill_path)
                };
                skills.insert(
                    skill_name.to_string(),
                    SkillEntry {
                        path: skill_path.clone(),
                        description: None,
                        homepage: None,
                    },
                );
            }
        }
    }

    let description = Some(format!("Skills from {}/{}", github_url.owner, github_url.repo));

    Ok(TapRegistry {
        name: tap_name.to_string(),
        description,
        skills,
    })
}

/// Parse SKILL.md content to extract name and description from YAML frontmatter
fn parse_skill_md_content(content: &str) -> Option<(String, Option<String>)> {
    // Extract YAML frontmatter between --- markers
    let parts: Vec<&str> = content.splitn(3, "---").collect();
    if parts.len() < 3 {
        return None;
    }

    let yaml_content = parts[1].trim();
    let metadata: SkillMetadata = serde_yaml::from_str(yaml_content).ok()?;

    Some((metadata.name, metadata.description))
}

/// Get the latest commit SHA for a path in a repository
pub fn get_latest_commit(github_url: &GitHubUrl, path: Option<&str>, resolved_branch: &str) -> Result<String> {
    let client = build_client()?;

    let mut url = format!("{}/commits?sha={}&per_page=1", github_url.api_url(), resolved_branch);

    if let Some(p) = path {
        url.push_str(&format!("&path={}", p));
    }

    let response = with_auth(client.get(&url))
        .send()
        .with_context(|| format!("Failed to fetch commits from {}", url))?;

    if !response.status().is_success() {
        anyhow::bail!("Failed to fetch commits: HTTP {}", response.status());
    }

    let commits: Vec<serde_json::Value> = response.json()?;

    commits
        .first()
        .and_then(|c| c["sha"].as_str())
        .map(|s| s[..7].to_string()) // Short SHA
        .with_context(|| "No commits found")
}

/// Download and extract a skill from a GitHub repository
///
/// Downloads the tarball, extracts the specific skill folder, and copies to destination.
pub fn download_skill(github_url: &GitHubUrl, skill_path: &str, dest: &Path, commit: Option<&str>) -> Result<String> {
    // Resolve branch: use specified branch or fetch the repository's default branch
    let resolved_branch = match &github_url.branch {
        Some(b) => b.clone(),
        None => get_default_branch(&github_url.owner, &github_url.repo)?,
    };

    let git_ref = commit.unwrap_or(&resolved_branch);

    let client = build_client()?;

    // Download tarball
    let tarball_url = github_url.tarball_url(git_ref);
    let response = with_auth(client.get(&tarball_url))
        .send()
        .with_context(|| format!("Failed to download from {}", tarball_url))?;

    if !response.status().is_success() {
        anyhow::bail!(
            "Failed to download tarball: HTTP {} from {}",
            response.status(),
            tarball_url
        );
    }

    let bytes = response.bytes()?;

    // Get the actual commit SHA from response headers or fetch it
    let commit_sha = commit.map(|s| s.to_string()).unwrap_or_else(|| {
        get_latest_commit(github_url, Some(skill_path), &resolved_branch).unwrap_or_else(|err| {
            println!(
                "Warning: failed to resolve latest commit for {} ({}), using {}",
                github_url.repo, err, git_ref
            );
            git_ref.to_string()
        })
    });

    // Extract tarball
    let cursor = Cursor::new(bytes);
    let decoder = GzDecoder::new(cursor);
    let mut archive = Archive::new(decoder);

    // Create temp directory for extraction
    let temp_dir = tempfile::tempdir()?;

    // Extract all files
    archive.unpack(temp_dir.path())?;

    // Find the extracted directory (GitHub tarballs have a prefix like "owner-repo-sha/")
    let extracted_dir = fs::read_dir(temp_dir.path())?
        .filter_map(|e| e.ok())
        .find(|e| e.path().is_dir())
        .with_context(|| "Failed to find extracted directory")?
        .path();

    // Find the skill within the extracted archive
    // For root-level skills (empty path), the skill is the extracted directory itself
    let skill_source = if skill_path.is_empty() {
        extracted_dir.clone()
    } else {
        extracted_dir.join(skill_path)
    };

    if !skill_source.exists() {
        anyhow::bail!("Skill path '{}' not found in repository", skill_path);
    }

    // Verify it has SKILL.md
    if !skill_source.join("SKILL.md").exists() {
        anyhow::bail!(
            "Invalid skill: no SKILL.md found in '{}'",
            if skill_path.is_empty() { "(root)" } else { skill_path }
        );
    }

    // Create destination and copy files
    fs::create_dir_all(dest)?;
    copy_dir_contents(&skill_source, dest)?;

    Ok(commit_sha)
}

/// Extract skill directory paths from a list of tree entries.
///
/// Finds entries that are SKILL.md files (either at root or in subdirectories)
/// and returns the parent directory path for each. A root-level SKILL.md
/// produces an empty string path.
fn extract_skill_paths(tree: &[TreeEntry]) -> Vec<String> {
    tree.iter()
        .filter(|entry| entry.entry_type == "blob" && (entry.path == "SKILL.md" || entry.path.ends_with("/SKILL.md")))
        .map(|entry| {
            entry
                .path
                .rsplit_once('/')
                .map(|(parent, _)| parent.to_string())
                .unwrap_or_default()
        })
        .collect()
}

/// Recursively copy directory contents
fn copy_dir_contents(src: &Path, dst: &Path) -> Result<()> {
    for entry in fs::read_dir(src)? {
        let entry = entry?;
        let src_path = entry.path();
        let dst_path = dst.join(entry.file_name());

        if src_path.is_dir() {
            fs::create_dir_all(&dst_path)?;
            copy_dir_contents(&src_path, &dst_path)?;
        } else {
            fs::copy(&src_path, &dst_path)?;
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_skill_md_content() {
        let content = r#"---
name: test-skill
description: A test skill
---
# Test Skill
Some content here.
"#;
        let result = parse_skill_md_content(content);
        assert!(result.is_some());
        let (name, desc) = result.unwrap();
        assert_eq!(name, "test-skill");
        assert_eq!(desc, Some("A test skill".to_string()));
    }

    #[test]
    fn test_parse_skill_md_content_no_description() {
        let content = r#"---
name: minimal-skill
---
# Minimal
"#;
        let result = parse_skill_md_content(content);
        assert!(result.is_some());
        let (name, desc) = result.unwrap();
        assert_eq!(name, "minimal-skill");
        assert!(desc.is_none());
    }

    #[test]
    fn test_parse_skill_md_content_invalid() {
        let content = "# No frontmatter here";
        let result = parse_skill_md_content(content);
        assert!(result.is_none());
    }

    #[test]
    fn test_parse_github_url_simple() {
        let url = parse_github_url("https://github.com/owner/repo").unwrap();
        assert_eq!(url.owner, "owner");
        assert_eq!(url.repo, "repo");
        assert!(url.branch.is_none()); // No branch specified = None (use repo's default)
        assert!(url.path.is_none());
    }

    #[test]
    fn test_parse_github_url_with_branch() {
        let url = parse_github_url("https://github.com/owner/repo/tree/develop").unwrap();
        assert_eq!(url.owner, "owner");
        assert_eq!(url.repo, "repo");
        assert_eq!(url.branch, Some("develop".to_string()));
        assert!(url.path.is_none());
    }

    #[test]
    fn test_parse_github_url_with_path() {
        let url = parse_github_url("https://github.com/owner/repo/tree/main/path/to/folder").unwrap();
        assert_eq!(url.owner, "owner");
        assert_eq!(url.repo, "repo");
        assert_eq!(url.branch, Some("main".to_string()));
        assert_eq!(url.path, Some("path/to/folder".to_string()));
    }

    #[test]
    fn test_parse_github_url_with_master_branch() {
        // Explicitly specifying master branch should work
        let url = parse_github_url("https://github.com/owner/repo/tree/master").unwrap();
        assert_eq!(url.owner, "owner");
        assert_eq!(url.repo, "repo");
        assert_eq!(url.branch, Some("master".to_string()));
        assert!(url.path.is_none());
    }

    #[test]
    fn test_parse_github_url_no_protocol() {
        let url = parse_github_url("github.com/owner/repo").unwrap();
        assert_eq!(url.owner, "owner");
        assert_eq!(url.repo, "repo");
        assert!(url.branch.is_none()); // No branch specified = None
    }

    #[test]
    fn test_parse_github_url_trailing_slash() {
        let url = parse_github_url("https://github.com/owner/repo/").unwrap();
        assert_eq!(url.owner, "owner");
        assert_eq!(url.repo, "repo");
        assert!(url.branch.is_none());
    }

    #[test]
    fn test_parse_github_url_invalid() {
        assert!(parse_github_url("https://gitlab.com/owner/repo").is_err());
        assert!(parse_github_url("https://github.com/owner").is_err());
        assert!(parse_github_url("not-a-url").is_err());
    }

    #[test]
    fn test_parse_github_url_repo_id_simple() {
        let url = parse_github_url("owner/repo").unwrap();
        assert_eq!(url.owner, "owner");
        assert_eq!(url.repo, "repo");
        assert!(url.branch.is_none()); // No branch specified = None (use repo's default)
        assert!(url.path.is_none());
    }

    #[test]
    fn test_parse_github_url_repo_id_with_hyphens() {
        let url = parse_github_url("my-org/my-repo").unwrap();
        assert_eq!(url.owner, "my-org");
        assert_eq!(url.repo, "my-repo");
        assert!(url.branch.is_none());
    }

    #[test]
    fn test_parse_github_url_repo_id_with_underscores() {
        let url = parse_github_url("user_name/repo_name").unwrap();
        assert_eq!(url.owner, "user_name");
        assert_eq!(url.repo, "repo_name");
        assert!(url.branch.is_none());
    }

    #[test]
    fn test_parse_github_url_repo_id_with_dots() {
        let url = parse_github_url("owner/repo.js").unwrap();
        assert_eq!(url.owner, "owner");
        assert_eq!(url.repo, "repo.js");
        assert!(url.branch.is_none());
    }

    #[test]
    fn test_is_valid_repo_id() {
        assert!(is_valid_repo_id("owner/repo"));
        assert!(is_valid_repo_id("my-org/my-repo"));
        assert!(is_valid_repo_id("user123/repo_name"));
        assert!(is_valid_repo_id("Owner/Repo.js"));
    }

    #[test]
    fn test_is_valid_repo_id_invalid() {
        // Not enough parts
        assert!(!is_valid_repo_id("just-one-part"));
        // Too many parts
        assert!(!is_valid_repo_id("owner/repo/extra"));
        // Empty parts
        assert!(!is_valid_repo_id("/repo"));
        assert!(!is_valid_repo_id("owner/"));
        // Starts with invalid char
        assert!(!is_valid_repo_id("-owner/repo"));
        assert!(!is_valid_repo_id(".owner/repo"));
        // Invalid characters
        assert!(!is_valid_repo_id("owner/repo name"));
    }

    /// Helper to create a TreeEntry for tests
    fn tree_entry(path: &str, entry_type: &str) -> TreeEntry {
        TreeEntry {
            path: path.to_string(),
            entry_type: entry_type.to_string(),
        }
    }

    #[test]
    fn test_extract_skill_paths_subdirectory() {
        let tree = vec![
            tree_entry("skills/code-reviewer/SKILL.md", "blob"),
            tree_entry("skills/test-skill/SKILL.md", "blob"),
            tree_entry("README.md", "blob"),
        ];
        let paths = extract_skill_paths(&tree);
        assert_eq!(paths, vec!["skills/code-reviewer", "skills/test-skill"]);
    }

    #[test]
    fn test_extract_skill_paths_root_level() {
        // Repo that IS a skill (SKILL.md at root)
        let tree = vec![tree_entry("SKILL.md", "blob"), tree_entry("README.md", "blob")];
        let paths = extract_skill_paths(&tree);
        assert_eq!(paths, vec![""]);
    }

    #[test]
    fn test_extract_skill_paths_root_and_subdirectory() {
        // Repo with both root-level and subdirectory skills
        let tree = vec![
            tree_entry("SKILL.md", "blob"),
            tree_entry("skills/other-skill/SKILL.md", "blob"),
            tree_entry("README.md", "blob"),
        ];
        let paths = extract_skill_paths(&tree);
        assert_eq!(paths, vec!["", "skills/other-skill"]);
    }

    #[test]
    fn test_extract_skill_paths_no_skills() {
        let tree = vec![tree_entry("README.md", "blob"), tree_entry("src/main.rs", "blob")];
        let paths = extract_skill_paths(&tree);
        assert!(paths.is_empty());
    }

    #[test]
    fn test_extract_skill_paths_ignores_trees() {
        // Directories (type "tree") should be ignored even if named SKILL.md
        let tree = vec![
            tree_entry("SKILL.md", "tree"),
            tree_entry("skills/test/SKILL.md", "blob"),
        ];
        let paths = extract_skill_paths(&tree);
        assert_eq!(paths, vec!["skills/test"]);
    }

    #[test]
    fn test_extract_skill_paths_deep_nesting() {
        let tree = vec![tree_entry("a/b/c/SKILL.md", "blob")];
        let paths = extract_skill_paths(&tree);
        assert_eq!(paths, vec!["a/b/c"]);
    }
}