Skip to content

Commit

Permalink
Merge pull request #76 from Algeroth84/master
Browse files Browse the repository at this point in the history
Avoid re-applying configuration due to users-groups changed order
  • Loading branch information
jesperes authored Jan 16, 2024
2 parents 989ec10 + 31e115d commit 841a653
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
37 changes: 36 additions & 1 deletion src/bec_config_ss.erl
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ to_wz_branch_reviewer(#{<<"paths">> := Paths} = R0) ->
AtomifyMap = [atomify_keys(add_default_mandatory(P)) || P <- Paths],
R = R0#{<<"paths">> => lists:sort(AtomifyMap)},
Merged = add_default_mandatory(R),
atomify_keys(Merged).
sort_users_and_groups(atomify_keys(Merged)).

-spec atomify_keys(map()) -> map().
atomify_keys(Map) ->
Expand All @@ -191,3 +191,38 @@ add_default_mandatory(WZMap) ->
Mandatory = #{ <<"mandatory-users">> => []
, <<"mandatory-groups">> => []},
maps:merge(Mandatory,WZMap).

-spec sort_users_and_groups(map()) -> map().
sort_users_and_groups(Map) when is_map(Map) ->
maps:fold(fun(Key, Value, Acc) ->
case Key of
users ->
case is_list(Value) of
true -> maps:put(Key, lists:sort(Value), Acc);
false -> error({badarg, {Key, Value}})
end;
groups ->
case is_list(Value) of
true -> maps:put(Key, lists:sort(Value), Acc);
false -> error({badarg, {Key, Value}})
end;
'mandatory-users' ->
case is_list(Value) of
true -> maps:put(Key, lists:sort(Value), Acc);
false -> error({badarg, {Key, Value}})
end;
'mandatory-groups' ->
case is_list(Value) of
true -> maps:put(Key, lists:sort(Value), Acc);
false -> error({badarg, {Key, Value}})
end;
paths ->
case is_list(Value) of
true ->
maps:put(Key, lists:map(fun sort_users_and_groups/1, Value), Acc);
false -> error({badarg, {Key, Value}})
end;
_ ->
maps:put(Key, Value, Acc)
end
end, #{}, Map).
41 changes: 37 additions & 4 deletions test/bec_config_ss_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
-export([ to_wz_branch_reviewers_adds_default_mandatory/1
, to_wz_branch_reviewers_keeps_original_mandatory/1
, to_wz_branch_reviewers_adds_default_mandatory_in_path/1
, to_wz_branch_reviewers_ordered_keys/1
]).

%%==============================================================================
Expand All @@ -23,10 +24,11 @@ all() ->
[ to_wz_branch_reviewers_adds_default_mandatory
, to_wz_branch_reviewers_keeps_original_mandatory
, to_wz_branch_reviewers_adds_default_mandatory_in_path
, to_wz_branch_reviewers_ordered_keys
].

to_wz_branch_reviewers_adds_default_mandatory(_Config) ->
Reviewers =
Reviewers =
[ #{ <<"branch-id">> => <<"master">>
, <<"groups">> => []
, <<"paths">> => []
Expand All @@ -45,7 +47,7 @@ to_wz_branch_reviewers_adds_default_mandatory(_Config) ->
?assertEqual(ExpectedResult, bec_config_ss:to_wz_branch_reviewers(Reviewers)).

to_wz_branch_reviewers_keeps_original_mandatory(_Config) ->
Reviewers =
Reviewers =
[ #{ <<"branch-id">> => <<"master">>
, <<"groups">> => []
, <<"paths">> => []
Expand All @@ -66,7 +68,7 @@ to_wz_branch_reviewers_keeps_original_mandatory(_Config) ->
?assertEqual(ExpectedResult, bec_config_ss:to_wz_branch_reviewers(Reviewers)).

to_wz_branch_reviewers_adds_default_mandatory_in_path(_Config) ->
Reviewers =
Reviewers =
[ #{ <<"branch-id">> => <<"master">>
, <<"groups">> => []
, <<"paths">> => [#{ <<"file-path-pattern">> => []
Expand All @@ -91,4 +93,35 @@ to_wz_branch_reviewers_adds_default_mandatory_in_path(_Config) ->
, 'mandatory-groups' => []
}
],
?assertEqual(ExpectedResult, bec_config_ss:to_wz_branch_reviewers(Reviewers)).
?assertEqual(ExpectedResult, bec_config_ss:to_wz_branch_reviewers(Reviewers)).

to_wz_branch_reviewers_ordered_keys(_Config) ->
Reviewers =
[ #{ <<"branch-id">> => <<"master">>
, <<"groups">> => [ <<"group.b">>, <<"group.a">> ]
, <<"paths">> => [#{ <<"file-path-pattern">> => [ <<"path.b">>, <<"path.a">> ]
, <<"users">> => [ <<"user.d">>, <<"user.c">> ]
, <<"groups">> => [ <<"group.d">>, <<"group.c">> ]
, <<"mandatory-users">> => [ <<"user.f">>, <<"user.e">> ]
, <<"mandatory-groups">> => [ <<"group.f">>, <<"group.e">> ]
}]
, <<"users">> => [ <<"user.b">>, <<"user.a">> ]
, <<"mandatory-users">> => [ <<"user.h">>, <<"user.g">> ]
, <<"mandatory-groups">> => [ <<"group.h">>, <<"group.g">> ]
}
],
ExpectedResult =
[ #{ 'branch-id' => <<"master">>
, 'groups' => [ <<"group.a">>, <<"group.b">> ]
, 'paths' => [#{ 'file-path-pattern' => [ <<"path.b">>, <<"path.a">> ]
, 'users' => [ <<"user.c">>, <<"user.d">> ]
, 'groups' => [ <<"group.c">>, <<"group.d">> ]
, 'mandatory-users' => [ <<"user.e">>, <<"user.f">> ]
, 'mandatory-groups' => [ <<"group.e">>, <<"group.f">> ]
}]
, 'users' => [ <<"user.a">>, <<"user.b">> ]
, 'mandatory-users' => [ <<"user.g">>, <<"user.h">> ]
, 'mandatory-groups' => [ <<"group.g">>, <<"group.h">> ]
}
],
?assertEqual(ExpectedResult, bec_config_ss:to_wz_branch_reviewers(Reviewers)).

0 comments on commit 841a653

Please sign in to comment.