-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathUpdate-GitHubIssueAndPullRequest.ps1
89 lines (75 loc) · 2.22 KB
/
Update-GitHubIssueAndPullRequest.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
param(
[string] $JiraBaseUrl,
[string] $GitHubRepository,
[string] $Branch,
[string] $Title,
[string] $Body,
[string] $Assignee,
[string] $PullRequestId
)
$jiraBrowseUrl = $JiraBaseUrl.TrimEnd('/') + '/browse/'
$originalTitle = $Title
$originalBody = $Body
if ($Branch -NotLike '*issue*')
{
Exit
}
$Branch -match '(\w+-\d+)' | Out-Null
if ($null -eq $Matches)
{
throw "Issue key not found in the branch name '$Branch'!"
}
$issueKey = $Matches[0]
$issueLink = "[$issueKey]($jiraBrowseUrl$issuekey)"
if ($Title -NotLike "*$issueKey*")
{
$Title = $issueKey + ': ' + $Title
}
if (-Not $Body)
{
$Body = $issueLink
}
elseif ($Body -NotLike "*$issueKey*")
{
$Body = $issueLink + "`n" + $Body
}
elseif ($Body -NotLike "*``[$issueKey``]``($jiraBrowseUrl$issuekey``)*")
{
$Body = $Body.replace($issueKey, $issueLink)
}
if ((gh repo view $GitHubRepository --json hasIssuesEnabled | ConvertFrom-Json).hasIssuesEnabled)
{
$issueQuery = "$issueKey in:title"
$output = gh issue list --search $issueQuery --repo $GitHubRepository
$issueItem = ($output | Select-Object -First 1)
if ($issueItem)
{
$issueNumber = $issueItem -split '\t' | Select-Object -First 1
$fixesIssue = "Fixes #$issueNumber"
if ($Body -NotLike "*$fixesIssue*")
{
$Body = "$Body`n$fixesIssue"
}
if ($issueNumber)
{
gh api -X PATCH "/repos/$GitHubRepository/issues/$issueNumber" -f "assignee=$Assignee"
}
}
else
{
Write-Output "No issue was found with the query '$issueQuery' in the repository '$GitHubRepository'."
}
}
else
{
Write-Output "Issues are disabled in the '$GitHubRepository' repository."
}
if (($Title -ne $originalTitle) -or ($Body -ne $originalBody))
{
# Escape the quote characters. This is necessary because PowerShell mangles the quote characters when passing
# arguments into a native command such as the GitHub CLI. See https://github.com/cli/cli/issues/3425 for details.
$Title = $Title -replace '"', '\"'
$Body = $Body -replace '"', '\"'
# See https://cli.github.com/manual/gh_pr_edit
gh pr edit $PullRequestId --title $Title --body $Body --repo $GitHubRepository
}