Post

How to create resource in Bitbucket via curl and Ansible

How to create resource in Bitbucket via curl and Ansible

How to create resource in Bitbucket via curl and Ansible

There are some situation when one can have credentials to some web page that does not have API properly exposed and TOKEN can not be used. However, when trying to do certain action e.g. import Bitbucket repo from already existing repo - this is doable by clicking in a web browser using username and password. In this particular situation the reference/sample code can be found in variable SAMPLE_PROJECT_URL.

In this case one can take an advantage of cookies and trying to simulate a browser.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
export PASSWORD='...'
export USERNAME='...'
export BB_PROJECT_NAME="PROJECT-NAME"
export BITBUCKET_REPO_NAME="REPO-NAME"
export SAMPLE_PROJECT_URL="https://example.net/scm/example-solutions/sample-valid.git"

# repo level
export WRITERS="<username1> <username2> ..."
export ADMINS="<username4> <username5> ..."

echo -e "\n\nCreting a project\n"

curl -u "${USERNAME}:${PASSWORD}" -k --url "https://bitbucket.url.example/rest/api/latest/projects" \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data '{
  "key": "'"${BB_PROJECT_NAME}"'"
}'

echo -e "\n\nGET a project\n"

curl -u "${USERNAME}:${PASSWORD}" -k --url 'https://bitbucket.url.example/rest/api/latest/projects/'"${BB_PROJECT_NAME}"'' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json'

echo -e "\n\nGrant permissions to ed pas horizon\n"

curl -u "${USERNAME}:${PASSWORD}" -k --request PUT \
  --url 'https://bitbucket.url.example/rest/api/latest/projects/'"${BB_PROJECT_NAME}"'/permissions/groups' \
  --header 'Accept: application/json' \
  --url-query "name=<group-name>" \
  --url-query "permission=PROJECT_ADMIN"

# ............................................
# Import/crate new repo with sample code
# ............................................

rm /tmp/cookies.txt
curl -G -k -c - 'https://bitbucket.url.example/j_atl_security_check' \
  -H "Content-Type: application/x-www-form-urlencoded; Charset=utf-8" \
  --data-urlencode "j_username=${USERNAME}" \
  --data-urlencode "j_password=${PASSWORD}" \
  --data-urlencode "queryString=native_login=" \
  --data-urlencode "submit=Log in" > /tmp/cookies.txt

curl -k  -b /tmp/cookies.txt 'https://bitbucket.url.example/rest/importer/latest/projects/'"${BB_PROJECT_NAME}"'/import/repos' \
  --header 'Content-Type: application/json' \
  --data '{
    "externalRepositories":[{
      "cloneUrl":"'"${SAMPLE_PROJECT_URL}"'",
      "name":"'"${BITBUCKET_REPO_NAME}"'",
      "scmId":"git"
    }]
  }'


# echo -e "\n\nCreate repository\n"

# curl -u "${USERNAME}:${PASSWORD}" -k --url 'https://bitbucket.url.example/rest/api/1.0/projects/'"${BB_PROJECT_NAME}"'/repos' \
# --header 'Accept: application/json' \
# --header 'Content-Type: application/json' \
# --data '{
#   "name": "'"${BITBUCKET_REPO_NAME}"'",
#   "project": {
#     "key": "'"${BB_PROJECT_NAME}"'"
#   },
#   "slug": "'"${BITBUCKET_REPO_NAME}"'",
#   "scmId": "git"
# }'

echo -e "\n\nGrant users WRITE permissions\n"

curl  -k -X PUT \
-u "${USERNAME}:${PASSWORD}" \
--url 'https://bitbucket.url.example/rest/api/latest/projects/'"${BB_PROJECT_NAME}"'/repos/'"${BITBUCKET_REPO_NAME}"'/permissions/users' \
--header 'Accept: application/json' --header 'Content-Type: application/json' $(for i in $(echo $WRITERS); do echo -n --url-query name=$i" " ; done) \
--url-query "permission=REPO_WRITE"

echo -e "\n\nGrant users ADMIN permissions\n"

curl  -k -X PUT \
-u "${USERNAME}:${PASSWORD}" \
--url 'https://bitbucket.url.example/rest/api/latest/projects/'"${BB_PROJECT_NAME}"'/repos/'"${BITBUCKET_REPO_NAME}"'/permissions/users' \
--header 'Accept: application/json' --header 'Content-Type: application/json' $(for i in $(echo $ADMINS); do echo -n --url-query name=$i" " ; done) \
--url-query "permission=REPO_ADMIN"

Ansible version

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
---
# .........................................................................
# 1. Creating Bitbucket project
# .........................................................................
- name: Read input YAML file
  ansible.builtin.shell: cat 
  register: result

- name: Load input data into data variable
  set_fact:
    data: ""

- name: Create Bitbucket project
  uri:
    url: "/rest/api/latest/projects"
    user: ""
    password: ""
    force_basic_auth: yes
    headers:
      Content-Type: "application/json"
      Accept: "application/json"
    method: POST
    body:
      key: ""
    body_format: json
    validate_certs: no
    status_code: [200, 201, 409]
  register: response

# .........................................................................
# 2. Granting PROJECT_ADMIN to BB_GROUP_NAME group at Bitbucket project level
# .........................................................................
- name: Grant permissions to group BB_GROUP_NAME
  uri:
    url: "/rest/api/latest/projects//permissions/groups?name=&permission=PROJECT_ADMIN"
    user: ""
    password: ""
    force_basic_auth: yes
    headers:
      Accept: "application/json"
    method: PUT
    validate_certs: no
    status_code: [200, 201, 204]
  register: response

# .........................................................................
# 3. Import/create new repo with sample code
# .........................................................................
- name: Import/create new repo with sample code
  uri:
    url: "/rest/importer/latest/projects//import/repos"
    user: ""
    password: ""
    force_basic_auth: yes
    headers:
      Content-Type: "application/json"
      Accept: "application/json"
    method: POST
    body:
      externalRepositories:
        - cloneUrl: ""
          name: ""
          scmId: "git"
    body_format: json
    validate_certs: no
    status_code: [200, 201, 409]
  register: response

# .........................................................................
# 4. Grant necessary permissions to Bitbucket repo
# .........................................................................
- name: Construct permissions query string for REPO_ADMIN
  set_fact:
    admins_query: "permission=REPO_ADMIN"
  when: data.bb_repo_admins is defined

- name: Grant necessary permissions to Bitbucket repo REPO_ADMIN
  uri:
    url: "/rest/api/latest/projects//repos//permissions/users?"
    user: ""
    password: ""
    force_basic_auth: yes
    headers:
      Content-Type: "application/json"
      Accept: "application/json"
    method: PUT
    validate_certs: no
    status_code: [200, 201, 204]
  register: response
  when: data.bb_repo_admins is defined
---
This post is licensed under CC BY 4.0 by the author.