-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCreate-PullRequest.ps1
130 lines (111 loc) · 3.67 KB
/
Create-PullRequest.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
Param (
[switch]$all,
[string]$filter,
[switch]$help
)
# Define the base branch (e.g., main or master)
$BASE_BRANCH = "main"
# Function to display help message
function Show-Help {
Write-Output "Usage: .\New-ChangeSet.ps1 [--all] [--filter <regex>] [--help]"
Write-Output ""
Write-Output "Options:"
Write-Output " --all Include 'Chores' and 'Other' sections in the output."
Write-Output " --filter <regex> Filter out commits matching the given regular expression."
Write-Output " --help Show this help message and exit."
}
# Parse command-line arguments
$show_all = $false
$filter_regex = ""
if ($help) {
Show-Help
exit 0
}
if ($all) {
$show_all = $true
}
if ($filter) {
$filter_regex = $filter
}
# Get the current branch name
$current_branch = git rev-parse --abbrev-ref HEAD
# Extract the issue identifier from the branch name
$issue_identifier = if ($current_branch -match '(patch|feature)/[a-z0-9]+-[0-9]+') { $matches[0] -match '[A-Z]+-[0-9]+'; $matches[0] }
# Fetch the commit messages from the current branch that are not in the base branch and reverse the order
$commits = git log "$BASE_BRANCH..HEAD" --pretty=format:"%s" | ForEach-Object { $_ } | Sort-Object { $_ } -Descending
# Initialize the changeset
$changeset = ""
# Initialize variables for each conventional commit type
$docs_commits = ""
$feat_commits = ""
$fix_commits = ""
$test_commits = ""
$chore_commits = ""
$ci_commits = ""
$other_commits = ""
# Filter and format the commits
foreach ($commit in $commits) {
# Apply filter if specified
if ($filter_regex -ne "" -and $commit -match $filter_regex) {
continue
}
# Extract the conventional commit type and the message
if ($commit -match '^(Merge.*|.*\(#\d+\))$') {
continue
}
if ($commit -match '^(feat|fix|docs|test|chore|ci)(\([^\)]+\))?:\s*(.*)$') {
$commit_type = $matches[1]
$commit_message = $matches[3]
switch ($commit_type) {
"docs" { $docs_commits += "- $commit_message`n" }
"feat" { $feat_commits += "- $commit_message`n" }
"test" { $test_commits += "- $commit_message`n" }
"fix" { $fix_commits += "- $commit_message`n" }
"chore" { $chore_commits += "- $commit_message`n" }
default { $other_commits += "- $commit_message`n" }
}
}
else {
if ($show_all) {
$other_commits += "- $commit`n"
}
}
}
# Format the changeset
if ($feat_commits -ne "") {
$changeset += "### Features:`n`n$feat_commits`n"
}
if ($fix_commits -ne "") {
$changeset += "### Fixes:`n`n$fix_commits`n"
}
if ($test_commits -ne "") {
$changeset += "### Tests:`n`n$test_commits`n"
}
if ($docs_commits -ne "") {
$changeset += "### Documentation:`n`n$docs_commits`n"
}
if ($ci_commits -ne "") {
$changeset += "### Continuous Integration:`n`n$ci_commits`n"
}
if ($show_all) {
if ($chore_commits -ne "") {
$changeset += "### Chores:`n`n$chore_commits`n"
}
if ($other_commits -ne "") {
$changeset += "### Other:`n`n$other_commits`n"
}
}
# Find the first pull_request_template.md file in the repository
$template_file = Get-ChildItem -Recurse -Filter "pull_request_template.md" -Force | Select-Object -First 1
# Check if the template file was found
if (-not $template_file) {
Write-Warning "pull_request_template.md file not found in the repository."
}
else {
# Read the pull request template
$template_content = Get-Content -Path $template_file.FullName -Raw
}
# Append updated content to end of the template
$updated_content = $template_content + $changeset
# Output the updated content
Write-Output $updated_content