Skip to content

Commit

Permalink
Provide flag/environment variable to disable subgroups
Browse files Browse the repository at this point in the history
  • Loading branch information
lucperkins committed Jun 19, 2024
1 parent a38ae3d commit 8406f3a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
6 changes: 6 additions & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,12 @@ pub(crate) struct FlakeHubPushCli {

#[clap(long, env = "FLAKEHUB_PUSH_INCLUDE_OUTPUT_PATHS", value_parser = EmptyBoolParser, default_value_t = false)]
pub(crate) include_output_paths: bool,

// Gitlab has a concept of subgroups, which enables repo names like https://gitlab.com/a/b/c/d/e/f/g. By default,
// flakehub-push would parse that to flake name `a/b-c-d-e-f-g`. This flag/environment variable provides a
// mechanism to disable this behavior.
#[clap(long, env = "FLAKEHUB_PUSH_DISABLE_SUBGROUPS", default_value_t = false)]
pub(crate) disable_subgroups: bool,
}

#[derive(Clone, Debug)]
Expand Down
24 changes: 17 additions & 7 deletions src/push_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl PushContext {
};

let (project_owner, project_name) =
get_project_owner_and_name(repository, exec_env.clone())?;
get_project_owner_and_name(repository, exec_env.clone(), cli.disable_subgroups)?;

let maybe_git_root = match &cli.git_root.0 {
Some(gr) => Ok(gr.to_owned()),
Expand Down Expand Up @@ -389,6 +389,7 @@ impl PushContext {
fn get_project_owner_and_name(
repository: &str,
exec_env: ExecutionEnvironment,
disable_subgroups: bool,
) -> Result<(String, String)> {
let mut repository_split = repository.split('/');

Expand All @@ -407,7 +408,7 @@ fn get_project_owner_and_name(
repository_split.next(),
) {
(Some(owner), Some(subgroup), Some(name))
if matches!(exec_env, ExecutionEnvironment::GitLab) =>
if !disable_subgroups && matches!(exec_env, ExecutionEnvironment::GitLab) =>
{
// Gitlab subgroups can be nested quite deeply. This logic supports any level of nesting
// by appending `-{segment}` for each additional segment.
Expand Down Expand Up @@ -469,40 +470,49 @@ mod tests {
];

for (repo, env, (expected_owner, expected_name)) in success_cases {
let (owner, name) = get_project_owner_and_name(&String::from(repo), env).unwrap();
let (owner, name) =
get_project_owner_and_name(&String::from(repo), env, false).unwrap();
assert_eq!(
(String::from(expected_owner), String::from(expected_name)),
(owner.clone(), name.clone()),
"expected {expected_owner}/{expected_name} from repository {repo} but got {owner}/{name}"
);
}

let failure_cases: Vec<(&str, ExecutionEnvironment, &str)> = vec![
let failure_cases: Vec<(&str, ExecutionEnvironment, &str, bool)> = vec![
(
"just-an-owner",
ExecutionEnvironment::GitHub,
"Could not determine project owner and name; pass `--repository` formatted like `determinatesystems/flakehub-push`",
false,
),
(
"just-an-owner",
ExecutionEnvironment::Local,
"Could not determine project owner and name; pass `--repository` formatted like `determinatesystems/flakehub-push`",
false,
),
(
"just-an-owner",
ExecutionEnvironment::GitLab,
"Could not determine project owner and name; pass `--repository` formatted like `determinatesystems/flakehub-push` or `determinatesystems/my-subgroup/flakehub-push`",
false,
),
(
"DeterminateSystems/subgroup/flakehub",
ExecutionEnvironment::GitHub,
"Could not determine project owner and name; pass `--repository` formatted like `determinatesystems/flakehub-push`",
false,
),
("a/b/c/d", ExecutionEnvironment::GitHub, "Could not determine project owner and name; pass `--repository` formatted like `determinatesystems/flakehub-push`"),
("a/b/c/d", ExecutionEnvironment::GitHub, "Could not determine project owner and name; pass `--repository` formatted like `determinatesystems/flakehub-push`", false),
// In these cases, --disable-subgroups is set, which makes this Gitlab repo name work like GitHub and local,
// that is, names of the form `owner/name` are required.
("a/b/c/d/e/f/g", ExecutionEnvironment::GitLab, "Could not determine project owner and name; pass `--repository` formatted like `determinatesystems/flakehub-push` or `determinatesystems/my-subgroup/flakehub-push`", true),
("a/b/c", ExecutionEnvironment::GitLab, "Could not determine project owner and name; pass `--repository` formatted like `determinatesystems/flakehub-push` or `determinatesystems/my-subgroup/flakehub-push`", true),
];

for (repo, env, expected_error) in failure_cases {
let result = get_project_owner_and_name(&String::from(repo), env);
for (repo, env, expected_error, disable_subgroups) in failure_cases {
let result = get_project_owner_and_name(&String::from(repo), env, disable_subgroups);
assert_eq!(
String::from(expected_error),
result.err().unwrap().to_string(),
Expand Down

0 comments on commit 8406f3a

Please sign in to comment.