Skip to content

Commit

Permalink
Merge pull request #7 from klarna-incubator/master
Browse files Browse the repository at this point in the history
Sync from upstream
  • Loading branch information
Wingless-Archangel committed Jul 14, 2022
2 parents ba8712e + 0816e08 commit 0456aad
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 15 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.3.0] - 2022-07-06

- Made merge-condition optional in wz pr restrictions from_map/1 conversion.
- Added test data for merge-condition for wz pr restrictions data generation.
- Added delay parameter to throttle trying each repo_config

## [1.0.0] - 2020-08-16

- Open sourcing of an existing internal project
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,14 @@ Then you can run _BEC_:
```
$ bec
Usage: bec [-h] [-c [<config>]] [-r [<repo_config>]]
[-d [<delay>]]
[-e [<enforce>]] [-k [<keep>]]
[-v [<verbosity>]]
-h, --help Show this help message
-c, --config The BitBucket Config File [default: bitbucket.config]
-r, --repo_config The Repo Config to check or configure [default: undefined]
-d, --delay Delay (in seconds) between trying each repo_config
-e, --enforce Enforce values when they do not match expectations [default: false]
-k, --keep Keep going after the first error (always true when enforce == true) [default: false]
-v, --verbosity Verbosity Level [default: 1]
Expand Down
4 changes: 4 additions & 0 deletions src/bec.erl
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ do_main(Options) ->
RepoConfig ->
Enforce = proplists:get_value(enforce, Options),
K = proplists:get_value(keep, Options),
Delay = proplists:get_value(delay, Options),
case bitbucket_repo_config:verify( RepoConfig,
[ {enforce, Enforce}
, {delay, Delay}
, {abort_on_error, not K}
]) of
true ->
Expand Down Expand Up @@ -70,6 +72,8 @@ specs() ->
, "The BitBucket Config File"}
, { repo_config, $r, "repo_config", {string, undefined}
, "The Repo Config to check or configure"}
, { delay, $d, "delay", {integer, 0}
, "Delay (in seconds) between trying each repo_config"}
, { enforce, $e, "enforce", {boolean, false}
, "Enforce values when they do not match expectations"}
, { keep, $k, "keep", {boolean, false}
Expand Down
16 changes: 8 additions & 8 deletions src/bec_wz_pr_restriction_t.erl
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ from_map(#{ <<"refName">> := RefName
, <<"approvalQuota">> := ApprovalQuota
, <<"groupQuota">> := GroupQuota
, <<"ignoreContributingReviewersApproval">> := IgnoreSelfApprove
, <<"mergeCondition">> := MergeCondition
}) ->
} = Map) ->
MergeCondition = maps:get(<<"mergeCondition">>, Map, <<"">>),
#{ 'branch-id' => bec_wz_utils:strip_prefix(RefName)
, 'approval-quota' => binary_to_integer(ApprovalQuota)
, 'group-quota' => GroupQuota
Expand All @@ -44,19 +44,19 @@ from_map(#{ <<"refName">> := RefName
}.

-spec to_map(restriction()) -> map().
to_map(#{ 'branch-id' := BranchId
} = Map) ->
ApprovalQuota = maps:get('approval-quota', Map, 0),
GroupQuota = maps:get('group-quota', Map, 0),
MergeCondition = maps:get('merge-condition', Map, ""),
to_map(#{ 'branch-id' := BranchId } = Map) ->
ApprovalQuota = maps:get('approval-quota', Map, 0),
GroupQuota = maps:get('group-quota', Map, 0),
MergeCondition = maps:get('merge-condition', Map, ""),
IgnoreSelfApprove = maps:get('ignore-self-approve', Map, false),
RefName = bec_wz_utils:add_prefix(BranchId),
#{ <<"approvalQuota">> => ApprovalQuota
, <<"approvalQuotaEnabled">> => true
, <<"automergeUsers">> => []
, <<"deleteSourceBranch">> => false
, <<"groupQuota">> => GroupQuota
, <<"mergeCondition">> => MergeCondition
, <<"refName">> => bec_wz_utils:add_prefix(BranchId)
, <<"refName">> => RefName
, <<"requiredBuildsCount">> => <<>>
, <<"requiredSignaturesCount">> => <<>>
, <<"srcRefName">> => <<>>
Expand Down
16 changes: 10 additions & 6 deletions src/bitbucket_repo_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ verify(Path, Options) ->
Dirname = filename:dirname(Path),
Basename = filename:basename(Path, ".ymlt"),
VarsPath = filename:join([Dirname, Basename ++ ".ymlv"]),
%% Convert Delay to milliseconds
Delay = proplists:get_value(delay, Options, 0) * 1000,
Vars = read_vars(VarsPath),
All = [begin
[Config] = read_template(Path, Var),
do_verify(Options, Config)
Result = do_verify(Options, Config),
timer:sleep(Delay),
Result
end || Var <- Vars],
lists:all(fun(true) -> true; (false) -> false end, All);
Ext ->
Expand Down Expand Up @@ -170,11 +174,11 @@ do_verify(ProjectKey, RepoSlug, Key, Expected) ->
true ->
true;
false ->
%% How to pretty-format these messages with lager?
ok = io:format( "[~s/~s] Actual ==> ~p ~n"
, [ProjectKey, RepoSlug, Actual]),
ok = io:format( "[~s/~s] Expected ==> ~p ~n"
, [ProjectKey, RepoSlug, Adapted]),
%% Not showing details here since they may contain secrets.
%% They were debug-logged above.
ok = lager:error( "[~s/~s] Actual does not match Expected"
" (increase verbosity to see details)~n"
, [ProjectKey, RepoSlug]),
false
end.

Expand Down
5 changes: 4 additions & 1 deletion test/bec_proper_gen.erl
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ wz_pr_restrictions() ->

wz_pr_restriction() ->
?LET( {BranchId, ApprovalQuota, GroupQuota, IgnoreSelfApprove, MergeCondition}
, {branch_id(), non_zero_nat(), non_zero_nat(), bool(), binary()}
, {branch_id(), non_zero_nat(), non_zero_nat(), bool(), merge_condition()}
, #{ 'branch-id' => BranchId
, 'approval-quota' => ApprovalQuota
, 'group-quota' => GroupQuota
Expand Down Expand Up @@ -325,6 +325,9 @@ webhook() ->
webhooks() ->
unique_list(webhook(), {map, 'name'}).

merge_condition() ->
oneof([<<"">>, <<"approvalCount > 1 & groupQuota >= 1">>]).

%%==============================================================================
%% Helpers
%%==============================================================================
Expand Down

0 comments on commit 0456aad

Please sign in to comment.