[DO-963] Add class for JFrog Artifactory (#42)

DO-963

Co-authored-by: denis.patrakeev <denis.patrakeev@avroid.tech>
Reviewed-on: https://git.avroid.tech/DevOps/jenkins-shared-lib/pulls/42
This commit is contained in:
Denis Patrakeev
2024-11-08 18:38:20 +03:00
parent 7afce88c2f
commit 494aeb22b0
2 changed files with 132 additions and 0 deletions

11
.gitignore vendored Normal file
View File

@@ -0,0 +1,11 @@
.idea
.vscode
__pycache__
*~
**/.DS_Store
*.log
._*

View File

@@ -0,0 +1,121 @@
package tech.avroid.api
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
}
}