-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathGitHubAppInstallation.ps1
More file actions
152 lines (131 loc) · 5.36 KB
/
Copy pathGitHubAppInstallation.ps1
File metadata and controls
152 lines (131 loc) · 5.36 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
class GitHubAppInstallation {
# The installation ID on the target.
[System.Nullable[UInt64]] $ID
# The app that is installed.
[GitHubApp] $App
# The target of the installation.
[GitHubOwner] $Target
# The type of target.
[string] $Type
# The type of repository selection.
[string] $RepositorySelection
# The permissions that the app has on the target.
[GitHubPermission[]] $Permissions
# The events that the app is subscribing to.
[string[]] $Events
# The file paths that the app has access to.
[string[]] $FilePaths
# The creation date of the installation.
# Example: 2008-01-14T04:33:35Z
[System.Nullable[datetime]] $CreatedAt
# The last update date of the installation.
# Example: 2008-01-14T04:33:35Z
[System.Nullable[datetime]] $UpdatedAt
# The date the installation was suspended.
# Example: 2008-01-14T04:33:35Z
[System.Nullable[datetime]] $SuspendedAt
# The account that suspended the installation.
[GitHubUser] $SuspendedBy
# The URL to the target's profile based on the target type.
[string] $Url
# The status indicating if the installation permissions and events match the app's configuration.
[string] $Status
GitHubAppInstallation() {}
GitHubAppInstallation([PSCustomObject] $Object) {
$this.ID = $Object.id
$this.App = if ($null -ne $Object.App) {
$Object.App
} else {
[GitHubApp]@{
ClientID = $Object.client_id
Slug = $Object.app_slug
}
}
$this.Target = if ($null -ne $Object.Target) {
[GitHubOwner]::new($Object.Target)
} elseif ($null -ne $Object.Account) {
[GitHubOwner]::new($Object.Account)
} else {
$null
}
$this.Type = $Object.target_type
$this.RepositorySelection = $Object.repository_selection
$this.Permissions = [GitHubPermission]::NewPermissionList($Object.permissions, $this.Type)
$this.Events = , ($Object.events)
$this.FilePaths = , ($Object.single_file_paths)
$this.CreatedAt = $Object.created_at
$this.UpdatedAt = $Object.updated_at
$this.SuspendedAt = $Object.suspended_at
$this.SuspendedBy = [GitHubUser]::new($Object.suspended_by)
$this.Url = $Object.html_url
$this.Status = $Object.Status ?? 'Unknown'
}
GitHubAppInstallation([PSCustomObject] $Object, [GitHubApp] $App) {
$this.ID = $Object.id
$this.App = $App
$this.Target = [GitHubOwner]::new($Object.account)
$this.Type = $Object.target_type
$this.RepositorySelection = $Object.repository_selection
$this.Permissions = [GitHubPermission]::NewPermissionList($Object.permissions, $this.Type)
$this.Events = , ($Object.events)
$this.FilePaths = , ($Object.single_file_paths)
$this.CreatedAt = $Object.created_at
$this.UpdatedAt = $Object.updated_at
$this.SuspendedAt = $Object.suspended_at
$this.SuspendedBy = [GitHubUser]::new($Object.suspended_by)
$this.Url = $Object.html_url
$this.SetStatus()
}
GitHubAppInstallation([PSCustomObject] $Object, [string] $Target, [string] $Type, [string] $HostName) {
$this.ID = $Object.id
$this.App = [GitHubApp]@{
ClientID = $Object.client_id
Slug = $Object.app_slug
}
$this.Target = [GitHubOwner]@{
Name = $Target
Type = $Type
Url = "https://$HostName/$Target"
}
$this.Type = $Type
$this.RepositorySelection = $Object.repository_selection
$this.Permissions = [GitHubPermission]::NewPermissionList($Object.permissions, $this.Type)
$this.Events = , ($Object.events)
$this.FilePaths = $Object.single_file_paths
$this.CreatedAt = $Object.created_at
$this.UpdatedAt = $Object.updated_at
$this.SuspendedAt = $Object.suspended_at
$this.SuspendedBy = [GitHubUser]::new($Object.suspended_by)
$this.Url = "https://$HostName/$($Type.ToLower())s/$Target/settings/installations/$($Object.id)"
$this.Status = 'Unknown'
}
# Sets the Status property by comparing installation permissions with app permissions
# filtered by the appropriate scope based on installation type
[void] SetStatus() {
if (-not $this.App -or -not $this.App.Permissions) {
$this.Status = 'Unknown'
return
}
if (-not $this.Permissions) {
$this.Status = 'Unknown'
return
}
# Get app permissions filtered by installation type scope
$appPermissionsFiltered = switch ($this.Type) {
'Enterprise' {
$this.App.Permissions | Where-Object { $_.Scope -eq 'Enterprise' }
}
'Organization' {
$this.App.Permissions | Where-Object { $_.Scope -in @('Organization', 'Repository') }
}
'User' {
$this.App.Permissions | Where-Object { $_.Scope -in @('Repository') }
}
default {
$this.App.Permissions
}
}
$permissionsMatch = Compare-Object -ReferenceObject $appPermissionsFiltered -DifferenceObject $this.Permissions | Measure-Object
$this.Status = $permissionsMatch ? 'Ok' : 'Outdated'
}
}