diff --git a/download/ExpireFileInRepos/ExpireFileInReposTest.groovy b/download/ExpireFileInRepos/ExpireFileInReposTest.groovy new file mode 100644 index 00000000..fa9ceb6e --- /dev/null +++ b/download/ExpireFileInRepos/ExpireFileInReposTest.groovy @@ -0,0 +1,10 @@ +import spock.lang.Specification + +class PluginTest extends Specification { + def 'not implemented plugin test'() { + when: + throw new Exception("Not implemented.") + then: + false + } +} \ No newline at end of file diff --git a/download/ExpireFileInRepos/README.md b/download/ExpireFileInRepos/README.md new file mode 100644 index 00000000..5bd548c2 --- /dev/null +++ b/download/ExpireFileInRepos/README.md @@ -0,0 +1,23 @@ +Artifactory expireFileInRepos User Plugin +==================================================== + +An implementation of the beforeDownloadRequest execution point. This +plugin causes artifacts (including binaries) to expire and be re-cached when downloaded from a remote/virtual +repository. + +## Features + +To use the plugin, modify the script by adding the names of the repositories you wish to expire artifacts in. +The names are to be added to the list 'reposToExpire'. +Note: using this plugin, when downloading artifacts from virtual repositories - artifacts are expired and served from +the source repo for all underlying repos + +## Installation + +* Place script under your plugins folder at `${ARTIFACTORY_HOME}/etc/plugins/` +* Restart your artifactory instance or [use the API to reload plugins][1] + + + +[1]:https://www.jfrog.com/confluence/display/RTF/User+Plugins#UserPlugins-ReloadingPlugins + diff --git a/download/ExpireFileInRepos/expireFileInRepos.groovy b/download/ExpireFileInRepos/expireFileInRepos.groovy new file mode 100644 index 00000000..9ec64a7d --- /dev/null +++ b/download/ExpireFileInRepos/expireFileInRepos.groovy @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2022 JFrog Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import org.artifactory.repo.RepoPath +import org.artifactory.request.Request + +final List reposToExpire = List.of(/*TODO: add repo names*/) + +download { + beforeDownloadRequest { Request request, RepoPath repoPath -> + if ((isRemote(repoPath.repoKey) || isVirtual(repoPath.repoKey)) + && reposToExpire.contains(repoPath.repoKey)) { + log.debug 'Repository ${repoPath.repoKey} is marked for file expiration. Expiring file: ${repoPath.name}' + expired = true + } + } +} + +def isRemote(String repoKey) { + return repositories.getRemoteRepositories().contains(repoKey) +} + +def isVirtual(String repoKey) { + return repositories.getVirtualRepositories().contains(repoKey) +} \ No newline at end of file