Plugins/GitLab.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# Author: Josh Ameli <contact@jekotia.net>
# Based off of the Git plugin by Miodrag Milic <miodrag.milic@gmail.com>
# Last Change: 20-Aug-2019.

# https://www.appveyor.com/docs/how-to/git-push/

param(
    $Info,

    # GitLab username
    [string] $User,

    # GitLab API key.
    [string] $API_Key,

    # Repository HTTP(S) URL
    [string]$PushURL,

    # Force git commit when package is updated but not pushed.
    [switch] $Force,

    # Commit strategy:
    # single - 1 commit with all packages
    # atomic - 1 commit per package
    # atomictag - 1 commit and tag per package
    [ValidateSet('single', 'atomic', 'atomictag')]
    [string]$commitStrategy = 'single',

    # Branch name
    [string]$Branch = 'master'
)

[array]$packages = if ($Force) { $Info.result.updated } else { $Info.result.pushed }
if ($packages.Length -eq 0) { Write-Host "No package updated, skipping"; return }

$root = Split-Path $packages[0].Path

Push-Location $root
$origin  = git config --get remote.origin.url
$origin -match '(?<=:/+)[^/]+' | Out-Null
$machine = $Matches[0]

### Construct RepoURL to be set as new origin
$RepoURL = (
    $PushURL.split('://')[0] `
    + "://" `
    + $User `
    + ":" `
    + $API_Key `
    + "@" `
    + $PushURL.TrimStart(
        $(
            $PushURL.split('://')[0] `
            + "://"
        )
    )
)

### Set new push URL
git remote set-url origin $RepoURL

### Ensure local is up-to-date to avoid conflicts
Write-Host "Executing git pull"
git checkout -q $Branch
git pull -q origin $Branch

### Commit
if  ($commitStrategy -like 'atomic*') {
    $packages | ForEach-Object {
        Write-Host "Adding update package to git repository: $($_.Name)"
        git add -u $_.Path
        git status

        Write-Host "Commiting $($_.Name)"
        $message = "AU: $($_.Name) upgraded from $($_.NuspecVersion) to $($_.RemoteVersion)"
        $gist_url = $Info.plugin_results.Gist -split '\n' | Select-Object -Last 1
        $snippet_url = $Info.plugin_results.Snippet -split '\n' | Select-Object -Last 1
        git commit -m "$message`n[skip ci] $gist_url $snippet_url" --allow-empty

        if ($commitStrategy -eq 'atomictag') {
          $tagcmd = "git tag -a $($_.Name)-$($_.RemoteVersion) -m '$($_.Name)-$($_.RemoteVersion)'"
          Invoke-Expression $tagcmd
        }
    }
}
else {
    Write-Host "Adding updated packages to git repository: $( $packages | % Name)"
    $packages | ForEach-Object { git add -u $_.Path }
    git status

    Write-Host "Commiting"
    $message = "AU: $($packages.Length) updated - $($packages | % Name)"
    $gist_url = $Info.plugin_results.Gist -split '\n' | Select-Object -Last 1
    $snippet_url = $Info.plugin_results.Snippet -split '\n' | Select-Object -Last 1
    git commit -m "$message`n[skip ci] $gist_url $snippet_url" --allow-empty

}

### Push
Write-Host "Pushing changes"
git push -q 
if ($commitStrategy -eq 'atomictag') {
    write-host 'Atomic Tag Push'
    git push -q --tags
}
Pop-Location

git remote set-url origin $origin