[DO-1650] move some classes to packages (!66)

Co-authored-by: Rustam Tagaev <rustam.tagaev@avroid.tech>
Reviewed-on: https://git.avroid.tech/DevOps/jenkins-shared-lib/pulls/66
Reviewed-by: Vasiliy Chipizhin <vasiliy.chipizhin@avroid.team>
Reviewed-by: Denis Patrakeev <denis.patrakeev@avroid.team>
Co-authored-by: Rustam Tagaev <rustam.tagaev@avroid.team>
Co-committed-by: Rustam Tagaev <rustam.tagaev@avroid.team>
This commit is contained in:
Rustam Tagaev
2025-02-27 16:58:45 +03:00
committed by Denis Patrakeev
parent 00702c1d25
commit 80f3b8bedb
10 changed files with 673 additions and 0 deletions

View File

@@ -11,6 +11,7 @@ import groovy.json.JsonSlurper
* Need plugin Jenkins:<br>
* <a href="https://plugins.jenkins.io/http_request">HTTP Request</a>
*/
@Deprecated
class Artifactory implements Serializable {
// See https://www.baeldung.com/java-serial-version-uid

View File

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

View File

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

View File

@@ -1,5 +1,6 @@
package tech.avroid.api
@Deprecated
class NextCloud implements Serializable {
private script

View File

@@ -1,5 +1,6 @@
package tech.avroid.api
@Deprecated
class Nexus implements Serializable {
private script

View File

@@ -0,0 +1,121 @@
package tech.avroid.artifactory
import groovy.json.JsonSlurper
/**
* Work with REST API Artifactory<br>
* Official example:<br>
* <a href="https://github.com/jfrog/artifactory-scripts/blob/master/cleanup/aqlCleanup.groovy">
* aqlCleanup.groovy
* </a><br>
* Need plugin Jenkins:<br>
* <a href="https://plugins.jenkins.io/http_request">HTTP Request</a>
*/
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 <a href="https://www.jfrog.com/confluence/display/JFROG/Artifactory+REST+API</a>
*/
public def aqlRequest(String request) {
def response = this.script.httpRequest(
url: this.urlArtifactory + '/api/search/aql',
authentication: this.credentials,
httpMode: 'POST',
validResponseCodes: '200',
contentType: 'TEXT_PLAIN',
requestBody: request,
ignoreSslErrors: true
)
if (response.content != null && response.content != '') {
return response.content
} else {
return null
}
}
/**
* Request for remove item in Artifactory
* @param listItemsPaths List - list item`s path for remove, example: ['avroid_dev/_/zlib/1.2.13_dev']
* @param dryRun Boolean - dry run (default - true, don`t remove items)
*/
public void itemsDelete(List listItemsPaths, Boolean dryRun = true) {
if (dryRun) {
this.script.println('*** This is a dry run ***')
}
this.script.println('Delete items in Artifactory:')
listItemsPaths.each { itemPath ->
if (dryRun) {
this.script.println(this.urlArtifactory + '/' + itemPath)
} else {
this.script.httpRequest(
url: this.urlArtifactory + '/' + itemPath,
authentication: this.credentials,
httpMode: 'DELETE',
validResponseCodes: '200,202,204',
ignoreSslErrors: true,
responseHandle: 'NONE'
)
}
}
this.script.println('Items in Artifactory has been successfully deleted')
if (dryRun) {
this.script.println('*** This is a dry run ***')
}
}
/**
* Reconstruction full path item`s from response by AQL-request
* If the path is '.' (file is on the root) we ignores it
* and construct the full path from the repo and the file name only
* @param aqlResponse Map - map with description item`s from response by AQL-request
* @return String - reconstruction full path
*/
public static String reconstructionPathItemFromAqlResponse(Map aqlResponse) {
if (aqlResponse.path.toString().equals('.')) {
return aqlResponse.repo + '/' + aqlResponse.name
}
return aqlResponse.repo + '/' + aqlResponse.path + '/' + aqlResponse.name
}
/**
* Convert output AQL-request to List
* @param aqlResponse String - body response by AQL-request
* @return List - body response converted to list
*/
public static List convertAqlResponseItemToList(String aqlResponse) {
List results = []
def jsonAqlResponse = new JsonSlurper().parseText(aqlResponse)
jsonAqlResponse.results.each { result ->
results.add(this.reconstructionPathItemFromAqlResponse(result))
}
return results
}
}

View File

@@ -0,0 +1,96 @@
package tech.avroid.eva
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
*/
public void 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)
Object result = new JsonSlurper().parseText(response).result
if (result) {
return result.id
}
return null
}
/**
* 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()
}
}
}

View File

@@ -0,0 +1,113 @@
package tech.avroid.gitea
import groovy.json.JsonOutput
/**
* Class for work with API gitea
*/
class Gitea implements Serializable {
private Script script
private String projectURL
private String creds
Gitea(Script script, String projectURL, String creds) {
this.projectURL = projectURL
this.creds = creds
this.script = script
}
//TODO: refactor with Gitea token
/**
* Create new branch from source branch
* @param srcRef String - source branch/tag/commit
* @param dstBranch String - new branch
* @return Boolean - success or failure create
*/
public Boolean createBranch(String srcRef, String dstBranch) {
Map body = [
new_branch_name: dstBranch,
old_ref_name: srcRef
]
Object res = script.httpRequest(
url: "${projectURL}/branches",
ignoreSslErrors: true,
httpMode: 'POST',
validResponseCodes: '201,409',
authentication: creds,
contentType: 'APPLICATION_JSON',
requestBody: JsonOutput.toJson(body)
)
Map result = [
'409': false,
'201': true
]
return result[res.status.toString()]
}
/**
* create new tag
* @param targetBranch String - target branch
* @param tagName String - tag name
* @param message String - message for tag
* @return Boolean - True success create False - already exists
*/
public Boolean createTag(String targetBranch, String tagName, String message = '') {
Map body = [
message: message,
tag_name: tagName,
target: targetBranch
]
Object bodyRequest = JsonOutput.toJson(body)
Object res = script.httpRequest(
url: "${projectURL}/tags",
ignoreSslErrors: true,
httpMode: 'POST',
validResponseCodes: '201,409',
authentication: creds,
contentType: 'APPLICATION_JSON',
requestBody: bodyRequest
)
/* groovylint-disable-next-line DuplicateMapLiteral */
Map result = [
'409': false,
'201': true
]
return result[res.status.toString()]
}
/**
* create comment in PR
* @param prIndex String - number of PR
* @param message String - comment message in PR
* @return Boolean - True success create
*/
public Boolean createPrComment(String prIndex, String message = '') {
Map body = [
body: message,
]
Object bodyRequest = JsonOutput.toJson(body)
Object res = script.httpRequest(
url: "${projectURL}/issues/${prIndex}/comments",
ignoreSslErrors: true,
httpMode: 'POST',
validResponseCodes: '201',
authentication: creds,
contentType: 'APPLICATION_JSON',
requestBody: bodyRequest
)
if (res.status == 201) {
return true
} else {
return false
}
}
}

View File

@@ -0,0 +1,152 @@
package tech.avroid.nextcloud
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

@@ -0,0 +1,186 @@
package tech.avroid.nexus
class Nexus 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
*/
Nexus(Script script, String host, String credentials) {
this.script = script
this.host = host
this.credentials = credentials
}
/**
* Upload artifact in Nexus.
* When call method need either artifactUrl or repository, and path
* either
*
@param artifactPath String - Local path to artifact
@param artifactUrl String - URL (optional)
@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 type String - type of repository
@return String - artifact url in Nexus
*/
@Deprecated
public String upload(Map args = [:], String type = "raw") {
String artifactName = args.artifactName ?: args.artifactPath.split('/').last()
String artifactUrl = args.artifactUrl ?: "${host}/repository/${args.repository}/${args.path}/${artifactName}"
script.httpRequest(
url: artifactUrl,
authentication: credentials,
httpMode: "PUT",
uploadFile: "${args.artifactPath}",
validResponseCodes: "201",
wrapAsMultipart: false
)
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
}
/**
* 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
)
}
}