-
Notifications
You must be signed in to change notification settings - Fork 1
/
Github-Jira-interaction.ps1
113 lines (94 loc) · 3.96 KB
/
Github-Jira-interaction.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
110
111
112
113
$ErrorActionPreference = 'SilentlyContinue'
function Initialize-JiraGithubCredentials()
{
Param([string]$envariable,
[string]$message)
$envvalue = [environment]::GetEnvironmentVariable($envariable,"User")
if(!$envvalue)
{
$envvalue = Read-Host "Please provide $message "
[Environment]::SetEnvironmentVariable($envariable, $envvalue, "User")
}
return $envvalue
}
#insufficient Posh-Github function Get-GitHubPullRequests so have to create our own
function Find-ExistingPullRequest()
{
Param([string]$Base,
[string]$Owner,
[string]$Repository,
[string]$State)
$totalCount = 0
#get head
$localUser = git remote -v show |
? { $_ -match 'origin\t.*github.com\/(.*)\/.* \((fetch|push)\)' } |
% { $matches[1] } |
Select -First 1
$branchName = git symbolic-ref -q HEAD |
% { $_ -replace 'refs/heads/', ''}
$Head = "$($localUser):$($branchName)"
#Write-Host "Getting $State pull requests for $Owner/$Repository from $head to $base"
$uri = "https://api.github.com/repos/$Owner/$Repository/pulls?access_token=${Env:\GITHUB_OAUTH_TOKEN}&base=$Base&head=$Head&state=$State"
$global:GITHUB_API_OUTPUT = Invoke-RestMethod -Uri $uri
$global:GITHUB_API_OUTPUT |
% {
$totalCount++
$created = [DateTime]::Parse($_.created_at)
$updated = [DateTime]::Parse($_.updated_at)
$open = ([DateTime]::Now - $created).ToString('%d')
Write-Host "`n$($_.number) from $($_.user.login) - $($_.title) "
Write-Host "`tOpen for $open day(s) / Last Updated - $($updated.ToString('g'))"
Write-Host "`t$($_.issue_url)"
}
Write-Host "`nFound $totalCount $State pull requests for $Owner/$Repository"
return $totalCount
}
# Git\Set Github and Jira username\passwords
$JiraUserName = Initialize-JiraGithubCredentials -envariable "jira_username" -message "Jira username"
$JiraPassword = Initialize-JiraGithubCredentials -envariable "jira_password" -message "Jira password"
$JiraBaseUrl = "https://adazzle.atlassian.net"
$GithubToken = [environment]::GetEnvironmentVariable("GITHUB_OAUTH_TOKEN","User")
if (!$GithubToken){
$UserName = Read-Host "Please provide Github username "
$Password = Read-Host "Please provide Github password "
New-GitHubOAuthToken -Username $UserName -Password $Password
}else{
$env:GITHUB_OAUTH_TOKEN = $GithubToken
}
$TotalExistingPRs = Find-ExistingPullRequest -Base 'dev' -Owner "adazzle" -Repository "mediaAppSln" -State "open"
if ($TotalExistingPRs -eq "0")
{
Write-Host "Create new PR"
$JiraIssue = Read-Host "Specify Jira issue no(example: AD-1234)"
if ($JiraIssue)
{
$headers = @{Authorization="Basic " + [Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("${JiraUserName}:$JiraPassword"))}
[Uri]$uri = "$JiraBaseUrl/rest/api/2/issue/$JiraIssue/"
#Write-Host $issue
try{
$result=Invoke-RestMethod -Uri $uri -Headers ($headers) -ContentType "application/json"
}catch{}
if($result){
#Write-Host "Get title for PR"
$summary = ($result | Select -ExpandProperty fields) | Select -ExpandProperty summary
#Write-Host "$summary"
New-GitHubPullRequest -Title "$summary - [$JiraIssue]" -Body "$JiraBaseUrl/browse/$JiraIssue" -Base 'dev' -Owner "adazzle" -Repository "mediaAppSln"
$html_url = "PR:" + ($GITHUB_API_OUTPUT | Select -ExpandProperty html_url)
$commits_url = "Commits:" + ($GITHUB_API_OUTPUT | Select -ExpandProperty commits_url)
[Uri]$uri = "$JiraBaseUrl/rest/api/2/issue/$JiraIssue/comment"
$issue = @{
body = "$html_url $commits_url"
} | ConvertTo-Json
#Write-Host $issue
Write-Host "Going to create PR link Jira issue"
$result=Invoke-RestMethod -Uri $uri -Headers ($headers) -Method Post -Body $issue -ContentType "application/json"
Write-Host "PR link created in Jira issue"
}else{
Write-Host "This is not a valid Jira issue"
}
}else{
Write-Host "With no Jira issue provided, this git helper is useless so Please create PR in Github."
}
}else{
Write-Host "One PR already exists - another cannot be created."
}