---
# clone our other repositories into this repo
# NOTE: since you can't loop against blocks (and we're using them for failure #
#       recovery when the request branch doesn't exist) we have to define each
#       of these separately rather than using a loop.
- name: Get Smart Gateway Operator
  block:
    - name: Try cloning same-named branch or override branch from specified repository
      git:
        repo: "{{ sgo_repository }}"
        dest: working/smart-gateway-operator
        version: "{{ sgo_branch | default(branch, true) }}"
  rescue:
    - name: "Get {{ version_branches.sgo }} upstream branch because specified branch or repository doesn't exist"
      git:
        repo: https://github.com/infrawatch/smart-gateway-operator
        dest: working/smart-gateway-operator
        version: "{{ version_branches.sgo }}"

- name: Get sg-core
  block:
    - name: Try cloning same-named branch or override branch from specified repository
      git:
        repo: "{{ sg_core_repository }}"
        dest: working/sg-core
        version: "{{ sg_core_branch | default(branch, true) }}"
  rescue:
    - name: "Get {{ version_branches.sg_core }} upstream branch because specified branch or repository doesn't exist"
      git:
        repo: https://github.com/infrawatch/sg-core
        dest: working/sg-core
        version: "{{ version_branches.sg_core }}"

- name: Get sg-bridge
  block:
    - name: Try cloning same-named branch or override branch from specified repository
      git:
        repo: "{{ sg_bridge_repository }}"
        dest: working/sg-bridge
        version: "{{ sg_bridge_branch | default(branch, true) }}"
  rescue:
    - name: "Get {{ version_branches.sg_bridge }} upstream branch because specified branch or repository doesn't exist"
      git:
        repo: https://github.com/infrawatch/sg-bridge
        dest: working/sg-bridge
        version: "{{ version_branches.sg_bridge }}"

- name: Get prometheus-webhook-snmp
  block:
    - name: Try cloning same-named branch or override branch from specified repository
      git:
        repo: "{{ prometheus_webhook_snmp_repository }}"
        dest: working/prometheus-webhook-snmp
        version: "{{ prometheus_webhook_snmp_branch | default(branch, true) }}"
  rescue:
    - name: "Get {{ version_branches.prometheus_webhook_snmp }} upstream branch because specified branch or repository doesn't exist"
      git:
        repo: https://github.com/infrawatch/prometheus-webhook-snmp
        dest: working/prometheus-webhook-snmp
        version: "{{ version_branches.prometheus_webhook_snmp }}"

# Branches for Loki Operator don't work the same as with other repositories.
# We don't have write access to the upstream repository to create our own
# branches there.
- name: Get Loki Operator
  block:
    - name: Try cloning same-named branch or override branch from specified repository
      git:
        repo: "{{ loki_operator_repository }}"
        dest: working/loki-operator
        version: "{{ loki_operator_branch | default(branch, true) }}"
        force: yes
  rescue:
    - name: "Get {{ version_branches.loki_operator }} upstream branch because specified branch or repository doesn't exist"
      git:
        repo: https://github.com/viaq/loki-operator
        dest: working/loki-operator
        version: "{{ version_branches.loki_operator }}"
        force: yes
  when:
    - __service_telemetry_observability_strategy == "use_community"
