Skip to content

Commit 215193d

Browse files
committed
Feature: Tag support
This commit adds the ability to manage tags with your Sonatype Nexus repository installation
1 parent dba60c3 commit 215193d

File tree

7 files changed

+388
-0
lines changed

7 files changed

+388
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
function Get-NexusFormat {
2+
<#
3+
.SYNOPSIS
4+
Returns detailed format information
5+
6+
.DESCRIPTION
7+
Returns detailed information about the upload specifications for each format supported
8+
9+
.PARAMETER RepositoryFormat
10+
Retrieve information about a specific format
11+
12+
.EXAMPLE
13+
Get-NexusFormat
14+
15+
.EXAMPLE
16+
Get-NexusFormat -RepositoryFormat nuget
17+
#>
18+
[Cmdletbinding(HelpUri='https://steviecoaster.github.io/NexuShell/Formats/Get-NexusFormat/')]
19+
Param(
20+
[Parameter()]
21+
[Alias('Format')]
22+
[ValidateSet('helm',
23+
'r',
24+
'pypi',
25+
'docker',
26+
'yum',
27+
'rubygems',
28+
'nuget',
29+
'npm',
30+
'raw',
31+
'apt',
32+
'maven2'
33+
)]
34+
[String]
35+
$RepositoryFormat
36+
)
37+
begin {
38+
if (-not $header) {
39+
throw "Not connected to Nexus server! Run Connect-NexusServer first."
40+
}
41+
42+
$urislug = if (-not $RepositoryFormat) {
43+
"/service/rest/v1/formats/upload-specs"
44+
}
45+
else {
46+
"/service/rest/v1/formats/$RepositoryFormat/upload-specs"
47+
}
48+
}
49+
process {
50+
Invoke-Nexus -UriSlug $urislug -Method 'GET'
51+
}
52+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
function Add-NexusTagAssociation {
2+
<#
3+
.SYNOPSIS
4+
Tags a component with the provided tag name based on the search query parameters
5+
6+
.DESCRIPTION
7+
Tags a component based on the Query terms based via hashtable to the API endpoint.
8+
9+
.PARAMETER Tag
10+
The tag to apply to the component(s)
11+
12+
.PARAMETER SearchQuery
13+
A hashtable of search parameters by which to tag components
14+
15+
.EXAMPLE
16+
Add-NexusTag -Tag SampleTag -SearchQuery @{ format = 'nuget' ; repository = 'ChocolateyPackages'}
17+
18+
.LINK
19+
https://help.sonatype.com/en/tagging.html
20+
#>
21+
[CmdletBinding(HelpUri='https://steviecoaster.github.io/NexuShell/Tags/Add-NexusTagAssociation/')]
22+
Param(
23+
[Parameter(Mandatory)]
24+
[String]
25+
$Tag,
26+
27+
[Parameter()]
28+
[hashtable]
29+
$SearchQuery
30+
)
31+
32+
begin {
33+
if (-not $header) {
34+
throw "Not connected to Nexus server! Run Connect-NexusServer first."
35+
}
36+
37+
$urislug = '/service/rest/v1/tags/associate/{0}' -f $Tag
38+
}
39+
40+
process {
41+
$UriBase = "$($protocol)://$($Hostname):$($port)$($ContextPath)"
42+
$Uri = $UriBase + $UriSlug
43+
44+
$tagUrl = New-HttpQueryString -Uri $uri -QueryParameter $SearchQuery
45+
46+
$Params = @{
47+
Headers = $header
48+
ContentType = 'application/json'
49+
Uri = $tagUrl
50+
Method = 'POST'
51+
UseBasicParsing = $true
52+
}
53+
54+
Invoke-RestMethod @Params
55+
}
56+
}

src/public/Tags/Get-NexusTag.ps1

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function Get-NexusTag {
2+
[Cmdletbinding(HelpUri = 'https://steviecoaster.github.io/NexuShell/Tags/Get-NexusTag/')]
3+
Param(
4+
[Parameter()]
5+
[String]
6+
$Name
7+
)
8+
9+
begin {
10+
11+
if (-not $header) {
12+
throw "Not connected to Nexus server! Run Connect-NexusServer first."
13+
}
14+
15+
$urislug = if (-not $Name) {
16+
'/service/rest/v1/tags'
17+
}
18+
else {
19+
"/service/rest/v1/tags/$Name"
20+
}
21+
22+
}
23+
24+
process {
25+
if(-not $Name){
26+
$result = Invoke-Nexus -Urislug $urislug -Method GET
27+
$result.items
28+
} else {
29+
Invoke-Nexus -UriSlug $urislug -Method GET
30+
}
31+
}
32+
33+
}

src/public/Tags/New-NexusTag.ps1

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
function New-NexusTag {
2+
<#
3+
.SYNOPSIS
4+
Create a tag
5+
6+
.DESCRIPTION
7+
Create a tag in Nexus Repository. Useful with CI/CD platforms, similar to git tags.
8+
9+
.PARAMETER Tag
10+
The friendly name of the tag
11+
12+
.PARAMETER Attributes
13+
Any additional metadata you wish to tag a component with
14+
15+
.EXAMPLE
16+
$tag = @{
17+
name = 'SampleTag'
18+
attributes = @{
19+
jvm = '9'
20+
builtby = 'Jenkins'
21+
}
22+
}
23+
24+
New-NexusTag @tag
25+
26+
.LINK
27+
https://help.sonatype.com/en/tagging.html
28+
#>
29+
[CmdletBinding(HelpUri='https://steviecoaster.github.io/NexuShell/Tags/New-NexusTag/')]
30+
Param(
31+
[Parameter(Mandatory)]
32+
[ValidateLength(1,256)]
33+
[String]
34+
$Tag,
35+
36+
[Parameter()]
37+
[hashtable]
38+
$Attributes
39+
)
40+
begin{
41+
if (-not $header) {
42+
throw "Not connected to Nexus server! Run Connect-NexusServer first."
43+
}
44+
45+
$urislug = '/service/rest/v1/tags'
46+
47+
if ($Tag.Length -gt 256) {
48+
throw "Name cannot exceed 256 characters."
49+
}
50+
51+
if ($Tag -notmatch '^[a-zA-Z0-9_.-]+$') {
52+
throw "Name can only contain letters, numbers, underscores, hyphens, and dots."
53+
}
54+
55+
if ($Tag -match '^[_\.]') {
56+
throw "Name cannot start with an underscore or dot."
57+
}
58+
}
59+
60+
process {
61+
62+
$body = @{
63+
name = $Tag
64+
attributes = $Attributes
65+
}
66+
67+
Invoke-Nexus -Urislug $urislug -Body $body -Method POST
68+
}
69+
70+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
function Remove-NexusTag {
2+
<#
3+
.SYNOPSIS
4+
Removes a tag from Sonatype Nexus
5+
6+
.DESCRIPTION
7+
Removes a tag from Nexus. Completely disassociates all tagged components. Use Remove-NexusTagAssociation to modify tags on individual components.
8+
9+
.PARAMETER Tag
10+
The tag to remove
11+
12+
.PARAMETER Force
13+
Don't prompt for confirmation.
14+
15+
.EXAMPLE
16+
Remove-NexusTag -Tag SampleTag
17+
18+
This will prompt you to confirm the deletion
19+
20+
.EXAMPLE
21+
Remove-NexusTag -Tag SampleTag -Force
22+
23+
No prompts for confirmation. Potentially dangerous. Use -Whatif if not sure of results
24+
25+
.LINK
26+
https://help.sonatype.com/en/tagging.html
27+
#>
28+
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High', HelpUri = 'https://steviecoaster.github.io/NexuShell/Tags/Remove-NexusTag/')]
29+
Param(
30+
[Parameter(Mandatory)]
31+
[String]
32+
$Tag,
33+
34+
[Parameter()]
35+
[Switch]
36+
$Force
37+
)
38+
begin {
39+
if (-not $header) {
40+
throw "Not connected to Nexus server! Run Connect-NexusServer first."
41+
}
42+
43+
$urislug = '/service/rest/v1/tags/{0}' -f $Tag
44+
}
45+
process {
46+
if ($Force -and -not $Confirm) {
47+
$ConfirmPreference = 'None'
48+
if ($PSCmdlet.ShouldProcess("$($_)", "Remove Tag")) {
49+
Invoke-Nexus -Urislug $urislug -Method 'DELETE'
50+
}
51+
}
52+
else {
53+
if ($PSCmdlet.ShouldProcess("$($_)", "Remove Tag")) {
54+
Invoke-Nexus -Urislug $urislug -Method 'DELETE'
55+
}
56+
}
57+
}
58+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
function Remove-NexusTagAssociation {
2+
<#
3+
.SYNOPSIS
4+
Removes a tag from a component
5+
6+
.DESCRIPTION
7+
Disassociates a tag from a component within Nexus. Does not remove the tag itself from the system.
8+
9+
.PARAMETER Tag
10+
The tag to disassociate
11+
12+
.PARAMETER SearchQuery
13+
The search parameters to find components with the tag to remove
14+
15+
.PARAMETER Force
16+
Don't prompt to remove the tag
17+
18+
.EXAMPLE
19+
Remove-NexusTagAssociation -Tag SampleTag -SearchQuery @{ format = 'nuget' ; repository = 'ChocolateyInternal'}
20+
21+
.LINK
22+
https://help.sonatype.com/en/tagging.html
23+
#>
24+
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High', HelpUri = 'https://steviecoaster.github.io/NexuShell/Tags/Remove-NexusTagAssociation/')]
25+
Param(
26+
[Parameter(Mandatory)]
27+
[String]
28+
$Tag,
29+
30+
[Parameter(Mandatory)]
31+
[hashtable]
32+
$SearchQuery,
33+
34+
[Parameter()]
35+
[Switch]
36+
$Force
37+
)
38+
39+
begin {
40+
if (-not $header) {
41+
throw "Not connected to Nexus server! Run Connect-NexusServer first."
42+
}
43+
44+
$urislug = '/service/rest/v1/tags/associate/{0}' -f $Tag
45+
}
46+
47+
process {
48+
$UriBase = "$($protocol)://$($Hostname):$($port)$($ContextPath)"
49+
$Uri = $UriBase + $UriSlug
50+
51+
$tagUrl = New-HttpQueryString -Uri $uri -QueryParameter $SearchQuery
52+
53+
$Params = @{
54+
Headers = $header
55+
ContentType = 'application/json'
56+
Uri = $tagUrl
57+
Method = 'DELETE'
58+
UseBasicParsing = $true
59+
}
60+
61+
if ($Force -and -not $Confirm) {
62+
$ConfirmPreference = 'None'
63+
if ($PSCmdlet.ShouldProcess("$($_)", "Remove Tag Association")) {
64+
Invoke-RestMethod @Params
65+
}
66+
}
67+
else {
68+
if ($PSCmdlet.ShouldProcess("$($_)", "Remove Tag Association")) {
69+
Invoke-RestMethod @Params
70+
}
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)