← Back to team overview

sts-sponsors team mailing list archive

[Merge] ~ack/maas-ci/+git/maas-ci-config:generic-gh-repo-tester into ~maas-committers/maas-ci/+git/maas-ci-config:master

 

Alberto Donato has proposed merging ~ack/maas-ci/+git/maas-ci-config:generic-gh-repo-tester into ~maas-committers/maas-ci/+git/maas-ci-config:master.

Commit message:
rework ansible_job_creator definition to make it reusable



Requested reviews:
  MAAS Committers (maas-committers)

For more details, see:
https://code.launchpad.net/~ack/maas-ci/+git/maas-ci-config/+merge/439309
-- 
Your team MAAS Committers is requested to review the proposed merge of ~ack/maas-ci/+git/maas-ci-config:generic-gh-repo-tester into ~maas-committers/maas-ci/+git/maas-ci-config:master.
diff --git a/jenkins/jobs/ansible_playbook.yaml b/jenkins/jobs/ansible_playbook.yaml
index df5b247..d31028b 100644
--- a/jenkins/jobs/ansible_playbook.yaml
+++ b/jenkins/jobs/ansible_playbook.yaml
@@ -16,7 +16,7 @@
           default: https://github.com/maas/maas-ansible-playbook
     triggers:
       - timed: "H * * * *"
-    dsl: !include-jinja2: ansible_job_creator.groovy
+    dsl: !include-jinja2: gh_repo_tester.groovy
 
 - job-template:
     name: ansible-playbook-tests
diff --git a/jenkins/jobs/ansible_job_creator.groovy b/jenkins/jobs/gh_repo_tester.groovy
similarity index 87%
rename from jenkins/jobs/ansible_job_creator.groovy
rename to jenkins/jobs/gh_repo_tester.groovy
index c80c7d3..8a52f42 100644
--- a/jenkins/jobs/ansible_job_creator.groovy
+++ b/jenkins/jobs/gh_repo_tester.groovy
@@ -1,6 +1,8 @@
 import groovy.json.JsonSlurper
 
-def parseJobs(jsonString) {
+MAAS_GH_USER = "maas"
+
+def parseJobs(repoName, jsonString) {
     def parsed_jobs = new JsonSlurper().parseText(jsonString)
     def jobs = []
 
@@ -20,10 +22,9 @@ def parseJobs(jsonString) {
             // Don't test PRs that aren't targeting the main branch
             target_repo_name = parsed_job["base"]["repo"]["full_name"]
             default_branch = parsed_job["base"]["repo"]["default_branch"]
-            def correct_repo = false
-            if (target_branch==default_branch && target_repo_name=="maas/maas-ansible-playbook") {
-                correct_repo = true
-            }
+            def correct_repo = (
+                target_branch==default_branch && target_repo_name=="${MAAS_GH_USER}/${repoName}"
+            )
 
             // Don't test PRs that have the WIP label applied
             parsed_pr_labels = parsed_job["labels"]
@@ -37,13 +38,9 @@ def parseJobs(jsonString) {
             }
 
             // Don't test PRs that have already been tested (ie: they have a status applied to the commit)
-            commit_status_out = sh(script: "curl -s https://api.github.com/repos/maas/maas-ansible-playbook/commits/${source_latest_commit}/statuses";, returnStdout: true).trim()
+            commit_status_out = sh(script: "curl -s https://api.github.com/repos/${MAAS_GH_USER}/${repoName}/commits/${source_latest_commit}/statuses";, returnStdout: true).trim()
             commit_status = new JsonSlurper().parseText(commit_status_out)
-            def tested = false
-            if (commit_status.size() > 0) {
-                tested = true
-            }
-
+            def tested = (commit_status.size() > 0)
             // add this commit to be tested
             if (correct_repo && !wip && !tested) {
                 job["updated"] = pr_updated
@@ -85,6 +82,7 @@ pipeline {
                     repo_name = split_repo_url[split_repo_url.length-1]
                     user_name = split_repo_url[split_repo_url.length-2]
                     output = sh(script: "curl -s https://api.github.com/repos/${user_name}/${repo_name}/pulls?state=open";, returnStdout: true).trim()
+                    env._gh_repo_name = repo_name
                     env._pr_to_review = output
                 }
             }
@@ -93,7 +91,7 @@ pipeline {
             when { not { environment name: "_pr_to_review", value: "[]" }}
             steps {
                 script {
-                    jobs = parseJobs(env._pr_to_review)
+                    jobs = parseJobs(env._gh_repo_name, env._pr_to_review)
                     if (jobs) {
                         // Only parse one job for now
                         makeBuild(jobs[0])
@@ -102,4 +100,4 @@ pipeline {
             }
         }
     }
-}
\ No newline at end of file
+}