This repository was 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
Expand file tree
/
Copy pathGet-GitLabProjectBranch.ps1
More file actions
72 lines (64 loc) · 2.53 KB
/
Copy pathGet-GitLabProjectBranch.ps1
File metadata and controls
72 lines (64 loc) · 2.53 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
function Get-GitLabProjectBranch
{
<#
.SYNOPSIS
Gets GitLab Project Branches.
.DESCRIPTION
Get branch of specified project.
By default all branches of the project are retrieved.
when -Branch is specified only the specified branch is returned.
If the branch is not found a 404 error is returned.
.EXAMPLE
Get-GitLabProjectBranch -ProjectID 20
---------------------------------------------------------------
Gets all branches for project 20
.EXAMPLE
Get-GitLabProjectBranch -ProjectID 20 -Branch 'master'
---------------------------------------------------------------
Gets branch 'master' for project 20
#>
[CmdletBinding(DefaultParameterSetName = 'AllBranches')]
[Alias()]
[OutputType()]
Param
(
# The ID of the project
[Parameter(Mandatory = $true, HelpMessage = 'ProjectID')]
[Alias('ID')]
[String]$ProjectID,
# Name of the branch
[Parameter(ParameterSetName = 'SingleBranch', Mandatory = $true,
HelpMessage = 'The name of the branch')]
[String]$Branch,
# Branch protection rules (inlcuding patterns)
[Parameter(ParameterSetName = 'ProtectedBranch', Mandatory = $true,
HelpMessage = 'Get branch protection rules (acutal branches and patterns)')]
[Parameter(ParameterSetName = 'SingleBranch', Mandatory = $false,
HelpMessage = 'Get branch protection rules (acutal branches and patterns)')]
[Switch]$ProtectionRules,
# Existing GitlabConnector Object, can be retrieved with Get-GitlabConnect
[Parameter(HelpMessage = 'Specify Existing GitlabConnector',
Mandatory = $false, DontShow = $true)]
[psobject]$GitlabConnect = (Get-GitlabConnect)
)
$httpmethod = 'get'
$id = [System.Web.HttpUtility]::UrlEncode($projectId)
$apiurl = "projects/$id/repository/branches"
$parameters = @{}
$filterProtectionRules = $false
if ($PSCmdlet.MyInvocation.BoundParameters.keys -contains "ProtectionRules" -and $ProtectionRules) {
$apiurl = "projects/$id/protected_branches"
$filterProtectionRules = $PSCmdlet.ParameterSetName -like 'SingleBranch*'
} elseif($PSCmdlet.ParameterSetName -like 'SingleBranch*') {
$apiurl += "\$Branch"
}
$res = $GitlabConnect.callapi($apiurl,$httpmethod,$parameters)
if ($res -eq $null) {
$res = @()
}
if($filterProtectionRules) {
$res | ?{ $Branch -like $_.name } | write-output
} else {
@() + $res | write-output
}
}