Skip to content
This repository was archived by the owner on Jul 19, 2024. It is now read-only.

Commit 962244c

Browse files
Add scripts to handle PR flow
Logic to point to the last commit of branch being reviewed in PR Set correct requirements psake and pester Use deploy instead of stages Only deploy on master when tagged Small improvement to debug logging Change the name of module so we can test it sneakily
1 parent 1234382 commit 962244c

10 files changed

+102
-145
lines changed

Diff for: .travis.yml

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
sudo: required
2+
3+
language: csharp
4+
dotnet: 2.1
5+
mono: none
6+
7+
services:
8+
- docker
9+
10+
dist: trusty
11+
addons:
12+
apt:
13+
sources:
14+
- sourceline: "deb [arch=amd64] https://packages.microsoft.com/ubuntu/14.04/prod trusty main"
15+
key_url: "https://packages.microsoft.com/keys/microsoft.asc"
16+
packages:
17+
- powershell
18+
19+
before_install:
20+
- chmod ugo+x ./scripts/setup_repo.sh && ./scripts/setup_repo.sh
21+
- sudo apt-get install -y powershell
22+
23+
install:
24+
- pwsh -f "./Install-Prerequisites.ps1"
25+
- export GitVersion_Version=$(docker run --rm -v "$(pwd):/repo" gittools/gitversion:5.0.0-linux-debian-9-netcoreapp2.2 /repo -output json -showvariable majorminorpatch)
26+
- "echo \"GitVersion says: ${GitVersion_Version} \""
27+
28+
script:
29+
- pwsh -c "Invoke-Psake -buildFile Build.ps1 -taskList build,test"
30+
31+
deploy:
32+
provider: script
33+
script: pwsh -c "Invoke-Psake -buildFile Build.ps1 -taskList build,test,publish"
34+
on:
35+
tags: true
36+
branch: master

Diff for: Build.ps1

+6-110
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,3 @@
1-
# This is a PSake script that supports the following tasks:
2-
# clean, build, test and publish. The default task is build.
3-
#
4-
# The publish task uses the Publish-Module command to publish
5-
# to either the PowerShell Gallery (the default) or you can change
6-
# the $Repository property to the name of an alternate repository.
7-
#
8-
# The test task invokes Pester to run any Pester tests in your
9-
# workspace folder. Name your test scripts <TestName>.Tests.ps1
10-
# and Pester will find and run the tests contained in the files.
11-
#
12-
# You can run this build script directly using the invoke-psake
13-
# command which will execute the build task. This task "builds"
14-
# a temporary folder from which the module can be published.
15-
#
16-
# PS C:\> invoke-psake build.ps1
17-
#
18-
# You can run your Pester tests (if any) by running the following command.
19-
#
20-
# PS C:\> invoke-psake build.ps1 -taskList test
21-
#
22-
# You can execute the publish task with the following command. Note that
23-
# the publish task will run the test task first. The Pester tests must pass
24-
# before the publish task will run. The first time you run the publish
25-
# command, you will be prompted to enter your PowerShell Gallery NuGetApiKey.
26-
# After entering the key, it is encrypted and stored so you will not have to
27-
# enter it again.
28-
#
29-
# PS C:\> invoke-psake build.ps1 -taskList publish
30-
#
31-
# You can verify the stored and encrypted NuGetApiKey by running the following
32-
# command. This will display your NuGetApiKey in plain text!
33-
#
34-
# PS C:\> invoke-psake build.ps1 -taskList showKey
35-
#
36-
# You can store a new NuGetApiKey with this command. You can leave off
37-
# the -properties parameter and you'll be prompted for the key.
38-
#
39-
# PS C:\> invoke-psake build.ps1 -taskList storeKey -properties @{NuGetApiKey='test123'}
40-
#
411

422
###############################################################################
433
# Customize these properties for your module.
@@ -63,31 +23,18 @@ Properties {
6323
(Split-Path $PSCommandPath -Leaf)
6424
)
6525

66-
# Name of the repository you wish to publish to. Default repo is the PSGallery.
67-
$PublishRepository = $null
68-
69-
# Your NuGet API key for the PSGallery. Leave it as $null and the first time
70-
# you publish you will be prompted to enter your API key. The build will
71-
# store the key encrypted in a file, so that on subsequent publishes you
72-
# will no longer be prompted for the API key.
73-
$NuGetApiKey = $null
74-
$EncryptedApiKeyPath = "$env:LOCALAPPDATA\vscode-powershell\NuGetApiKey.clixml"
7526
}
7627

7728
###############################################################################
7829
# Customize these tasks for performing operations before and/or after publish.
7930
###############################################################################
8031
Task PrePublish {
8132
$functionDeclarations = @( Get-ChildItem -Path $PublishDir\Public\*.ps1 -ErrorAction SilentlyContinue )
82-
$functionNames = @()
83-
foreach ($function in $functionDeclarations) {
84-
$functionNames += $function.BaseName
85-
}
86-
$functionsToExport = $functionNames -join ","
33+
[string[]]$functionNames = @($functionDeclarations.BaseName)
8734

8835
Update-ModuleManifest -Path $PublishDir\${ModuleName}.psd1 `
89-
-ModuleVersion "0.0.14" `
90-
-FunctionsToExport $functionsToExport
36+
-ModuleVersion "$env:GitVersion_Version" `
37+
-FunctionsToExport $functionNames
9138
}
9239

9340
Task PostPublish {
@@ -102,8 +49,8 @@ Task default -depends Build
10249
Task Publish -depends Test, PrePublish, PublishImpl, PostPublish {
10350
}
10451

105-
Task PublishImpl -depends Test -requiredVariables PublishDir, EncryptedApiKeyPath {
106-
$NuGetApiKey = Get-NuGetApiKey $NuGetApiKey $EncryptedApiKeyPath
52+
Task PublishImpl -depends Test -requiredVariables PublishDir {
53+
$NuGetApiKey = $env:PSGalleryAPIToken
10754

10855
$publishParams = @{
10956
Path = $PublishDir
@@ -119,7 +66,7 @@ Task PublishImpl -depends Test -requiredVariables PublishDir, EncryptedApiKeyPat
11966

12067
Task Test -depends Build {
12168
Import-Module Pester
122-
Invoke-Pester $PSScriptRoot
69+
Invoke-Pester $PSScriptRoot/Tests
12370
}
12471

12572
Task Build -depends Clean -requiredVariables PublishDir, Exclude, ModuleName {
@@ -148,58 +95,7 @@ Task Init -requiredVariables PublishDir {
14895
}
14996
}
15097

151-
Task StoreKey -requiredVariables EncryptedApiKeyPath {
152-
if (Test-Path $EncryptedApiKeyPath) {
153-
Remove-Item $EncryptedApiKeyPath
154-
}
155-
156-
$null = Get-NuGetApiKey $NuGetApiKey $EncryptedApiKeyPath
157-
"The NuGetApiKey has been stored in $EncryptedApiKeyPath"
158-
}
159-
160-
Task ShowKey -requiredVariables EncryptedApiKeyPath {
161-
$NuGetApiKey = Get-NuGetApiKey $NuGetApiKey $EncryptedApiKeyPath
162-
"The stored NuGetApiKey is: $NuGetApiKey"
163-
}
164-
16598
Task ? -description 'Lists the available tasks' {
16699
"Available tasks:"
167100
$psake.context.Peek().tasks.Keys | Sort
168101
}
169-
170-
###############################################################################
171-
# Helper functions
172-
###############################################################################
173-
function Get-NuGetApiKey($NuGetApiKey, $EncryptedApiKeyPath) {
174-
$storedKey = $null
175-
if (!$NuGetApiKey) {
176-
if (Test-Path $EncryptedApiKeyPath) {
177-
$storedKey = Import-Clixml $EncryptedApiKeyPath | ConvertTo-SecureString
178-
$cred = New-Object -TypeName PSCredential -ArgumentList 'kh',$storedKey
179-
$NuGetApiKey = $cred.GetNetworkCredential().Password
180-
Write-Verbose "Retrieved encrypted NuGetApiKey from $EncryptedApiKeyPath"
181-
}
182-
else {
183-
$apiKeySS = Read-Host -Prompt "Enter your NuGet API Key" -AsSecureString
184-
$cred = New-Object -TypeName PSCredential -ArgumentList 'dw',$apiKeySS
185-
$NuGetApiKey = $cred.GetNetworkCredential().Password
186-
}
187-
}
188-
189-
if (!$storedKey) {
190-
# Store encrypted NuGet API key to use for future invocations
191-
if (!$apiKeySS) {
192-
$apiKeySS = ConvertTo-SecureString -String $NuGetApiKey -AsPlainText -Force
193-
}
194-
195-
$parentDir = Split-Path $EncryptedApiKeyPath -Parent
196-
if (!(Test-Path -Path $parentDir)) {
197-
$null = New-Item -Path $parentDir -ItemType Directory
198-
}
199-
200-
$apiKeySS | ConvertFrom-SecureString | Export-Clixml $EncryptedApiKeyPath
201-
Write-Verbose "Stored encrypted NuGetApiKey to $EncryptedApiKeyPath"
202-
}
203-
204-
$NuGetApiKey
205-
}

Diff for: GitVersion.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
mode: ContinuousDelivery
2+
branches: {}
3+
ignore:
4+
sha: []

Diff for: Install-Prerequisites.ps1

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# The psake module is needed to run tests and publish the module to powershell gallery.
2+
Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted
3+
Install-Module -Name psake -RequiredVersion 4.8.0 -Repository "PSGallery"
4+
Install-Module -Name pester -RequiredVersion 4.8.0 -Repository "PSGallery"
5+
6+

Diff for: Public/Invoke-Jenkins.ps1

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ function Invoke-Jenkins {
2222

2323
$attempts = 0
2424

25+
Write-Debug "---- Invoke-Jenkins ----"
26+
Write-Debug "Resource: $Resource"
27+
Write-Debug "Request: $request"
28+
Write-Debug "Method: $Method"
29+
Write-Debug "Body: $Body"
30+
Write-Debug "Query: $Query"
31+
2532
while ($attempts -lt $MaximumAttempts) {
2633

2734
try {

Diff for: ReleaseNotes.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
Release Notes for v0.0.14
2-
====================================
1+
# Release Notes
2+
3+
## v0.1.0
4+
Initial version that supports making basic requests to Jenkins via the Remote API.
5+
Includes convenience functions to make and delete jobs.
36

4-
Really nothing special to note yet.

Diff for: Tests/Invoke-Jenkins.Tests.ps1

+15-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Import-Module "$PSScriptRoot\..\ThreeShape.Jenkins.psm1" -Force
1+
Import-Module "$PSScriptRoot\..\Threeshape.Jenkins.Beta.psm1" -Force
22
. $PSScriptRoot\..\Private\Get-JenkinsRequestHeaders.ps1
33
. $PSScriptRoot\..\Private\ConvertTo-BasicAuth.ps1
44
. $PSScriptRoot\..\Private\Get-CrumbHeader.ps1
@@ -36,6 +36,7 @@ function Get-MockHttpResponseException {
3636
Describe "Initialize-Jenkins method" {
3737

3838
BeforeAll {
39+
$script:moduleName = (Get-Item $PSScriptRoot\..\*.psd1)[0].BaseName
3940
$jenkinsUrl = "http://localhost"
4041
$apiUser = "Bobby Bobbelhead"
4142
$apiPassword = "Fallout4ever" | ConvertTo-SecureString -AsPlainText -Force
@@ -44,8 +45,8 @@ Describe "Initialize-Jenkins method" {
4445
-ApiUsername $apiUser `
4546
-ApiPassword $apiPassword
4647

47-
Mock -ModuleName ThreeShape.Jenkins -CommandName Get-CrumbHeader { return "this=crumb" }
48-
Mock -ModuleName ThreeShape.Jenkins -CommandName Invoke-RestMethod { return "dfgdfg:sdfsdsf" }
48+
Mock -ModuleName $script:moduleName -CommandName Get-CrumbHeader { return "this=crumb" }
49+
Mock -ModuleName $script:moduleName -CommandName Invoke-RestMethod { return "dfgdfg:sdfsdsf" }
4950
}
5051

5152
Context 'When invalid MaximumAttempts args are passed to Invoke-Jenkins' {
@@ -63,34 +64,34 @@ Describe "Initialize-Jenkins method" {
6364
Context 'When a request to a jenkins backend is succesful' {
6465

6566
It 'Then Invoke-WebRequest is called within the Invoke-Jenkins method once and only once' {
66-
Mock -ModuleName ThreeShape.Jenkins Invoke-WebRequest { "return 200 OK"}
67+
Mock -ModuleName $script:moduleName Invoke-WebRequest { "return 200 OK"}
6768

6869
$result = Invoke-JenkinsRequest -Resource "nowhere" `
6970
-Username "rasmus" `
7071
-Password $script:apiPassword
7172

7273
$result | Should -Be "return 200 OK"
7374

74-
Assert-MockCalled -ModuleName ThreeShape.Jenkins -Scope "It" Invoke-WebRequest -Exactly -Times 1
75+
Assert-MockCalled -ModuleName $script:moduleName -Scope "It" Invoke-WebRequest -Exactly -Times 1
7576
}
7677
}
7778

7879
Context 'When a request to jenkins fails' {
7980
It 'then Invoke-WebRequest call is retried three times by default' {
80-
Mock -ModuleName ThreeShape.Jenkins Invoke-WebRequest { throw [System.Net.Http.HttpRequestException]::new("Simulate an exception") }
81+
Mock -ModuleName $script:moduleName Invoke-WebRequest { throw [System.Net.Http.HttpRequestException]::new("Simulate an exception") }
8182

8283
$result = Invoke-JenkinsRequest -Resource "nowhere" `
8384
-Username "rasmus" `
8485
-Password $script:apiPassword
8586

8687
$result | Should -BeExactly $null
8788

88-
Assert-MockCalled -ModuleName ThreeShape.Jenkins -Scope "It" Invoke-WebRequest -Exactly -Times 3
89+
Assert-MockCalled -ModuleName $script:moduleName -Scope "It" Invoke-WebRequest -Exactly -Times 3
8990
}
9091

9192

9293
It 'Then Invoke-WebRequest is retried up to the number of times specified' {
93-
Mock -ModuleName ThreeShape.Jenkins Invoke-WebRequest { throw [System.Net.Http.HttpRequestException]::new("Simulate an exception") }
94+
Mock -ModuleName $script:moduleName Invoke-WebRequest { throw [System.Net.Http.HttpRequestException]::new("Simulate an exception") }
9495

9596
$result = Invoke-JenkinsRequest -Resource "nowhere" `
9697
-Username "rasmus" `
@@ -99,13 +100,13 @@ Describe "Initialize-Jenkins method" {
99100

100101
$result | Should -BeExactly $null
101102

102-
Assert-MockCalled -ModuleName ThreeShape.Jenkins -Scope "It" Invoke-WebRequest -Exactly -Times 5
103+
Assert-MockCalled -ModuleName $script:moduleName -Scope "It" Invoke-WebRequest -Exactly -Times 5
103104
}
104105

105106
It 'Any failure causes an exception to be re-thrown' {
106107
$exceptionBlock = Get-MockHttpResponseException -HttpStatusCode "[System.Net.HttpStatusCode]::Forbidden"
107108

108-
Mock -ModuleName ThreeShape.Jenkins Invoke-WebRequest $exceptionBlock
109+
Mock -ModuleName $script:moduleName Invoke-WebRequest $exceptionBlock
109110

110111
$request = {
111112
Invoke-JenkinsRequest -Resource "nowhere" `
@@ -117,7 +118,7 @@ Describe "Initialize-Jenkins method" {
117118
}
118119

119120
It 'Any unexpected exception is rethrown for the caller to handle' {
120-
Mock -ModuleName ThreeShape.Jenkins Invoke-WebRequest { throw [System.AccessViolationException]::new() }
121+
Mock -ModuleName $script:moduleName Invoke-WebRequest { throw [System.AccessViolationException]::new() }
121122

122123
$request = {
123124
Invoke-JenkinsRequest -Resource "nowhere" `
@@ -132,19 +133,19 @@ Describe "Initialize-Jenkins method" {
132133
Context 'When a request returns a redirect' {
133134
It 'The redirect is not treated as an exception when TreatRedirectsAsSucces is true as is the case by default)' {
134135
$exceptionBlock = Get-MockHttpResponseException -HttpStatusCode "[System.Net.HttpStatusCode]::Redirect"
135-
Mock -ModuleName ThreeShape.Jenkins Invoke-WebRequest $exceptionBlock
136+
Mock -ModuleName $script:moduleName Invoke-WebRequest $exceptionBlock
136137

137138
$result = Invoke-JenkinsRequest -Resource "nowhere" `
138139
-Username "rasmus" `
139140
-Password $script:apiPassword
140141

141-
Assert-MockCalled -ModuleName ThreeShape.Jenkins -Scope "It" Invoke-WebRequest -Exactly -Times 1
142+
Assert-MockCalled -ModuleName $script:moduleName -Scope "It" Invoke-WebRequest -Exactly -Times 1
142143
$result | Should -Not -BeNullOrEmpty
143144
}
144145

145146
It 'The redirect causes an exception if the caller sets TreatRedirectsAsSucces = false' {
146147
$exceptionBlock = Get-MockHttpResponseException -HttpStatusCode "[System.Net.HttpStatusCode]::Redirect"
147-
Mock -ModuleName ThreeShape.Jenkins Invoke-WebRequest $exceptionBlock
148+
Mock -ModuleName $script:moduleName Invoke-WebRequest $exceptionBlock
148149

149150
$request = {
150151
Invoke-JenkinsRequest -Resource "nowhere" `

0 commit comments

Comments
 (0)