1

通过 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。

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

4

2 回答 2

3

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

您可以使用元数据 REST API 进行查询。请注意,您需要使用管理员访问令牌进行身份验证。例如:

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"
                                    }
                                ]
                            }
                        ]
                    }
                }
            ]
        }
    }
}
于 2020-10-07T19:44:09.477 回答
1

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

您将需要定义或替换 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 .
于 2020-10-06T19:28:05.293 回答