Introduction
There are occasions when you need detailed repository information that isn't readily available through the standard Get Repositories REST API in Artifactory. For instance, if you're looking to query newly created repositories or those within a specific time frame, the Artifactory Query Language (AQL) provides the flexibility and power to meet these requirements.
Artifactory Query Language (AQL) is specifically designed to help you uncover any data related to the artifacts and builds stored within Artifactory. Its syntax offers a straightforward way to formulate complex queries that specify numerous search criteria, filters, sorting options, and output parameters. AQL is exposed as a RESTful API that uses data streaming to provide output data, resulting in extremely fast response times and low memory consumption. It can extract data residing in your instance of Artifactory, including Local Repositories, Remote Repositories Caches, and Virtual Repositories. Starting from Artifactory version 7.17.4, you can also search within remote repositories using AQL.
Resolution
Querying Repositories Created During a Specific Period
To find repositories created during a specific period, you can leverage AQL. The query below searches across all repositories for folders created after a particular date.
Example AQL Query:
curl -u admin https://<ARTIFACTORY_URL>/artifactory/api/search/aql \
-X POST -H 'Content-Type: text/plain' \
-d 'items.find({"type": "folder", "repo": {"$match": "*"}, "created": {"$gt" : "2023-09-30"}}).include("repo", "created")'
Query Breakdown:
- curl Command:
- Sends an HTTP request to the Artifactory API.
- Uses Basic Authentication with the admin user.
- AQL Query Payload:
- "type": "folder": Ensures the search only returns folders.
- "repo": {"$match": "*"}: Searches across all repositories.
- The asterisk (*) is a commonly used wildcard character in search queries to denote "any number of characters."
- "created": {"$gt": "2023-09-30"}: Retrieves folders created after September 30, 2023.
- Note: Ensure the date is valid—September has 30 days.
- .include("repo", "created"): Returns the repository name and the folder's creation date.
Example Output:
{
"results": [
{
"repo": "homebrew-cache",
"created": "2023-10-01T22:38:36.210Z"
},
{
"repo": "npm-remote-cache",
"created": "2023-10-07T00:22:47.631Z"
},
{
"repo": "libs-release-local",
"created": "2023-10-19T19:51:16.268Z"
}
],
"range": {
"start_pos": 0,
"end_pos": 3,
"total": 3
}
}
Searching Individual Repositories
If you'd like to search within a specific repository, you can use the following command:
curl -u <user>:<password> -X GET "https://<ARTIFACTORY_URL>/artifactory/api/storage/<REPO_KEY>"
Example Output:
{
"repo": "calico",
"path": "/",
"created": "2023-10-16T21:12:51.112Z",
"lastModified": "2023-10-16T21:12:51.112Z",
"lastUpdated": "2023-10-16T19:43:39.474Z",
"children": [
{
"uri": "/index.yaml",
"folder": false
},
{
"uri": "/v3.28.0",
"folder": true
}
],
"uri": "https://<ARTIFACTORY_URL>/artifactory/api/storage/calico"
}
Conclusion
By utilizing the Artifactory Query Language (AQL), you can perform complex and flexible queries to retrieve detailed information about repositories and artifacts stored within Artifactory. Whether you're auditing repository creation dates or extracting specific metadata, AQL empowers you to tailor your searches precisely to your needs. This capability enhances your ability to manage, monitor, and optimize your Artifactory instance effectively.
Useful Resources
Artifactory Query Language (AQL) Documentation
Artifactory REST API Documentation
Artifactory Entities and Fields