From 4bec307eb7b152395ec887d7be5b8879f7c75eaf Mon Sep 17 00:00:00 2001
From: Michal Kovarik <mkovarik@redhat.com>
Date: Dec 04 2019 07:13:06 +0000
Subject: [PATCH 1/3] Move pagure handlers to lib


---

diff --git a/vars/pagure.groovy b/vars/pagure.groovy
index 36518b4..facd729 100644
--- a/vars/pagure.groovy
+++ b/vars/pagure.groovy
@@ -2,3 +2,48 @@ def client(Map args) {
   args.steps = steps
   return new com.redhat.c3i.util.PagureClient(args)
 }
+
+def withPagure(args=[:], cl) {
+  args.apiUrl = env.PAGURE_API
+  args.repo = env.PAGURE_REPO_NAME
+  args.isFork = env.PAGURE_REPO_IS_FORK == 'true'
+  def pagureClient = client(args)
+  return cl(pagureClient)
+}
+
+def withPagureCreds(args=[:], cl) {
+  def pagureClient = null
+  withCredentials([string(credentialsId: "${env.PIPELINE_NAMESPACE}-${env.PAGURE_API_KEY_SECRET_NAME}", variable: 'TOKEN')]) {
+    args.token = env.TOKEN
+    pagureClient = withPagure(args, cl)
+  }
+  return pagureClient
+}
+
+def setBuildStatusOnPR(percent, String comment) {
+  withPagureCreds {
+    it.updatePRStatus(username: 'c3i-jenkins', uid: "ci-pre-merge-${env.GIT_COMMIT.take(8)}",
+      url: env.BUILD_URL, percent: percent, comment: comment, pr: env.PR_NO)
+  }
+}
+
+def flagCommit(status, percent, comment) {
+  withPagureCreds {
+    it.flagCommit(username: 'c3i-jenkins', uid: "ci-post-merge-${env.GIT_COMMIT.take(8)}", status: status,
+      url: env.BUILD_URL, percent: percent, comment: comment, commit: env.GIT_COMMIT)
+  }
+}
+
+def commentOnPR(String comment, String pr) {
+  withPagureCreds {
+    it.commentOnPR(comment: comment, pr: pr)
+  }
+}
+
+def getPR(String pr) {
+  def prInfo
+  withPagure {
+    prInfo = it.getPR(pr)
+  }
+  return prInfo
+}

From 61d3779b5ec82dc18265d97347e80ec6d2221f0a Mon Sep 17 00:00:00 2001
From: Michal Kovarik <mkovarik@redhat.com>
Date: Dec 04 2019 07:13:06 +0000
Subject: [PATCH 2/3] Add pipeline handlers


---

diff --git a/vars/controller.groovy b/vars/controller.groovy
new file mode 100644
index 0000000..30c400e
--- /dev/null
+++ b/vars/controller.groovy
@@ -0,0 +1,47 @@
+def httpGet(String path, Boolean json = false) {
+  def out = sh script: "curl -k https://${env.PIPELINE_ID}.${env.PAAS_DOMAIN}${path}", returnStdout: true
+  if (json) {
+    return readJSON(text: out)
+  }
+  return out
+}
+
+def httpPost(String path, String data, Boolean json = false) {
+  def out = sh script: "curl -k -X POST -H 'Content-Type: application/json' -d '${data}' https://${env.PIPELINE_ID}.${env.PAAS_DOMAIN}${path}", returnStdout: true
+  if (json) {
+    return readJSON(text: out)
+  }
+  return out
+}
+
+def createCert(String server, String... sans) {
+  def sanslist = []
+  for (san in sans) {
+    sanslist.add("\"${san}\"")
+  }
+  return httpPost("/ca/${server}",
+              "{\"sans\": [${sanslist.join(',')}]}",
+              true)
+}
+
+def getVar(String key) {
+  return httpGet("/vars/${key}")
+}
+
+def setVar(String key, String value) {
+  return httpPost("/vars/${key}", "{\"value\": \"${value}\"}" )
+}
+
+def getKrb5Vars(String principal) {
+  password = httpGet("/krb5/principal/${principal}")
+  vars = [
+    principal: principal,
+    password: password,
+    realm: getVar("KRB5_REALM"),
+    domain: getVar("KRB5_DOMAIN"),
+    kdc_host: getVar("KRB5_KDC_HOST")+":"+getVar("KRB5_KDC_PORT"),
+    admin_host: getVar("KRB5_ADMIN_HOST")+":"+getVar("KRB5_ADMIN_PORT"),
+    kpasswd_host: getVar("KRB5_KPASSWD_HOST")+":"+getVar("KRB5_KPASSWD_PORT")
+  ]
+  return vars
+}
diff --git a/vars/pagure.groovy b/vars/pagure.groovy
index facd729..953b3a3 100644
--- a/vars/pagure.groovy
+++ b/vars/pagure.groovy
@@ -13,7 +13,7 @@ def withPagure(args=[:], cl) {
 
 def withPagureCreds(args=[:], cl) {
   def pagureClient = null
-  withCredentials([string(credentialsId: "${env.PIPELINE_NAMESPACE}-${env.PAGURE_API_KEY_SECRET_NAME}", variable: 'TOKEN')]) {
+  withCredentials([string(credentialsId: "${env.TRIGGER_NAMESPACE}-${env.PAGURE_API_KEY_SECRET_NAME}", variable: 'TOKEN')]) {
     args.token = env.TOKEN
     pagureClient = withPagure(args, cl)
   }

From 30fe25eda5664a77c4f26727065404c91dc21643 Mon Sep 17 00:00:00 2001
From: Michal Kovarik <mkovarik@redhat.com>
Date: Dec 04 2019 07:13:06 +0000
Subject: [PATCH 3/3] Controller: add getVars


---

diff --git a/vars/controller.groovy b/vars/controller.groovy
index 30c400e..e2a7a77 100644
--- a/vars/controller.groovy
+++ b/vars/controller.groovy
@@ -32,16 +32,21 @@ def setVar(String key, String value) {
   return httpPost("/vars/${key}", "{\"value\": \"${value}\"}" )
 }
 
+def getVars() {
+  return httpGet("/vars", true)
+}
+
 def getKrb5Vars(String principal) {
+  vars = getVars()
   password = httpGet("/krb5/principal/${principal}")
-  vars = [
+  krb_vars = [
     principal: principal,
     password: password,
-    realm: getVar("KRB5_REALM"),
-    domain: getVar("KRB5_DOMAIN"),
-    kdc_host: getVar("KRB5_KDC_HOST")+":"+getVar("KRB5_KDC_PORT"),
-    admin_host: getVar("KRB5_ADMIN_HOST")+":"+getVar("KRB5_ADMIN_PORT"),
-    kpasswd_host: getVar("KRB5_KPASSWD_HOST")+":"+getVar("KRB5_KPASSWD_PORT")
+    realm: vars.KRB5_REALM,
+    domain: vars.KRB5_DOMAIN,
+    kdc_host: "${vars.KRB5_KDC_HOST}:${vars.KRB5_KDC_PORT}",
+    admin_host: "${vars.KRB5_ADMIN_HOST}:${vars.KRB5_ADMIN_PORT}",
+    kpasswd_host: "${vars.KRB5_KPASSWD_HOST}:${vars.KRB5_KPASSWD_PORT}"
   ]
-  return vars
+  return krb_vars
 }

