MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3 4Class CgData {5 [pscredential]$Pat6 [string]$Organization7 [string]$Project8 9 CgData ([pscredential]$Pat, [string]$Organization, [string]$Project) {10 $this.Pat = $Pat11 $this.Organization = $Organization12 $this.Project = $Project13 }14}15 16# Get the Component Governance data needed to connect to the API17function Get-CgData {18 if($Script:cgData){return $Script:cgData}19 throw "First call Set-CgCredentials"20}21 22# Get a Component Governance API URI23function Get-Uri {24 param(25 [Parameter(Mandatory=$true)]26 [string]$PathPortion27 )28 $cgData = Get-CgData29 $baseUri = "https://governance.dev.azure.com/$($cgData.Organization)/$($cgData.Project)/_apis/componentgovernance/$PathPortion"30 Write-Verbose "uri: $baseUri" -Verbose31 return $baseUri32}33 34# A class representing a Component Governance repository35class CgRepository {36 #Json formatted summary information about this repository. Currently contains number of active nuget.config alerts.37 [Object] $additionalInformation38 39 #The associations for this governed repository. For example, all service tree related entries.40 [Object] $associations41 42 #Creator of the governed repository.43 [Object] $createdBy44 45 [string] $createdDate46 47 [int] $id48 49 [string] $modifiedBy50 51 [string] $modifiedDate52 53 [string] $name54 55 #The policies that are configured in the governed repository.56 [object[]] $policies57 58 [object] $projectReference59 60 [string] $repositoryMoniker61 62 [object] $repositoryOptions63 64 [object] $type65 66 [string] $url67 68 [object] $userRole69}70 71# Gets a list of all repositories governed in the current project72function Get-CgRepositories {73 $uri = Get-Uri -PathPortion "governedrepositories?api-version=6.1-preview.1"74 $cgData = Get-CgData75 [CgRepository[]] (Invoke-RestMethod -Uri $uri -Authentication Basic -Credential $cgData.Pat).value76}77 78# Gets this PowerShell master repository79Function Get-CgPsRepository {80 Get-CgRepositories | Where-Object {$_.name -eq 'PowerShell' -and $_.repositoryMoniker -notlike '*/*'}81}82 83# Gets the Component Governance Snapshot Type (unique to each Pipeline and Job) in the repository84function Get-CgSnapshotType {85 param(86 [CgRepository]87 $CgRepository88 )89 90 $id = $CgRepository.Id91 $uri = Get-Uri -PathPortion "GovernedRepositories/$id/snapshottypes?api-version=6.1-preview.1"92 $cgData = Get-CgData93 (Invoke-RestMethod -Authentication Basic -Credential $cgData.Pat -Uri $uri).Value94}95 96# Gets a Component Governance Notice for a given snapshot type97function Get-CgNotice {98 param(99 [CgRepository]100 $CgRepository,101 [int]102 $SnapshotTypeId103 )104 105 $id = $CgRepository.Id106 $uri = Get-Uri -PathPortion "GovernedRepositories/${id}/notice?snapshotTypeId=${SnapshotTypeId}&api-version=6.1-preview.1"107 $cgData = Get-CgData108 (Invoke-RestMethod -Authentication Basic -Credential $cgData.Pat -Uri $uri).content109}110 111# Sets the Component Governance credentials used by other functions to connect to the API112function Set-CgCredentials {113 param(114 [Parameter(Mandatory=$true)]115 [securestring] $Pat,116 117 [Parameter(Mandatory=$true)]118 [string] $Organization,119 120 [Parameter(Mandatory=$true)]121 [string] $Project122 )123 124 $pscred = [PSCredential]::new("PAT",$Pat)125 $script:cgData = [CgData]::new($pscred, $Organization, $Project)126}127 128Export-ModuleMember -Function @(129 'Get-CgRepositories'130 'Set-CgCredentials'131 'Get-CgRepository'132 'Get-CgPsRepository'133 'Get-CgSnapshotType'134 'Get-CgNotice'135)136 