From 86740d0fb50dd9294a9eb0171a4a4f49ebeccf13 Mon Sep 17 00:00:00 2001 From: "aleksandr.vodyanov" Date: Fri, 29 Dec 2023 11:09:23 +0300 Subject: [PATCH] [DO-103] eva_api_class (#9) [DO-103] Created class for work with Eva API Added method that to create link in eva objects Added method for getting taskId Reviewed-on: https://git.avroid.tech/DevOps/jenkins-shared-lib/pulls/9 Co-authored-by: aleksandr.vodyanov Co-committed-by: aleksandr.vodyanov --- src/tech/avroid/api/Eva.groovy | 91 ++++++++++++++++++++++++++++++++ src/tech/avroid/api/Gitea.groovy | 3 +- 2 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 src/tech/avroid/api/Eva.groovy diff --git a/src/tech/avroid/api/Eva.groovy b/src/tech/avroid/api/Eva.groovy new file mode 100644 index 0000000..7654b47 --- /dev/null +++ b/src/tech/avroid/api/Eva.groovy @@ -0,0 +1,91 @@ +import groovy.json.JsonOutput +import groovy.json.JsonSlurper + +/** + * Class for work with API Eva + */ +class Eva implements Serializable { + private Script script + private String host + private String creds + + /** + * Constructor + * @param script Script - script context + * @param host String - url of Eva service + * @param creds String - jenkins credentials with eva token for API work + */ + Eva(Script script, String host, String creds) { + this.script = script + this.host = host + this.creds = creds + } + + /** + * Create new link for Eva Object. For example, tasks + * !WARNING: can to create multiple similar links + * @param parentId String - id of Object for which created link + * @param linkName String - name of link + * @param url String - full url of link + * @return Boolean - success or failure create + */ + public createLink(String parentId, String linkName, String url) { + Map body = [ + jsonrpc: "1.2", + method: "CmfLink.create", + kwargs: [ + parent: parentId, + name: linkName, + url: url + ]] + + script.withCredentials([script.string(credentialsId: creds, variable: 'token')]) { + script.httpRequest( + url: "$host/api/", + contentType: 'APPLICATION_JSON', + httpMode: 'POST', + customHeaders: [[name: 'Authorization', value: "Bearer $script.token"]], + validResponseCodes: '200', + requestBody: JsonOutput.toJson(body) + ) + } + } + + /** + * Getting Eva task id + * @param filterValue String - value for search task + * @param filterParam String - type of filter for search task. Default value - "code" + * @return String - id of task + */ + public String getTaskId(String filterValue, String filterParam = "code") { + String response = getTask(filterValue, filterParam) + + return new JsonSlurper().parseText(response).result.id + } + + /** + * Getting Eva task information + * @param filterValue String - value for search task + * @param filterParam String - type of filter for search task. Default value - "code" + * @return String - id of task + */ + private String getTask(String filterValue, String filterParam = "code") { + Map body = [ + jsonrpc: "1.2", + method: "CmfTask.get", + kwargs: [ + filter: [filterParam, "==", filterValue], + ]] + + script.withCredentials([script.string(credentialsId: creds, variable: 'token')]) { + return script.httpRequest( + url: "$host/api/", + contentType: 'APPLICATION_JSON', + httpMode: 'POST', + customHeaders: [[name: 'Authorization', value: "Bearer $script.token"]], + validResponseCodes: '200', + requestBody: JsonOutput.toJson(body) + ).content.toString() + } + } +} diff --git a/src/tech/avroid/api/Gitea.groovy b/src/tech/avroid/api/Gitea.groovy index 4a9911e..8f402ec 100644 --- a/src/tech/avroid/api/Gitea.groovy +++ b/src/tech/avroid/api/Gitea.groovy @@ -4,7 +4,6 @@ import groovy.json.JsonOutput * Class for work with API gitea */ class Gitea implements Serializable { - private Script script private String projectURL private String creds @@ -14,7 +13,7 @@ class Gitea implements Serializable { this.creds = creds this.script = script } - + //TODO: refactor with Gitea token /** * Create new branch from source branch