Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changes/fix-github-remote-parsing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"cargo-rail" = "patch"
---

Fixed GitHub repository detection for remote URLs with a trailing slash, and rejected non-repository paths before they could produce incorrect changelog or release links.
23 changes: 19 additions & 4 deletions src/release/changelog/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,8 @@ pub fn detect_github_repo(workspace_root: &Path) -> Option<(String, String)> {

/// Parse a GitHub remote URL into (org, repo)
fn parse_github_remote(url: &str) -> Option<(String, String)> {
let trimmed = url.trim().trim_end_matches(".git").trim_end_matches('/');
let trimmed = url.trim().trim_end_matches('/');
let trimmed = trimmed.strip_suffix(".git").unwrap_or(trimmed);

let repo_part = if let Some(ssh) = trimmed.strip_prefix("git@github.com:") {
ssh
Expand All @@ -440,9 +441,10 @@ fn parse_github_remote(url: &str) -> Option<(String, String)> {
trimmed.strip_prefix("https://github.com/")?
};

let mut parts = repo_part.split('/');
let org = parts.next()?;
let repo = parts.next()?;
let (org, repo) = repo_part.split_once('/')?;
if org.is_empty() || repo.is_empty() || repo.contains('/') || org.contains(['?', '#']) || repo.contains(['?', '#']) {
return None;
}

Some((org.to_string(), repo.to_string()))
}
Expand Down Expand Up @@ -708,9 +710,22 @@ mod tests {
parse_github_remote("ssh://git@github.com/org/repo"),
Some(("org".to_string(), "repo".to_string()))
);
assert_eq!(
parse_github_remote("https://github.com/org/repo.git/"),
Some(("org".to_string(), "repo".to_string()))
);
assert_eq!(parse_github_remote("git@gitlab.com:org/repo.git"), None);
}

#[test]
fn parse_github_remote_rejects_non_repository_paths() {
assert_eq!(parse_github_remote("https://github.com/org/repo/issues"), None);
assert_eq!(parse_github_remote("https://github.com//repo.git"), None);
assert_eq!(parse_github_remote("https://github.com/org/.git"), None);
assert_eq!(parse_github_remote("https://github.com/org/repo?tab=readme"), None);
assert_eq!(parse_github_remote("https://github.com/org/repo#readme"), None);
}

#[test]
fn crate_link_overrides_win() {
let shape = ChangelogShape::default();
Expand Down