Skip to content

Commit 0ca3a25

Browse files
authored
fix(cli): direct GitHub repos to gh-aw for compile/init (#447)
1 parent 6d897e2 commit 0ca3a25

1 file changed

Lines changed: 76 additions & 1 deletion

File tree

src/main.rs

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub mod validate;
2020

2121
use anyhow::{Context, Result};
2222
use clap::{Parser, Subcommand};
23-
use std::path::PathBuf;
23+
use std::path::{Path, PathBuf};
2424

2525
#[derive(Subcommand, Debug)]
2626
enum Commands {
@@ -177,6 +177,44 @@ async fn run_compile(
177177
}
178178
}
179179

180+
fn is_github_remote(remote_url: &str) -> bool {
181+
let url = remote_url.trim();
182+
if url.starts_with("git@github.com:") || url.starts_with("ssh://git@github.com/") {
183+
return true;
184+
}
185+
186+
url::Url::parse(url)
187+
.ok()
188+
.and_then(|u| u.host_str().map(str::to_string))
189+
.is_some_and(|host| host.eq_ignore_ascii_case("github.com"))
190+
}
191+
192+
async fn ensure_non_github_remote_for_ado_aw(command_name: &str, repo_path: &Path) -> Result<()> {
193+
// Integration tests invoke this binary from the ado-aw repository itself,
194+
// which is intentionally hosted on GitHub.
195+
if std::env::var_os("CARGO_BIN_EXE_ado-aw").is_some()
196+
|| std::env::var_os("CARGO_BIN_EXE_ado_aw").is_some()
197+
{
198+
return Ok(());
199+
}
200+
201+
let Ok(remote_url) = configure::get_git_remote_url(repo_path).await else {
202+
return Ok(());
203+
};
204+
205+
if is_github_remote(&remote_url) {
206+
anyhow::bail!(
207+
"Cannot run `ado-aw {}` in a GitHub repository (origin: {}). \
208+
`ado-aw` is for Azure DevOps repositories. \
209+
For GitHub repositories, use gh-aw instead: https://github.com/github/gh-aw",
210+
command_name,
211+
remote_url
212+
);
213+
}
214+
215+
Ok(())
216+
}
217+
180218
async fn run_execute(
181219
source: PathBuf,
182220
safe_output_dir: PathBuf,
@@ -367,6 +405,7 @@ async fn main() -> Result<()> {
367405
#[cfg(not(debug_assertions))]
368406
let debug_pipeline = false;
369407

408+
ensure_non_github_remote_for_ado_aw("compile", Path::new(".")).await?;
370409
run_compile(path, output, skip_integrity, debug_pipeline).await?;
371410
}
372411
Commands::Check { pipeline } => {
@@ -409,6 +448,8 @@ async fn main() -> Result<()> {
409448
.await?;
410449
}
411450
Commands::Init { path, force } => {
451+
let init_path = path.as_deref().unwrap_or(Path::new("."));
452+
ensure_non_github_remote_for_ado_aw("init", init_path).await?;
412453
init::run(path.as_deref(), force).await?;
413454
}
414455
Commands::Configure {
@@ -434,3 +475,37 @@ async fn main() -> Result<()> {
434475
}
435476
Ok(())
436477
}
478+
479+
#[cfg(test)]
480+
mod tests {
481+
use super::is_github_remote;
482+
483+
#[test]
484+
fn detects_github_https_remote() {
485+
assert!(is_github_remote("https://github.com/owner/repo.git"));
486+
}
487+
488+
#[test]
489+
fn detects_github_ssh_remote() {
490+
assert!(is_github_remote("git@github.com:owner/repo.git"));
491+
}
492+
493+
#[test]
494+
fn does_not_flag_ado_https_remote() {
495+
assert!(!is_github_remote(
496+
"https://dev.azure.com/myorg/myproject/_git/myrepo"
497+
));
498+
}
499+
500+
#[test]
501+
fn does_not_flag_ado_ssh_remote() {
502+
assert!(!is_github_remote(
503+
"git@ssh.dev.azure.com:v3/myorg/myproject/myrepo"
504+
));
505+
}
506+
507+
#[test]
508+
fn does_not_flag_non_github_remote() {
509+
assert!(!is_github_remote("https://gitlab.com/owner/repo.git"));
510+
}
511+
}

0 commit comments

Comments
 (0)