Plugins/Snippet.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
# Author: dimqua <dimqua@lavabit.com>
# Last Change: 11-Oct-2018.
<#
.SYNOPSIS
    Upload update history report to Gitlab snippet.
 
.DESCRIPTION
    Plugin uploads update history report (created by Report plugin) to the snippet with the given id and filename. You can use gitlab.com instance (default) or self-hosted one.
#>

param(
    $Info,

    # Snippet id
    [string] $Id,

    # Gitlab API Token, create in User Settings -> Access Tokens -> Create personal access token
    # Make sure token has 'api' scope.
    [string] $ApiToken,

    # File paths to attach to snippet
    [string[]] $Path,

    # Snippet file name
    [string] $FileName = 'Update-AUPackages.md',

    # GitLab instance's (sub)domain name
    [string] $Domain = 'gitlab.com'

)

# Create snippet
ls $Path | % {
    $file_name = Split-Path $_ -Leaf
    $content = gc $_ -Raw
    $snippet = '{"content": "' + $content + '"}'
    }

$params = @{
    ContentType = 'application/json'
    Method      = "PUT"
    Uri         = "https://$Domain/api/v4/snippets/$Id"
    Body        = ($snippet | ConvertTo-Json).replace('"{\"content\": \"','{"content": "').replace('\"}"','"') + ', "file_name": "' + $FileName + '"}'
    Headers = @{ 'PRIVATE-TOKEN'=$ApiToken }
}

# Request
$res = Invoke-WebRequest @params
"https://$Domain/snippets/$Id"