diff --git a/roles/scm/bitbucket.org/defaults/main.yml b/roles/scm/bitbucket.org/defaults/main.yml new file mode 100644 index 000000000..052423c37 --- /dev/null +++ b/roles/scm/bitbucket.org/defaults/main.yml @@ -0,0 +1,13 @@ +--- +bitbucket_api_url: "https://api.bitbucket.org" +bitbucket_url: "https://bitbucket.org" +default_branch_restrictions: + - { kind: "require_approvals_to_merge", users: [], groups: [], pattern: "master", value: 1 } + - { kind: "require_tasks_to_be_completed", users: [], groups: [], pattern: "master" } + - { kind: "reset_pullrequest_approvals_on_change", users: [], groups: [], pattern: "master" } + - { kind: "force", users: [], groups: [], pattern: "master" } + - { kind: "delete", users: [], groups: [], pattern: "master" } + - { kind: "push", users: [], groups: [], pattern: "master" } + +build_branch_restriction: + - { kind: "require_passing_builds_to_merge", users: [], groups: [], pattern: "master", value: 1 } \ No newline at end of file diff --git a/roles/scm/bitbucket.org/meta/main.yml b/roles/scm/bitbucket.org/meta/main.yml new file mode 100644 index 000000000..7f867d73b --- /dev/null +++ b/roles/scm/bitbucket.org/meta/main.yml @@ -0,0 +1,2 @@ +--- +dependencies: diff --git a/roles/scm/bitbucket.org/tasks/configure-repos.yml b/roles/scm/bitbucket.org/tasks/configure-repos.yml new file mode 100644 index 000000000..bee385c76 --- /dev/null +++ b/roles/scm/bitbucket.org/tasks/configure-repos.yml @@ -0,0 +1,94 @@ +--- + +- name: Set default branch restrictions + set_fact: + branch_restrictions: "{{ default_branch_restrictions }}" + +- name: Set branch restrictions if build restrictions are added + set_fact: + branch_restrictions: "{{ branch_restrictions | union(build_branch_restriction) }}" + when: + - repo.has_build_branch_restriction is defined + - repo.has_build_branch_restriction + +- name: Set branch restrictions if they are passed in + set_fact: + branch_restrictions: "{{ repo.branch_restrictions }}" + when: + - repo.branch_restrictions is defined + +- name: Create a repo with {{ repo.repo_name }} in bitbucket project with {{ repo.project_name }} + uri: + url: "{{ bitbucket_api_url }}/2.0/repositories/{{ repo.team_name }}/{{ repo.repo_name }}" + method: POST + user: "{{ bitbucket_username | b64decode | replace('\n', '') }}" + password: "{{ bitbucket_admin_password | b64decode | replace('\n', '') }}" + force_basic_auth: yes + body_format: json + body: + scm: "git" + project: + key: "{{ repo.project_name }}" + is_private: "{{ repo.is_private | default(true) }}" + has_wiki: "{{ repo.has_wiki | default(true) }}" + has_issues: "{{ repo.has_issues | default(true) }}" + status_code: 200, 400 + register: create_repo_output + tags: + - create-repo + +- name: Fail if repo doesn't already exist + fail: + msg: "{{ repo.repo_name }} doesn't exist and there was still a status code: {{ create_repo_output.status }}" + when: + - create_repo_output.json.error.message != "Repository with this Slug and Owner already exists." + +- name: Add default reviewers for {{ repo.repo_name }} + uri: + url: "{{ bitbucket_api_url }}/2.0/repositories/{{ repo.team_name }}/{{ repo.repo_name }}/default-reviewers/{{ item }}" + method: PUT + user: "{{ bitbucket_username | b64decode | replace('\n', '') }}" + password: "{{ bitbucket_admin_password | b64decode | replace('\n', '') }}" + force_basic_auth: yes + with_items: + - "{{ bitbucket_users }}" + tags: + - default-reviewers + +- name: Add branch restrictions on master and passing build for {{ repo.repo_name }} + uri: + url: "{{ bitbucket_api_url }}/2.0/repositories/{{ repo.team_name }}/{{ repo.repo_name }}/branch-restrictions" + method: POST + user: "{{ bitbucket_username | b64decode | replace('\n', '') }}" + password: "{{ bitbucket_admin_password | b64decode | replace('\n', '') }}" + headers: + Content-Type: "application/json" + Accept: "application/json" + force_basic_auth: yes + body_format: json + body: "{{ item }}" + status_code: 201, 409 + with_items: "{{ branch_restrictions }}" + tags: + - branch-restrictions + +# TODO: bitbucket.org API 2.0 does not currently support changing the default merge strategies +#- name: Create default merge strategy for {{ repo.repo_name }} +# uri: +# url: "{{ bitbucket_api_url }}/2.0/repositories/{{ repo.team_name }}/{{ repo.repo_name }}/merge-strategies" +# method: GET +# user: "{{ bitbucket_username | b64decode | replace('\n', '') }}" +# password: "{{ bitbucket_admin_password | b64decode | replace('\n', '') }}" +# force_basic_auth: yes +# headers: +# Content-Type: "application/json" +# Accept: "application/json" +# body_format: json +# body: +# mergeConfig: +# defaultStrategy: +# id: "squash" +# strategies: +# - id: "squash" +# tags: +# - default-merge-strategy diff --git a/roles/scm/bitbucket.org/tasks/main.yml b/roles/scm/bitbucket.org/tasks/main.yml new file mode 100644 index 000000000..c4b00b70f --- /dev/null +++ b/roles/scm/bitbucket.org/tasks/main.yml @@ -0,0 +1,14 @@ +--- + +- name: "Check for bitbucket usernames and passwords Result" + fail: + msg: "BitBucket Username and Password variables MUST be set" + when: + - bitbucket_username is not defined or bitbucket_username|trim == '' + - bitbucket_admin_password is not defined or bitbucket_admin_password|trim == '' + +- name: "Configure Repos" + include_tasks: configure-repos.yml + loop: "{{ bitbucket_repos | default([]) }}" + loop_control: + loop_var: repo diff --git a/roles/scm/bitbucket.org/tests/inventory b/roles/scm/bitbucket.org/tests/inventory new file mode 100644 index 000000000..30ee4cd5d --- /dev/null +++ b/roles/scm/bitbucket.org/tests/inventory @@ -0,0 +1,2 @@ +[bitbucket] +localhost ansible_connection=local diff --git a/roles/scm/bitbucket.org/tests/test.yml b/roles/scm/bitbucket.org/tests/test.yml new file mode 100644 index 000000000..75c0d9c5c --- /dev/null +++ b/roles/scm/bitbucket.org/tests/test.yml @@ -0,0 +1,31 @@ +--- + +- name: "Configure BitBucket" + hosts: bitbucket + vars: + bitbucket_repos: + - repo_name: "test-repo" + team_name: "testteam" + project_name: "TEST" + - repo_name: "test-repo" + team_name: "testteam" + project_name: "TEST" + is_private: false + - repo_name: "test-repo" + team_name: "testteam" + project_name: "TEST" + has_build_branch_restriction: true + - repo_name: "test-repo" + team_name: "testteam" + project_name: "TEST" + branch_restrictions: + - { kind: "force", users: [], groups: [], pattern: "master" } + - { kind: "delete", users: [], groups: [], pattern: "master" } + - { kind: "push", users: [], groups: [], pattern: "master" } + bitbucket_users: + - testUsername + bitbucket_username: "bitbucketAuthUser" + bitbucket_admin_username: "bitbucketAuthPassword" + tasks: + - include_role: + name: roles/configure-bitbucket \ No newline at end of file