File size: 3,692 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

Class CgData {
    [pscredential]$Pat
    [string]$Organization
    [string]$Project

    CgData ([pscredential]$Pat, [string]$Organization, [string]$Project) {
        $this.Pat = $Pat
        $this.Organization = $Organization
        $this.Project = $Project
    }
}

# Get the Component Governance data needed to connect to the API
function Get-CgData {
    if($Script:cgData){return $Script:cgData}
    throw "First call Set-CgCredentials"
}

# Get a Component Governance API URI
function Get-Uri {
    param(
        [Parameter(Mandatory=$true)]
        [string]$PathPortion
    )
    $cgData = Get-CgData
    $baseUri = "https://governance.dev.azure.com/$($cgData.Organization)/$($cgData.Project)/_apis/componentgovernance/$PathPortion"
    Write-Verbose "uri: $baseUri" -Verbose
    return $baseUri
}

# A class representing a Component Governance repository
class CgRepository {
    #Json formatted summary information about this repository. Currently contains number of active nuget.config alerts.
    [Object] $additionalInformation

    #The associations for this governed repository. For example, all service tree related entries.
    [Object] $associations

    #Creator of the governed repository.
    [Object] $createdBy

    [string] $createdDate

    [int] $id

    [string] $modifiedBy

    [string] $modifiedDate

    [string] $name

    #The policies that are configured in the governed repository.
    [object[]] $policies

    [object] $projectReference

    [string] $repositoryMoniker

    [object] $repositoryOptions

    [object] $type

    [string] $url

    [object] $userRole
}

# Gets a list of all repositories governed in the current project
function Get-CgRepositories {
    $uri = Get-Uri -PathPortion "governedrepositories?api-version=6.1-preview.1"
    $cgData = Get-CgData
    [CgRepository[]] (Invoke-RestMethod -Uri $uri -Authentication Basic -Credential $cgData.Pat).value
}

# Gets this PowerShell master repository
Function Get-CgPsRepository {
    Get-CgRepositories | Where-Object {$_.name -eq 'PowerShell' -and $_.repositoryMoniker -notlike '*/*'}
}

# Gets the Component Governance Snapshot Type (unique to each Pipeline and Job) in the repository
function Get-CgSnapshotType {
    param(
        [CgRepository]
        $CgRepository
    )

    $id = $CgRepository.Id
    $uri = Get-Uri -PathPortion "GovernedRepositories/$id/snapshottypes?api-version=6.1-preview.1"
    $cgData = Get-CgData
    (Invoke-RestMethod -Authentication Basic -Credential $cgData.Pat -Uri $uri).Value
}

# Gets a Component Governance Notice for a given snapshot type
function Get-CgNotice {
    param(
        [CgRepository]
        $CgRepository,
        [int]
        $SnapshotTypeId
    )

    $id = $CgRepository.Id
    $uri = Get-Uri -PathPortion "GovernedRepositories/${id}/notice?snapshotTypeId=${SnapshotTypeId}&api-version=6.1-preview.1"
    $cgData = Get-CgData
    (Invoke-RestMethod -Authentication Basic -Credential $cgData.Pat -Uri $uri).content
}

# Sets the Component Governance credentials used by other functions to connect to the API
function Set-CgCredentials {
    param(
        [Parameter(Mandatory=$true)]
        [securestring] $Pat,

        [Parameter(Mandatory=$true)]
        [string] $Organization,

        [Parameter(Mandatory=$true)]
        [string] $Project
    )

    $pscred = [PSCredential]::new("PAT",$Pat)
    $script:cgData = [CgData]::new($pscred, $Organization, $Project)
}

Export-ModuleMember -Function @(
    'Get-CgRepositories'
    'Set-CgCredentials'
    'Get-CgRepository'
    'Get-CgPsRepository'
    'Get-CgSnapshotType'
    'Get-CgNotice'
)