【问题标题】:Is there a way to import import AQL file externally in artifactory cleanup groovy script有没有办法在工件清理 groovy 脚本中从外部导入导入 AQL 文件
【发布时间】:2017-01-18 17:20:23
【问题描述】:

我正在使用下面的脚本根据 AQL 查询的某些条件删除 Artifactory 中的一些工件。我是 groovy 脚本的新手,但我想知道是否可以从外部文件导入 AQL 查询,因此我们不必每次都编辑此脚本以更改 AQL 查询。以下查询当前按预期工作,但如果从外部文件调用 AQL,将不胜感激。

提前致谢

import groovyx.net.http.RESTClient
import groovyx.net.http.HttpResponseException
import org.apache.http.conn.HttpHostConnectException


 def query = '**items.find({"repo": "xxx-repo-for-poc","created_by" :  "xxx","name" :{"$match": "*.nupkg"})**' // replace this with your AQL query
    def artifactoryURL = 'http://Servername:8081/artifactory/' // replace this with Artifactory server
    def restClient = new RESTClient(artifactoryURL)
    restClient.setHeaders(['Authorization': 'Basic ' + "admin:password".getBytes('iso-8859-1').encodeBase64()]) //replace the 'admin:password' with your own credentials
    def dryRun = true //set the value to false if you want the script to actually delete the artifacts

    def itemsToDelete = this.&getAqlQueryResult(restClient, query)
    if (itemsToDelete != null && itemsToDelete.size() > 0) {
         this.&delete(restClient, itemsToDelete, dryRun)
    } else {
        println('NOTHING TO DELETE')
    }

    /**
     * Send the AQL to Artifactory and collect the response.
     */
    public List getAqlQueryResult(RESTClient restClient, String query) {
        def response
        try {
            response = restClient.post(path: 'api/search/aql',
                    body: query,
                    requestContentType: 'text/plain'
            )
        } catch (Exception e) {
            println(e.message)
        }
        if (response != null && response.getData()) {
            def results = [];
            response.getData().results.each {
                results.add(constructPath(it))
            }
            return results;
        } else return null
    }

    /**
     * Construct the full path form the returned items.
     * 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
     */
    public constructPath(groovy.json.internal.LazyMap item) {
        if (item.path.toString().equals(".")) {
            return item.repo + "/" + item.name
        }
        return item.repo + "/" + item.path + "/" + item.name
    }

    /**
     * Send DELETE request to Artifactory for each one of the returned items
     */
    public delete(RESTClient restClient, List itemsToDelete, def dryRun) {
        dryMessage = (dryRun) ? "*** This is a dry run ***" : "";
        itemsToDelete.each {
            println("Trying to delete artifact: '$it'. $dryMessage")
            try {
                if (!dryRun) {
                    restClient.delete(path: it)
                }
                println("Artifact '$it' has been successfully deleted. $dryMessage")
            } catch (HttpResponseException e) {
                println("Cannot delete artifact '$it': $e.message" +
                        ", $e.statusCode")
            } catch (HttpHostConnectException e) {
                println("Cannot delete artifact '$it': $e.message")
            }
        }
    }

【问题讨论】:

    标签: groovy artifactory artifactory-query-lang


    【解决方案1】:

    searches api 公开了aql 方法,使您能够执行AQL 查询并根据您认为合适的结果采取行动。

    您还应该检查用户插件documentation,它提供了有用的信息和使用示例。

    【讨论】:

      猜你喜欢
      • 2020-07-08
      • 1970-01-01
      • 2019-03-26
      • 1970-01-01
      • 2012-01-23
      • 2021-10-23
      • 2017-03-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多