---
- name: Set release name
  set_fact:
    artg_release: "{{ artg_change.branch | replace('stable/', '') }}"

- name: Set DLRN distro (legacy distgit branch naming)
  set_fact:
    artg_distro: "rpm-{{ artg_release }}"
  when: artg_release in ['master', 'mitaka']

- name: Set DLRN distro (new stable branch naming scheme)
  set_fact:
    artg_distro: "{{ artg_release }}-rdo"
  when: artg_release not in ['master', 'mitaka']

- name: Use local baseurl when using local mirrors
  set_fact:
    dlrn_baseurl: 'file:///tmp/fake-dlrn-repos'
  when: dlrn_use_local_mirrors|bool

- name: Override projects.ini settings
  lineinfile:
    dest: '{{ build_repo_dir }}/DLRN/projects.ini'
    regexp: '{{ project_item.regexp }}'
    line: '{{ project_item.line }}'
  with_items:
    - {regexp: 'baseurl=.*', line: 'baseurl={{ dlrn_baseurl }}'}
    - {regexp: 'distro=.*', line: 'distro={{ artg_distro }}'}
    - {regexp: 'source=.*', line: 'source={{ artg_change.branch }}'}
  loop_control:
    loop_var: project_item

- name: Map project name to DLRN project name
  register: project_name_mapped
  shell: >
    source {{ build_repo_dir }}/dlrn-venv/bin/activate;
    export PROJECT_NAME=$(echo {{ artg_change.project }} | sed "s|openstack/||");
    rdopkg findpkg -s $PROJECT_NAME -l rdoinfo | grep ^name | awk '{print $2}'
  args:
    chdir: '{{ build_repo_dir }}/DLRN'
  changed_when: false

- debug:
    msg: "WARNING: Unable to build {{ artg_change.project }}. No pkg found."
  when: project_name_mapped.stdout == ''

- name: Ensure artg_rdo_packages is defined
  set_fact:
    artg_rdo_packages: []
  when: artg_rdo_packages is not defined

- when: project_name_mapped.stdout != ''
  block:
    - name: Append project name to package list
      set_fact:
        artg_rdo_packages: '{{ artg_rdo_packages }} + {{ [ project_name_mapped.stdout ] }}'

    - name: Create data directory if doesn't exist yet
      file: path="{{ build_repo_dir }}/DLRN/data/" state=directory

    - name: Copy from ready directories if configured
      command: >
        cp -raf {{ artg_repos_dir }}/{{ artg_change.project|regex_replace('openstack[^/]*/', '') }}
        {{ build_repo_dir }}/DLRN/data/{{ project_name_mapped.stdout }}
      ignore_errors: true
      register: copy_dir
      when: artg_repos_dir is defined

    - name: Clone the gated change
      git:
        repo: '{{ artg_change.host }}/{{ artg_change.project }}'
        dest: '{{ build_repo_dir }}/DLRN/data/{{ project_name_mapped.stdout }}'
        refspec: '{{ artg_change.refspec }}'
        version: 'FETCH_HEAD'
      when: artg_repos_dir is not defined or copy_dir is failed

    - name: Clone the distgit change
      git:
        repo: '{{ artg_change.distgit.host }}/{{ artg_change.distgit.project }}'
        dest: '{{ build_repo_dir }}/DLRN/data/{{ project_name_mapped.stdout }}_distro'
        refspec: '{{ artg_change.distgit.refspec }}'
        version: 'FETCH_HEAD'
        accept_hostkey: true
      when: artg_change.distgit is defined and (artg_repos_dir is not defined or copy_dir is failed)

    - name: Run DLRN
      shell: >
        set +e;
        source {{ build_repo_dir }}/dlrn-venv/bin/activate;
        while true; do
            dlrn --config-file projects.ini --head-only --package-name {{ project_name_mapped.stdout }} --local --info-repo rdoinfo --dev;
            if [ $? -eq 0 ]; then
                # SUCCESS
                break;
            elif [ $? -eq 1 ]; then
                # FAILED
                exit 1;
            elif [ $? -eq 2 ]; then
                # RETRY
                continue;
            fi;
            # Unexpected DLRN return code
            exit $?;
        done;
      args:
        chdir: '{{ build_repo_dir }}/DLRN'
      register: repo_built
      when: artg_build_one|bool
