Skip to content

Commit

Permalink
add configure repos for bitbucket.org (#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
pcarney8 authored and oybed committed Dec 20, 2018
1 parent c2dc2b3 commit df1f25f
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 0 deletions.
13 changes: 13 additions & 0 deletions roles/scm/bitbucket.org/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -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 }
2 changes: 2 additions & 0 deletions roles/scm/bitbucket.org/meta/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
dependencies:
94 changes: 94 additions & 0 deletions roles/scm/bitbucket.org/tasks/configure-repos.yml
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions roles/scm/bitbucket.org/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions roles/scm/bitbucket.org/tests/inventory
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[bitbucket]
localhost ansible_connection=local
31 changes: 31 additions & 0 deletions roles/scm/bitbucket.org/tests/test.yml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit df1f25f

Please sign in to comment.