Artifactory API - 列出包版本的所有工件

Artifactory API - List all artifacts for a package version

通过 Artifactory API 列出给定包的所有资产的最佳方式是什么?

我正在尝试编写一个脚本来获取包的资产,我希望它能与多种存储库类型一起使用,例如 Maven 和 PyPI。我知道我可以使用文件夹信息 API 来获取我需要的内容,但这依赖于存储库布局,因此它不能跨存储库类型工作。

我目前正在使用此 AQL 搜索:

curl -u user:password -X POST http://<artifactory_url>/artifactory/api/search/aql \
-H "Content-Type: text/plain" \
-d 'items.find({"repo": "libs-release-local"}, {"artifact.module.name": "com.foo.bar:fizz-buzz:1.2"})'

响应几乎是我想要的,但它似乎包括我正在搜​​索的不同版本的包中的一些资产:

{
"results" : [ {
  "repo" : "libs-release-local",
  "path" : "com/foo/bar/fizz-buzz/1.0",
  "name" : "fizz-buzz-1.0.properties",
  "type" : "file",
  "size" : 790,
  "created" : "2020-09-29T15:35:59.233Z",
  "created_by" : "user",
  "modified" : "2020-09-29T15:35:59.181Z",
  "modified_by" : "user",
  "updated" : "2020-09-29T15:35:59.233Z"
},{
  "repo" : "libs-release-local",
  "path" : "com/foo/bar/fizz-buzz/1.1",
  "name" : "fizz-buzz-1.1.properties",
  "type" : "file",
  "size" : 790,
  "created" : "2020-09-29T15:42:34.982Z",
  "created_by" : "user",
  "modified" : "2020-09-29T15:42:34.931Z",
  "modified_by" : "user",
  "updated" : "2020-09-29T15:42:34.983Z"
},{
  "repo" : "libs-release-local",
  "path" : "com/foo/bar/fizz-buzz/1.2",
  "name" : "fizz-buzz-1.2-javadoc.jar",
  "type" : "file",
  "size" : 391843,
  "created" : "2020-09-30T18:54:41.599Z",
  "created_by" : "user",
  "modified" : "2020-09-30T18:54:40.650Z",
  "modified_by" : "user",
  "updated" : "2020-09-30T18:54:41.600Z"
},{
  "repo" : "libs-release-local",
  "path" : "com/foo/bar/fizz-buzz/1.2",
  "name" : "fizz-buzz-1.2-sources.jar",
  "type" : "file",
  "size" : 1089,
  "created" : "2020-09-30T18:54:41.764Z",
  "created_by" : "user",
  "modified" : "2020-09-30T18:54:41.710Z",
  "modified_by" : "user",
  "updated" : "2020-09-30T18:54:41.765Z"
},{
  "repo" : "libs-release-local",
  "path" : "com/foo/bar/fizz-buzz/1.2",
  "name" : "fizz-buzz-1.2.jar",
  "type" : "file",
  "size" : 1410,
  "created" : "2020-09-30T18:54:41.902Z",
  "created_by" : "user",
  "modified" : "2020-09-30T18:54:41.844Z",
  "modified_by" : "user",
  "updated" : "2020-09-30T18:54:41.903Z"
},{
  "repo" : "libs-release-local",
  "path" : "com/foo/bar/fizz-buzz/1.2",
  "name" : "fizz-buzz-1.2.module",
  "type" : "file",
  "size" : 3481,
  "created" : "2020-09-30T18:54:42.015Z",
  "created_by" : "user",
  "modified" : "2020-09-30T18:54:41.962Z",
  "modified_by" : "user",
  "updated" : "2020-09-30T18:54:42.015Z"
},{
  "repo" : "libs-release-local",
  "path" : "com/foo/bar/fizz-buzz/1.2",
  "name" : "fizz-buzz-1.2.pom",
  "type" : "file",
  "size" : 781,
  "created" : "2020-09-30T18:54:42.238Z",
  "created_by" : "user",
  "modified" : "2020-09-30T18:54:42.190Z",
  "modified_by" : "user",
  "updated" : "2020-09-30T18:54:42.238Z"
},{
  "repo" : "libs-release-local",
  "path" : "com/foo/bar/fizz-buzz/1.2",
  "name" : "fizz-buzz-1.2.properties",
  "type" : "file",
  "size" : 790,
  "created" : "2020-09-30T18:54:42.124Z",
  "created_by" : "user",
  "modified" : "2020-09-30T18:54:42.078Z",
  "modified_by" : "user",
  "updated" : "2020-09-30T18:54:42.125Z"
} ],
"range" : {
  "start_pos" : 0,
  "end_pos" : 8,
  "total" : 8
}
}

请注意它是如何包含 fizz-buzz 1.0 和 1.1 的属性文件的,即使我在搜索中指定了 1.2。

是否有更好的方法来获取我正在寻找的信息?

尝试下面的方法,它使用路径仅在 repo libs-release-local 中查找与 com/foo/bar/fizz-buzz 匹配的工件,然后在末尾添加一些 jq 以使输出更好。还注意到类型:文件,它消除了元数据方面的一些噪音。

您将需要定义或替换 USER、API_KEY 和 ARTIFACTORY_URL。

curl -su "${USER}:${API_KEY}" -X POST "${ARTIFACTORY_URL}/artifactory/api/search/aql" \
   -H "content-type: text/plain" \
   -d "items.find({\"type\" : \"file\",\"$and\":[{\"path\" : {\"$match\" : \"com/foo/bar/fizz-buzz*\"}, \"repo\" : {\"$match\" : \"libs-release-local\"} }]}).include(\"name\",\"repo\",\"path\",\"size\").sort({\"$desc\": [\"size\"]})" \
|  jq -r "to_entries|map(\"\(.key)=\(.value|tostring)\")|.[]" | grep results | cut -f 2 -d = | jq .

您可以使用 Artifactory 7.9 中添加的新 GraphQL 功能。
这项新功能允许您使用 GraphQL 查询语言查询 Artifactory 拥有的关于包、版本、工件等的丰富元数据。

您可以使用元数据 REST API 进行查询。请注意您需要使用 admin access token 进行身份验证。例如:

curl -H "Authorization: Bearer <Your Token>" -XPOST http://localhost:8082/metadata/api/v1/query -d '{"query":"..." }'

例如,以下查询正在获取属于名为 hello-world 的包的 1.0* 版的所有文件。此查询适用于可在 Artifactory 中管理的任何类型的包。

query {
    packages(
        filter: {
            name: "hello-world"
        }
    ) {
        edges {
            node {
                name
                packageType
                versions (filter: {name : "1.0*"}) {
                    name
                    repos {
                        name
                        leadFilePath
                    }
                    files {
                        name
                    }
                }
            }
        }
    }
}

结果看起来像

{
    "data": {
        "packages": {
            "edges": [
                {
                    "node": {
                        "name": "hello-world",
                        "packageType": "maven",
                        "versions": [
                            {
                                "name": "1.0-SNAPSHOT",
                                "repos": [
                                    {
                                        "name": "kotlin-local-snapshots",
                                        "leadFilePath": "org/jetbrains/kotlin/hello-world/1.0-SNAPSHOT/hello-world-1.0-20171225.112927-1.pom"
                                    }
                                ],
                                "files": [
                                    {
                                        "name": "hello-world-1.0-20171225.112927-1.jar"
                                    },
                                    {
                                        "name": "hello-world-1.0-20171225.112927-1.pom"
                                    }
                                ]
                            }
                        ]
                    }
                },
                {
                    "node": {
                        "name": "hello-world",
                        "packageType": "maven",
                        "versions": [
                            {
                                "name": "1.0-SNAPSHOT",
                                "repos": [
                                    {
                                        "name": "kotlin-local-snapshots",
                                        "leadFilePath": "org/jetbrains/kotlin/examples/hello-world/1.0-SNAPSHOT/hello-world-1.0-20171225.112138-1.pom"
                                    }
                                ],
                                "files": [
                                    {
                                        "name": "hello-world-1.0-20171225.112138-1.jar"
                                    },
                                    {
                                        "name": "hello-world-1.0-20171225.112138-1.pom"
                                    }
                                ]
                            }
                        ]
                    }
                }
            ]
        }
    }
}