Skip to content

Instantly share code, notes, and snippets.

@greggigon
Created June 19, 2013 09:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greggigon/5813049 to your computer and use it in GitHub Desktop.
Save greggigon/5813049 to your computer and use it in GitHub Desktop.
This simple Artifactory plugin does a job of Calculating repository size by recursively iterating over Repository items and adding sizes of the artifacts.
import org.artifactory.repo.RepoPathFactory
def getDirectorySizeRecursively(def repoPath){
def children = repositories.getChildren(repoPath)
if (children.size() == 0) return 0
def total = 0
children.each { item ->
if (item.folder) {
total += getDirectorySizeRecursively(item.repoPath)
} else {
total += item.size
}
}
total
}
executions {
/**
* This method expects parameter repoName to be passed. It will than calculate the TOTAL size of repository and return the value.
* If the repo name is invalid or not provided it will fail.
* Sample invokation:
* http://localhost:8081/artifactory/api/plugins/execute/getRepositorySize?params=repoName=ext-release-local
*/
getRepositorySize() { params ->
if (params.size() > 0 && params['repoName'] && params['repoName'].size() > 0){
def repoName = params['repoName'][0]
if (repositories.localRepositories.contains(repoName)){
message = getDirectorySizeRecursively(RepoPathFactory.create(repoName, '/'))
status = 200
} else {
message = "Repository $repoName doesn't exists"
status = 404
}
} else {
message = 'Missing repository name parameter repoName={name}'
status = 400
}
}
/**
* Prepare report on all repositories
* Sample invokation:
* http://localhost:8081/artifactory/api/plugins/execute/getAllRepositorySizes
*/
getAllRepositorySizes() { params ->
def repositoryReport = "[\n"
def totalNumberOfRepositories = repositories.localRepositories.size()
repositories.localRepositories.eachWithIndex { repoName, index ->
def repoPath = RepoPathFactory.create(repoName, '/')
def size = getDirectorySizeRecursively(repoPath)
repositoryReport+="{\"repoName\":\"$repoName\", \"sizeInBytes\": $size}"
if (index < totalNumberOfRepositories -1) {
repositoryReport+=",\n"
}
}
repositoryReport+="\n]"
message = repositoryReport
status = 200
}
}
[
{"repoName":"libs-release-local", "sizeInBytes": 122233},
{"repoName":"libs-snapshot-local", "sizeInBytes": 0},
{"repoName":"plugins-release-local", "sizeInBytes": 0},
{"repoName":"plugins-snapshot-local", "sizeInBytes": 0},
{"repoName":"ext-release-local", "sizeInBytes": 122233},
{"repoName":"ext-snapshot-local", "sizeInBytes": 0}
]
@jbaruch
Copy link

jbaruch commented Jun 24, 2013

This is a great one!
Care to contribute it to https://github.com/JFrogDev/artifactory-user-plugins?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment