Skip to content

Commit ad6860b

Browse files
committed
feat: renaming project
feat: Renaming project fix: PSscriptanalyzer feat: Updating with latest version fix(functions): 🚑 Login check did not work. PAT was always used feat(functions): Added a way to function to clear authentication header refactor: 🚚 Moving all the functions in different folders feat: Added function for branch control on pipeline feat(workflow): Added psscriptanalyzer chore(release): v0.1.0 [skip ci] fix(worklows): 🚀 Added repo parameter chore(release): v0.2.0 [skip ci] chore(release): v0.3.0 [skip ci] chore(release): v0.4.0 [skip ci] fix: 🚀 Fixing psgallery Api key fix: 🚀 Added build task to psake feat: adding version to publish fix: typo fix: changed to publish-module fix: Added update-modulemanifest fix: typo fix: -as [version] fix: changed to init fix: typo fix:typo fix: removed cast fix: testing build fix: Switch order fix: replaced path fix: update manifest fix: added functionsToExport to workflow fix: testing version from tripss action fix: fixed version fix: typo fix: testing new github release Deleted old
1 parent 59ee855 commit ad6860b

40 files changed

Lines changed: 1881 additions & 1612 deletions

File tree

AzureDevOpsPowerShellAPI/AzureDevOpsPowerShellAPI.psd1 renamed to AzureDevOpsPowerShell/AzureDevOpsPowerShell.psd1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
@{
1010

1111
# Script module or binary module file associated with this manifest.
12-
RootModule = 'AzureDevOpsPowerShellAPI.psm1'
12+
RootModule = 'AzureDevOpsPowerShell.psm1'
1313

1414
# Version number of this module.
1515
ModuleVersion = '0.2.0'
@@ -33,7 +33,7 @@
3333
Description = 'PowerShell module to deploy and adjust services on Azure DevOps'
3434

3535
# Minimum version of the PowerShell engine required by this module
36-
PowerShellVersion = '5.1'
36+
PowerShellVersion = '7.3'
3737

3838
# Name of the PowerShell host required by this module
3939
# PowerShellHostName = ''
File renamed without changes.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
function Invoke-AzDoRestMethod {
2+
<#
3+
.SYNOPSIS
4+
A short one-line action-based description, e.g. 'Tests if a function is valid'
5+
.DESCRIPTION
6+
A longer description of the function, its purpose, common use cases, etc.
7+
.NOTES
8+
Information or caveats about the function e.g. 'This function is not supported in Linux'
9+
.LINK
10+
Specify a URI to a help page, this will show when Get-Help -Online is used.
11+
.EXAMPLE
12+
Test-MyTestFunction -Verbose
13+
Explanation of the function or its result. You can include multiple examples with additional .EXAMPLE lines
14+
#>
15+
[CmdletBinding(SupportsShouldProcess)]
16+
param (
17+
[Parameter(Mandatory)]
18+
[string]
19+
$Uri,
20+
21+
[Parameter(Mandatory)]
22+
[string]
23+
$Version,
24+
25+
[Parameter(Mandatory)]
26+
[ValidateSet('GET', 'POST', 'PATCH', 'DELETE')]
27+
[string]
28+
$Method,
29+
30+
[Parameter()]
31+
[string]
32+
$PAT,
33+
34+
[Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName)]
35+
[PSCustomObject[]]
36+
$Body
37+
)
38+
39+
begin {
40+
if (-not($script:header)) {
41+
try {
42+
New-AzDoAuthHeader -PAT $PAT -ErrorAction Stop
43+
} catch {
44+
throw $_
45+
}
46+
}
47+
48+
$params = @{
49+
Uri = "$($Uri)?api-version=$($Version)"
50+
Method = $Method
51+
Headers = $script:header
52+
ContentType = 'application/json'
53+
}
54+
55+
Write-Verbose "Uri: $($params.Uri)"
56+
Write-Verbose "Method: $($params.Method)"
57+
}
58+
59+
process {
60+
if ($Method -eq 'POST' -or ($Method -eq 'PATCH')) {
61+
Write-Verbose "Body: $($Body | ConvertTo-Json -Depth 10)"
62+
$params.Body = $Body | ConvertTo-Json -Depth 10
63+
}
64+
if ($PSCmdlet.ShouldProcess($ProjectName, "Invoke Rest method on: $($PSStyle.Bold)$ProjectName$($PSStyle.Reset)")) {
65+
try {
66+
Invoke-RestMethod @params
67+
} catch {
68+
Write-AzdoError -Message ($_ | ConvertFrom-Json).message
69+
}
70+
}
71+
}
72+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
function New-AzDoAuthHeader {
2+
[CmdletBinding(SupportsShouldProcess)]
3+
param (
4+
# PAT to authenticate with the organization
5+
[Parameter()]
6+
[String]
7+
$PAT
8+
)
9+
if ($PSCmdlet.ShouldProcess("Creating new authentication header")) {
10+
Write-Verbose "Function: New-AzDoAuthHeader"
11+
if ($PAT -eq '') {
12+
# validate if user is logged in to Azure PowerShell
13+
Write-Verbose "Using Access Token"
14+
try {
15+
if ($null -eq (Get-AzContext).Account) {
16+
Write-Error 'Please login to Azure PowerShell first'
17+
$PSCmdlet.ThrowTerminatingError($PSItem)
18+
}
19+
Write-Verbose "Getting Access Token"
20+
$script:header = @{Authorization = 'Bearer ' + (Get-AzAccessToken -Resource 499b84ac-1321-427f-aa17-267ca6975798).token
21+
}
22+
} catch {
23+
throw 'Please login to Azure PowerShell first'
24+
}
25+
} else {
26+
Write-Verbose "Using PAT"
27+
Write-Verbose "Getting Access Token"
28+
$script:header = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($PAT)")) }
29+
}
30+
}
31+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function Write-AzDoError {
2+
[CmdletBinding()]
3+
param (
4+
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
5+
[string]
6+
$Message
7+
)
8+
9+
10+
process {
11+
$errorRec = [System.Management.Automation.ErrorRecord]::new(
12+
[Exception]::new($Message),
13+
'ErrorID',
14+
[System.Management.Automation.ErrorCategory]::OperationStopped,
15+
'TargetObject'
16+
)
17+
18+
$PScmdlet.ThrowTerminatingError($errorRec)
19+
}
20+
}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
function Add-AzDoPipelineBranchControl {
2+
<#
3+
.SYNOPSIS
4+
Creates a Build Validation policy on a branch
5+
.DESCRIPTION
6+
Creates a Build Validation policy on a branch
7+
.EXAMPLE
8+
$params = @{
9+
CollectionUri = "https://dev.azure.com/contoso"
10+
PAT = "***"
11+
Name = "Policy 1"
12+
RepoName = "Repo 1"
13+
ProjectName = "Project 1"
14+
Id = 1
15+
}
16+
Set-AzDoBranchPolicyBuildValidation @params
17+
18+
This example creates a policy with splatting parameters
19+
20+
.EXAMPLE
21+
$env:SYSTEM_ACCESSTOKEN = '***'
22+
New-AzDoPipeline -CollectionUri "https://dev.azure.com/contoso" -ProjectName "Project 1" -Name "Pipeline 1" -RepoName "Repo 1" -Path "main.yml"
23+
| Set-AzDoBranchPolicyBuildValidation
24+
25+
This example creates a new Azure Pipeline and sets this pipeline as Build Validation policy on the main branch
26+
27+
.OUTPUTS
28+
[PSCustomObject]@{
29+
CollectionUri = $CollectionUri
30+
ProjectName = $ProjectName
31+
Id = $_.id
32+
}
33+
.NOTES
34+
#>
35+
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')]
36+
param (
37+
# Collection Uri of the organization
38+
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
39+
[string]
40+
$CollectionUri,
41+
42+
# Project where the pipeline will be created.
43+
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
44+
[string]
45+
$ProjectName,
46+
47+
# Name of the Build Validation policy. Default is the name of the Build Definition
48+
[Parameter()]
49+
[string]
50+
$PolicyName = "Branch Control",
51+
52+
# The type of Azure DevOps resource to be protected by a build validation policy
53+
[Parameter(Mandatory)]
54+
[string]
55+
[ValidateSet("environment", "variablegroup", "repository")]
56+
$ResourceType,
57+
58+
# Name of the resource to be protected by a build validation policy
59+
[Parameter(Mandatory)]
60+
[string[]]
61+
$ResourceName,
62+
63+
# Valid duration of the Build Validation policy. Default is 720 minutes
64+
[Parameter()]
65+
[string]
66+
$AllowUnknownStatusBranches = "false",
67+
68+
# Setup an allow list of branches from which a pipeline must be run to access this resource
69+
[Parameter()]
70+
[string]
71+
$AllowedBranches = "refs/head/main",
72+
73+
# Setup a requirement of branch protection policies for the branch from which a pipeline will be run to access this resource
74+
[Parameter()]
75+
[string]
76+
[validateset("true", "false")]
77+
$EnsureProtectionOfBranch = "true",
78+
79+
# Valid duration of the Build Validation policy. Default is 720 minutes
80+
[Parameter()]
81+
[int]
82+
$Timeout = 1440
83+
)
84+
85+
begin {
86+
$body = New-Object -TypeName "System.Collections.ArrayList"
87+
}
88+
89+
Process {
90+
$projectId = (Get-AzDoproject -CollectionUri $CollectionUri -ProjectName $ProjectName).projectId
91+
92+
foreach ($name in $ResourceName) {
93+
94+
switch ($ResourceType) {
95+
"environment" {
96+
$resourceId = (Get-AzDoEnvironment -CollectionUri $CollectionUri -ProjectName $ProjectName -EnvironmentName $name).id
97+
}
98+
"variablegroup" {
99+
$resourceId = (Get-AzDoVariableGroup -CollectionUri $CollectionUri -ProjectName $ProjectName -Name $name).id
100+
}
101+
"repository" {
102+
$repoId = (Get-AzDoRepo -CollectionUri $CollectionUri -ProjectName $ProjectName -Name $name).id
103+
$resourceId = "$($projectId).$($repoId)"
104+
}
105+
}
106+
107+
#TODO: Check if policy already exists
108+
109+
$body = @{
110+
type = @{
111+
name = "Task Check"
112+
id = "fe1de3ee-a436-41b4-bb20-f6eb4cb879a7"
113+
}
114+
settings = @{
115+
displayName = $PolicyName
116+
definitionRef = @{
117+
id = "86b05a0c-73e6-4f7d-b3cf-e38f3b39a75b"
118+
name = "evaluatebranchProtection"
119+
version = "0.0.1"
120+
}
121+
inputs = @{
122+
allowUnknownStatusBranches = $AllowUnknownStatusBranches
123+
allowedBranches = $AllowedBranches
124+
ensureProtectionOfBranch = $EnsureProtectionOfBranch
125+
}
126+
}
127+
timeout = $Timeout
128+
resource = @{
129+
type = $ResourceType
130+
id = $resourceId
131+
}
132+
}
133+
134+
$params = @{
135+
uri = "$CollectionUri/$projectId/_apis/pipelines/checks/configurations"
136+
version = "7.2-preview.1"
137+
Method = "POST"
138+
body = $body
139+
}
140+
141+
if ($PSCmdlet.ShouldProcess($ProjectName, "Create envrironment named: $($PSStyle.Bold)$PolicyName$($PSStyle.Reset)")) {
142+
Invoke-AzDoRestMethod @params | ForEach-Object {
143+
[PSCustomObject]@{
144+
CollectionUri = $CollectionUri
145+
ProjectName = $ProjectName
146+
Id = $_.id
147+
}
148+
}
149+
} else {
150+
$Body | Format-List
151+
}
152+
}
153+
}
154+
}

0 commit comments

Comments
 (0)