-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgetpr.ps1
63 lines (47 loc) · 1.93 KB
/
getpr.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
[CmdletBinding(DefaultParameterSetName='interactive')]
param(
[Parameter(Mandatory = $true, ParameterSetName = "uri")]
[Alias("uri", "github")]
[ValidateNotNullOrEmpty()]
[String] $GithubURI,
[Parameter(Mandatory = $false, ParameterSetName = "interactive")]
[String] $Owner = '',
[Parameter(Mandatory = $false, ParameterSetName = "interactive")]
[String] $Project = '',
[Parameter(Mandatory = $false, ParameterSetName = "interactive")]
[Int] $PullRequest = -1
)
$CurrentDir = (Get-Location).Path
switch ($PsCmdlet.ParameterSetName) {
'uri' {
$arrURI = $GithubURI -split '/'
$Owner = $arrURI[3]
$Project = $arrURI[4]
$PullRequest = [Int]$arrURI[6]
}
'interactive' {
if ($Owner -eq '') { $Owner = Read-Host -Prompt "Enter the project owner" }
if ($Project -eq '') { $Project = Read-Host -Prompt "Enter the project name" }
if ($PullRequest -eq -1) { $PullRequest = Read-Host -Prompt "Enter the PR number" }
}
}
# Get the base branch
Write-Verbose "Attempting to the get PR information at https://api.github.com/repos/$Owner/$Project/pulls/$PullRequest"
$PRInfo = Invoke-RestMethod -URI "https://api.github.com/repos/$Owner/$Project/pulls/$PullRequest" -ErrorAction Stop
$Branch = $PRInfo.base.ref
Write-Verbose "PR Owner = $Owner"
Write-Verbose "PR Project = $Project"
Write-Verbose "PR Branch = $Branch"
Write-Verbose "PR Number = $PullRequest"
$TargetDir = Join-Path -Path $CurrentDir -ChildPath "$Project-pr$PullRequest"
if (Test-Path -Path $TargetDir) { Remove-Item -Path $TargetDir -Recurse -Force -Confirm:$false | Out-Null }
Write-Verbose "Cloning..."
& git clone "https://github.com/$Owner/$Project.git" $TargetDir
Push-Location $TargetDir
Write-Verbose "Fetching PR..."
& git fetch origin "refs/pull/$PullRequest/head:pr_$PullRequest"
Write-Verbose "Changing to intended branch..."
& git checkout $Branch
Write-Verbose "Merging PR..."
& git merge "pr_$PullRequest" --no-ff
Pop-Location