forked from stdlib-js/stdlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_pr
More file actions
executable file
·148 lines (120 loc) · 4.25 KB
/
merge_pr
File metadata and controls
executable file
·148 lines (120 loc) · 4.25 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/bin/bash
#/
# @license Apache-2.0
#
# Copyright (c) 2025 The Stdlib Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#/
# Script to merge a pull request.
#
# Usage: merge_pr PR_NUMBER
#
# Arguments:
#
# PR_NUMBER Pull request number.
#
# Environment variables:
#
# GITHUB_TOKEN GitHub token for authentication.
# Ensure that the exit status of pipelines is non-zero in the event that at least one of the commands in a pipeline fails:
set -o pipefail
# VARIABLES #
# Get the pull request number:
pr_number="$1"
# GitHub API base URL:
GITHUB_API_URL="https://api.github.com"
# Repository owner and name:
REPO_OWNER="stdlib-js"
REPO_NAME="stdlib"
# Exit codes:
SUCCESS=0
ERROR=1
# Identifier for PR commit message comments:
COMMENT_IDENTIFIER="<!-- PR_COMMIT_MESSAGE -->"
# FUNCTIONS #
# Error handler.
on_error() {
echo "ERROR: $1" >&2
exit $ERROR
}
# Makes GitHub API requests.
github_api() {
local method="$1"
local endpoint="$2"
local data="$3"
# Initialize an array to hold curl headers:
local headers=()
# If GITHUB_TOKEN is set, add the Authorization header:
if [ -n "$GITHUB_TOKEN" ]; then
headers+=("-H" "Authorization: token $GITHUB_TOKEN")
fi
# Determine the HTTP method and construct the curl command accordingly:
case "$method" in
GET)
curl -s "${headers[@]}" "$GITHUB_API_URL$endpoint"
;;
POST)
# For POST requests, always set the Content-Type header:
headers+=("-H" "Content-Type: application/json")
# If data is provided, include it in the request:
if [ -n "$data" ]; then
curl -s -X POST "${headers[@]}" -d "$data" "$GITHUB_API_URL$endpoint"
else
# Handle cases where POST data is required but not provided:
echo "POST request requires data."
on_error "POST request requires data"
fi
;;
*)
on_error "Invalid HTTP method: $method"
;;
esac
}
# Main execution sequence.
main() {
# Check if PR number is provided:
if [ -z "$pr_number" ]; then
on_error "PR number is required"
fi
# Fetch PR comments to look for commit message comment:
comments=$(github_api "GET" "/repos/$REPO_OWNER/$REPO_NAME/issues/$pr_number/comments")
# Look for the PR commit message comment with the identifier:
commit_message_comment=$(echo "$comments" | jq -r --arg id "$COMMENT_IDENTIFIER" '.[] | select(.body | contains($id)) | .body')
if [ -z "$commit_message_comment" ]; then
on_error "No PR commit message comment found for PR #$pr_number"
fi
# Extract commit message from the Markdown code block in the comment:
commit_message=$(echo "$commit_message_comment" | awk '/```text/{flag=1;next}/```/{flag=0}flag')
if [ -z "$commit_message" ]; then
on_error "Couldn't extract commit message from PR comment"
fi
# Extract subject (first line) and body (everything after the first line):
commit_subject=$(echo "$commit_message" | head -n 1)
commit_body=$(echo "$commit_message" | tail -n +2)
commit_body=$(echo "$commit_body" | awk 'NF {p=1} p') # Trim leading empty lines
if [ -z "$commit_subject" ]; then
on_error "Couldn't extract commit subject from PR commit message"
fi
if [ -z "$commit_body" ]; then
on_error "Couldn't extract commit body from PR commit message"
fi
# Squash and merge the PR with the extracted subject and body:
if ! gh pr merge "$pr_number" --admin --squash --subject "$commit_subject" --body "$commit_body"; then
on_error "Failed to merge PR #$pr_number"
fi
echo "Successfully merged PR #$pr_number"
exit $SUCCESS
}
# Call main with all command-line arguments:
main "$@"