# standard role for fetching a package dist-git into the Ansible controller for running its tests
# variables:
# - package: dist-git source package name
# - modulemd: file name of module metadata description, for getting correct branch name
---
- name: Install git
  dnf: name=git state=installed

- name: Clone package dist-git
  git:
    repo: git://pkgs.devel.redhat.com/rpms/{{ package }}
    # read package branch from module md file
    version: "{{ (lookup('file', modulemd) | from_yaml)['data']['components']['rpms'][package]['ref'] }}"
    dest: "/tmp/packages/{{ package }}"

# fetch can only get a single file, so we have to do this in a loop
- name: Get package test file list
  find:
    paths: "/tmp/packages/{{ package }}/tests"
    recurse: yes
  register: test_files_to_fetch

- name: Copy package test files to controller
  fetch:
    src: "{{ item.path }}"
    # strip off /tmp/ prefix
    dest: "{{ playbook_dir }}/{{ item.path[5:] }}"
    flat: yes
  with_items: "{{ test_files_to_fetch.files }}"

- name: Copy package files
  fetch:
    src: "/tmp/packages/{{ package }}/{{ item }}"
    dest: "{{ playbook_dir }}/packages/{{ package }}/"
    flat: yes
  with_items: ["{{ package }}.spec", "sources"]