DO-95/omp_from_nextCloud_to_nexus (#7)

+ Add new Class NextCloud
* Update class Nexus
Co-authored-by: aleksandr.vodyanov <aleksandr.vodyanov@avroid.tech>
Co-committed-by: aleksandr.vodyanov <aleksandr.vodyanov@avroid.tech>
This commit is contained in:
aleksandr.vodyanov
2023-11-02 13:41:39 +03:00
committed by Aleksandr Vodyanov
parent 12bca2e713
commit 23228632a8
2 changed files with 235 additions and 8 deletions

View File

@@ -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 '<?xml version="1.0" encoding="UTF-8"?>
<d:searchrequest xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns">
<d:basicsearch>
<d:select>
<d:prop>
<d:displayname/>
</d:prop>
</d:select>
<d:from>
<d:scope>
<d:href>/files/${dirPath}</d:href>
<d:depth>infinity</d:depth>
</d:scope>
</d:from>
<d:where>
<d:not>
<d:is-collection/>
</d:not>
</d:where>
</d:basicsearch>
</d:searchrequest>' > file.xml
cat file.xml
""").trim()
def path = /<d:href>(.*?)<\/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 '<?xml version="1.0" encoding="UTF-8"?>
<d:searchrequest xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns">
<d:basicsearch>
<d:select>
<d:prop>
<d:displayname/>
<d:getcontenttype/>
<d:getlastmodified/>
</d:prop>
</d:select>
<d:from>
<d:scope>
<d:href>/files/${path}</d:href>
<d:depth>infinity</d:depth>
</d:scope>
</d:from>
<d:where>
<d:eq>
<d:prop>
<d:displayname/>
</d:prop>
<d:literal>${fileName}</d:literal>
</d:eq>
</d:where>
</d:basicsearch>
</d:searchrequest>' > file.xml
cat file.xml
""").trim()
fileInfo.put("filePath", filePath)
fileInfo.put("displayName", fileName)
pattern = /<d:getcontenttype>(.*?)</
values = (xml =~ pattern).collect { it[1] }
fileInfo.put("contentType", values[0])
pattern = /<d:getlastmodified>(.*?)</
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
}
}

View File

@@ -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]
}
}