From 1534bff151dfd64a1430ad4497019a0a7e1e794e Mon Sep 17 00:00:00 2001
From: Michal Kovarik <mkovarik@redhat.com>
Date: Mar 16 2020 09:15:49 +0000
Subject: [PATCH 1/2] Create openshift project for pre/post merge


Pre-create openshift project for pre/post merge scenarios. To enable
parallel run use new namespace for each job.

---

diff --git a/roles/c3i/README.md b/roles/c3i/README.md
index 2f213e0..a529351 100644
--- a/roles/c3i/README.md
+++ b/roles/c3i/README.md
@@ -31,7 +31,7 @@ Due to pagure.io connection issue it's recommended to set 'SCM checkout retry co
 Workflows
 ---------
 ### Pre-merge
-Job is listening on PR changes - new, update, rebase, reopen. New changes are checkouted into current directory. Pagure PR is marked as `Pending` in pagure. Snippet `c3i_build_and_test_snippet` is executed. Test results should be sent as part of `c3i_build_and_test_snippet`. Pagure PR state is updated. Temporary images are deleted.
+Job is listening on PR changes - new, update, rebase, reopen. New changes are checkouted into current directory. New C3IaaS project is requested and provided via `PIPELINE_ID` variable. Pagure PR is marked as `Pending` in pagure. Snippet `c3i_build_and_test_snippet` is executed. Test results should be sent as part of `c3i_build_and_test_snippet`. Pagure PR state is updated. Temporary images are deleted.
 
 ### Post-merge
 Job is listening on commits in master branch. New changes are checkouted into current directory. Pagure PR is marked as `Pending` in pagure. Snippet `c3i_build_and_test_snippet` is executed. Test results should be sent as
@@ -56,7 +56,7 @@ Snippets used defining custom parts of Jenkins files. It's expected stage part i
 ### c3i\_build\_and\_test\_snippet
 Path to snippet for Build container and test acceptance for 'latest' tag.
 
-Git repository is checkout into current working directory.
+Git repository is checkout into current working directory. New openshift project is created and available in `PIPELINE_ID` variable.
 
 Input parameters are defined in [templates/build.yml](templates/build.yml). Input parameters can be extended by `c3i_build_custom_parameters`, eg.:
 ```yaml
diff --git a/roles/c3i/defaults/main.yml b/roles/c3i/defaults/main.yml
index 79f940e..0a12b45 100644
--- a/roles/c3i/defaults/main.yml
+++ b/roles/c3i/defaults/main.yml
@@ -51,9 +51,6 @@ c3i_test_subscriber:
 # Create service accounts and role bindings - can be done only by admin
 c3i_skip_service_accounts: false
 
-c3i_imagestream_namespace: "{{ c3i_ocp_namespace }}"
-c3i_imagestream_name: "{{ c3i_component }}"
-
 c3i_quay_address: quay.io
 c3i_quay_namespace: factory2
 
@@ -69,7 +66,13 @@ c3i_workflow_jenkins_image: docker-registry.upshift.redhat.com/factory2/pipeline
 
 c3i_tracked_container_repo: "{{ c3i_quay_address }}/{{ c3i_quay_namespace }}/{{ c3i_component }}"
 
-c3i_pipeline_as_a_service_namespace: c3i 
+c3i_c3iaas_default_lifetime: 60
+c3i_c3iaas_build_lifetime: "{{ c3i_c3iaas_default_lifetime }}"
+c3i_c3iaas_integration_test_lifetime: "{{ c3i_c3iaas_default_lifetime }}"
+
+c3i_c3iaas_namespace: c3i
+c3i_c3iaas_job_name: c3iaas-request-project
+c3i_pipeline_as_a_service_namespace: "{{ c3i_c3iaas_namespace }}"
 c3i_messaging_provider: Red Hat UMB
 c3i_fedmsg_provider: fedmsg
 c3i_cloud_name: openshift
diff --git a/roles/c3i/templates/build.Jenkinsfile b/roles/c3i/templates/build.Jenkinsfile
index b2c156a..7c73f86 100644
--- a/roles/c3i/templates/build.Jenkinsfile
+++ b/roles/c3i/templates/build.Jenkinsfile
@@ -144,6 +144,36 @@ pipeline {
       }
     }
     {% else %}
+    stage('Allocate C3IaaS project') {
+      steps {
+        script {
+          if (env.PR_NO) {
+            env.PIPELINE_ID = "c3i-{{ c3i_component }}-pr-${env.PR_NO}-git${env.GIT_COMMIT.take(8)}-${currentBuild.id}"
+          } else {
+            env.PIPELINE_ID = "c3i-{{ c3i_component }}-${env.GIT_REPO_REF}-git${env.GIT_COMMIT.take(8)}-${currentBuild.id}"
+          }
+          echo "Requesting new OpenShift project ${env.PIPELINE_ID}..."
+          openshift.withCluster() {
+            openshift.withProject(params.C3IAAS_NAMESPACE) {
+              c3i.buildAndWait(script: this, objs: "bc/${params.C3IAAS_JOB_NAME}",
+                '-e', "PROJECT_NAME=${env.PIPELINE_ID}",
+                '-e', "ADMIN_GROUPS=system:serviceaccounts:${env.TRIGGER_NAMESPACE},system:serviceaccounts:${env.C3IAAS_NAMESPACE}",
+                '-e', "LIFETIME_IN_MINUTES=${params.C3IAAS_LIFETIME}"
+              )
+            }
+          }
+        }
+      }
+      post {
+        success {
+          echo "Allocated project ${env.PIPELINE_ID}"
+        }
+        failure {
+          echo "Failed to allocate ${env.PIPELINE_ID} project"
+        }
+      }
+    }
+
     {{ task_var_build_and_test }}
     stage('Push container') {
       when {
@@ -187,19 +217,6 @@ pipeline {
     }
   }
   post {
-    cleanup {
-      script {
-        if (env.RESULTING_TAG) {
-          echo "Removing tag ${env.RESULTING_TAG} from the ImageStream..."
-          openshift.withCluster() {
-            openshift.withProject("${params.IMAGESTREAM_NAMESPACE}") {
-              openshift.tag("${params.IMAGESTREAM_NAME}:${env.RESULTING_TAG}",
-                "-d")
-            }
-          }
-        }
-      }
-    }
     success {
       script {
         // on pre-merge workflow success
diff --git a/roles/c3i/templates/build.yml b/roles/c3i/templates/build.yml
index a8f0c25..f89b179 100644
--- a/roles/c3i/templates/build.yml
+++ b/roles/c3i/templates/build.yml
@@ -41,12 +41,14 @@ spec:
         value: {{ c3i_fedmsg_provider }}
       - name: PAGURE_API_KEY_SECRET_NAME
         value: {{ c3i_pagure_api_key_secret }}
-      - name: IMAGESTREAM_NAMESPACE
-        value: {{ c3i_imagestream_namespace }}
-      - name: IMAGESTREAM_NAME
-        value: {{ c3i_imagestream_name }}
       - name: MAIL_ADDRESS
         value: {{ c3i_mail_address }}
+      - name: C3IAAS_NAMESPACE
+        value: {{ c3i_c3iaas_namespace }}
+      - name: C3IAAS_JOB_NAME
+        value: {{ c3i_c3iaas_job_name }}
+      - name: C3IAAS_LIFETIME
+        value: '"{{ c3i_c3iaas_build_lifetime }}"'
 {% for param in c3i_build_custom_parameters %}
       - name: {{ param.name }}
         value: {{ param.value }}
diff --git a/vars/c3i.groovy b/vars/c3i.groovy
index 51130f7..ce40f5a 100644
--- a/vars/c3i.groovy
+++ b/vars/c3i.groovy
@@ -226,6 +226,7 @@ def clone(Map args) {
  * @param args.provider UMB provider name defined in Jenkins.
  * @param args.docs Documentation link
  * @param args.email Contact email
+ * @param args.xunit URL of xunit results
  * @return Result from sendCIMessage.
  */
 def sendResultToMessageBus(Map args) {
@@ -278,7 +279,7 @@ def sendResultToMessageBus(Map args) {
       "type": "integration",
       "category": "${environment}",
       "status": "${currentBuild.result == null || currentBuild.result == 'SUCCESS' ? 'passed':'failed'}",
-      "xunit": "",
+      "xunit": "${args.xunit ?: ''}",
       "generated_at": "${new Date().format("yyyy-MM-dd'T'HH:mm:ss'Z'", TimeZone.getTimeZone('UTC'))}",
       "namespace": "c3i",
       "version": "0.1.0"

From 252000779ed2b381203de1b8ff99c6b3869e5fe8 Mon Sep 17 00:00:00 2001
From: Michal Kovarik <mkovarik@redhat.com>
Date: Mar 16 2020 11:11:47 +0000
Subject: [PATCH 2/2] fix build regexp


'waiverdb' matched also for 'xxx/forks/waiverdb'

---

diff --git a/roles/c3i/templates/build.Jenkinsfile b/roles/c3i/templates/build.Jenkinsfile
index 7c73f86..0ceae82 100644
--- a/roles/c3i/templates/build.Jenkinsfile
+++ b/roles/c3i/templates/build.Jenkinsfile
@@ -43,7 +43,7 @@ pipeline {
           name: params.MESSAGING_FEDMSG_PROVIDER,
           overrides: [topic: "{{ topic }}"],
           checks: [
-            [field: '$.repo.url_path', expectedValue: params.GIT_REPO.split('/')[3..-1].join('/').replace('forks/', 'fork/').replaceAll(/.git$/, '')],
+            [field: '$.repo.url_path', expectedValue: "^${params.GIT_REPO.split('/')[3..-1].join('/').replace('forks/', 'fork/').replaceAll(/.git$/, '')}\$"],
             [field: '$.branch', expectedValue: params.GIT_MAIN_BRANCH],
           ]
         )

