Skip to content

Commit

Permalink
See CHANGELOG.md, pipeline changes & added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
codaamok committed Feb 26, 2022
1 parent eb9452b commit 3b133a2
Show file tree
Hide file tree
Showing 20 changed files with 574 additions and 39 deletions.
15 changes: 14 additions & 1 deletion .github/workflows/build.yml → .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: "Create GitHub release and publish to the PowerShell Gallery"
name: "CI/CD Pipeline"

on:
push:
Expand All @@ -7,6 +7,8 @@ on:
branches:
- main
- master
schedule:
- cron: '11 7 * * *'
workflow_dispatch:

jobs:
Expand Down Expand Up @@ -51,17 +53,24 @@ jobs:
Invoke-Build -File "custom.build.ps1" @Params -Task PostBuild
shell: pwsh

- name: Pester Tests
if: hashFiles('tests/invoke.tests.ps1') != ''
run: pwsh -File "tests/invoke.tests.ps1"

- name: Custom pre-release tasks
if: ${{ github.event_name == 'push' || github.event_name == 'workflow_dispatch' }}
run: Invoke-Build -File "custom.build.ps1" -ModuleName $env:GH_PROJECTNAME -Author $env:GH_USERNAME -Version $env:GitVersion_SemVer -NewRelease $true -Task PreRelease
shell: pwsh

- name: Publish to PowerShell Gallery
if: ${{ github.event_name == 'push' || github.event_name == 'workflow_dispatch' }}
run: Invoke-Build -File "invoke.build.ps1" -ModuleName $env:GH_PROJECTNAME -Task "PublishModule"
shell: pwsh
env:
PSGALLERY_API_KEY: ${{ secrets.PSGALLERY_API_KEY }}

- name: Create release
if: ${{ github.event_name == 'push' || github.event_name == 'workflow_dispatch' }}
id: create_release
uses: actions/create-release@v1
env:
Expand All @@ -74,6 +83,7 @@ jobs:
prerelease: false

- name: Upload release asset
if: ${{ github.event_name == 'push' || github.event_name == 'workflow_dispatch' }}
id: upload_release_asset
uses: actions/upload-release-asset@v1
env:
Expand All @@ -85,17 +95,20 @@ jobs:
asset_content_type: application/zip

- name: Commit CHANGELOG.md and module manifest
if: ${{ github.event_name == 'push' || github.event_name == 'workflow_dispatch' }}
run: |
git config --global user.email "[email protected]"
git config --global user.name "GitHub Action"
git add CHANGELOG.md src/${GH_PROJECTNAME}.psd1 docs
git commit -m "Released ${{ env.GitVersion_SemVer }}"
- name: Push commit
if: ${{ github.event_name == 'push' || github.event_name == 'workflow_dispatch' }}
uses: ad-m/github-push-action@master
with:
github_token: ${{ secrets.GITHUB_TOKEN }}

- name: Custom post-release tasks
if: ${{ github.event_name == 'push' || github.event_name == 'workflow_dispatch' }}
run: Invoke-Build -File "custom.build.ps1" -ModuleName $env:GH_PROJECTNAME -Author $env:GH_USERNAME -Version $env:GitVersion_SemVer -NewRelease $true -Task PostRelease
shell: pwsh
8 changes: 4 additions & 4 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"label": "Tests",
"type": "process",
"command": "pwsh",
"args": ["-noprofile","-command","Import-Module ${input:CodeType}; $env:ShlinkServer = '${input:ShlinkServer}'; $env:ShlinkAPIKey = '${input:ShlinkAPIKey}' | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString; Invoke-Pester -Path './tests/*' -Output Detailed"],
"args": ["-noprofile", "-command", "$env:ShlinkServer = '${input:ShlinkServer}'; $env:ShlinkAPIKey = '${input:ShlinkAPIKey}' | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString; ./tests/invoke.tests.ps1"],
"group": {
"kind": "test",
"isDefault": true
Expand All @@ -63,13 +63,13 @@
"description": "Please enter Shlink server name",
"id": "ShlinkServer",
"type": "promptString",
"default": "https://acook.io"
"default": "http://psshlink.codaamok"
},
{
"description": "Please enter Shlink API key",
"id": "ShlinkAPIKey",
"type": "promptString",
"default": "",
"default": "18c65bc9-e4fb-449d-b3e0-c6427cbac735",
"password": true,
},
{
Expand All @@ -80,7 +80,7 @@
"./src/PSShlink.psd1",
"./build/PSShlink/PSShlink.psd1"
],
"default": "./src/PSShlink.psd1"
"default": "./build/PSShlink/PSShlink.psd1"
},
]
}
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- New parameter `-PassThru` added to function `Save-ShlinkUrlQrCode` which will return a `System.IO.FileSystemInfo` object for each QR code image file it creates when used. Default behaviour has not changed (return no object if successful).

### Changed
- Renamed parameter `-Tags` to be `-Tag` for function `Remove-ShlinkTag`

## [0.9.1] - 2022-02-14
### Fixed
Expand Down
6 changes: 1 addition & 5 deletions src/PSShlink.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,7 @@ FormatsToProcess = 'PSShlink.Format.ps1xml'
# NestedModules = @()

# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
FunctionsToExport = 'Get-ShlinkDomains', 'Get-ShlinkServer', 'Get-ShlinkTags',
'Get-ShlinkUrl', 'Get-ShlinkVisits', 'Get-ShlinkVisitsOrphan',
'New-ShlinkTag', 'New-ShlinkUrl', 'Remove-ShlinkTag',
'Remove-ShlinkUrl', 'Save-ShlinkUrlQrCode',
'Set-ShlinkDomainRedirects', 'Set-ShlinkTag', 'Set-ShlinkUrl'
FunctionsToExport = '*'

# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
CmdletsToExport = @()
Expand Down
18 changes: 9 additions & 9 deletions src/Public/Remove-ShlinkTag.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function Remove-ShlinkTag {
Remove a tag from an existing Shlink server.
.DESCRIPTION
Remove a tag from an existing Shlink server.
.PARAMETER Tags
.PARAMETER Tag
Name(s) of the tag(s) you want to remove.
.PARAMETER ShlinkServer
The URL of your Shlink server (including schema). For example "https://example.com".
Expand Down Expand Up @@ -38,7 +38,7 @@ function Remove-ShlinkTag {
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = "High")]
param (
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
[String[]]$Tags,
[String[]]$Tag,

[Parameter()]
[String]$ShlinkServer,
Expand All @@ -63,18 +63,18 @@ function Remove-ShlinkTag {
process {
$QueryString = [System.Web.HttpUtility]::ParseQueryString('')

foreach ($Tag in $Tags) {
if ($AllTags.tag -notcontains $Tag) {
foreach ($_Tag in $Tag) {
if ($AllTags.tag -notcontains $_Tag) {
$WriteErrorSplat = @{
Message = "Tag '{0}' does not exist on Shlink server '{1}'" -f $Tag, $Script:ShlinkServer
Message = "Tag '{0}' does not exist on Shlink server '{1}'" -f $_Tag, $Script:ShlinkServer
Category = "ObjectNotFound"
TargetObject = $Tag
TargetObject = $_Tag
}
Write-Error @WriteErrorSplat
continue
}
else {
$QueryString.Add("tags[]", $Tag)
$QueryString.Add("tags[]", $_Tag)
}

$Params = @{
Expand All @@ -85,9 +85,9 @@ function Remove-ShlinkTag {
}

if ($PSCmdlet.ShouldProcess(
("Would delete tag '{0}' from Shlink server '{1}'" -f $Tag, $Script:ShlinkServer),
("Would delete tag '{0}' from Shlink server '{1}'" -f $_Tag, $Script:ShlinkServer),
"Are you sure you want to continue?",
("Removing tag '{0}' from Shlink server '{1}'" -f $Tag, $Script:ShlinkServer))) {
("Removing tag '{0}' from Shlink server '{1}'" -f $_Tag, $Script:ShlinkServer))) {
try {
InvokeShlinkRestMethod @Params
}
Expand Down
12 changes: 10 additions & 2 deletions src/Public/Save-ShlinkUrlQrCode.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ function Save-ShlinkUrlQrCode {
.PARAMETER ShlinkApiKey
A SecureString object of your Shlink server's API key.
It is not required to use this parameter for every use of this function. When it is used once for any of the functions in the PSShlink module, its value is retained throughout the life of the PowerShell session and its value is only accessible within the module's scope.
.PARAMETER PassThru
Returns a System.IO.FileSystemInfo object of each QR image file it creates
.EXAMPLE
PS C:\> Save-ShlinkUrlQrCode -ShortCode "profile" -Domain "example.com" -Size 1000 -Format svg -Path "C:\temp"
Expand All @@ -51,7 +53,7 @@ function Save-ShlinkUrlQrCode {
Expects PSObjects with PSTypeName of 'PSTypeName', typically from Get-ShlinkUrl.
.OUTPUTS
System.Management.Automation.PSObject
System.IO.FileSystemInfo
#>
[CmdletBinding()]
param (
Expand Down Expand Up @@ -89,7 +91,10 @@ function Save-ShlinkUrlQrCode {
[String]$ShlinkServer,

[Parameter(ParameterSetName="SpecifyProperties")]
[SecureString]$ShlinkApiKey
[SecureString]$ShlinkApiKey,

[Parameter()]
[Switch]$PassThru
)
begin {
$QueryString = [System.Web.HttpUtility]::ParseQueryString('')
Expand Down Expand Up @@ -192,6 +197,9 @@ function Save-ShlinkUrlQrCode {

try {
Set-Content @Params
if ($PassThru) {
Get-Item $FileName
}
}
catch {
Write-Error -ErrorRecord $_
Expand Down
24 changes: 24 additions & 0 deletions tests/Public/Get-ShlinkDomains.Acceptance.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
BeforeAll {
# Always use built code if running in a pipeline
if ($env:USER -eq 'runner') {
Import-Module "$PSScriptRoot/../../build/PSShlink/PSShlink.psd1" -Force
}
# Check if module is already imported, as it can be via VSCode task where you can choose what code base to test
# and you might not want to cloober it with the non-built code
elseif (-not (Get-Module PSShlink)) {
Import-Module "$PSScriptRoot/../../src/PSShlink.psd1" -Force
}
}

Describe "Get-ShlinkDomains" {
It "Returns domains configured with Shlink" {
$Params = @{
ShlinkServer = $env:ShlinkServer
ShlinkApiKey = $env:ShlinkAPIKey | ConvertTo-SecureString
ErrorAction = 'Stop'
}
$Object = Get-ShlinkDomains @Params
$Object.data.domain | Should -Be 'psshlink.codaamok'
$Object.data.isDefault | Should -BeTrue
}
}
23 changes: 23 additions & 0 deletions tests/Public/Get-ShlinkServer.Acceptance.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
BeforeAll {
# Always use built code if running in a pipeline
if ($env:USER -eq 'runner') {
Import-Module "$PSScriptRoot/../../build/PSShlink/PSShlink.psd1" -Force
}
# Check if module is already imported, as it can be via VSCode task where you can choose what code base to test
# and you might not want to cloober it with the non-built code
elseif (-not (Get-Module PSShlink)) {
Import-Module "$PSScriptRoot/../../src/PSShlink.psd1" -Force
}
}

Describe "Get-ShlinkServer" {
It "Returns Shlink instance information" {
$Params = @{
ShlinkServer = $env:ShlinkServer
ErrorAction = 'Stop'
}
$Object = Get-ShlinkServer @Params
$Object.status | Should -Be 'Pass'
[System.Version]$Object.version | Should -BeOfType [System.Version]
}
}
35 changes: 35 additions & 0 deletions tests/Public/Get-ShlinkTags.Acceptance.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
BeforeAll {
# Always use built code if running in a pipeline
if ($env:USER -eq 'runner') {
Import-Module "$PSScriptRoot/../../build/PSShlink/PSShlink.psd1" -Force
}
# Check if module is already imported, as it can be via VSCode task where you can choose what code base to test
# and you might not want to cloober it with the non-built code
elseif (-not (Get-Module PSShlink)) {
Import-Module "$PSScriptRoot/../../src/PSShlink.psd1" -Force
}
}

Describe "Get-ShlinkTags" {
It "Returns all tags on the Shlink instance" {
$Params = @{
ShlinkServer = $env:ShlinkServer
ShlinkApiKey = $env:ShlinkAPIKey | ConvertTo-SecureString
ErrorAction = 'Stop'
}
$Object = Get-ShlinkTags @Params
$Object.tag | Should -Be 'psshlinktag1', 'psshlinktag2'
$Object.shortUrlsCount | Should -Be '1','1'
}

It "Returns tags on Shlink instance using search term '<_>'" -ForEach 1,2 {
$Params = @{
SearchTerm = $_
ShlinkServer = $env:ShlinkServer
ShlinkApiKey = $env:ShlinkAPIKey | ConvertTo-SecureString
ErrorAction = 'Stop'
}
$Object = Get-ShlinkTags @Params
$Object.tag | Should -Be ('psshlinktag{0}' -f $_)
}
}
30 changes: 30 additions & 0 deletions tests/Public/Get-ShlinkUrl.Acceptance.Tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
BeforeAll {
# Always use built code if running in a pipeline
if ($env:USER -eq 'runner') {
Import-Module "$PSScriptRoot/../../build/PSShlink/PSShlink.psd1" -Force
}
# Check if module is already imported, as it can be via VSCode task where you can choose what code base to test
# and you might not want to cloober it with the non-built code
elseif (-not (Get-Module PSShlink)) {
Import-Module "$PSScriptRoot/../../src/PSShlink.psd1" -Force
}
}

Describe "Get-ShlinkUrl" {
It "Retrieve 'PSShlink-Test' short URL <type>" -ForEach @(
@{ Type = 'exactly'; Parameter = 'ShortCode'; Value = 'PSShlink-Test' }
@{ Type = 'vaguely'; Parameter = 'SearchTerm'; Value = 'test' }
) {
$Params = @{
$Parameter = $Value
ShlinkServer = $env:ShlinkServer
ShlinkApiKey = $env:ShlinkAPIKey | ConvertTo-SecureString
ErrorAction = 'Stop'
}
$Object = Get-ShlinkUrl @Params
$Object.shortCode | Should -Be 'PSShlink-Test'
$Object.shortUrl | Should -Be ('{0}/PSShlink-Test' -f $env:ShlinkServer)
$Object.tags | Should -Be 'PSShlinkTag1', 'PSShlinkTag2'
$Object.longUrl | Should -Be 'https://google.co.uk'
}
}
Loading

0 comments on commit 3b133a2

Please sign in to comment.