From e2a95426a01a1f062c5c74d5e557f9197b15d4f5 Mon Sep 17 00:00:00 2001 From: Robert de Bock Date: Mon, 9 Jan 2023 08:50:25 +0100 Subject: [PATCH] Sweeper. --- gitlab-public.yml | 54 +++++++++++++++++++++++++++++++++++++ gitlab-pull-mirror.yml | 61 +++++++++++++++++++----------------------- gitlab.yml | 40 +++++++++++++-------------- 3 files changed, 102 insertions(+), 53 deletions(-) create mode 100755 gitlab-public.yml diff --git a/gitlab-public.yml b/gitlab-public.yml new file mode 100755 index 0000000..e742f0c --- /dev/null +++ b/gitlab-public.yml @@ -0,0 +1,54 @@ +#!/usr/bin/env ansible-playbook +--- +- name: Create a pull mirror gitlab project + hosts: localhost + become: no + gather_facts: no + + vars: + github_owner: robertdebock + gitlab_namespace: robertdebock-iac + + vars_files: + - defaults/main.yml + - vars/main.yml + - vars/vault.yml + + tasks: + - name: see if all variables are set + assert: + that: + - github_owner is defined + - role is defined + quiet: yes + + - name: Get a detailed list of owned GitLab projects + uri: + url: "https://gitlab.com/api/v4/projects?owned=true&search={{ role }}" + method: GET + headers: + PRIVATE-TOKEN: "{{ gitlab_private_token }}" + return_content: yes + register: gitlab_projects + + - name: Pick project_ids from GitLab projects + set_fact: + gitlab_projects: "{{ gitlab_projects.json | selectattr('name', 'equalto', role) | list }}" + + - name: pick a single project. + set_fact: + gitlab_project_id: "{{ gitlab_projects[0].id }}" + + - name: Patch existing project to mirror + uri: + url: "https://gitlab.com/api/v4/projects/{{ gitlab_project_id }}" + method: PUT + headers: + PRIVATE-TOKEN: "{{ gitlab_private_token }}" + body_format: json + body: + visibility: public + status_code: + - 200 + register: create_project + changed_when: yes diff --git a/gitlab-pull-mirror.yml b/gitlab-pull-mirror.yml index b20a7b5..9f29778 100755 --- a/gitlab-pull-mirror.yml +++ b/gitlab-pull-mirror.yml @@ -29,45 +29,40 @@ headers: Authorization: "Bearer {{ github_token }}" register: github_repo - -# - name: Show shit -# debug: -# msg: "{{ github_repo.json }}" - - - name: Get GitLab namespace_id + + - name: Get a detailed list of owned GitLab projects uri: - url: "https://gitlab.com/api/v4/namespaces" + url: "https://gitlab.com/api/v4/projects?owned=true&search={{ role }}" + method: GET + headers: + PRIVATE-TOKEN: "{{ gitlab_private_token }}" + return_content: yes + register: gitlab_projects + + - name: Pick project_ids from GitLab projects + set_fact: + gitlab_projects: "{{ gitlab_projects.json | selectattr('name', 'equalto', role) | list }}" + + - name: pick a single project. + set_fact: + gitlab_project_id: "{{ gitlab_projects[0].id }}" + + - name: Patch existing project to mirror + uri: + url: "https://gitlab.com/api/v4/projects/{{ gitlab_project_id }}" + method: PUT headers: PRIVATE-TOKEN: "{{ gitlab_private_token }}" body_format: json body: - search: "{{ gitlab_namespace }}" - register: gitlab_namespace - -# - name: Show shit -# debug: -# msg: "{{ gitlab_namespace.json }}" - - - name: Create a project that pulls from GitHub - uri: - url: "https://gitlab.com/api/v4/projects" - method: POST - headers: - PRIVATE-TOKEN: "{{ gitlab_private_token }}" - body_format: json - body: - name: "{{ github_repo.json.name }}" - description: "{{ github_repo.json.description }}" - # TODO: Should the `path` include the namespace: - path: "{{ github_repo.json.name }}" - namespace_id: 59906445 import_url: "{{ github_repo.json.clone_url }}" mirror: true - mirror_triggers_builds: true + mirror_trigger_builds: true status_code: - - 201 - register: gitlab_project + - 200 + register: create_project + changed_when: yes -# - name: Show shit -# debug: -# msg: "{{ gitlab_project }}" + # - name: Show shit + # debug: + # msg: "{{ create_project }}" diff --git a/gitlab.yml b/gitlab.yml index b881657..45a60d4 100755 --- a/gitlab.yml +++ b/gitlab.yml @@ -23,7 +23,7 @@ - name: urlencode path set_fact: - encoded_path: "{{ namespace + '%2F' + 'ansible-role-' + role }}" + encoded_path: "{{ namespace + '%2F' + role }}" - name: place variable block: @@ -56,25 +56,25 @@ protects: true masked: true - - name: list pipeline schedules - uri: - url: "https://gitlab.com/api/v4/projects/{{ encoded_path }}/pipeline_schedules" - method: GET - headers: - PRIVATE-TOKEN: "{{ gitlab_private_token }}" - register: gitlab_schedules - - - name: delete all pipeline schedules - uri: - url: "https://gitlab.com/api/v4/projects/{{ encoded_path }}/pipeline_schedules/{{ item.id }}" - method: DELETE - headers: - PRIVATE-TOKEN: "{{ gitlab_private_token }}" - status_code: - - 204 - loop: "{{ gitlab_schedules.json }}" - loop_control: - label: "{{ item.id }}" +# - name: list pipeline schedules +# uri: +# url: "https://gitlab.com/api/v4/projects/{{ encoded_path }}/pipeline_schedules" +# method: GET +# headers: +# PRIVATE-TOKEN: "{{ gitlab_private_token }}" +# register: gitlab_schedules +# +# - name: delete all pipeline schedules +# uri: +# url: "https://gitlab.com/api/v4/projects/{{ encoded_path }}/pipeline_schedules/{{ item.id }}" +# method: DELETE +# headers: +# PRIVATE-TOKEN: "{{ gitlab_private_token }}" +# status_code: +# - 204 +# loop: "{{ gitlab_schedules.json }}" +# loop_control: +# label: "{{ item.id }}" - name: create a new pipeline schedule uri: