domain-status 0.2.1

Concurrent URL status checker that captures comprehensive metadata in SQLite.
Documentation
//! GitHub commit SHA fetching operations.

use serde::Deserialize;
use std::time::Duration;

/// Gets the latest commit SHA for a GitHub repository path
///
/// This extracts the Git commit hash that identifies the exact version of the
/// ruleset being used. This is important for reproducibility - you can see
/// exactly which version of the fingerprints was used for each detection.
#[allow(clippy::cognitive_complexity, clippy::too_many_lines)] // Parses GitHub API JSON with multiple fallback paths for commit SHA extraction
pub(crate) async fn get_latest_commit_sha(repo_path: &str) -> Option<String> {
    // Extract repo and path from URL
    // e.g., https://raw.githubusercontent.com/HTTPArchive/wappalyzer/main/src/technologies
    // URL structure: https://raw.githubusercontent.com/{owner}/{repo}/{branch}/{path}
    let parts: Vec<&str> = repo_path.split('/').collect();
    log::debug!("URL parts count: {}, URL: {}", parts.len(), repo_path);

    if parts.len() < 7 {
        log::debug!(
            "Invalid GitHub URL format for SHA extraction. Expected: \
            'https://raw.githubusercontent.com/owner/repo/branch/path', got: '{}' (parsed {} parts)",
            repo_path,
            parts.len()
        );
        return None;
    }

    // parts[0] = "https:"
    // parts[1] = ""
    // parts[2] = "raw.githubusercontent.com"
    // parts[3] = owner (e.g., "HTTPArchive")
    // parts[4] = repo (e.g., "wappalyzer")
    // parts[5] = branch (e.g., "main")
    // parts[6..] = path (e.g., "src/technologies")

    let Some(owner) = parts.get(3) else {
        log::debug!("No owner found in URL parts");
        return None;
    };
    let Some(repo) = parts.get(4) else {
        log::debug!("No repo found in URL parts");
        return None;
    };
    let path = parts[6..].join("/");
    log::debug!("Extracted: owner={owner}, repo={repo}, path={path}");

    let api_url =
        format!("https://api.github.com/repos/{owner}/{repo}/commits?path={path}&per_page=1");

    log::debug!("Fetching commit SHA from: {api_url}");

    use crate::config::TCP_CONNECT_TIMEOUT_SECS;

    let client = reqwest::Client::builder()
        .timeout(Duration::from_secs(10))
        .connect_timeout(Duration::from_secs(TCP_CONNECT_TIMEOUT_SECS))
        .build()
        .ok()?;

    #[derive(Deserialize)]
    struct Commit {
        sha: String,
    }

    log::debug!("Fetching commit SHA from GitHub API: {api_url}");
    match super::github_api_get(&client, &api_url).await {
        Ok(resp) => {
            let status = resp.status();
            if status.is_success() {
                match resp.text().await {
                    Ok(text) => match serde_json::from_str::<Vec<Commit>>(&text) {
                        Ok(commits) => {
                            if let Some(commit) = commits.first() {
                                log::debug!("Found commit SHA: {}", commit.sha);
                                Some(commit.sha.clone())
                            } else {
                                log::warn!("No commits found in response for path: {path}");
                                None
                            }
                        }
                        Err(e) => {
                            log::warn!(
                                "Failed to parse commit response: {} (first 200 chars: {})",
                                e,
                                &text[..text.len().min(200)]
                            );
                            None
                        }
                    },
                    Err(e) => {
                        log::warn!("Failed to read commit response: {e}");
                        None
                    }
                }
            } else {
                log::warn!("GitHub API returned status: {status} for URL: {api_url}");
                None
            }
        }
        Err(e) => {
            log::warn!("Failed to fetch commit SHA from {api_url}: {e}");
            None
        }
    }
}

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

    #[tokio::test]
    async fn test_get_latest_commit_sha_invalid_url() {
        // Test with invalid URL format
        let result = get_latest_commit_sha("not-a-github-url").await;
        assert_eq!(result, None);
    }

    #[tokio::test]
    async fn test_get_latest_commit_sha_short_url() {
        // Test with URL that doesn't have enough parts
        let result = get_latest_commit_sha("https://raw.githubusercontent.com/owner").await;
        assert_eq!(result, None);
    }

    #[tokio::test]
    async fn test_get_latest_commit_sha_nonexistent_repo() {
        // Test with non-existent repository
        let url = "https://raw.githubusercontent.com/nonexistent/repo/main/path";
        let result = get_latest_commit_sha(url).await;
        // Should return None for non-existent repos
        assert_eq!(result, None);
    }

    #[tokio::test]
    async fn test_get_latest_commit_sha_owner_extraction_failure() {
        // Test that missing owner in URL parts returns None gracefully
        // This is critical - prevents panics from index out of bounds
        // The code at line 35-40 handles missing owner
        // We test with a URL that has fewer than 7 parts, which should return None at line 18
        // But we also want to test the specific owner extraction failure path
        // A URL with exactly 7 parts but empty owner would be: https://raw.githubusercontent.com//repo/branch/path
        // However, split('/') on that would give parts[3] = "" (empty string), not None
        // So the code at line 35 would get Some(""), which is valid
        // To test the None case, we need a URL with fewer than 4 parts before the path
        let url = "https://raw.githubusercontent.com";
        let result = get_latest_commit_sha(url).await;
        // Should return None (parts.len() < 7)
        assert_eq!(result, None);
    }

    #[tokio::test]
    async fn test_get_latest_commit_sha_repo_extraction_failure() {
        // Test that missing repo in URL parts returns None gracefully
        // This is critical - prevents panics from index out of bounds
        // The code at line 42-47 handles missing repo
        let url = "https://raw.githubusercontent.com/owner";
        let result = get_latest_commit_sha(url).await;
        // Should return None (parts.len() < 7)
        assert_eq!(result, None);
    }
}