use serde::Deserialize;
use std::time::Duration;
#[allow(clippy::cognitive_complexity, clippy::too_many_lines)] pub(crate) async fn get_latest_commit_sha(repo_path: &str) -> Option<String> {
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;
}
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() {
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() {
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() {
let url = "https://raw.githubusercontent.com/nonexistent/repo/main/path";
let result = get_latest_commit_sha(url).await;
assert_eq!(result, None);
}
#[tokio::test]
async fn test_get_latest_commit_sha_owner_extraction_failure() {
let url = "https://raw.githubusercontent.com";
let result = get_latest_commit_sha(url).await;
assert_eq!(result, None);
}
#[tokio::test]
async fn test_get_latest_commit_sha_repo_extraction_failure() {
let url = "https://raw.githubusercontent.com/owner";
let result = get_latest_commit_sha(url).await;
assert_eq!(result, None);
}
}