Skip to content

Commit 0456aad

Browse files
Merge pull request #7 from klarna-incubator/master
Sync from upstream
2 parents ba8712e + 0816e08 commit 0456aad

File tree

6 files changed

+34
-15
lines changed

6 files changed

+34
-15
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [1.3.0] - 2022-07-06
9+
10+
- Made merge-condition optional in wz pr restrictions from_map/1 conversion.
11+
- Added test data for merge-condition for wz pr restrictions data generation.
12+
- Added delay parameter to throttle trying each repo_config
13+
814
## [1.0.0] - 2020-08-16
915

1016
- Open sourcing of an existing internal project

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,14 @@ Then you can run _BEC_:
5656
```
5757
$ bec
5858
Usage: bec [-h] [-c [<config>]] [-r [<repo_config>]]
59+
[-d [<delay>]]
5960
[-e [<enforce>]] [-k [<keep>]]
6061
[-v [<verbosity>]]
6162
6263
-h, --help Show this help message
6364
-c, --config The BitBucket Config File [default: bitbucket.config]
6465
-r, --repo_config The Repo Config to check or configure [default: undefined]
66+
-d, --delay Delay (in seconds) between trying each repo_config
6567
-e, --enforce Enforce values when they do not match expectations [default: false]
6668
-k, --keep Keep going after the first error (always true when enforce == true) [default: false]
6769
-v, --verbosity Verbosity Level [default: 1]

src/bec.erl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ do_main(Options) ->
3939
RepoConfig ->
4040
Enforce = proplists:get_value(enforce, Options),
4141
K = proplists:get_value(keep, Options),
42+
Delay = proplists:get_value(delay, Options),
4243
case bitbucket_repo_config:verify( RepoConfig,
4344
[ {enforce, Enforce}
45+
, {delay, Delay}
4446
, {abort_on_error, not K}
4547
]) of
4648
true ->
@@ -70,6 +72,8 @@ specs() ->
7072
, "The BitBucket Config File"}
7173
, { repo_config, $r, "repo_config", {string, undefined}
7274
, "The Repo Config to check or configure"}
75+
, { delay, $d, "delay", {integer, 0}
76+
, "Delay (in seconds) between trying each repo_config"}
7377
, { enforce, $e, "enforce", {boolean, false}
7478
, "Enforce values when they do not match expectations"}
7579
, { keep, $k, "keep", {boolean, false}

src/bec_wz_pr_restriction_t.erl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ from_map(#{ <<"refName">> := RefName
3434
, <<"approvalQuota">> := ApprovalQuota
3535
, <<"groupQuota">> := GroupQuota
3636
, <<"ignoreContributingReviewersApproval">> := IgnoreSelfApprove
37-
, <<"mergeCondition">> := MergeCondition
38-
}) ->
37+
} = Map) ->
38+
MergeCondition = maps:get(<<"mergeCondition">>, Map, <<"">>),
3939
#{ 'branch-id' => bec_wz_utils:strip_prefix(RefName)
4040
, 'approval-quota' => binary_to_integer(ApprovalQuota)
4141
, 'group-quota' => GroupQuota
@@ -44,19 +44,19 @@ from_map(#{ <<"refName">> := RefName
4444
}.
4545

4646
-spec to_map(restriction()) -> map().
47-
to_map(#{ 'branch-id' := BranchId
48-
} = Map) ->
49-
ApprovalQuota = maps:get('approval-quota', Map, 0),
50-
GroupQuota = maps:get('group-quota', Map, 0),
51-
MergeCondition = maps:get('merge-condition', Map, ""),
47+
to_map(#{ 'branch-id' := BranchId } = Map) ->
48+
ApprovalQuota = maps:get('approval-quota', Map, 0),
49+
GroupQuota = maps:get('group-quota', Map, 0),
50+
MergeCondition = maps:get('merge-condition', Map, ""),
5251
IgnoreSelfApprove = maps:get('ignore-self-approve', Map, false),
52+
RefName = bec_wz_utils:add_prefix(BranchId),
5353
#{ <<"approvalQuota">> => ApprovalQuota
5454
, <<"approvalQuotaEnabled">> => true
5555
, <<"automergeUsers">> => []
5656
, <<"deleteSourceBranch">> => false
5757
, <<"groupQuota">> => GroupQuota
5858
, <<"mergeCondition">> => MergeCondition
59-
, <<"refName">> => bec_wz_utils:add_prefix(BranchId)
59+
, <<"refName">> => RefName
6060
, <<"requiredBuildsCount">> => <<>>
6161
, <<"requiredSignaturesCount">> => <<>>
6262
, <<"srcRefName">> => <<>>

src/bitbucket_repo_config.erl

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,14 @@ verify(Path, Options) ->
2525
Dirname = filename:dirname(Path),
2626
Basename = filename:basename(Path, ".ymlt"),
2727
VarsPath = filename:join([Dirname, Basename ++ ".ymlv"]),
28+
%% Convert Delay to milliseconds
29+
Delay = proplists:get_value(delay, Options, 0) * 1000,
2830
Vars = read_vars(VarsPath),
2931
All = [begin
3032
[Config] = read_template(Path, Var),
31-
do_verify(Options, Config)
33+
Result = do_verify(Options, Config),
34+
timer:sleep(Delay),
35+
Result
3236
end || Var <- Vars],
3337
lists:all(fun(true) -> true; (false) -> false end, All);
3438
Ext ->
@@ -170,11 +174,11 @@ do_verify(ProjectKey, RepoSlug, Key, Expected) ->
170174
true ->
171175
true;
172176
false ->
173-
%% How to pretty-format these messages with lager?
174-
ok = io:format( "[~s/~s] Actual ==> ~p ~n"
175-
, [ProjectKey, RepoSlug, Actual]),
176-
ok = io:format( "[~s/~s] Expected ==> ~p ~n"
177-
, [ProjectKey, RepoSlug, Adapted]),
177+
%% Not showing details here since they may contain secrets.
178+
%% They were debug-logged above.
179+
ok = lager:error( "[~s/~s] Actual does not match Expected"
180+
" (increase verbosity to see details)~n"
181+
, [ProjectKey, RepoSlug]),
178182
false
179183
end.
180184

test/bec_proper_gen.erl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ wz_pr_restrictions() ->
176176

177177
wz_pr_restriction() ->
178178
?LET( {BranchId, ApprovalQuota, GroupQuota, IgnoreSelfApprove, MergeCondition}
179-
, {branch_id(), non_zero_nat(), non_zero_nat(), bool(), binary()}
179+
, {branch_id(), non_zero_nat(), non_zero_nat(), bool(), merge_condition()}
180180
, #{ 'branch-id' => BranchId
181181
, 'approval-quota' => ApprovalQuota
182182
, 'group-quota' => GroupQuota
@@ -325,6 +325,9 @@ webhook() ->
325325
webhooks() ->
326326
unique_list(webhook(), {map, 'name'}).
327327

328+
merge_condition() ->
329+
oneof([<<"">>, <<"approvalCount > 1 & groupQuota >= 1">>]).
330+
328331
%%==============================================================================
329332
%% Helpers
330333
%%==============================================================================

0 commit comments

Comments
 (0)