---
# Parameters:
#   playbook inventory
#     stage := the name of the stage (e.g. dev, int, qa, prod)
#   environment variable
#     GITLAB_API_TOKEN := Access token from gitlab

#############################################################
#   Creating inventory dynamically for given parameters
#############################################################

- hosts: localhost
  gather_facts: false
  connection: local

  tasks:
    - name: Add hosts
      add_host:
        name: "{{ stage }}-gitlab"
        groups: "{{ ['stage_' + stage ] }}"

#############################################################
#   Creating gitlab mirrors for current stage
#############################################################

- hosts: "stage_{{ stage }}"
  serial: "{{ serial_number | default(1) }}"
  gather_facts: false
  connection: local
  vars:
    projects:
    - id: 1210
      name: argocd
    - id: 1216
      name: operator-awx
    - id: 1212
      name: operator-jaeger
    - id: 1231
      name: operator-knative
    - id: 1233
      name: smardigo-awx
    - id: 1232
      name: smardigo-jaeger

  pre_tasks:
    - name: "Add repository remote mirror to project"
      delegate_to: 127.0.0.1
      become: false
      uri:
        url: "https://git.dev-at.de/api/v4/projects/{{ item.id }}/remote_mirrors"
        method: POST
        body_format: json
        body:
          enabled: true
          only_protected_branches: true
          url: "https://{{ gitea_admin_username }}:{{ gitea_admin_password }}@{{ shared_service_gitea_hostname }}/argocd/{{ item.name }}.git"
        headers:
          PRIVATE-TOKEN: "{{ lookup('env', 'GITLAB_API_TOKEN') }}"
        status_code: [201]
      loop: "{{ projects }}"