-
Notifications
You must be signed in to change notification settings - Fork 19
Delete yum artifacts regex #72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
f18m
wants to merge
20
commits into
thiagofigueiro:master
Choose a base branch
from
f18m:delete_yum_artifacts_regex
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
6ac9c06
Add repository del_assets command
0cca23d
Add variant to use wildcard
a67c777
Improve error handling from Groovy script; use f-strings to address r…
6f19be9
Merge new delete command with previous one
26e45ce
Merge branch 'master' into delete_yum_artifacts_regex
6569a9c
fix whitespaces
d8913b6
Fix some obvious problems
f18m cb9d3a5
Merge branch 'master' into delete_yum_artifacts_regex
4087402
Restore functionalities after integrating fixes from the MASTER branch.
f18m b987072
Remove previous delete command implementation (not going through APIs
f18m d7e6ebf
Fix linter warnings
f18m 806cfa0
Fix for tests
f18m acf6586
Address reviewers comments
f18m 32e59be
Fix integration tests
f18m 0ccbed4
Fix unit tests
f18m 2a70340
Fix flake8 errors/warnings; improve integration test to verify that t…
f18m 1c9fabe
Merge branch 'master' into delete_yum_artifacts_regex
f18m 0b1d9da
Fix more flake8 errors
f18m b9fe5cc
Fix CodeFactor error
f18m 0506853
more flake8 fixes
f18m File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
src/nexuscli/api/script/groovy/nexus3-cli-repository-delete-assets.groovy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| // Original from: | ||
| // https://github.com/hlavki/nexus-scripts | ||
| // Modified to include some improvements to | ||
| // - logging | ||
| // - option to do a "dry run" | ||
| // - support for EXACT_NAME, WILDCARD or REGEX matching methods | ||
|
|
||
| import org.sonatype.nexus.repository.storage.Asset | ||
| import org.sonatype.nexus.repository.storage.Query | ||
| import org.sonatype.nexus.repository.storage.StorageFacet | ||
| import org.sonatype.nexus.repository.raw.internal.RawFormat | ||
|
|
||
| import groovy.json.JsonOutput | ||
| import groovy.json.JsonSlurper | ||
|
|
||
| def log_prefix = "nexus3-cli GROOVY SCRIPT: " | ||
|
|
||
| // https://gist.github.com/kellyrob99/2d1483828c5de0e41732327ded3ab224 | ||
| // https://gist.github.com/emexelem/bcf6b504d81ea9019ad4ab2369006e66 | ||
|
|
||
| def request = new JsonSlurper().parseText(args) | ||
| assert request.repoName: 'repoName parameter is required' | ||
| assert request.assetName: 'name regular expression parameter is required, format: regexp' | ||
| assert request.assetMatchType != null: 'assetMatchType parameter is required' | ||
| assert request.assetMatchType == 'EXACT_NAME' || request.assetMatchType == 'WILDCARD' || request.assetMatchType == 'REGEX': 'assetMatchType parameter value is invalid: ${request.assetName}' | ||
| assert request.dryRun != null: 'dryRun parameter is required' | ||
|
|
||
| def repo = repository.repositoryManager.get(request.repoName) | ||
| if (repo == null) { | ||
| log.warn(log_prefix + "Repository ${request.repoName} does not exist") | ||
|
|
||
| def result = JsonOutput.toJson([ | ||
| success : false, | ||
| error : "Repository '${request.repoName}' does not exist.", | ||
| assets : null | ||
| ]) | ||
| return result | ||
| } | ||
| else if (!repo.type.toString().equals('hosted')) { | ||
| log.warn(log_prefix + "Repository ${request.repoName} has type ${repo.type}; only HOSTED repositories are supported for delete operations.") | ||
|
|
||
| def result = JsonOutput.toJson([ | ||
| success : false, | ||
| error : "Repository '${request.repoName}' has invalid type '${repo.type}'; expecting an 'hosted' repository.", | ||
| assets : null | ||
| ]) | ||
| return result | ||
| } | ||
|
|
||
| log.info(log_prefix + "Valid repository: ${request.repoName}, of type: ${repo.type} and format: ${repo.format}") | ||
|
|
||
| StorageFacet storageFacet = repo.facet(StorageFacet) | ||
| def tx = storageFacet.txSupplier().get() | ||
|
|
||
| try { | ||
| tx.begin() | ||
|
|
||
| log.info(log_prefix + "Gathering list of assets from repository: ${request.repoName} matching pattern: ${request.assetName} assetMatchType: ${request.assetMatchType}") | ||
| Iterable<Asset> assets | ||
| if (request.assetMatchType == 'EXACT_NAME') | ||
| assets = tx.findAssets(Query.builder().where('name = ').param(request.assetName).build(), [repo]) | ||
| else if (request.assetMatchType == 'WILDCARD') | ||
| assets = tx.findAssets(Query.builder().where('name like ').param(request.assetName).build(), [repo]) | ||
| else if (request.assetMatchType == 'REGEX') | ||
| assets = tx.findAssets(Query.builder().where('name MATCHES ').param(request.assetName).build(), [repo]) | ||
|
|
||
| def urls = assets.collect { "/${repo.name}/${it.name()}" } | ||
|
|
||
| if (request.dryRun == false) { | ||
| // add in the transaction a delete command for each asset | ||
| assets.each { asset -> | ||
| log.info(log_prefix + "Deleting asset ${asset.name()}") | ||
| tx.deleteAsset(asset); | ||
|
|
||
| def assetId = asset.componentId() | ||
| if (assetId != null) { | ||
| def component = tx.findComponent(assetId); | ||
| if (component != null) { | ||
| log.info(log_prefix + "Deleting component with ID ${assetId} that belongs to asset ${asset.name()}") | ||
| tx.deleteComponent(component); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| tx.commit() | ||
|
|
||
| numAssets = urls.size() | ||
| log.info(log_prefix + "Transaction committed successfully; number of assets matched: ${numAssets}") | ||
|
|
||
| def result = JsonOutput.toJson([ | ||
| success : true, | ||
| error : "", | ||
| assets : urls | ||
| ]) | ||
| return result | ||
|
|
||
| } catch (all) { | ||
| log.warn(log_prefix + "Exception: ${all}") | ||
| all.printStackTrace() | ||
| log.info(log_prefix + "Rolling back changes...") | ||
| tx.rollback() | ||
| log.info(log_prefix + "Rollback done.") | ||
|
|
||
| def result = JsonOutput.toJson([ | ||
| success : false, | ||
| error : "Exception during processing.", | ||
| assets : null | ||
| ]) | ||
| return result | ||
|
|
||
| } finally { | ||
| // @todo Fix me! Danger Will Robinson! | ||
| tx.close() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.