forked from voiplab/Misc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoteMergeBitbucket.ps1
95 lines (82 loc) · 3.26 KB
/
RemoteMergeBitbucket.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
Param (
[string]$username = "",
[string]$password = "",
[string]$repo = "",
[string]$branchSource = "",
[string]$branchDestination = ""
)
if (!$username -or !$password -or !$repo -or !$branchSource -or !$branchDestination)
{
Write-TeamcityError "Parameters(username,password,repo,branchSource,branchDestination) could not be blank"
}
$user = $username
$pass = $password
$pair = "$($user):$($pass)"
$result = $null
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$basicAuthValue = "Basic $encodedCreds"
$Headers = @{
Authorization = $basicAuthValue
}
$bitbucket_pull_requests_url = "https://bitbucket.org/api/2.0/repositories/muranosoft/$repo/pullrequests/"
Write-Host "Base url" $bitbucket_pull_requests_url
$json_create_pull_request= '{ "title": "Automerge from Teamcity", "description": "Automerge", "source": { "branch": { "name": "' + $branchSource + '" }, "repository": { "full_name": "muranosoft/' + $repo + '" } }, "destination": { "branch": { "name": "'+ $branchDestination +'" } }, "reviewers": [ { "username": "some other user needed to review changes" } ], "close_source_branch": false }'
Function WebRequest([string] $url, [string] $json_data){
return Invoke-WebRequest -Headers $headers -UseBasicParsing $url -ContentType "application/json" -Method POST -Body $json_data
}
Function Write-TeamcityError([string] $message)
{
Write-Host "Problem: $message"
Write-Host "##teamcity[buildProblem description='$message']"
}
try{
# Creating pull request
$result = WebRequest $bitbucket_pull_requests_url $json_create_pull_request
if ($result.StatusCode -eq "201") {
Write-Host "Pull request successfully created"
$result_json_content = $result.Content | ConvertFrom-Json
$bitbucket_url_create_pull_request = $bitbucket_pull_requests_url + $result_json_content.id + '/merge/'
Write-Host "API Url: " $bitbucket_url_create_pull_request
#Merge pull request
try{
$result = Invoke-WebRequest -Headers $headers -UseBasicParsing $bitbucket_url_create_pull_request -ContentType "application/json" -Method POST -Body $json_create_pull_request
Write-Host $result
if ($result.StatusCode -eq "200")
{
Write-Host "Result: <" $branchSource "> successfully merged to <" $branchDestination ">"
}
}
catch
{
$result = $_ | ConvertFrom-Json
Write-Host $_
Write-TeamcityError $result.error.message
# We need to remove pull request If we've got some error
Write-Host "Removing pull request"
Invoke-WebRequest -Headers $headers -UseBasicParsing ($bitbucket_pull_requests_url + $result_json_content.id + '/decline/') -ContentType "application/json" -Method POST -Body $json_create_pull_request
exit 1
}
}
}
catch
{
$error_msg = $_
try
{
$result = $error_msg | ConvertFrom-Json
#Hack for There are no changes to be pulled
if ($result.error.message -eq "There are no changes to be pulled")
{
Write-Host $error_msg
exit 0
}
else
{
Write-TeamcityError $result.error.message
}
}
catch
{
Write-TeamcityError $error_msg
}
}