From c73f3f4e419fe77e9ea83f3a2dd3d8b1cfdbabc4 Mon Sep 17 00:00:00 2001 From: Mike Bonnet Date: Feb 28 2019 19:20:33 +0000 Subject: add client and helper methods for interacting with https://pagure.io The PagureClient class allows retrieving information about a PR, updating the status of a PR, and adding comments. The class can be instantiated directly, or used via the helper methods, which grab parameters from environment variables. This code was taken with minimal modification from the PagureClient written by Yuxiang Zhu for the WaiverDB project. --- diff --git a/src/com/redhat/c3i/util/PagureClient.groovy b/src/com/redhat/c3i/util/PagureClient.groovy new file mode 100644 index 0000000..5da6328 --- /dev/null +++ b/src/com/redhat/c3i/util/PagureClient.groovy @@ -0,0 +1,66 @@ +// A class to handle interaction with Pagure. +// Taken from the original PagureClient written by +// Yuxiang Zhu for WaiverDB. + +package com.redhat.c3i.util + +import java.net.URLEncoder + +class PagureClient implements Serializable { + String apiUrl + String token + String repo + boolean isFork + def steps + + def callApi(String httpMode, String apiPath, Map payload = null) { + def headers = [] + if (token) { + headers << [name: "Authorization", value: "token ${token}", maskValue: true] + } + def payloadItems = [] + if (payload) { + for (it in payload) { + if (it == null || it.value == null) { + continue + } + payloadItems << (URLEncoder.encode(it.key.toString(), "utf-8") + + "=" + URLEncoder.encode(it.value.toString(), "utf-8")) + } + } + return steps.httpRequest( + httpMode: httpMode, + url: "${apiUrl}/${apiPath}", + acceptType: "APPLICATION_JSON", + contentType: "APPLICATION_FORM", + requestBody: payloadItems.join("&"), + customHeaders: headers, + ) + } + + def getPR(pr) { + def apiPath = "${isFork ? "fork/" : ""}${repo}/pull-request/${pr}" + def response = callApi("GET", apiPath) + return steps.readJSON(text: response.content) + } + + def updatePRStatus(Map args) { + def apiPath = "${isFork ? "fork/" : ""}${repo}/pull-request/${args.pr}/flag" + def response = callApi("POST", apiPath, [ + "username": args.username, + "uid": args.uid, + "percent": args.percent, + "comment": args.comment, + "url": args.url, + ]) + return steps.readJSON(text: response.content) + } + + def commentOnPR(Map args) { + def apiPath = "${isFork ? "fork/" : ""}${repo}/pull-request/${args.pr}/comment" + def response = callApi("POST", apiPath, [ + "comment": args.comment, + ]) + return steps.readJSON(text: response.content) + } +} diff --git a/vars/pagure.groovy b/vars/pagure.groovy new file mode 100644 index 0000000..36518b4 --- /dev/null +++ b/vars/pagure.groovy @@ -0,0 +1,4 @@ +def client(Map args) { + args.steps = steps + return new com.redhat.c3i.util.PagureClient(args) +}