Skip to content

Commit

Permalink
Streamline subgroups allowed/disallowed
Browse files Browse the repository at this point in the history
  • Loading branch information
lucperkins committed Jun 19, 2024
1 parent 8406f3a commit 1f8a5c4
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/push_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,16 +389,20 @@ impl PushContext {
fn get_project_owner_and_name(
repository: &str,
exec_env: ExecutionEnvironment,
disable_subgroups: bool,
subgroups_explicitly_disabled: bool,
) -> Result<(String, String)> {
let subgroups_enabled =
!subgroups_explicitly_disabled && matches!(exec_env, ExecutionEnvironment::GitLab);

let mut repository_split = repository.split('/');

let error_msg = match exec_env {
ExecutionEnvironment::GitLab => "Could not determine project owner and name; pass `--repository` formatted like `determinatesystems/flakehub-push` or `determinatesystems/my-subgroup/flakehub-push`",
_ => "Could not determine project owner and name; pass `--repository` formatted like `determinatesystems/flakehub-push`"
let error_msg = if subgroups_enabled {
"Could not determine project owner and name; pass `--repository` formatted like `determinatesystems/flakehub-push` or `determinatesystems/my-subgroup/flakehub-push`"
} else {
"Could not determine project owner and name; pass `--repository` formatted like `determinatesystems/flakehub-push`"
};

if !matches!(exec_env, ExecutionEnvironment::GitLab) && repository_split.clone().count() > 3 {
if !subgroups_enabled && repository_split.clone().count() > 3 {
return Err(eyre!(error_msg));
};

Expand All @@ -407,9 +411,7 @@ fn get_project_owner_and_name(
repository_split.next(),
repository_split.next(),
) {
(Some(owner), Some(subgroup), Some(name))
if !disable_subgroups && matches!(exec_env, ExecutionEnvironment::GitLab) =>
{
(Some(owner), Some(subgroup), Some(name)) if subgroups_enabled => {
// Gitlab subgroups can be nested quite deeply. This logic supports any level of nesting
// by appending `-{segment}` for each additional segment.
let name_from_segments =
Expand Down Expand Up @@ -507,8 +509,8 @@ mod tests {
("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),
("a/b/c/d/e/f/g", ExecutionEnvironment::GitLab, "Could not determine project owner and name; pass `--repository` formatted like `determinatesystems/flakehub-push`", true),
("a/b/c", ExecutionEnvironment::GitLab, "Could not determine project owner and name; pass `--repository` formatted like `determinatesystems/flakehub-push`", true),
];

for (repo, env, expected_error, disable_subgroups) in failure_cases {
Expand Down

0 comments on commit 1f8a5c4

Please sign in to comment.