This repository has been archived by the owner on Sep 29, 2023. It is now read-only.
/
Set-GitLabProjectRepositoryFile.ps1
90 lines (79 loc) · 2.83 KB
/
Set-GitLabProjectRepositoryFile.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
78
79
80
81
82
83
84
85
86
87
88
89
90
function Set-GitLabProjectRepositoryFile
{
<#
.SYNOPSIS
Update file in repository
.DESCRIPTION
The Set-GitLabProjectRepositoryFile function edits an existing file in the repository.
Content is expected in text but can be pased base 64 encoded using -Encoding base64.
If the commit fails for any reason we return a 400 error with a non-specific error message. Possible causes for a failed commit include:
- the file_path contained /../ (attempted directory traversal);
- the new file contents were identical to the current file contents, i.e. the user tried to make an empty commit;
- the branch was updated by a Git push while the file edit was in progress.
Currently gitlab-shell has a boolean return code, preventing GitLab from specifying the error.
.EXAMPLE
Set-GitLabProjectRepositoryFile -ProjectID 20 -BranchName Develop -FilePath README.md -Content 'New Readme Content' -CommitMessage 'Update readme'
---------------------------------------------------------------
updates the content of README.md to 'New Readme Content' with the message 'Update readme'
#>
[CmdletBinding()]
[Alias()]
[OutputType()]
Param
(
# The ID of the project
[Parameter(HelpMessage = 'ProjectID',
Mandatory = $true)]
[Alias('ID')]
[String]$ProjectID,
# The name of the branch
[Parameter(HelpMessage = 'Commit SHA or branch name',
Mandatory = $true)]
[Alias('branch_name')]
[string]$BranchName,
# The path of the file inside the projects repository.
[Parameter(Helpmessage = 'The path of the file',
Mandatory = $true)]
[alias('file_path')]
[String]$FilePath,
# Encoding of seplied content. 'text' or 'base64'. Text is default.
[Parameter(HelpMessage = 'Encoding of content (text|base64)')]
[validateset('text','base64')]
[string]$Encoding = 'text',
# File content
[Parameter(Helpmessage = 'File content',
Mandatory = $true)]
[string]$Content,
# Commit message
[Parameter(HelpMessage = ' Commit message',
Mandatory = $true)]
[alias('commit_message')]
[string]$CommitMessage,
# Specify Existing GitlabConnector
[Parameter(HelpMessage = 'Specify Existing GitlabConnector',
Mandatory = $false,
DontShow = $true)]
[psobject]$GitlabConnect = (Get-GitlabConnect)
)
$httpmethod = 'put'
$apiurl = "/projects/$([System.Web.HttpUtility]::UrlEncode($projectId))/repository/files"
$parameters = @{
'file_path' = $FilePath
'branch_name' = $BranchName
'commit_message' = $CommitMessage
'content' = $content
}
switch($encoding){
'text'
{
$parameters.encoding = 'text'
break
}
'base64'
{
$parameters.encoding = 'base64'
break
}
}
$GitlabConnect.callapi($apiurl,$httpmethod,$parameters)
}