---
# file: roles/sonarqube/tasks/install_sonarqube.yml
- name: "add sonarqube_talas_community_branch_plugin to sonarqube_internal_plugins_url"
  ansible.builtin.set_fact:
    sonarqube_internal_plugins_url: "{{ sonarqube_internal_plugins_url | default([]) + ['https://nexus.talas.dev/repository/github.com/talas/sonarqube-community-branch-plugin/releases/download/{{ sonarqube_talas_community_branch_plugin_version }}.talas/sonarqube-community-branch-plugin-{{ sonarqube_talas_community_branch_plugin_version }}.talas.jar'] }}"
  when: sonarqube_talas_community_branch_plugin_version is defined

- name: "add sonarqube_talas_sonar_plugin to sonarqube_internal_plugins_url"
  ansible.builtin.set_fact:
    sonarqube_internal_plugins_url: "{{ sonarqube_internal_plugins_url | default([]) + ['https://nexus.talas.dev/repository/maven-releases/com/cos/sonar_plugin/talas-sonar-plugin/{{ sonarqube_talas_sonar_plugin_version }}/talas-sonar-plugin-{{ sonarqube_talas_sonar_plugin_version }}.jar'] }}"
  when: sonarqube_talas_sonar_plugin_version is defined

- name: "add sonarqube_community_branch_plugin to sonarqube_external_plugins_url"
  ansible.builtin.set_fact:
    sonarqube_external_plugins_url: "{{ sonarqube_external_plugins_url | default([]) + ['https://github.com/mc1arke/sonarqube-community-branch-plugin/releases/download/{{ sonarqube_community_branch_plugin_version }}/sonarqube-community-branch-plugin-{{ sonarqube_community_branch_plugin_version }}.jar'] }}"
  when: sonarqube_community_branch_plugin_version  is defined

- name: "add sonarqube_redaalaoui_sonar_java_no_var_plugin to sonarqube_external_plugins_url"
  ansible.builtin.set_fact:
    sonarqube_external_plugins_url: "{{ sonarqube_external_plugins_url | default([]) + ['https://repo1.maven.org/maven2/me/redaalaoui/sonar/sonar-java-no-var-plugin/{{ sonarqube_redaalaoui_sonar_java_no_var_plugin_version }}/sonar-java-no-var-plugin-{{ sonarqube_redaalaoui_sonar_java_no_var_plugin_version }}.jar'] }}"
  when: sonarqube_redaalaoui_sonar_java_no_var_plugin_version is defined

- name: "/opt/sonarqube/stateless/extensions/plugins"
  ansible.builtin.file:
    path: "/opt/sonarqube/stateless/extensions/plugins"
    state: directory
    owner: sonarqube
    group: sonarqube
  tags: sonarqube

- name: "install additional internal plugins"
  ansible.builtin.get_url:
    url: "{{ item }}"
    dest: "/opt/sonarqube/stateful/extensions/plugins"
    owner: sonarqube
    group: sonarqube
    url_username: "{{ repo_talas_com_user }}"
    url_password: "{{ repo_talas_com_pass }}"
  loop: "{{ sonarqube_internal_plugins_url }}"
  when: sonarqube_internal_plugins_url is defined
  tags: sonarqube

- name: "install additional external plugins"
  ansible.builtin.get_url:
    url: "{{ item }}"
    dest: "/opt/sonarqube/stateful/extensions/plugins"
    owner: sonarqube
    group: sonarqube
  loop: "{{ sonarqube_external_plugins_url }}"
  when: sonarqube_external_plugins_url is defined
  tags: sonarqube

- name: "search old plugins"
  ansible.builtin.set_fact:
    sonarqube_plugins_name: "{{ (sonarqube_internal_plugins_url | default([]) |map('split','/')|map('last')|list()) + (sonarqube_external_plugins_url | default([]) | map('split','/') | map('last') | list()) }}"
  tags: sonarqube

- name: "find old plugins"
  ansible.builtin.find:
    paths: "/opt/sonarqube/stateful/extensions/plugins"
    patterns: "*.jar"
    excludes: "{{ sonarqube_plugins_name | default([]) }}"
  register: sonarqube_old_plugins
  tags: sonarqube

- name: "delete old plugins"
  ansible.builtin.file:
    path: "{{ item.path }}"
    state: absent
  loop: "{{ sonarqube_old_plugins.files }}"
  tags: sonarqube
