From 23228632a8c2418b736b3cfa93b372443c99af94 Mon Sep 17 00:00:00 2001 From: "aleksandr.vodyanov" Date: Thu, 2 Nov 2023 13:41:39 +0300 Subject: [PATCH] DO-95/omp_from_nextCloud_to_nexus (#7) + Add new Class NextCloud * Update class Nexus Co-authored-by: aleksandr.vodyanov Co-committed-by: aleksandr.vodyanov --- src/tech/avroid/api/NextCloud.groovy | 152 +++++++++++++++++++++++++++ src/tech/avroid/api/Nexus.groovy | 91 ++++++++++++++-- 2 files changed, 235 insertions(+), 8 deletions(-) create mode 100644 src/tech/avroid/api/NextCloud.groovy diff --git a/src/tech/avroid/api/NextCloud.groovy b/src/tech/avroid/api/NextCloud.groovy new file mode 100644 index 0000000..8bcc28a --- /dev/null +++ b/src/tech/avroid/api/NextCloud.groovy @@ -0,0 +1,152 @@ +package tech.avroid.api + +class NextCloud implements Serializable { + + private script + private host + private credentials + + /** + * + @param script Script - context pointer on step in Pipelines + @param host String - Nexus host name + @param credentials String - User's name and pass in Nexus + */ + NextCloud(Script script, String host, String credentials) { + this.script = script + this.host = host + this.credentials = credentials + } + + /** + * Search artifacts in NextCloud. + * @param dirPath String - Directory path in which search file + * @return List of artifact with concrete pattern + */ + public List search(String dirPath) { + def parsedXml + String xml + + this.script.withCredentials([this.script.usernamePassword( + credentialsId: this.credentials, + usernameVariable: 'username', + passwordVariable: 'password')]) { + 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/${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 + } + + /** + * 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] + } }