From f4b69be83b3ec3dc32a65f500cf77f73ed99837c Mon Sep 17 00:00:00 2001 From: Henk Meulekamp Date: Mon, 26 Sep 2022 22:24:41 +0200 Subject: [PATCH 01/26] First pass at adding autolink reference --- GitHubRepositoryAutolink.ps1 | 394 +++++++++++++++++++++++++++++++++++ PowerShellForGitHub.psd1 | 4 + USAGE.md | 23 ++ 3 files changed, 421 insertions(+) create mode 100644 GitHubRepositoryAutolink.ps1 diff --git a/GitHubRepositoryAutolink.ps1 b/GitHubRepositoryAutolink.ps1 new file mode 100644 index 00000000..56bb26ce --- /dev/null +++ b/GitHubRepositoryAutolink.ps1 @@ -0,0 +1,394 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +filter Get-GitHubRepositoryAutolink +{ +<# + .SYNOPSIS + Gets the list of autolinks of the specified repository on GitHub. + + .DESCRIPTION + Gets the list of autolinks of the specified repository on GitHub. + + The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub + + .PARAMETER OwnerName + Owner of the repository. + If not supplied here, the DefaultOwnerName configuration property value will be used. + + .PARAMETER RepositoryName + Name of the repository. + If not supplied here, the DefaultRepositoryName configuration property value will be used. + + .PARAMETER Uri + Uri for the repository. + The OwnerName and RepositoryName will be extracted from here instead of needing to provide + them individually. + + .PARAMETER Sort + The sort order for results. + + .PARAMETER AccessToken + If provided, this will be used as the AccessToken for authentication with the + REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated. + + .INPUTS + GitHub.Branch + GitHub.Content + GitHub.Event + GitHub.Issue + GitHub.IssueComment + GitHub.Label + GitHub.Milestone + GitHub.PullRequest + GitHub.Project + GitHub.ProjectCard + GitHub.ProjectColumn + GitHub.Reaction + GitHub.Release + GitHub.ReleaseAsset + GitHub.Repository + + .OUTPUTS + GitHub.Autolink + + .EXAMPLE + Get-GitHubRepositoryAutolink -OwnerName microsoft -RepositoryName PowerShellForGitHub + + Gets all of the autolink references for the microsoft\PowerShellForGitHub repository. +#> + [CmdletBinding(DefaultParameterSetName = 'Elements')] + [OutputType({$script:GitHubRepositoryTypeName})] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSReviewUnusedParameter", "", Justification="The Uri parameter is only referenced by Resolve-RepositoryElements which get access to it from the stack via Get-Variable -Scope 1.")] + param( + [Parameter(ParameterSetName='Elements')] + [string] $OwnerName, + + [Parameter(ParameterSetName='Elements')] + [string] $RepositoryName, + + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName, + ParameterSetName='Uri')] + [Alias('RepositoryUrl')] + [string] $Uri, + + [ValidateSet('Newest', 'Oldest', 'Stargazers')] + [string] $Sort = 'Newest', + + [string] $AccessToken + ) + + Write-InvocationLog + + $elements = Resolve-RepositoryElements + $OwnerName = $elements.ownerName + $RepositoryName = $elements.repositoryName + + $telemetryProperties = @{ + 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) + 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) + 'Sort' = $Sort + } + + $getParams = @( + "sort=$($Sort.ToLower())" + ) + + $params = @{ + 'UriFragment' = "repos/$OwnerName/$RepositoryName/autolinks`?" + ($getParams -join '&') + 'Description' = "Getting all autolinks of $RepositoryName" + 'AccessToken' = $AccessToken + 'TelemetryEventName' = $MyInvocation.MyCommand.Name + 'TelemetryProperties' = $telemetryProperties + } + + return (Invoke-GHRestMethodMultipleResult @params | Add-GitHubRepositoryAdditionalProperties) +} + +filter New-GitHubRepositoryAutolink +{ +<# + .SYNOPSIS + Creates a new autolink on given repository on GitHub. + + .DESCRIPTION + Creates a new autolink on given repository on GitHub. + + The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub + + .PARAMETER OwnerName + Owner of the repository. + If not supplied here, the DefaultOwnerName configuration property value will be used. + + .PARAMETER RepositoryName + Name of the repository. + If not supplied here, the DefaultRepositoryName configuration property value will be used. + + .PARAMETER Uri + Uri for the repository. + The OwnerName and RepositoryName will be extracted from here instead of needing to provide + them individually. + + .PARAMETER UriPrefix + This prefix appended by certain characters will generate a link any time it is found in + an issue, pull request, or commit. + + .PARAMETER Urltemplate + The URL must contain for the reference number. matches different characters + depending on the value of is_alphanumeric. + + .PARAMETER IsNumericOnly + Whether this autolink reference matches numeric characters only. + If true, the parameter of the url_template matches numeric characters only. + If false, this autolink reference only matches alphanumeric characters A-Z + (case insensitive), 0-9, and -. + + .PARAMETER AccessToken + If provided, this will be used as the AccessToken for authentication with the + REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated. + + .INPUTS + GitHub.Branch + GitHub.Content + GitHub.Event + GitHub.Issue + GitHub.IssueComment + GitHub.Label + GitHub.Milestone + GitHub.PullRequest + GitHub.Project + GitHub.ProjectCard + GitHub.ProjectColumn + GitHub.Reaction + GitHub.Release + GitHub.ReleaseAsset + GitHub.Repository + + .OUTPUTS + GitHub.Autolink + + .EXAMPLE + New-GitHubRepositoryAutolink -OwnerName microsoft -RepositoryName PowerShellForGitHub -UriPrefix PRJ- -Urltemplate https://company.issuetracker.com/browse/prj- + + Creates an autlink reference on this repository under the current authenticated user's account. + + .EXAMPLE + New-GitHubRepositoryAutolink -OwnerName microsoft -RepositoryName PowerShellForGitHub -OrganizationName OctoLabs -UriPrefix PRJ- -Urltemplate https://company.issuetracker.com/browse/prj- -IsNumericOnly + + Creates an autlink reference on this repository under the OctoLabs organization. +#> + [CmdletBinding( + SupportsShouldProcess, + DefaultParameterSetName='Elements')] + [OutputType({$script:GitHubRepositoryTypeName})] + param( + [Parameter(ParameterSetName='Elements')] + [string] $OwnerName, + + [Parameter(ParameterSetName='Elements')] + [string] $RepositoryName, + + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName, + ParameterSetName='Uri')] + [Alias('RepositoryUrl')] + [string] $Uri, + + [Parameter(Mandatory)] + #todo [ValidateScript({if ($_ -match '^#?[a-zA-Z0-9.-_+=:\/#]$') { $true } else { throw "Reference prefix must only contain letters, numbers, or .-_+=:/#." }})] + [Alias('UriPrefix')] + [string] $UriPrefix, + + [Parameter(Mandatory)] + #todo [ValidateScript({if ($_ -match '^#?[]$') { $true } else { throw "Target URL is missing a token." }})] + [Alias('Urltemplate')] + [string] $Urltemplate, + + [switch] $IsNumericOnly, + + [string] $AccessToken + ) + + Write-InvocationLog + + $elements = Resolve-RepositoryElements + $OwnerName = $elements.ownerName + $RepositoryName = $elements.repositoryName + + $telemetryProperties = @{ + 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) + 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) + } + + $hashBody = @{ + 'key_prefix' = $UriPrefix + 'url_template' = $Urltemplate + } + + if ($PSBoundParameters.ContainsKey('IsNumericOnly')) { $hashBody['is_alphanumeric'] = $false } + + if (-not $PSCmdlet.ShouldProcess($UriPrefix, 'Create Repository Autolink')) + { + return + } + + $params = @{ + 'UriFragment' = "repos/$OwnerName/$RepositoryName/autolinks" + 'Body' = (ConvertTo-Json -InputObject $hashBody) + 'Method' = 'Post' + 'Description' = "Repository Autolink $UriPrefix in $RepositoryName" + 'AcceptHeader' = $script:symmetraAcceptHeader + 'AccessToken' = $AccessToken + 'TelemetryEventName' = $MyInvocation.MyCommand.Name + 'TelemetryProperties' = $telemetryProperties + } + + return (Invoke-GHRestMethod @params | Add-GitHubLabelAdditionalProperties) +} + + +filter Remove-GitHubRepositoryAutolink +{ +<# + .SYNOPSIS + Deletes an autolink reference from a given GitHub repository. + + .DESCRIPTION + Deletes an autolink reference from a given GitHub repository. + + The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub + + .PARAMETER OwnerName + Owner of the repository. + If not supplied here, the DefaultOwnerName configuration property value will be used. + + .PARAMETER RepositoryName + Name of the repository. + If not supplied here, the DefaultRepositoryName configuration property value will be used. + + .PARAMETER Uri + Uri for the repository. + The OwnerName and RepositoryName will be extracted from here instead of needing to provide + them individually. + + .PARAMETER AutolinkId + The unique identifier of the autolink to be deleted. + + .PARAMETER Force + If this switch is specified, you will not be prompted for confirmation of command execution. + + .PARAMETER AccessToken + If provided, this will be used as the AccessToken for authentication with the + REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated. + + .INPUTS + GitHub.Branch + GitHub.Content + GitHub.Event + GitHub.Issue + GitHub.IssueComment + GitHub.Label + GitHub.Milestone + GitHub.PullRequest + GitHub.Project + GitHub.ProjectCard + GitHub.ProjectColumn + GitHub.Reaction + GitHub.Release + GitHub.ReleaseAsset + GitHub.Repository + + .EXAMPLE + Remove-GitHubRepositoryAutolink -OwnerName microsoft -RepositoryName PowerShellForGitHub -AutolinkId 1 + + Removes the autolink reference with autolinkId 1 from the PowerShellForGitHub project. + + .EXAMPLE + $autolink = $repo | New-GitHubRepositoryAutolink -KeyPrefix 'PRJ-123' + $autolink | Remove-GitHubRepositoryAutolink + + Removes the autolink reference we just created using the pipeline, but will prompt for confirmation + because neither -Confirm:$false nor -Force was specified. + + .EXAMPLE + Remove-GitHubRepositoryAutolink -OwnerName microsoft -RepositoryName PowerShellForGitHub -AutolinkId 1 -Confirm:$false + + Removes the autolink reference with autolinkId 1 from the PowerShellForGitHub project. + Will not prompt for confirmation, as -Confirm:$false was specified. + + .EXAMPLE + Remove-GitHubRepositoryAutolink -OwnerName microsoft -RepositoryName PowerShellForGitHub -AutolinkId 1 -Force + + Removes the autolink reference with autolinkId 1 from the PowerShellForGitHub project. + Will not prompt for confirmation, as -Force was specified. +#> + [CmdletBinding( + SupportsShouldProcess, + DefaultParameterSetName='Elements', + ConfirmImpact="High")] + [Alias('Delete-GitHubRepositoryAutolink')] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSReviewUnusedParameter", "", Justification="The Uri parameter is only referenced by Resolve-RepositoryElements which get access to it from the stack via Get-Variable -Scope 1.")] + param( + [Parameter(ParameterSetName='Elements')] + [string] $OwnerName, + + [Parameter(ParameterSetName='Elements')] + [string] $RepositoryName, + + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName, + ParameterSetName='Uri')] + [Alias('RepositoryUrl')] + [string] $Uri, + + [Parameter( + Mandatory, + ValueFromPipeline, + ValueFromPipelineByPropertyName)] + [ValidateNotNullOrEmpty()] + [Alias('AutolinkId')] + [string] $AutolinkId, + + [switch] $Force, + + [string] $AccessToken + ) + + Write-InvocationLog + + $elements = Resolve-RepositoryElements + $OwnerName = $elements.ownerName + $RepositoryName = $elements.repositoryName + + $telemetryProperties = @{ + 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) + 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) + } + + if ($Force -and (-not $Confirm)) + { + $ConfirmPreference = 'None' + } + + if (-not $PSCmdlet.ShouldProcess($Label, 'Remove GitHub Repository Autolink Reference')) + { + return + } + + $params = @{ + 'UriFragment' = "repos/$OwnerName/$RepositoryName/autolinks/$AutolinkId" + 'Method' = 'Delete' + 'Description' = "Deleting GitHub Autolink Reference $AutolinkId from $RepositoryName" + 'AcceptHeader' = $script:symmetraAcceptHeader + 'AccessToken' = $AccessToken + 'TelemetryEventName' = $MyInvocation.MyCommand.Name + 'TelemetryProperties' = $telemetryProperties + } + + return Invoke-GHRestMethod @params +} + diff --git a/PowerShellForGitHub.psd1 b/PowerShellForGitHub.psd1 index 44369427..007082b3 100644 --- a/PowerShellForGitHub.psd1 +++ b/PowerShellForGitHub.psd1 @@ -104,6 +104,7 @@ 'Get-GitHubReleaseAsset', 'Get-GitHubRepository', 'Get-GitHubRepositoryActionsPermission', + 'Get-GitHubRepositoryAutolink', 'Get-GitHubRepositoryBranch', 'Get-GitHubRepositoryBranchPatternProtectionRule', 'Get-GitHubRepositoryBranchProtectionRule', @@ -144,6 +145,7 @@ 'New-GitHubRelease', 'New-GitHubReleaseAsset', 'New-GitHubRepository', + 'New-GitHubRepositoryAutolink', 'New-GitHubRepositoryFromTemplate', 'New-GitHubRepositoryBranch', 'New-GitHubRepositoryBranchPatternProtectionRule', @@ -167,6 +169,7 @@ 'Remove-GitHubRelease', 'Remove-GitHubReleaseAsset', 'Remove-GitHubRepository', + 'Remove-GitHubRepositoryAutolink', 'Remove-GitHubRepositoryBranch' 'Remove-GitHubRepositoryBranchPatternProtectionRule', 'Remove-GitHubRepositoryBranchProtectionRule', @@ -228,6 +231,7 @@ 'Delete-GitHubRelease', 'Delete-GitHubReleaseAsset', 'Delete-GitHubRepository', + 'Delete-GitHubRepositoryAutolink', 'Delete-GitHubRepositoryBranch', 'Delete-GitHubRepositoryBranchPatternProtectionRule', 'Delete-GitHubRepositoryBranchProtectionRule', diff --git a/USAGE.md b/USAGE.md index 1edffac6..55c9896d 100644 --- a/USAGE.md +++ b/USAGE.md @@ -70,6 +70,10 @@ * [Forks](#forks) * [Get all the forks for a repository](#get-all-the-forks-for-a-repository) * [Create a new fork](#create-a-new-fork) + * [Autolink](#autolink) + * [Get all the autolinks for a repository](#get-all-the-autolinks-for-a-repository) + * [Create a new autolink](#create-a-new-autolink) + * [Remove a repository autolink](#remove-a-repository-autolink) * [Content](#content) * [Get html output for a file](#get-html-output-for-a-file) * [Get raw output for a file](#get-raw-output-for-a-file) @@ -740,6 +744,25 @@ New-GitHubRepositoryFork -OwnerName microsoft -RepositoryName PowerShellForGitHu ---------- +### Autolinks + +#### Get all the autolinks for a repository +```powershell +Get-GitHubRepositoryAutolink -OwnerName microsoft -RepositoryName PowerShellForGitHub +``` + +#### Create a new autolink +```powershell +New-GitHubRepositoryAutolink -OwnerName microsoft -RepositoryName PowerShellForGitHub -UriPrefix JIRA-123 -Urltemplate https://psg.jira.com/browse/jira- +``` + +#### Removing a repository autolink +```powershell +Remove-GitHubRepositoryAutolink -OwnerName microsoft -RepositoryName PowerShellForGitHub -AutolinkId 1 +``` + +---------- + ### Content #### Get html output for a file From 11922a6d8f6c6de4133c1fd2fd434c6c0c38e786 Mon Sep 17 00:00:00 2001 From: HJ Meulekamp Date: Tue, 27 Sep 2022 14:28:00 +0200 Subject: [PATCH 02/26] Fix header title to plural --- USAGE.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/USAGE.md b/USAGE.md index 55c9896d..a8eaa05c 100644 --- a/USAGE.md +++ b/USAGE.md @@ -70,7 +70,7 @@ * [Forks](#forks) * [Get all the forks for a repository](#get-all-the-forks-for-a-repository) * [Create a new fork](#create-a-new-fork) - * [Autolink](#autolink) + * [Autolinks](#autolinks) * [Get all the autolinks for a repository](#get-all-the-autolinks-for-a-repository) * [Create a new autolink](#create-a-new-autolink) * [Remove a repository autolink](#remove-a-repository-autolink) From 78b7ab907ac11268456785afe75cd64ba9d59a2b Mon Sep 17 00:00:00 2001 From: HJ Meulekamp Date: Tue, 27 Sep 2022 14:31:31 +0200 Subject: [PATCH 03/26] Remove brand names from examples --- USAGE.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/USAGE.md b/USAGE.md index a8eaa05c..6822cc8b 100644 --- a/USAGE.md +++ b/USAGE.md @@ -753,7 +753,7 @@ Get-GitHubRepositoryAutolink -OwnerName microsoft -RepositoryName PowerShellForG #### Create a new autolink ```powershell -New-GitHubRepositoryAutolink -OwnerName microsoft -RepositoryName PowerShellForGitHub -UriPrefix JIRA-123 -Urltemplate https://psg.jira.com/browse/jira- +New-GitHubRepositoryAutolink -OwnerName microsoft -RepositoryName PowerShellForGitHub -UriPrefix PRJ-123 -Urltemplate https://company.issuetracker.com/browse/prj- ``` #### Removing a repository autolink From 4d92f41e7ce2b1c65ad1de9de2d90a396359dd8b Mon Sep 17 00:00:00 2001 From: Henk Meulekamp Date: Tue, 27 Sep 2022 21:12:10 +0200 Subject: [PATCH 04/26] Fix for missing nestedmodule --- PowerShellForGitHub.psd1 | 1 + 1 file changed, 1 insertion(+) diff --git a/PowerShellForGitHub.psd1 b/PowerShellForGitHub.psd1 index 007082b3..5aa244df 100644 --- a/PowerShellForGitHub.psd1 +++ b/PowerShellForGitHub.psd1 @@ -52,6 +52,7 @@ 'GitHubReactions.ps1', 'GitHubReleases.ps1', 'GitHubRepositories.ps1', + 'GitHubRepositoryAutolink.ps1', 'GitHubRepositoryForks.ps1', 'GitHubRepositoryTraffic.ps1', 'GitHubTeams.ps1', From f4d5d31ef0caa463f94fe36665377073a916321f Mon Sep 17 00:00:00 2001 From: Henk Meulekamp Date: Tue, 27 Sep 2022 21:12:24 +0200 Subject: [PATCH 05/26] Some fixes after manual testing --- GitHubRepositoryAutolink.ps1 | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/GitHubRepositoryAutolink.ps1 b/GitHubRepositoryAutolink.ps1 index 56bb26ce..aeb6ee9f 100644 --- a/GitHubRepositoryAutolink.ps1 +++ b/GitHubRepositoryAutolink.ps1 @@ -104,10 +104,10 @@ filter Get-GitHubRepositoryAutolink 'TelemetryProperties' = $telemetryProperties } - return (Invoke-GHRestMethodMultipleResult @params | Add-GitHubRepositoryAdditionalProperties) + return (Invoke-GHRestMethodMultipleResult @params ) } -filter New-GitHubRepositoryAutolink +function New-GitHubRepositoryAutolink { <# .SYNOPSIS @@ -199,12 +199,10 @@ filter New-GitHubRepositoryAutolink [Parameter(Mandatory)] #todo [ValidateScript({if ($_ -match '^#?[a-zA-Z0-9.-_+=:\/#]$') { $true } else { throw "Reference prefix must only contain letters, numbers, or .-_+=:/#." }})] - [Alias('UriPrefix')] [string] $UriPrefix, [Parameter(Mandatory)] #todo [ValidateScript({if ($_ -match '^#?[]$') { $true } else { throw "Target URL is missing a token." }})] - [Alias('Urltemplate')] [string] $Urltemplate, [switch] $IsNumericOnly, @@ -226,6 +224,7 @@ filter New-GitHubRepositoryAutolink $hashBody = @{ 'key_prefix' = $UriPrefix 'url_template' = $Urltemplate + 'is_alphanumeric' = $true } if ($PSBoundParameters.ContainsKey('IsNumericOnly')) { $hashBody['is_alphanumeric'] = $false } @@ -246,11 +245,11 @@ filter New-GitHubRepositoryAutolink 'TelemetryProperties' = $telemetryProperties } - return (Invoke-GHRestMethod @params | Add-GitHubLabelAdditionalProperties) + return (Invoke-GHRestMethod @params) } -filter Remove-GitHubRepositoryAutolink +function Remove-GitHubRepositoryAutolink { <# .SYNOPSIS @@ -350,7 +349,6 @@ filter Remove-GitHubRepositoryAutolink ValueFromPipeline, ValueFromPipelineByPropertyName)] [ValidateNotNullOrEmpty()] - [Alias('AutolinkId')] [string] $AutolinkId, [switch] $Force, From 5c83a2cb81ef1e5fcd60b9f3b92503a0701163f1 Mon Sep 17 00:00:00 2001 From: Henk Meulekamp Date: Tue, 27 Sep 2022 22:25:37 +0200 Subject: [PATCH 06/26] Fixes after testing --- GitHubRepositoryAutolink.ps1 | 98 +++++++++++++++++++++++++++++++----- 1 file changed, 86 insertions(+), 12 deletions(-) diff --git a/GitHubRepositoryAutolink.ps1 b/GitHubRepositoryAutolink.ps1 index aeb6ee9f..4ad3db49 100644 --- a/GitHubRepositoryAutolink.ps1 +++ b/GitHubRepositoryAutolink.ps1 @@ -1,5 +1,10 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. +@{ + GitHubRepositoryAutolinkTypeName = 'GitHub.RepositoryAutolink' + }.GetEnumerator() | ForEach-Object { + Set-Variable -Scope Script -Option ReadOnly -Name $_.Key -Value $_.Value + } filter Get-GitHubRepositoryAutolink { @@ -50,7 +55,7 @@ filter Get-GitHubRepositoryAutolink GitHub.Repository .OUTPUTS - GitHub.Autolink + GitHub.RepositoryAutolink .EXAMPLE Get-GitHubRepositoryAutolink -OwnerName microsoft -RepositoryName PowerShellForGitHub @@ -58,7 +63,7 @@ filter Get-GitHubRepositoryAutolink Gets all of the autolink references for the microsoft\PowerShellForGitHub repository. #> [CmdletBinding(DefaultParameterSetName = 'Elements')] - [OutputType({$script:GitHubRepositoryTypeName})] + [OutputType({$script:GitHubRepositoryAutolinkTypeName})] [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSReviewUnusedParameter", "", Justification="The Uri parameter is only referenced by Resolve-RepositoryElements which get access to it from the stack via Get-Variable -Scope 1.")] param( [Parameter(ParameterSetName='Elements')] @@ -104,10 +109,10 @@ filter Get-GitHubRepositoryAutolink 'TelemetryProperties' = $telemetryProperties } - return (Invoke-GHRestMethodMultipleResult @params ) + return (Invoke-GHRestMethodMultipleResult @params | Add-GitHubRepositoryAutolinkAdditionalProperties) } -function New-GitHubRepositoryAutolink +filter New-GitHubRepositoryAutolink { <# .SYNOPSIS @@ -167,7 +172,7 @@ function New-GitHubRepositoryAutolink GitHub.Repository .OUTPUTS - GitHub.Autolink + GitHub.RepositoryAutolink .EXAMPLE New-GitHubRepositoryAutolink -OwnerName microsoft -RepositoryName PowerShellForGitHub -UriPrefix PRJ- -Urltemplate https://company.issuetracker.com/browse/prj- @@ -182,7 +187,7 @@ function New-GitHubRepositoryAutolink [CmdletBinding( SupportsShouldProcess, DefaultParameterSetName='Elements')] - [OutputType({$script:GitHubRepositoryTypeName})] + [OutputType({$script:GitHubRepositoryAutolinkTypeName})] param( [Parameter(ParameterSetName='Elements')] [string] $OwnerName, @@ -199,13 +204,16 @@ function New-GitHubRepositoryAutolink [Parameter(Mandatory)] #todo [ValidateScript({if ($_ -match '^#?[a-zA-Z0-9.-_+=:\/#]$') { $true } else { throw "Reference prefix must only contain letters, numbers, or .-_+=:/#." }})] + [Alias('AutolinkUriPrefix')] [string] $UriPrefix, [Parameter(Mandatory)] #todo [ValidateScript({if ($_ -match '^#?[]$') { $true } else { throw "Target URL is missing a token." }})] + [Alias('AutolinkUrltemplate')] [string] $Urltemplate, - [switch] $IsNumericOnly, + [Alias('AutlinkIsNumeric')] + [switch] $IsNumeric, [string] $AccessToken ) @@ -227,7 +235,7 @@ function New-GitHubRepositoryAutolink 'is_alphanumeric' = $true } - if ($PSBoundParameters.ContainsKey('IsNumericOnly')) { $hashBody['is_alphanumeric'] = $false } + if ($PSBoundParameters.ContainsKey('IsNumeric')) { $hashBody['is_alphanumeric'] = $false } if (-not $PSCmdlet.ShouldProcess($UriPrefix, 'Create Repository Autolink')) { @@ -245,11 +253,10 @@ function New-GitHubRepositoryAutolink 'TelemetryProperties' = $telemetryProperties } - return (Invoke-GHRestMethod @params) + return (Invoke-GHRestMethod @params | Add-GitHubRepositoryAutolinkAdditionalProperties) } - -function Remove-GitHubRepositoryAutolink +filter Remove-GitHubRepositoryAutolink { <# .SYNOPSIS @@ -349,7 +356,7 @@ function Remove-GitHubRepositoryAutolink ValueFromPipeline, ValueFromPipelineByPropertyName)] [ValidateNotNullOrEmpty()] - [string] $AutolinkId, + [int64] $AutolinkId, [switch] $Force, @@ -390,3 +397,70 @@ function Remove-GitHubRepositoryAutolink return Invoke-GHRestMethod @params } + +filter Add-GitHubRepositoryAutolinkAdditionalProperties +{ +<# + .SYNOPSIS + Adds type name and additional properties to ease pipelining to GitHub Autolink objects. + + .PARAMETER InputObject + The GitHub object to add additional properties to. + + .PARAMETER RepositoryUrl + Optionally supplied if the Autolink object doesn't have this value already. + + .PARAMETER TypeName + The type that should be assigned to the object. + + .INPUTS + [PSCustomObject] + + .OUTPUTS + GitHub.Label +#> + [CmdletBinding()] + [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseSingularNouns", "", Justification="Internal helper that is definitely adding more than one property.")] + param( + [Parameter( + Mandatory, + ValueFromPipeline)] + [AllowNull()] + [AllowEmptyCollection()] + [PSCustomObject[]] $InputObject, + + [string] $RepositoryUrl, + + [ValidateNotNullOrEmpty()] + [string] $TypeName = $script:GitHubRepositoryAutolinkTypeName + ) + + foreach ($item in $InputObject) + { + $item.PSObject.TypeNames.Insert(0, $TypeName) + + if (-not (Get-GitHubConfiguration -Name DisablePipelineSupport)) + { + if (-not [System.String]::IsNullOrEmpty($item.url)) + { + $elements = Split-GitHubUri -Uri $item.url + $RepositoryUrl = Join-GitHubUri @elements + } + + if ($null -ne $item.id) + { + Add-Member -InputObject $item -Name 'AutolinkId' -Value $item.id -MemberType NoteProperty -Force + } + + Add-Member -InputObject $item -Name 'AutolinkKeyPrefix' -Value $item.key_prefix -MemberType NoteProperty -Force + Add-Member -InputObject $item -Name 'AutolinkUrlTemplate' -Value $item.url_template -MemberType NoteProperty -Force + + if(!$item.is_alphanumeric) + { + Add-Member -InputObject $item -Name 'AutolinkIsNumeric' -Value $true -MemberType NoteProperty -Force + } + } + + Write-Output $item + } +} \ No newline at end of file From 9c855452080cbd7c160af429269a66d24bf081a1 Mon Sep 17 00:00:00 2001 From: Henk Meulekamp Date: Tue, 27 Sep 2022 22:47:20 +0200 Subject: [PATCH 07/26] fix usage example paramvalue --- USAGE.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/USAGE.md b/USAGE.md index 6822cc8b..dab998cd 100644 --- a/USAGE.md +++ b/USAGE.md @@ -753,7 +753,7 @@ Get-GitHubRepositoryAutolink -OwnerName microsoft -RepositoryName PowerShellForG #### Create a new autolink ```powershell -New-GitHubRepositoryAutolink -OwnerName microsoft -RepositoryName PowerShellForGitHub -UriPrefix PRJ-123 -Urltemplate https://company.issuetracker.com/browse/prj- +New-GitHubRepositoryAutolink -OwnerName microsoft -RepositoryName PowerShellForGitHub -UriPrefix PRJ- -Urltemplate https://company.issuetracker.com/browse/prj- ``` #### Removing a repository autolink From 92cd70e93752c98fff1c35ba57966746afd4991c Mon Sep 17 00:00:00 2001 From: Henk Meulekamp Date: Tue, 4 Oct 2022 21:56:13 +0200 Subject: [PATCH 08/26] Add Tests --- GitHubRepositoryAutolink.ps1 | 2 +- Tests/GitHubRepositoryAutolinks.tests.ps1 | 91 +++++++++++++++++++++++ 2 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 Tests/GitHubRepositoryAutolinks.tests.ps1 diff --git a/GitHubRepositoryAutolink.ps1 b/GitHubRepositoryAutolink.ps1 index 4ad3db49..9ab9f36d 100644 --- a/GitHubRepositoryAutolink.ps1 +++ b/GitHubRepositoryAutolink.ps1 @@ -417,7 +417,7 @@ filter Add-GitHubRepositoryAutolinkAdditionalProperties [PSCustomObject] .OUTPUTS - GitHub.Label + GitHub.RepositoryAutolink #> [CmdletBinding()] [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseSingularNouns", "", Justification="Internal helper that is definitely adding more than one property.")] diff --git a/Tests/GitHubRepositoryAutolinks.tests.ps1 b/Tests/GitHubRepositoryAutolinks.tests.ps1 new file mode 100644 index 00000000..afcbe69d --- /dev/null +++ b/Tests/GitHubRepositoryAutolinks.tests.ps1 @@ -0,0 +1,91 @@ +# Copyright (c) Microsoft Corporation. All rights reserved. +# Licensed under the MIT License. + +<# +.Synopsis + Tests for GitHubRepositoryAutolink.ps1 module +#> + +[CmdletBinding()] +[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments', '', + Justification='Suppress false positives in Pester code blocks')] +param() + +# This is common test code setup logic for all Pester test files +$moduleRootPath = Split-Path -Path $PSScriptRoot -Parent +. (Join-Path -Path $moduleRootPath -ChildPath 'Tests\Common.ps1') + +try +{ + # Define Script-scoped, readonly, hidden variables. + @{ + defaultRepoDesc = "This is a description." + defaultRepoHomePage = "https://www.microsoft.com/" + defaultRepoTopic = "microsoft" + }.GetEnumerator() | ForEach-Object { + Set-Variable -Force -Scope Script -Option ReadOnly -Visibility Private -Name $_.Key -Value $_.Value + } + + Describe 'GitHubRepositoryAutolink\Get-GitHubRepositoryAutolink' { + BeforeAll { + $organizationName = $script:organizationName + } + + Context 'When getting a GitHub Repository Autolinks by repository' { + BeforeAll { + $repoName = [Guid]::NewGuid().Guid + + $repo = New-GitHubRepository -RepositoryName $repoName -OrganizationName $organizationName + + $uriPrefix = 'PRJ-' + $urltemplate = 'https://company.issuetracker.com/browse/prj-' + $isNumericOnly = $true + + $newGitHubRepositoryAutolinkParms = @{ + OrganizationName = $organizationName + UriPrefix = $uriPrefix + UrlTemplate = $urltemplate + IsNumericOnly = $isNumericOnly + } + + New-GitHubRepositoryAutolink @newGitHubRepositoryAutolinkParms | Out-Null + + $autoLinks = Get-GitHubRepositoryAutolink -OwnerName $organizationName -RepositoryName $repoName + $autolink = $autoLinks | Where-Object -Property AutolinkKeyPrefix -eq $uriPrefix + } + + It 'Should have the expected type and additional properties' { + $team.PSObject.TypeNames[0] | Should -Be 'GitHub.RepositoryAutolink' + $team.AutolinkKeyPrefix | Should -Be $uriPrefix + $team.AutolinkUrlTemplate | Should -Be $urltemplate + $team.AutolinkIsNumeric | Should -BeTrue + $team.AutolinkId | Should -BeGreaterThan 0 + $team.UriPrefix | Should -Be $autolink.uriPrefix + $team.Urltemplate | Should -Be $autolink.urltemplate + $team.AutolinkId | Should -Be $autolink.id + $team.OrganizationName | Should -Be $organizationName + } + + AfterAll { + if (Get-Variable -Name repo -ErrorAction SilentlyContinue) + { + $repo | Remove-GitHubRepository -Force + } + + if (Get-Variable -Name team -ErrorAction SilentlyContinue) + { + $autolink | Remove-GitHubRepositoryAutolink -Force + } + } + } + } +} +finally +{ + if (Test-Path -Path $script:originalConfigFile -PathType Leaf) + { + # Restore the user's configuration to its pre-test state + Restore-GitHubConfiguration -Path $script:originalConfigFile + $script:originalConfigFile = $null + } +} From 020a356688e9f247b288b29ce76e7223c244b771 Mon Sep 17 00:00:00 2001 From: Henk Meulekamp Date: Tue, 4 Oct 2022 21:58:09 +0200 Subject: [PATCH 09/26] Move patch level --- PowerShellForGitHub.psd1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PowerShellForGitHub.psd1 b/PowerShellForGitHub.psd1 index 5aa244df..df13730c 100644 --- a/PowerShellForGitHub.psd1 +++ b/PowerShellForGitHub.psd1 @@ -7,7 +7,7 @@ CompanyName = 'Microsoft Corporation' Copyright = 'Copyright (C) Microsoft Corporation. All rights reserved.' - ModuleVersion = '0.16.1' + ModuleVersion = '0.17.0' Description = 'PowerShell wrapper for GitHub API' # Script module or binary module file associated with this manifest. From 8f17ed39689c19535ffb44aca456b02291478992 Mon Sep 17 00:00:00 2001 From: Henk Meulekamp Date: Tue, 4 Oct 2022 22:01:40 +0200 Subject: [PATCH 10/26] Update contributors --- CONTRIBUTING.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 6b335618..4e185661 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -614,6 +614,7 @@ Thank you to all of our contributors, no matter how big or small the contributio - **[Giuseppe Campanelli (@themilanfan)](https://github.com/themilanfan)** - **[Christoph Bergmeister (@bergmeister)](https://github.com/bergmeister)** - **[Simon Heather (@X-Guardian)](https://github.com/X-Guardian)** +- **[Henk Meulekamp (@henkmeulekamp)](https://github.com/henkmeulekamp)** ---------- From e7a4a3dea3826bd6727f149f77353e0d0f76e81c Mon Sep 17 00:00:00 2001 From: Henk Meulekamp Date: Thu, 6 Oct 2022 21:17:55 +0200 Subject: [PATCH 11/26] revert change to version number - codereview comment, needed only when releasing --- PowerShellForGitHub.psd1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PowerShellForGitHub.psd1 b/PowerShellForGitHub.psd1 index df13730c..5aa244df 100644 --- a/PowerShellForGitHub.psd1 +++ b/PowerShellForGitHub.psd1 @@ -7,7 +7,7 @@ CompanyName = 'Microsoft Corporation' Copyright = 'Copyright (C) Microsoft Corporation. All rights reserved.' - ModuleVersion = '0.17.0' + ModuleVersion = '0.16.1' Description = 'PowerShell wrapper for GitHub API' # Script module or binary module file associated with this manifest. From bcb428b20878e5e923f7d9fa085b79b589b27e00 Mon Sep 17 00:00:00 2001 From: Henk Meulekamp Date: Thu, 6 Oct 2022 21:38:24 +0200 Subject: [PATCH 12/26] fix param names KeyPrefix and UrlTemplate --- GitHubRepositoryAutolink.ps1 | 24 +++++++++++------------ Tests/GitHubRepositoryAutolinks.tests.ps1 | 18 ++++++++--------- USAGE.md | 2 +- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/GitHubRepositoryAutolink.ps1 b/GitHubRepositoryAutolink.ps1 index 9ab9f36d..84d95e75 100644 --- a/GitHubRepositoryAutolink.ps1 +++ b/GitHubRepositoryAutolink.ps1 @@ -136,11 +136,11 @@ filter New-GitHubRepositoryAutolink The OwnerName and RepositoryName will be extracted from here instead of needing to provide them individually. - .PARAMETER UriPrefix + .PARAMETER KeyPrefix This prefix appended by certain characters will generate a link any time it is found in an issue, pull request, or commit. - .PARAMETER Urltemplate + .PARAMETER UrlTemplate The URL must contain for the reference number. matches different characters depending on the value of is_alphanumeric. @@ -175,12 +175,12 @@ filter New-GitHubRepositoryAutolink GitHub.RepositoryAutolink .EXAMPLE - New-GitHubRepositoryAutolink -OwnerName microsoft -RepositoryName PowerShellForGitHub -UriPrefix PRJ- -Urltemplate https://company.issuetracker.com/browse/prj- + New-GitHubRepositoryAutolink -OwnerName microsoft -RepositoryName PowerShellForGitHub -KeyPrefix 'PRJ-' -UrlTemplate 'https://company.issuetracker.com/browse/prj-' Creates an autlink reference on this repository under the current authenticated user's account. .EXAMPLE - New-GitHubRepositoryAutolink -OwnerName microsoft -RepositoryName PowerShellForGitHub -OrganizationName OctoLabs -UriPrefix PRJ- -Urltemplate https://company.issuetracker.com/browse/prj- -IsNumericOnly + New-GitHubRepositoryAutolink -OwnerName microsoft -RepositoryName PowerShellForGitHub -OrganizationName OctoLabs -KeyPrefix 'PRJ-' -UrlTemplate 'https://company.issuetracker.com/browse/prj-' -IsNumericOnly Creates an autlink reference on this repository under the OctoLabs organization. #> @@ -204,13 +204,13 @@ filter New-GitHubRepositoryAutolink [Parameter(Mandatory)] #todo [ValidateScript({if ($_ -match '^#?[a-zA-Z0-9.-_+=:\/#]$') { $true } else { throw "Reference prefix must only contain letters, numbers, or .-_+=:/#." }})] - [Alias('AutolinkUriPrefix')] - [string] $UriPrefix, + [Alias('AutolinkKeyPrefix')] + [string] $KeyPrefix, [Parameter(Mandatory)] #todo [ValidateScript({if ($_ -match '^#?[]$') { $true } else { throw "Target URL is missing a token." }})] - [Alias('AutolinkUrltemplate')] - [string] $Urltemplate, + [Alias('AutolinkUrlTemplate')] + [string] $UrlTemplate, [Alias('AutlinkIsNumeric')] [switch] $IsNumeric, @@ -230,14 +230,14 @@ filter New-GitHubRepositoryAutolink } $hashBody = @{ - 'key_prefix' = $UriPrefix - 'url_template' = $Urltemplate + 'key_prefix' = $KeyPrefix + 'url_template' = $UrlTemplate 'is_alphanumeric' = $true } if ($PSBoundParameters.ContainsKey('IsNumeric')) { $hashBody['is_alphanumeric'] = $false } - if (-not $PSCmdlet.ShouldProcess($UriPrefix, 'Create Repository Autolink')) + if (-not $PSCmdlet.ShouldProcess($KeyPrefix, 'Create Repository Autolink')) { return } @@ -246,7 +246,7 @@ filter New-GitHubRepositoryAutolink 'UriFragment' = "repos/$OwnerName/$RepositoryName/autolinks" 'Body' = (ConvertTo-Json -InputObject $hashBody) 'Method' = 'Post' - 'Description' = "Repository Autolink $UriPrefix in $RepositoryName" + 'Description' = "Repository Autolink $KeyPrefix in $RepositoryName" 'AcceptHeader' = $script:symmetraAcceptHeader 'AccessToken' = $AccessToken 'TelemetryEventName' = $MyInvocation.MyCommand.Name diff --git a/Tests/GitHubRepositoryAutolinks.tests.ps1 b/Tests/GitHubRepositoryAutolinks.tests.ps1 index afcbe69d..e87b10f7 100644 --- a/Tests/GitHubRepositoryAutolinks.tests.ps1 +++ b/Tests/GitHubRepositoryAutolinks.tests.ps1 @@ -37,31 +37,31 @@ try $repo = New-GitHubRepository -RepositoryName $repoName -OrganizationName $organizationName - $uriPrefix = 'PRJ-' - $urltemplate = 'https://company.issuetracker.com/browse/prj-' + $keyPrefix = 'PRJ-' + $urlTemplate = 'https://company.issuetracker.com/browse/prj-' $isNumericOnly = $true $newGitHubRepositoryAutolinkParms = @{ OrganizationName = $organizationName - UriPrefix = $uriPrefix - UrlTemplate = $urltemplate + KeyPrefix = $keyPrefix + UrlTemplate = $urlTemplate IsNumericOnly = $isNumericOnly } New-GitHubRepositoryAutolink @newGitHubRepositoryAutolinkParms | Out-Null $autoLinks = Get-GitHubRepositoryAutolink -OwnerName $organizationName -RepositoryName $repoName - $autolink = $autoLinks | Where-Object -Property AutolinkKeyPrefix -eq $uriPrefix + $autolink = $autoLinks | Where-Object -Property AutolinkKeyPrefix -eq $keyPrefix } It 'Should have the expected type and additional properties' { $team.PSObject.TypeNames[0] | Should -Be 'GitHub.RepositoryAutolink' - $team.AutolinkKeyPrefix | Should -Be $uriPrefix - $team.AutolinkUrlTemplate | Should -Be $urltemplate + $team.AutolinkKeyPrefix | Should -Be $keyPrefix + $team.AutolinkUrlTemplate | Should -Be $urlTemplate $team.AutolinkIsNumeric | Should -BeTrue $team.AutolinkId | Should -BeGreaterThan 0 - $team.UriPrefix | Should -Be $autolink.uriPrefix - $team.Urltemplate | Should -Be $autolink.urltemplate + $team.KeyPrefix | Should -Be $autolink.keyPrefix + $team.UrlTemplate | Should -Be $autolink.urlTemplate $team.AutolinkId | Should -Be $autolink.id $team.OrganizationName | Should -Be $organizationName } diff --git a/USAGE.md b/USAGE.md index dab998cd..c479003c 100644 --- a/USAGE.md +++ b/USAGE.md @@ -753,7 +753,7 @@ Get-GitHubRepositoryAutolink -OwnerName microsoft -RepositoryName PowerShellForG #### Create a new autolink ```powershell -New-GitHubRepositoryAutolink -OwnerName microsoft -RepositoryName PowerShellForGitHub -UriPrefix PRJ- -Urltemplate https://company.issuetracker.com/browse/prj- +New-GitHubRepositoryAutolink -OwnerName microsoft -RepositoryName PowerShellForGitHub -KeyPrefix 'PRJ-' -UrlTemplate 'https://company.issuetracker.com/browse/prj-' ``` #### Removing a repository autolink From cb51686742ac1088fee3d5b6c2d37f141de7fb36 Mon Sep 17 00:00:00 2001 From: Henk Meulekamp Date: Thu, 6 Oct 2022 21:47:20 +0200 Subject: [PATCH 13/26] Add notes mentioning required rights on autolink --- GitHubRepositoryAutolink.ps1 | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/GitHubRepositoryAutolink.ps1 b/GitHubRepositoryAutolink.ps1 index 84d95e75..a8ca51f7 100644 --- a/GitHubRepositoryAutolink.ps1 +++ b/GitHubRepositoryAutolink.ps1 @@ -61,6 +61,9 @@ filter Get-GitHubRepositoryAutolink Get-GitHubRepositoryAutolink -OwnerName microsoft -RepositoryName PowerShellForGitHub Gets all of the autolink references for the microsoft\PowerShellForGitHub repository. + + .NOTES + Information about autolinks are only available to repository administrators. #> [CmdletBinding(DefaultParameterSetName = 'Elements')] [OutputType({$script:GitHubRepositoryAutolinkTypeName})] @@ -183,6 +186,9 @@ filter New-GitHubRepositoryAutolink New-GitHubRepositoryAutolink -OwnerName microsoft -RepositoryName PowerShellForGitHub -OrganizationName OctoLabs -KeyPrefix 'PRJ-' -UrlTemplate 'https://company.issuetracker.com/browse/prj-' -IsNumericOnly Creates an autlink reference on this repository under the OctoLabs organization. + + .NOTES + Only users with admin access to the repository can create an autolink. #> [CmdletBinding( SupportsShouldProcess, @@ -330,6 +336,9 @@ filter Remove-GitHubRepositoryAutolink Removes the autolink reference with autolinkId 1 from the PowerShellForGitHub project. Will not prompt for confirmation, as -Force was specified. + + .NOTES + Only users with admin access to the repository can delete an autolink. #> [CmdletBinding( SupportsShouldProcess, From 7a56cd4c31ee291b5507225e86528d339526d0f0 Mon Sep 17 00:00:00 2001 From: Henk Meulekamp Date: Thu, 6 Oct 2022 21:50:12 +0200 Subject: [PATCH 14/26] Remove copy-past mistake; Sort --- GitHubRepositoryAutolink.ps1 | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/GitHubRepositoryAutolink.ps1 b/GitHubRepositoryAutolink.ps1 index a8ca51f7..d728d0a5 100644 --- a/GitHubRepositoryAutolink.ps1 +++ b/GitHubRepositoryAutolink.ps1 @@ -30,9 +30,6 @@ filter Get-GitHubRepositoryAutolink The OwnerName and RepositoryName will be extracted from here instead of needing to provide them individually. - .PARAMETER Sort - The sort order for results. - .PARAMETER AccessToken If provided, this will be used as the AccessToken for authentication with the REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated. @@ -82,9 +79,6 @@ filter Get-GitHubRepositoryAutolink [Alias('RepositoryUrl')] [string] $Uri, - [ValidateSet('Newest', 'Oldest', 'Stargazers')] - [string] $Sort = 'Newest', - [string] $AccessToken ) @@ -97,13 +91,8 @@ filter Get-GitHubRepositoryAutolink $telemetryProperties = @{ 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) - 'Sort' = $Sort } - $getParams = @( - "sort=$($Sort.ToLower())" - ) - $params = @{ 'UriFragment' = "repos/$OwnerName/$RepositoryName/autolinks`?" + ($getParams -join '&') 'Description' = "Getting all autolinks of $RepositoryName" From 483e6089739665244c8a6f1a40189ec1e5af65cd Mon Sep 17 00:00:00 2001 From: Henk Meulekamp Date: Thu, 6 Oct 2022 21:55:34 +0200 Subject: [PATCH 15/26] Better grammar --- GitHubRepositoryAutolink.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/GitHubRepositoryAutolink.ps1 b/GitHubRepositoryAutolink.ps1 index d728d0a5..09cd4dfa 100644 --- a/GitHubRepositoryAutolink.ps1 +++ b/GitHubRepositoryAutolink.ps1 @@ -108,10 +108,10 @@ filter New-GitHubRepositoryAutolink { <# .SYNOPSIS - Creates a new autolink on given repository on GitHub. + Creates a new autolink on given GitHub repository. .DESCRIPTION - Creates a new autolink on given repository on GitHub. + Creates a new autolink on given GitHub repository. The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub From 5b8488ea4576ceee6e0fc263b60e3054c54a6670 Mon Sep 17 00:00:00 2001 From: Henk Meulekamp Date: Thu, 6 Oct 2022 22:00:16 +0200 Subject: [PATCH 16/26] More textual improvements --- GitHubRepositoryAutolink.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/GitHubRepositoryAutolink.ps1 b/GitHubRepositoryAutolink.ps1 index 09cd4dfa..61199448 100644 --- a/GitHubRepositoryAutolink.ps1 +++ b/GitHubRepositoryAutolink.ps1 @@ -10,10 +10,10 @@ filter Get-GitHubRepositoryAutolink { <# .SYNOPSIS - Gets the list of autolinks of the specified repository on GitHub. + Gets the list of autolinks of the specified GitHub repository. .DESCRIPTION - Gets the list of autolinks of the specified repository on GitHub. + Gets the list of autolinks of the specified GitHub repository. The Git repo for this module can be found here: http://aka.ms/PowerShellForGitHub From f8fd5c9c7b7312b53f1eb6433a41ca177145b94f Mon Sep 17 00:00:00 2001 From: Henk Meulekamp Date: Thu, 6 Oct 2022 22:08:57 +0200 Subject: [PATCH 17/26] address IsNumeric vs IsNumericOnly --- GitHubRepositoryAutolink.ps1 | 9 +++++---- Tests/GitHubRepositoryAutolinks.tests.ps1 | 2 +- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/GitHubRepositoryAutolink.ps1 b/GitHubRepositoryAutolink.ps1 index 61199448..bedabe6a 100644 --- a/GitHubRepositoryAutolink.ps1 +++ b/GitHubRepositoryAutolink.ps1 @@ -207,8 +207,8 @@ filter New-GitHubRepositoryAutolink [Alias('AutolinkUrlTemplate')] [string] $UrlTemplate, - [Alias('AutlinkIsNumeric')] - [switch] $IsNumeric, + [Alias('AutlinkIsNumericOnly')] + [switch] $IsNumericOnly, [string] $AccessToken ) @@ -222,6 +222,7 @@ filter New-GitHubRepositoryAutolink $telemetryProperties = @{ 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) + 'IsNumericOnly' = $IsNumericOnly.IsPresent() } $hashBody = @{ @@ -230,7 +231,7 @@ filter New-GitHubRepositoryAutolink 'is_alphanumeric' = $true } - if ($PSBoundParameters.ContainsKey('IsNumeric')) { $hashBody['is_alphanumeric'] = $false } + if ($PSBoundParameters.ContainsKey('IsNumericOnly')) { $hashBody['is_alphanumeric'] = $false } if (-not $PSCmdlet.ShouldProcess($KeyPrefix, 'Create Repository Autolink')) { @@ -455,7 +456,7 @@ filter Add-GitHubRepositoryAutolinkAdditionalProperties if(!$item.is_alphanumeric) { - Add-Member -InputObject $item -Name 'AutolinkIsNumeric' -Value $true -MemberType NoteProperty -Force + Add-Member -InputObject $item -Name 'AutolinkIsNumericOnly' -Value $true -MemberType NoteProperty -Force } } diff --git a/Tests/GitHubRepositoryAutolinks.tests.ps1 b/Tests/GitHubRepositoryAutolinks.tests.ps1 index e87b10f7..e4efa4fe 100644 --- a/Tests/GitHubRepositoryAutolinks.tests.ps1 +++ b/Tests/GitHubRepositoryAutolinks.tests.ps1 @@ -58,7 +58,7 @@ try $team.PSObject.TypeNames[0] | Should -Be 'GitHub.RepositoryAutolink' $team.AutolinkKeyPrefix | Should -Be $keyPrefix $team.AutolinkUrlTemplate | Should -Be $urlTemplate - $team.AutolinkIsNumeric | Should -BeTrue + $team.AutolinkIsNumericOnly | Should -BeTrue $team.AutolinkId | Should -BeGreaterThan 0 $team.KeyPrefix | Should -Be $autolink.keyPrefix $team.UrlTemplate | Should -Be $autolink.urlTemplate From 4541c6be98e6a8e266749418219017082125e923 Mon Sep 17 00:00:00 2001 From: Henk Meulekamp Date: Thu, 6 Oct 2022 22:13:05 +0200 Subject: [PATCH 18/26] more efficient way to set value --- GitHubRepositoryAutolink.ps1 | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/GitHubRepositoryAutolink.ps1 b/GitHubRepositoryAutolink.ps1 index bedabe6a..e3006ad2 100644 --- a/GitHubRepositoryAutolink.ps1 +++ b/GitHubRepositoryAutolink.ps1 @@ -228,11 +228,9 @@ filter New-GitHubRepositoryAutolink $hashBody = @{ 'key_prefix' = $KeyPrefix 'url_template' = $UrlTemplate - 'is_alphanumeric' = $true + 'is_alphanumeric' = (-not $IsNumericOnly.IsPresent()) } - if ($PSBoundParameters.ContainsKey('IsNumericOnly')) { $hashBody['is_alphanumeric'] = $false } - if (-not $PSCmdlet.ShouldProcess($KeyPrefix, 'Create Repository Autolink')) { return From 0a067315057fafcce0ac5c68958e7e52e8e7ef28 Mon Sep 17 00:00:00 2001 From: Henk Meulekamp Date: Thu, 6 Oct 2022 22:35:57 +0200 Subject: [PATCH 19/26] Fix ValueFromPipelineByPropertyName Issues - requires testing - Test cleanup --- GitHubRepositoryAutolink.ps1 | 16 +++++++++------- Tests/GitHubRepositoryAutolinks.tests.ps1 | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/GitHubRepositoryAutolink.ps1 b/GitHubRepositoryAutolink.ps1 index e3006ad2..ab74fbfb 100644 --- a/GitHubRepositoryAutolink.ps1 +++ b/GitHubRepositoryAutolink.ps1 @@ -197,17 +197,22 @@ filter New-GitHubRepositoryAutolink [Alias('RepositoryUrl')] [string] $Uri, - [Parameter(Mandatory)] + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName)] #todo [ValidateScript({if ($_ -match '^#?[a-zA-Z0-9.-_+=:\/#]$') { $true } else { throw "Reference prefix must only contain letters, numbers, or .-_+=:/#." }})] [Alias('AutolinkKeyPrefix')] [string] $KeyPrefix, - [Parameter(Mandatory)] + [Parameter( + Mandatory, + ValueFromPipelineByPropertyName)] #todo [ValidateScript({if ($_ -match '^#?[]$') { $true } else { throw "Target URL is missing a token." }})] [Alias('AutolinkUrlTemplate')] [string] $UrlTemplate, - [Alias('AutlinkIsNumericOnly')] + [Parameter( + ValueFromPipelineByPropertyName)] [switch] $IsNumericOnly, [string] $AccessToken @@ -452,10 +457,7 @@ filter Add-GitHubRepositoryAutolinkAdditionalProperties Add-Member -InputObject $item -Name 'AutolinkKeyPrefix' -Value $item.key_prefix -MemberType NoteProperty -Force Add-Member -InputObject $item -Name 'AutolinkUrlTemplate' -Value $item.url_template -MemberType NoteProperty -Force - if(!$item.is_alphanumeric) - { - Add-Member -InputObject $item -Name 'AutolinkIsNumericOnly' -Value $true -MemberType NoteProperty -Force - } + Add-Member -InputObject $item -Name 'IsNumericOnly' -Value (-not $item.is_alphanumeric) -MemberType NoteProperty -Force } Write-Output $item diff --git a/Tests/GitHubRepositoryAutolinks.tests.ps1 b/Tests/GitHubRepositoryAutolinks.tests.ps1 index e4efa4fe..8c7fbfb9 100644 --- a/Tests/GitHubRepositoryAutolinks.tests.ps1 +++ b/Tests/GitHubRepositoryAutolinks.tests.ps1 @@ -58,7 +58,7 @@ try $team.PSObject.TypeNames[0] | Should -Be 'GitHub.RepositoryAutolink' $team.AutolinkKeyPrefix | Should -Be $keyPrefix $team.AutolinkUrlTemplate | Should -Be $urlTemplate - $team.AutolinkIsNumericOnly | Should -BeTrue + $team.IsNumericOnly | Should -BeTrue $team.AutolinkId | Should -BeGreaterThan 0 $team.KeyPrefix | Should -Be $autolink.keyPrefix $team.UrlTemplate | Should -Be $autolink.urlTemplate From b044f1ebef60772fc313234ae3c65a5def42def5 Mon Sep 17 00:00:00 2001 From: Henk Meulekamp Date: Thu, 6 Oct 2022 22:37:08 +0200 Subject: [PATCH 20/26] Fix ValueFromPipelineByPropertyName 2 --- GitHubRepositoryAutolink.ps1 | 6 ++---- Tests/GitHubRepositoryAutolinks.tests.ps1 | 26 ++++++++--------------- 2 files changed, 11 insertions(+), 21 deletions(-) diff --git a/GitHubRepositoryAutolink.ps1 b/GitHubRepositoryAutolink.ps1 index ab74fbfb..d9bd77c8 100644 --- a/GitHubRepositoryAutolink.ps1 +++ b/GitHubRepositoryAutolink.ps1 @@ -201,14 +201,12 @@ filter New-GitHubRepositoryAutolink Mandatory, ValueFromPipelineByPropertyName)] #todo [ValidateScript({if ($_ -match '^#?[a-zA-Z0-9.-_+=:\/#]$') { $true } else { throw "Reference prefix must only contain letters, numbers, or .-_+=:/#." }})] - [Alias('AutolinkKeyPrefix')] [string] $KeyPrefix, [Parameter( Mandatory, ValueFromPipelineByPropertyName)] #todo [ValidateScript({if ($_ -match '^#?[]$') { $true } else { throw "Target URL is missing a token." }})] - [Alias('AutolinkUrlTemplate')] [string] $UrlTemplate, [Parameter( @@ -454,8 +452,8 @@ filter Add-GitHubRepositoryAutolinkAdditionalProperties Add-Member -InputObject $item -Name 'AutolinkId' -Value $item.id -MemberType NoteProperty -Force } - Add-Member -InputObject $item -Name 'AutolinkKeyPrefix' -Value $item.key_prefix -MemberType NoteProperty -Force - Add-Member -InputObject $item -Name 'AutolinkUrlTemplate' -Value $item.url_template -MemberType NoteProperty -Force + Add-Member -InputObject $item -Name 'KeyPrefix' -Value $item.key_prefix -MemberType NoteProperty -Force + Add-Member -InputObject $item -Name 'UrlTemplate' -Value $item.url_template -MemberType NoteProperty -Force Add-Member -InputObject $item -Name 'IsNumericOnly' -Value (-not $item.is_alphanumeric) -MemberType NoteProperty -Force } diff --git a/Tests/GitHubRepositoryAutolinks.tests.ps1 b/Tests/GitHubRepositoryAutolinks.tests.ps1 index 8c7fbfb9..3f77f6fe 100644 --- a/Tests/GitHubRepositoryAutolinks.tests.ps1 +++ b/Tests/GitHubRepositoryAutolinks.tests.ps1 @@ -17,14 +17,6 @@ $moduleRootPath = Split-Path -Path $PSScriptRoot -Parent try { - # Define Script-scoped, readonly, hidden variables. - @{ - defaultRepoDesc = "This is a description." - defaultRepoHomePage = "https://www.microsoft.com/" - defaultRepoTopic = "microsoft" - }.GetEnumerator() | ForEach-Object { - Set-Variable -Force -Scope Script -Option ReadOnly -Visibility Private -Name $_.Key -Value $_.Value - } Describe 'GitHubRepositoryAutolink\Get-GitHubRepositoryAutolink' { BeforeAll { @@ -55,15 +47,15 @@ try } It 'Should have the expected type and additional properties' { - $team.PSObject.TypeNames[0] | Should -Be 'GitHub.RepositoryAutolink' - $team.AutolinkKeyPrefix | Should -Be $keyPrefix - $team.AutolinkUrlTemplate | Should -Be $urlTemplate - $team.IsNumericOnly | Should -BeTrue - $team.AutolinkId | Should -BeGreaterThan 0 - $team.KeyPrefix | Should -Be $autolink.keyPrefix - $team.UrlTemplate | Should -Be $autolink.urlTemplate - $team.AutolinkId | Should -Be $autolink.id - $team.OrganizationName | Should -Be $organizationName + $autolink.PSObject.TypeNames[0] | Should -Be 'GitHub.RepositoryAutolink' + $autolink.KeyPrefix | Should -Be $keyPrefix + $autolink.UrlTemplate | Should -Be $urlTemplate + $autolink.IsNumericOnly | Should -BeTrue + $autolink.AutolinkId | Should -BeGreaterThan 0 + $autolink.KeyPrefix | Should -Be $autolink.keyPrefix + $autolink.UrlTemplate | Should -Be $autolink.urlTemplate + $autolink.AutolinkId | Should -Be $autolink.id + $autolink.OrganizationName | Should -Be $organizationName } AfterAll { From 797fb82640efb3507111c1c8e357079b16dc4421 Mon Sep 17 00:00:00 2001 From: Henk Meulekamp Date: Thu, 6 Oct 2022 22:45:58 +0200 Subject: [PATCH 21/26] two more cleanups of copy/past code --- GitHubRepositoryAutolink.ps1 | 6 ------ Tests/GitHubRepositoryAutolinks.tests.ps1 | 1 - 2 files changed, 7 deletions(-) diff --git a/GitHubRepositoryAutolink.ps1 b/GitHubRepositoryAutolink.ps1 index d9bd77c8..94af3fa0 100644 --- a/GitHubRepositoryAutolink.ps1 +++ b/GitHubRepositoryAutolink.ps1 @@ -441,12 +441,6 @@ filter Add-GitHubRepositoryAutolinkAdditionalProperties if (-not (Get-GitHubConfiguration -Name DisablePipelineSupport)) { - if (-not [System.String]::IsNullOrEmpty($item.url)) - { - $elements = Split-GitHubUri -Uri $item.url - $RepositoryUrl = Join-GitHubUri @elements - } - if ($null -ne $item.id) { Add-Member -InputObject $item -Name 'AutolinkId' -Value $item.id -MemberType NoteProperty -Force diff --git a/Tests/GitHubRepositoryAutolinks.tests.ps1 b/Tests/GitHubRepositoryAutolinks.tests.ps1 index 3f77f6fe..89f9aef9 100644 --- a/Tests/GitHubRepositoryAutolinks.tests.ps1 +++ b/Tests/GitHubRepositoryAutolinks.tests.ps1 @@ -55,7 +55,6 @@ try $autolink.KeyPrefix | Should -Be $autolink.keyPrefix $autolink.UrlTemplate | Should -Be $autolink.urlTemplate $autolink.AutolinkId | Should -Be $autolink.id - $autolink.OrganizationName | Should -Be $organizationName } AfterAll { From 1b99b8123fc9ae71748e9c934bfbd978cefefd66 Mon Sep 17 00:00:00 2001 From: Henk Meulekamp Date: Thu, 6 Oct 2022 23:06:54 +0200 Subject: [PATCH 22/26] Add option to get asingle autolink by autolinkId --- GitHubRepositoryAutolink.ps1 | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/GitHubRepositoryAutolink.ps1 b/GitHubRepositoryAutolink.ps1 index 94af3fa0..fdfeb643 100644 --- a/GitHubRepositoryAutolink.ps1 +++ b/GitHubRepositoryAutolink.ps1 @@ -30,6 +30,9 @@ filter Get-GitHubRepositoryAutolink The OwnerName and RepositoryName will be extracted from here instead of needing to provide them individually. + .PARAMETER AutolinkId + Optional, the unique identifier of the autolink to be retrieved + .PARAMETER AccessToken If provided, this will be used as the AccessToken for authentication with the REST Api. Otherwise, will attempt to use the configured value or will run unauthenticated. @@ -59,6 +62,11 @@ filter Get-GitHubRepositoryAutolink Gets all of the autolink references for the microsoft\PowerShellForGitHub repository. + .EXAMPLE + Get-GitHubRepositoryAutolink -OwnerName microsoft -RepositoryName PowerShellForGitHub -AutolinkId 42 + + Gets autolink reference with autolinkId 42 from the microsoft\PowerShellForGitHub repository. + .NOTES Information about autolinks are only available to repository administrators. #> @@ -79,6 +87,12 @@ filter Get-GitHubRepositoryAutolink [Alias('RepositoryUrl')] [string] $Uri, + [Parameter( + ValueFromPipeline, + ValueFromPipelineByPropertyName)] + [ValidateNotNullOrEmpty()] + [int64] $AutolinkId, + [string] $AccessToken ) @@ -93,8 +107,15 @@ filter Get-GitHubRepositoryAutolink 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) } + $uriFragment = [String]::Empty + if ($AutolinkId.IsPresent()) { + $uriFragment = "repos/$OwnerName/$RepositoryName/autolinks/$AutolinkId" + } else { + $uriFragment = "repos/$OwnerName/$RepositoryName/autolinks" + } + $params = @{ - 'UriFragment' = "repos/$OwnerName/$RepositoryName/autolinks`?" + ($getParams -join '&') + 'UriFragment' = $uriFragment + "?" + ($getParams -join '&') 'Description' = "Getting all autolinks of $RepositoryName" 'AccessToken' = $AccessToken 'TelemetryEventName' = $MyInvocation.MyCommand.Name @@ -429,8 +450,6 @@ filter Add-GitHubRepositoryAutolinkAdditionalProperties [AllowEmptyCollection()] [PSCustomObject[]] $InputObject, - [string] $RepositoryUrl, - [ValidateNotNullOrEmpty()] [string] $TypeName = $script:GitHubRepositoryAutolinkTypeName ) From d32b5a7434fa2dc8572b09f8a5cca6c27294c2f8 Mon Sep 17 00:00:00 2001 From: Henk Meulekamp Date: Thu, 6 Oct 2022 23:08:19 +0200 Subject: [PATCH 23/26] Remove not sused param --- GitHubRepositoryAutolink.ps1 | 7 ------- 1 file changed, 7 deletions(-) diff --git a/GitHubRepositoryAutolink.ps1 b/GitHubRepositoryAutolink.ps1 index fdfeb643..02b53338 100644 --- a/GitHubRepositoryAutolink.ps1 +++ b/GitHubRepositoryAutolink.ps1 @@ -211,13 +211,6 @@ filter New-GitHubRepositoryAutolink [Parameter(ParameterSetName='Elements')] [string] $RepositoryName, - [Parameter( - Mandatory, - ValueFromPipelineByPropertyName, - ParameterSetName='Uri')] - [Alias('RepositoryUrl')] - [string] $Uri, - [Parameter( Mandatory, ValueFromPipelineByPropertyName)] From 97785325c8f352526e7903d90094224fc61cf289 Mon Sep 17 00:00:00 2001 From: Henk Meulekamp Date: Thu, 6 Oct 2022 23:19:48 +0200 Subject: [PATCH 24/26] one fix IsPresent only on switch --- GitHubRepositoryAutolink.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GitHubRepositoryAutolink.ps1 b/GitHubRepositoryAutolink.ps1 index 02b53338..5b9af239 100644 --- a/GitHubRepositoryAutolink.ps1 +++ b/GitHubRepositoryAutolink.ps1 @@ -108,7 +108,7 @@ filter Get-GitHubRepositoryAutolink } $uriFragment = [String]::Empty - if ($AutolinkId.IsPresent()) { + if ($AutolinkId -gt 0) { $uriFragment = "repos/$OwnerName/$RepositoryName/autolinks/$AutolinkId" } else { $uriFragment = "repos/$OwnerName/$RepositoryName/autolinks" From d467f6386867f1d18066946efa9832c54351fa62 Mon Sep 17 00:00:00 2001 From: Henk Meulekamp Date: Thu, 6 Oct 2022 23:22:53 +0200 Subject: [PATCH 25/26] fis isPresent --- GitHubRepositoryAutolink.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/GitHubRepositoryAutolink.ps1 b/GitHubRepositoryAutolink.ps1 index 5b9af239..e54bb071 100644 --- a/GitHubRepositoryAutolink.ps1 +++ b/GitHubRepositoryAutolink.ps1 @@ -239,13 +239,13 @@ filter New-GitHubRepositoryAutolink $telemetryProperties = @{ 'OwnerName' = (Get-PiiSafeString -PlainText $OwnerName) 'RepositoryName' = (Get-PiiSafeString -PlainText $RepositoryName) - 'IsNumericOnly' = $IsNumericOnly.IsPresent() + 'IsNumericOnly' = $IsNumericOnly.IsPresent } $hashBody = @{ 'key_prefix' = $KeyPrefix 'url_template' = $UrlTemplate - 'is_alphanumeric' = (-not $IsNumericOnly.IsPresent()) + 'is_alphanumeric' = (-not $IsNumericOnly.IsPresent) } if (-not $PSCmdlet.ShouldProcess($KeyPrefix, 'Create Repository Autolink')) From f960cf567a534b8c78b4f56a77433a92c17f82ea Mon Sep 17 00:00:00 2001 From: Henk Meulekamp Date: Tue, 11 Oct 2022 22:28:09 +0200 Subject: [PATCH 26/26] fix todo on param validation - num block is important and requires validation --- GitHubRepositoryAutolink.ps1 | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/GitHubRepositoryAutolink.ps1 b/GitHubRepositoryAutolink.ps1 index e54bb071..5ae21158 100644 --- a/GitHubRepositoryAutolink.ps1 +++ b/GitHubRepositoryAutolink.ps1 @@ -214,13 +214,12 @@ filter New-GitHubRepositoryAutolink [Parameter( Mandatory, ValueFromPipelineByPropertyName)] - #todo [ValidateScript({if ($_ -match '^#?[a-zA-Z0-9.-_+=:\/#]$') { $true } else { throw "Reference prefix must only contain letters, numbers, or .-_+=:/#." }})] [string] $KeyPrefix, [Parameter( Mandatory, ValueFromPipelineByPropertyName)] - #todo [ValidateScript({if ($_ -match '^#?[]$') { $true } else { throw "Target URL is missing a token." }})] + [ValidateScript({if ($_ -match '^https?://([^\s,]+)') { $true } else { throw "Target URL invalid or is missing a token." }})] [string] $UrlTemplate, [Parameter(