From 80f3b8bedb9ff6bd51125b207185f38d3d3b0855 Mon Sep 17 00:00:00 2001 From: Rustam Tagaev Date: Thu, 27 Feb 2025 16:58:45 +0300 Subject: [PATCH] [DO-1650] move some classes to packages (!66) Co-authored-by: Rustam Tagaev Reviewed-on: https://git.avroid.tech/DevOps/jenkins-shared-lib/pulls/66 Reviewed-by: Vasiliy Chipizhin Reviewed-by: Denis Patrakeev Co-authored-by: Rustam Tagaev Co-committed-by: Rustam Tagaev --- src/tech/avroid/api/Artifactory.groovy | 1 + src/tech/avroid/api/Eva.groovy | 1 + src/tech/avroid/api/Gitea.groovy | 1 + src/tech/avroid/api/NextCloud.groovy | 1 + src/tech/avroid/api/Nexus.groovy | 1 + .../avroid/artifactory/Artifactory.groovy | 121 ++++++++++++ src/tech/avroid/eva/Eva.groovy | 96 +++++++++ src/tech/avroid/gitea/Gitea.groovy | 113 +++++++++++ src/tech/avroid/nextcloud/NextCloud.groovy | 152 ++++++++++++++ src/tech/avroid/nexus/Nexus.groovy | 186 ++++++++++++++++++ 10 files changed, 673 insertions(+) create mode 100644 src/tech/avroid/artifactory/Artifactory.groovy create mode 100644 src/tech/avroid/eva/Eva.groovy create mode 100644 src/tech/avroid/gitea/Gitea.groovy create mode 100644 src/tech/avroid/nextcloud/NextCloud.groovy create mode 100644 src/tech/avroid/nexus/Nexus.groovy diff --git a/src/tech/avroid/api/Artifactory.groovy b/src/tech/avroid/api/Artifactory.groovy index ee2647f..d4459e0 100644 --- a/src/tech/avroid/api/Artifactory.groovy +++ b/src/tech/avroid/api/Artifactory.groovy @@ -11,6 +11,7 @@ import groovy.json.JsonSlurper * Need plugin Jenkins:
* HTTP Request */ +@Deprecated class Artifactory implements Serializable { // See https://www.baeldung.com/java-serial-version-uid diff --git a/src/tech/avroid/api/Eva.groovy b/src/tech/avroid/api/Eva.groovy index 9fbaa23..3f9acb1 100644 --- a/src/tech/avroid/api/Eva.groovy +++ b/src/tech/avroid/api/Eva.groovy @@ -6,6 +6,7 @@ import groovy.json.JsonSlurper /** * Class for work with API Eva */ +@Deprecated class Eva implements Serializable { private Script script private String host diff --git a/src/tech/avroid/api/Gitea.groovy b/src/tech/avroid/api/Gitea.groovy index 5dd9546..14b196d 100644 --- a/src/tech/avroid/api/Gitea.groovy +++ b/src/tech/avroid/api/Gitea.groovy @@ -5,6 +5,7 @@ import groovy.json.JsonOutput /** * Class for work with API gitea */ +@Deprecated class Gitea implements Serializable { private Script script private String projectURL diff --git a/src/tech/avroid/api/NextCloud.groovy b/src/tech/avroid/api/NextCloud.groovy index 8bcc28a..0222818 100644 --- a/src/tech/avroid/api/NextCloud.groovy +++ b/src/tech/avroid/api/NextCloud.groovy @@ -1,5 +1,6 @@ package tech.avroid.api +@Deprecated class NextCloud implements Serializable { private script diff --git a/src/tech/avroid/api/Nexus.groovy b/src/tech/avroid/api/Nexus.groovy index 77017d8..cddcf1c 100644 --- a/src/tech/avroid/api/Nexus.groovy +++ b/src/tech/avroid/api/Nexus.groovy @@ -1,5 +1,6 @@ package tech.avroid.api +@Deprecated class Nexus implements Serializable { private script diff --git a/src/tech/avroid/artifactory/Artifactory.groovy b/src/tech/avroid/artifactory/Artifactory.groovy new file mode 100644 index 0000000..d0e680a --- /dev/null +++ b/src/tech/avroid/artifactory/Artifactory.groovy @@ -0,0 +1,121 @@ +package tech.avroid.artifactory + +import groovy.json.JsonSlurper + +/** + * Work with REST API Artifactory
+ * Official example:
+ * + * aqlCleanup.groovy + *
+ * Need plugin Jenkins:
+ * HTTP Request + */ +class Artifactory implements Serializable { + + // See https://www.baeldung.com/java-serial-version-uid + private static final long serialVersionUID = 1L + + private Script script + private String urlArtifactory + private String credentials + + /** + @param script Script - context pointer on step in Pipelines + @param host String - URL JFrog Artifactory server + @param credentials String - id Jenkins credentials with user's name and pass for Artifactory + */ + Artifactory(Script script, String urlArtifactory, String credentials) { + this.script = script + this.urlArtifactory = urlArtifactory + this.credentials = credentials + } + + /** + * Request type Artifactory Query Language (AQL) to REST API + * @param request String - AQL-request + * @return def - body response + * @see + + + + + + + + + + /files/${dirPath} + infinity + + + + + + + + + ' > file.xml + cat file.xml + """).trim() + def path = /(.*?)<\/d:href>/ + return (xml =~ path).collect { it[1] } + } + } + + /** + * Info about artifact in NextCloud. + * @param artifactPath String - path of artifact in Nexus + * @return Map with information about package + */ + public Map fileInfo(String filePath) { + String xml + Map fileInfo = [:] + def pattern + List values = [] + + this.script.withCredentials([this.script.usernamePassword( + credentialsId: this.credentials, + usernameVariable: 'username', + passwordVariable: 'password')]) { + List parts = filePath.tokenize('/') + String fileName = parts[-1] + String path = parts[0..-2].join('/') + + xml = script.sh(returnStdout: true, script: """ + curl "${this.host}/remote.php/dav/" \ + --user "${script.username}":"${script.password}" \ + -X SEARCH -H "content-Type: text/xml" \ + --data ' + + + + + + + + + + + + /files/${path} + infinity + + + + + + + + ${fileName} + + + + ' > file.xml + cat file.xml + """).trim() + + fileInfo.put("filePath", filePath) + fileInfo.put("displayName", fileName) + pattern = /(.*?)(.*?) + if (!index.assets[0].downloadUrl.contains(".html")) { + urls.add(index.assets[0].downloadUrl) + } + } + + int request = 0; + while (nextPage != 'null' && request < 2000) { + request++; + res = script.httpRequest( + url: "${host}/service/rest/v1/components?" + + "continuationToken=${nextPage}&repository=${repository}", + httpMode: 'GET', + quiet: true, + authentication: credentials + ) + + dataJSON = script.readJSON text: res.content + nextPage = dataJSON.continuationToken + + dataJSON.items.each { index -> + if (!index.assets[0].downloadUrl.contains(".html")) { + newUrl = index.assets[0].downloadUrl + urls.add("$newUrl") + } + } + } + return urls + } + + + /** + * Download artifact from Nexus repo + * + @param url String - URL, откуда будет скачан файл + @param outputFile String - Путь с именем файла, по которому будет сохранен(optional). + * По умолчанию с именем сохраняется в текущей директории с именем файла + @return String - Возвращает имя загруженного файла + */ + String download(String url, String outputFile = '') { + String artifactName = url.split('/').last() + String artifact = outputFile ?: "${this.script.pwd()}/${artifactName}" + + this.script.httpRequest( + url: url, + authentication: this.credentials, + httpMode: "GET", + outputFile: artifact, + contentType: 'APPLICATION_JSON', + acceptType: 'APPLICATION_JSON', + responseHandle: 'NONE' + ) + + return artifact + } + + /** + * Method get artifact's info from nexus + @param repository String - Repository name in Nexus. + @param name String - Artifact path in Nexus. + @return Object - artifacts's information + */ + public Object fileInfo(String repository, String name) { + Object res = script.httpRequest( + url: "${host}/service/rest/v1/components?repository=${repository}&name=${name}", + httpMode: 'GET', + quiet: true, + authentication: credentials + ) + + Object dataJSON = script.readJSON text: res.content + return dataJSON.items[0].assets[0] + } + + /** + * Функция загружает артефакт в Pypi-репозиторий Nexus + @param repository String - Имя репозитория в Nexus. + @param artifact String - Путь до артефакта. + @param artifactName String - Имя артефакта которое будет отображаться в Nexus. (optional) + */ + public void uploadPypiArtifact(Map args = [:]) { + String artifactName = args.artifactName ?: args.artifact.split('/').last() + + script.httpRequest( + url: "${host}/service/rest/v1/components?repository=${args.repository}", + httpMode: 'POST', + quiet: false, + formData: [[contentType: 'APPLICATION_FORM_DATA', + name: 'pypi.asset', + fileName: artifactName, + uploadFile: args.artifact]], + authentication: credentials + ) + } + + /** + * Функция загружает артефакт в Npm-репозиторий Nexus + @param repository String - Имя репозитория в Nexus. + @param artifact String - Путь до артефакта. + @param artifactName String - Имя артефакта которое будет отображаться в Nexus. (optional) + */ + public void uploadNpmArtifact(Map args = [:]) { + String artifactName = args.artifactName ?: args.artifact.split('/').last() + + script.httpRequest( + url: "${host}/service/rest/v1/components?repository=${args.repository}", + httpMode: 'POST', + quiet: false, + formData: [[contentType: 'APPLICATION_FORM_DATA', + name: 'npm.asset', + fileName: artifactName, + uploadFile: args.artifact]], + authentication: credentials + ) + } + +}