This repository has been archived by the owner on Sep 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGet-GitlabConnect.ps1
77 lines (69 loc) · 3.06 KB
/
Get-GitlabConnect.ps1
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
function Get-GitLabConnect
{
<#
.SYNOPSIS
Composes and Returns a GitlabConnect object.
.DESCRIPTION
Composes and Returns a GitlabConnect object.
Uses active token from stored config by default. you can add tokens with Add-GitlabToken.
Passing -ID of a token forces gitlabconnect to use that token to create a connect object.
Gitlabconnect objects can be passed to all functions to specify a connection to use for interaction with gitlab.
A gitlabconnectobject can also be generated on the fly by passing -GitlabURI and -Token
.EXAMPLE
Get-GitLabConnect
---------------------------------------------------------------
returns a Connect object based of the current active gitlabtoken
.EXAMPLE
Get-GitLabConnect -id 695c6eea-1fc3-4f78-886e-6b7c0f27102e
---------------------------------------------------------------
returns a Connect object based of token 695c6eea-1fc3-4f78-886e-6b7c0f27102e
tokenid can be retrieved with Get-GitlabToken
.EXAMPLE
Get-GitLabConnect -GitLabURI gitlab.com -Token XXXXXXXXXX
---------------------------------------------------------------
Returns a Connect object based on passed hostname and token.
These connect settings are not persistent
.EXAMPLE
$GitLabConnectObj = Get-GitLabConnect -GitLabURI gitlab.com -Token XXXXXXXXXX
Get-GitLabProject -GitLabConnect $GitLabConnectObj
---------------------------------------------------------------
Creates a connect object on the fly.
Returns all the projects that the token has access to.
#>
[CmdletBinding(DefaultParameterSetName = 'ByTokenID')]
[Alias()]
[OutputType()]
param(
#Specifies token to be used to create a connectobject, tokenid can be retrieved with Get-GitlabToken.
[parameter(Mandatory = $false,
ParameterSetName='ByTokenID')]
[Alias('KeyId')]
[system.guid]$id,
#Gitlab URI, e.a. https://gitab.com
[Parameter(HelpMessage = 'GitlabServer URI',
Mandatory = $true,
ParameterSetName='OnTheFly')]
[ValidatePattern("^(?:http|https):\/\/(?:[\w\.\-\+]+:{0,1}[\w\.\-\+]*@)?(?:[a-z0-9\-\.]+)(?::[0-9]+)?(?:\/|\/(?:[\w#!:\.\?\+=&%@!\-\/\(\)]+)|\?(?:[\w#!:\.\?\+=&%@!\-\/\(\)]+))?$")]
[string]$GitLabURI,
# Token supplied from gitlab. This can be an private and an Access Token.
[Parameter(Mandatory = $true,
ParameterSetName='OnTheFly')]
[string]$Token
)
if($PSCmdlet.ParameterSetName -like 'ByTokenID'){
if($id){
$Gitlabtoken = Get-GitLabToken | ?{$_.id -eq $id}
if(-not $Gitlabtoken){
$errormessage = 'No Token found with Id [$ID]'
Write-Error -Message $errormessage -Category ResourceUnavailable -ErrorAction Stop
}
}else{
$Gitlabtoken = Get-GitLabToken -active
}
return [gitlabconnect]::new($Gitlabtoken.gitlabhost,$Gitlabtoken.gitlabuser)
}
if($PSCmdlet.ParameterSetName -like 'OnTheFly')
{
return [gitlabconnect]::new($GitLabURI,$Token)
}
}