Skip to content

Commit

Permalink
Merge pull request #3 from klarna-incubator/master
Browse files Browse the repository at this point in the history
merge latest version from upstream
  • Loading branch information
Wingless-Archangel authored Mar 8, 2021
2 parents fbc4b6c + b090bfb commit f880741
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 8 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,20 @@ docker run --rm --name erlangbuilder -v ${PWD}:/bec -w=/bec erlang rebar3 escr

## Sample BitBucket Configuration

BEC supports both Basic Authentication (via username/password) and Token-Based Authentication (preferred).
If both a `token` and a `username/password` pair are provided, the token will take precedence.

Set BitBucket url and credentials in `bitbucket.config`:

```
{bitbucket_url, "https://my.bitbucket.server"}.
{bitbucket_username, "first.last"}.
{bitbucket_password, "password"}.
{bitbucket_token, "someToken"}.
```

Please follow [this](https://confluence.atlassian.com/bitbucketserver072/personal-access-tokens-1005335924.html#Personalaccesstokens-Generatingpersonalaccesstokens) guide if you want to generate the token to authenticate.

## Sample Repo Configuration

You can find a sample configuration file for a custom BitBucket repo
Expand Down
1 change: 1 addition & 0 deletions sample_repo_configuration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ pr-restrictions:
required-all-tasks-complete: true
required-approvers: 2
required-successful-builds: 1
unapprove-on-update: true
merge-config:
default-strategy: no-ff
enabled-strategies:
Expand Down
2 changes: 2 additions & 0 deletions src/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
5 changes: 5 additions & 0 deletions src/bec_pr_restriction_t.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
, 'required-all-tasks-complete' := boolean()
, 'required-approvers' := integer()
, 'required-successful-builds' := boolean()
, 'unapprove-on-update' := boolean()
}.

%%==============================================================================
Expand All @@ -35,12 +36,14 @@ from_map(#{ <<"mergeConfig">> := MergeConfig
, <<"requiredAllTasksComplete">> := RequiredAllTasksComplete
, <<"requiredApprovers">> := RequiresApprovers
, <<"requiredSuccessfulBuilds">> := RequiredSuccessfulBuilds
, <<"unapproveOnUpdate">> := UnapproveOnUpdate
}) ->
#{ 'merge-config' => bec_merge_config_t:from_map(MergeConfig)
, 'required-all-approvers' => RequiredAllApprovers
, 'required-all-tasks-complete' => RequiredAllTasksComplete
, 'required-approvers' => RequiresApprovers
, 'required-successful-builds' => RequiredSuccessfulBuilds
, 'unapprove-on-update' => UnapproveOnUpdate
}.

-spec to_map(restriction()) -> map().
Expand All @@ -49,10 +52,12 @@ to_map(#{ 'merge-config' := MergeConfig
, 'required-all-tasks-complete' := RequiredAllTasksComplete
, 'required-approvers' := RequiresApprovers
, 'required-successful-builds' := RequiredSuccessfulBuilds
, 'unapprove-on-update' := UnapproveOnUpdate
}) ->
#{ <<"mergeConfig">> => bec_merge_config_t:to_map(MergeConfig)
, <<"requiredAllApprovers">> => RequiredAllApprovers
, <<"requiredAllTasksComplete">> => RequiredAllTasksComplete
, <<"requiredApprovers">> => RequiresApprovers
, <<"requiredSuccessfulBuilds">> => RequiredSuccessfulBuilds
, <<"unapproveOnUpdate">> => UnapproveOnUpdate
}.
19 changes: 13 additions & 6 deletions src/bitbucket_http.erl
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,21 @@ do_http_request(Method, Request) ->

-spec headers() -> [{string(), string()}].
headers() ->
Username = application:get_env(bec, bitbucket_username, ""),
Password = application:get_env(bec, bitbucket_password, ""),
Credentials = base64:encode_to_string(Username ++ ":" ++ Password),
[
{"Authorization", "Basic " ++ Credentials}
, {"Accept", "application/json"}
[ {"Authorization", authorization()}
, {"Accept", "application/json"}
].

-spec authorization() -> string().
authorization() ->
case application:get_env(bec, bitbucket_token, "") of
"" ->
Username = application:get_env(bec, bitbucket_username, ""),
Password = application:get_env(bec, bitbucket_password, ""),
"Basic " ++ base64:encode_to_string(Username ++ ":" ++ Password);
Token ->
"Bearer " ++ Token
end.

-spec handle_result(httpc_result()) ->
{ok, map()} | {ok, [map()]} | {error, any()}.
handle_result({ok, {{_Ver, Status, _Phrase}, _H, Body}}) when Status =:= 200;
Expand Down
3 changes: 3 additions & 0 deletions src/bitbucket_repo_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ verify(Path) ->
-spec verify(string(), opts()) -> boolean().
verify(Path, Options) ->
case filename:extension(Path) of
".yaml" ->
Config = read(Path),
do_verify(Options, Config);
".yml" ->
Config = read(Path),
do_verify(Options, Config);
Expand Down
11 changes: 9 additions & 2 deletions test/bec_proper_gen.erl
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,20 @@ path() ->
%% PR Restrictions
%%==============================================================================
pr_restrictions() ->
?LET( {MergeConfig, AllApprovers, AllTasks, Approvers, Builds}
, {merge_config(), bool(), bool(), nat(), nat()}
?LET( { MergeConfig
, AllApprovers
, AllTasks
, Approvers
, Builds
, UnapproveOnUpdate
}
, {merge_config(), bool(), bool(), nat(), nat(), bool()}
, #{ 'merge-config' => MergeConfig
, 'required-all-approvers' => AllApprovers
, 'required-all-tasks-complete' => AllTasks
, 'required-approvers' => Approvers
, 'required-successful-builds' => Builds
, 'unapprove-on-update' => UnapproveOnUpdate
}).

merge_config() ->
Expand Down

0 comments on commit f880741

Please sign in to comment.