#35 Move functions for controller and pagure into library
Merged 5 years ago by mikeb. Opened 5 years ago by mkovarik.
mkovarik/c3i-library pagure-improvement  into  master

@@ -0,0 +1,52 @@ 

+ 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 getVars() {

+   return httpGet("/vars", true)

+ }

+ 

+ def getKrb5Vars(String principal) {

+   vars = getVars()

+   password = httpGet("/krb5/principal/${principal}")

+   krb_vars = [

+     principal: principal,

+     password: password,

+     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 krb_vars

+ }

file modified
+45
@@ -2,3 +2,48 @@ 

    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.TRIGGER_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

+ }

no initial comment

Could we add a method to get all the vars and return them as a map?

It would be more efficient to make one request to get all the vars, and then retrieve the vars you care about from the result, rather than making a bunch of separate requests.

rebased onto 4bec307

5 years ago

Looks great, thanks!

Pull-Request has been merged by mikeb

5 years ago
Metadata