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 = /(.*?)
+ values = (xml =~ pattern).collect { it[1] }
+ fileInfo.put("contentType", values[0])
+ pattern = /(.*?)
+ values = (xml =~ pattern).collect { it[1] }
+ fileInfo.put("lastModified", values[0])
+ }
+
+ return fileInfo
+ }
+
+ /**
+ * Download artifact from NextCloud
+ * @param filePath String - path of artifact in NextCloud
+ * @param path String - local path of artifact
+ * @return String - local path of file
+ */
+ public String download(String filePath, String path = "") {
+ if (path == "") {
+ path = filePath.split('/').last()
+ }
+ this.script.withCredentials([this.script.usernamePassword(
+ credentialsId: this.credentials,
+ usernameVariable: 'username',
+ passwordVariable: 'password')]) {
+ script.sh """
+ curl -u "${script.username}":"${script.password}" -o "$path" "$host/$filePath"
+ """
+ }
+
+ return path
+ }
+}
diff --git a/src/tech/avroid/api/Nexus.groovy b/src/tech/avroid/api/Nexus.groovy
index 54892ee..8bf9c5b 100644
--- a/src/tech/avroid/api/Nexus.groovy
+++ b/src/tech/avroid/api/Nexus.groovy
@@ -2,15 +2,21 @@ package tech.avroid.api
class Nexus implements Serializable {
- private script
- private host
- private credentials
+ 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
+ */
Nexus(Script script, String host, String credentials) {
- this.script = script
- this.host = host
- this.credentials = credentials
- }
+ this.script = script
+ this.host = host
+ this.credentials = credentials
+ }
/**
* Upload artifact in Nexus.
@@ -22,7 +28,8 @@ class Nexus implements Serializable {
@param repository String - Name of Nexus repository
@param path String - Path in Nexus repository
@param artifactName String - Name of artifact in Nexus (optional). Common calculate from artifactPath
- @param String type type of Repository
+ @param type String - type of repository
+ @return String - artifact url in Nexus
*/
public String upload(Map args = [:], String type = "raw") {
String artifactName = args.artifactName ?: args.artifactPath.split('/').last()
@@ -38,4 +45,72 @@ class Nexus implements Serializable {
)
return artifactUrl
}
+
+ /**
+ * Method get artifacts' urls from nexus repository
+ @param repository String - Repository name in Nexus.
+ @param type String - type of repository
+ @return List - list of artifacts' urls
+ */
+ public List search(String repository, String type = "raw") {
+ String newUrl = ""
+
+ Object res = script.httpRequest(
+ url: "${host}/service/rest/v1/components?repository=${repository}",
+ httpMode: 'GET',
+ quiet: true,
+ authentication: credentials
+ )
+
+ Object dataJSON = script.readJSON text: res.content
+ String nextPage = dataJSON.continuationToken
+ List urls = []
+
+ dataJSON.items.each { index ->
+ 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]
+ }
}