From e9d9a9c2469e2254bc7c8fe98f984bb30cf45f8b Mon Sep 17 00:00:00 2001
From: Fabien Boucher <fboucher@redhat.com>
Date: Feb 19 2020 15:49:37 +0000
Subject: [PATCH 1/2] source-to-tarball: better handling of computed release version


---

diff --git a/roles/source-to-tarball/files/detect-source-version.py b/roles/source-to-tarball/files/detect-source-version.py
index 8014647..4a1b3b8 100755
--- a/roles/source-to-tarball/files/detect-source-version.py
+++ b/roles/source-to-tarball/files/detect-source-version.py
@@ -2,6 +2,7 @@
 
 import os
 import sys
+import json
 import subprocess
 
 
@@ -21,7 +22,7 @@ def execute(argv, capture=False, cwd=None, test=False):
         if not test:
             print("Command %s failed" % argv)
         raise RuntimeError()
-    return out
+    return out.decode()
 
 
 def get_repo_version(repo):
@@ -33,8 +34,10 @@ def get_repo_version(repo):
         version = execute(["git", "rev-list", "--count", ref],
                           capture=True, cwd=repo, test=True).strip()
         # Make sure rev-list version is lower than first tag
-        version = "0.0.0.0-dev%s" % version
-    return version.replace('-', '.')
+        version = "0.0.0.0-0.dev%s" % version
+    version, release = version.replace('-g', 'g').split('-')
+    release = '0.%s%%{?dist}' % release
+    return json.dumps([version, release])
 
 
 if __name__ == "__main__":
diff --git a/roles/source-to-tarball/tasks/main.yaml b/roles/source-to-tarball/tasks/main.yaml
index 51a2bbc..4fa7d5d 100644
--- a/roles/source-to-tarball/tasks/main.yaml
+++ b/roles/source-to-tarball/tasks/main.yaml
@@ -13,6 +13,22 @@
   set_fact:
     tarball: "{{ ansible_user_dir }}/{{ zuul.project.src_dir }}/{{ package_name }}-HEAD.tgz"
 
+- name: Detect version and release of source from git
+  script:
+     cmd: >
+       files/detect-source-version.py
+       {{ ansible_user_dir }}/{{ zuul.project.src_dir }}
+  register: cmd
+
+- name: Load json command output
+  set_fact:
+    cmd_data: "{{ cmd.stdout | from_json }}"
+
+- name: Set source_version and source_release
+  set_fact:
+    source_version: "{{ cmd_data[0] }}"
+    source_release: "{{ cmd_data[1] }}"
+
 - block:
   - name: Detect version from specfile
     shell: >
@@ -22,26 +38,12 @@
     args:
       chdir: "{{ ansible_user_dir }}/{{ zuul.project.src_dir }}"
 
-  - name: Set source_version fact
+  - name: Override source_version fact
     set_fact:
       source_version: "{{ cmd.stdout|trim }}"
 
   when: version_from_specfile
 
-- block:
-  - name: Detect source version from git
-    script:
-       cmd: >
-         files/detect-source-version.py
-         {{ ansible_user_dir }}/{{ zuul.project.src_dir }}
-    register: cmd
-
-  - name: Set source_version fact
-    set_fact:
-      source_version: "{{ cmd.stdout|trim }}"
-
-  when: not version_from_specfile
-
 - name: Create the source tarball
   command: >
     tar -czf /tmp/HEAD.tgz
@@ -60,6 +62,12 @@
     regexp: "^[Vv]ersion:.*"
     replace: "Version: {{ source_version }}"
 
+- name: Set specfile Release
+  replace:
+    path: "{{ ansible_user_dir }}/{{ zuul.project.src_dir }}/{{ package_name }}.spec"
+    regexp: "^[Rr]elease:.*"
+    replace: "Release: {{ source_release }}"
+
 - name: Set specfile Source0
   replace:
     path: "{{ ansible_user_dir }}/{{ zuul.project.src_dir }}/{{ package_name }}.spec"

From 7c4ebb5dcb5f620d5360ffbe9dc861cd5237a107 Mon Sep 17 00:00:00 2001
From: Fabien Boucher <fboucher@redhat.com>
Date: Feb 19 2020 17:11:46 +0000
Subject: [PATCH 2/2] mock-rpm-build: build order


---

diff --git a/playbooks/rpm/mock-build.yaml b/playbooks/rpm/mock-build.yaml
index b8c3d5a..f0eab43 100644
--- a/playbooks/rpm/mock-build.yaml
+++ b/playbooks/rpm/mock-build.yaml
@@ -1,9 +1,8 @@
 - hosts: all
   tasks:
     - set_fact:
-        project_deps_to_build: "{{ zuul.projects.values() | list | json_query('[?required]') }}"
-        project_to_build:
-          - "{{ zuul.project }}"
+        projects_to_build: "{{ zuul.projects.values() | list }}"
+        projects_build_order: "{{ projects_build_order | default([]) }}"
         srpms: []
 
     - name: Add additional repositories
@@ -29,17 +28,19 @@
     - include_role:
         name: source-to-tarball
       vars:
+        query: "[?name=='{{item}}']"
         zuul:
-          project: "{{ item }}"
-      loop: "{{ project_deps_to_build | union(project_to_build) }}"
+          project: "{{ projects_to_build | json_query(query) | first }}"
+      loop: "{{ projects_build_order }}"
       when: "{{ build_from_source | default(false) }}"
 
     - include_role:
         name: mock-srpm-build
       vars:
+        query: "[?name=='{{item}}']"
         zuul:
-          project: "{{ item }}"
-      loop: "{{ project_deps_to_build | union(project_to_build) }}"
+          project: "{{ projects_to_build | json_query(query) | first }}"
+      loop: "{{ projects_build_order }}"
 
     - include_role:
         name: mock-build
diff --git a/roles/filter-artifacts/tasks/main.yml b/roles/filter-artifacts/tasks/main.yml
index e286be6..413c01a 100644
--- a/roles/filter-artifacts/tasks/main.yml
+++ b/roles/filter-artifacts/tasks/main.yml
@@ -2,9 +2,9 @@
   script:
     cmd: >
       filter.py '{{ zuul.artifacts | to_json }}'
-      '{{ project_deps_to_build | to_json }}'
+      '{{ projects_to_build | to_json }}'
   register: _filter
 
 - name: Store back the result in project_deps_to_build fact
   set_fact:
-    project_deps_to_build: "{{ _filter.stdout | trim }}"
+    projects_to_build: "{{ _filter.stdout | trim }}"
diff --git a/zuul.d/jobs.yaml b/zuul.d/jobs.yaml
index d6f1921..304eba7 100644
--- a/zuul.d/jobs.yaml
+++ b/zuul.d/jobs.yaml
@@ -31,6 +31,12 @@
          node and mock build root. eg.
          {name: 'nightly', url: 'scheme://...', gpgcheck: 1}
 
+      .. zuul:jobvar:: projects_build_order
+         : default: []
+
+         The ordered list a project name that describe the
+         build order.
+
     provides: repo
     requires: repo
     run: playbooks/rpm/mock-build.yaml

