forked from microsoft/Application-Insights-Workbooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MergeHelper.psm1
112 lines (98 loc) · 3.3 KB
/
MergeHelper.psm1
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
110
111
112
#Requires -Version 3.0
# Get input from the user with a regular expression to test for valid inputs and ask user until they give valid input.
function Get-UserInput
{
[cmdletbinding()]
Param(
[string]$prompt, [string]$validInputs
)
$input = Read-Host -Prompt $prompt
$valid = $false
while ($valid -ne $true) {
$valid = $input -Match $validInputs
if ($valid -eq $false) {
$input = Read-Host -Prompt "I didn't catch that. Can you re-enter? (Valid inputs: $validInputs)"
}
}
return $input
}
# Ensure the user's git workspace is clean. If not, we will exit the script.
function Get-CleanWorkspace
{
$status = & git status -s
if ($status -ne $null) {
Write-Error "Uh-oh, looks like your working directory isn't clean! Commit or reset your changes and run the script again."
exit 1
}
}
# Obtain a list of cherry-pick commits from the user
function Get-Cherrypicks
{
$anyCherrypicks = Get-UserInput -prompt "Do you need to cherry-pick any commits in AzureUX-Workbooks? (Yes or no)" -validInput '(y)|(n)|(yes)|(no)'
[System.Collections.ArrayList]$cherrypicks = @()
if ($anyCherrypicks -ieq 'y' -or $anyCherrypicks -ieq 'yes') {
$commitList = Get-UserInput -prompt "OK, enter the commits in a comma-separated list" -validInputs '\b([a-f]|[\d]){8}'
$null = $commitList.split(',') | ForEach-Object { $_.trim() } | ForEach-Object { $cherrypicks.Add($_) }
}
return $cherrypicks
}
# sync the given branch
function Sync-Branch
{
[cmdletbinding()]
Param(
[string]$branch
)
Write-Host "Checking out $branch branch"
& git checkout $branch
Write-Host "Syncing..."
& git pull
}
# create a new branch using time as part of branch name
function Get-NewBranch
{
[cmdletbinding()]
Param(
[string]$branch,
[string]$commit
)
$timestamp = Get-Date -format "yyyyMMdd-hhmmss"
if ($commit -eq $null) {
$commit = ''
}
if ($commit.length -gt 8) {
$commit = $commit.Substring(0, 8) + '-'
}
$releaseBranch = "$commit$branch-$timestamp"
return $releaseBranch
}
# cherry pick thte list of commits
function Invoke-Cherrypicks {
[cmdletbinding()]
Param(
[System.Collections.ArrayList]$cherrypicks
)
Write-Host "Cherry-picking commits $cherrypicks"
$remainingCherries = $cherrypicks;
foreach ($c in $cherrypicks) {
& git cherry-pick -m 1 $c
$remainingCherries.Remove(0)
if ($LASTEXITCODE -ne 0) {
$remaining = $remainingCherries -join ', '
Write-Warning "Oh no! Cherry-pick failed. If there were merge conflicts, resolve them and commit the result, then re-run this script with arguments '-resolved $true and -cherrypicks @($remaining)"
exit 1
}
}
}
# Publish branch and prompt for new pull request
function New-PullRequest {
[cmdletbinding()]
Param(
[string]$title,
[string]$sourceBranch,
[string]$targetBranch
)
Write-Host "Publishing branch to Github"
& git push https://github.com/microsoft/Application-Insights-Workbooks $sourceBranch
Write-Host "All done! Please create a pull request of $sourceBranch against $targetBranch here: https://github.com/microsoft/Application-Insights-Workbooks/pulls"
}