Skip to content

Commit d85208a

Browse files
committed
style: clean-up and follow conventions
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: na - task: lint_package_json status: na - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: na - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: na - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: na - task: lint_license_headers status: passed ---
1 parent 1b8be24 commit d85208a

File tree

1 file changed

+91
-79
lines changed

1 file changed

+91
-79
lines changed

.github/workflows/scripts/merge_pr

Lines changed: 91 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ set -o pipefail
3636
# VARIABLES #
3737

3838
# Get the pull request number:
39-
pr_number="$1"
39+
pr_number="${1}"
4040

4141
# GitHub API base URL:
4242
GITHUB_API_URL="https://api.github.com"
@@ -56,93 +56,105 @@ COMMENT_IDENTIFIER="<!-- PR_COMMIT_MESSAGE -->"
5656
# FUNCTIONS #
5757

5858
# Error handler.
59+
#
60+
# $1 - error message
5961
on_error() {
60-
echo "ERROR: $1" >&2
61-
exit $ERROR
62+
echo "ERROR: ${1}" >&2
63+
exit ${ERROR}
6264
}
6365

6466
# Makes GitHub API requests.
67+
#
68+
# $1 - HTTP method
69+
# $2 - API endpoint
70+
# $3 - request data (optional)
6571
github_api() {
66-
local method="$1"
67-
local endpoint="$2"
68-
local data="$3"
69-
70-
# Initialize an array to hold curl headers:
71-
local headers=()
72-
73-
# If GITHUB_TOKEN is set, add the Authorization header:
74-
if [ -n "$GITHUB_TOKEN" ]; then
75-
headers+=("-H" "Authorization: token $GITHUB_TOKEN")
76-
fi
77-
78-
# Determine the HTTP method and construct the curl command accordingly:
79-
case "$method" in
80-
GET)
81-
curl -s "${headers[@]}" "$GITHUB_API_URL$endpoint"
82-
;;
83-
POST)
84-
# For POST requests, always set the Content-Type header:
85-
headers+=("-H" "Content-Type: application/json")
86-
87-
# If data is provided, include it in the request:
88-
if [ -n "$data" ]; then
89-
curl -s -X POST "${headers[@]}" -d "$data" "$GITHUB_API_URL$endpoint"
90-
else
91-
# Handle cases where POST data is required but not provided:
92-
echo "POST request requires data."
93-
on_error "POST request requires data"
94-
fi
95-
;;
96-
*)
97-
on_error "Invalid HTTP method: $method"
98-
;;
99-
esac
72+
local method="${1}"
73+
local endpoint="${2}"
74+
local data="${3}"
75+
76+
# Initialize an array to hold curl headers:
77+
local headers=()
78+
79+
# If GITHUB_TOKEN is set, add the Authorization header:
80+
if [ -n "${GITHUB_TOKEN}" ]; then
81+
headers+=("-H" "Authorization: token ${GITHUB_TOKEN}")
82+
fi
83+
84+
# Determine the HTTP method and construct the curl command accordingly:
85+
case "${method}" in
86+
GET)
87+
curl -s "${headers[@]}" "${GITHUB_API_URL}${endpoint}"
88+
;;
89+
POST)
90+
# For POST requests, always set the Content-Type header:
91+
headers+=("-H" "Content-Type: application/json")
92+
93+
# If data is provided, include it in the request:
94+
if [ -n "${data}" ]; then
95+
curl -s -X POST "${headers[@]}" -d "${data}" "${GITHUB_API_URL}${endpoint}"
96+
else
97+
# Handle cases where POST data is required but not provided:
98+
echo "POST request requires data."
99+
on_error "POST request requires data"
100+
fi
101+
;;
102+
*)
103+
on_error "Invalid HTTP method: ${method}"
104+
;;
105+
esac
100106
}
101107

102108
# Main execution sequence.
103109
main() {
104-
# Check if PR number is provided:
105-
if [ -z "$pr_number" ]; then
106-
on_error "PR number is required"
107-
fi
108-
109-
# Fetch PR comments to look for commit message comment:
110-
comments=$(github_api "GET" "/repos/$REPO_OWNER/$REPO_NAME/issues/$pr_number/comments")
111-
112-
# Look for the PR commit message comment with the identifier:
113-
commit_message_comment=$(echo "$comments" | jq -r --arg id "$COMMENT_IDENTIFIER" '.[] | select(.body | contains($id)) | .body')
114-
115-
if [ -z "$commit_message_comment" ]; then
116-
on_error "No PR commit message comment found for PR #$pr_number"
117-
fi
118-
119-
# Extract commit message from the Markdown code block in the comment:
120-
commit_message=$(echo "$commit_message_comment" | awk '/```text/{flag=1;next}/```/{flag=0}flag')
121-
122-
if [ -z "$commit_message" ]; then
123-
on_error "Couldn't extract commit message from PR comment"
124-
fi
125-
126-
# Extract subject (first line) and body (everything after the first line):
127-
commit_subject=$(echo "$commit_message" | head -n 1)
128-
commit_body=$(echo "$commit_message" | tail -n +2)
129-
commit_body=$(echo "$commit_body" | awk 'NF {p=1} p') # Trim leading empty lines
130-
131-
if [ -z "$commit_subject" ]; then
132-
on_error "Couldn't extract commit subject from PR commit message"
133-
fi
134-
if [ -z "$commit_body" ]; then
135-
on_error "Couldn't extract commit body from PR commit message"
136-
fi
137-
138-
# Squash and merge the PR with the extracted subject and body:
139-
if ! gh pr merge "$pr_number" --admin --squash --subject "$commit_subject" --body "$commit_body"; then
140-
on_error "Failed to merge PR #$pr_number"
141-
fi
142-
143-
echo "Successfully merged PR #$pr_number"
144-
exit $SUCCESS
110+
local commit_message_comment
111+
local commit_message
112+
local commit_subject
113+
local commit_body
114+
local comments
115+
116+
# Check if PR number is provided:
117+
if [ -z "${pr_number}" ]; then
118+
on_error "PR number is required"
119+
fi
120+
121+
# Fetch PR comments to look for commit message comment:
122+
comments=$(github_api "GET" "/repos/${REPO_OWNER}/${REPO_NAME}/issues/${pr_number}/comments")
123+
124+
# Look for the PR commit message comment with the identifier:
125+
commit_message_comment=$(echo "${comments}" | jq -r --arg id "${COMMENT_IDENTIFIER}" '.[] | select(.body | contains($id)) | .body')
126+
127+
if [ -z "${commit_message_comment}" ]; then
128+
on_error "No PR commit message comment found for PR #${pr_number}"
129+
fi
130+
131+
# Extract commit message from the Markdown code block in the comment:
132+
commit_message=$(echo "${commit_message_comment}" | awk '/```text/{flag=1;next}/```/{flag=0}flag')
133+
134+
if [ -z "${commit_message}" ]; then
135+
on_error "Couldn't extract commit message from PR comment"
136+
fi
137+
138+
# Extract subject (first line) and body (everything after the first line):
139+
commit_subject=$(echo "${commit_message}" | head -n 1)
140+
commit_body=$(echo "${commit_message}" | tail -n +2)
141+
commit_body=$(echo "${commit_body}" | awk 'NF {p=1} p') # Trim leading empty lines
142+
143+
if [ -z "${commit_subject}" ]; then
144+
on_error "Couldn't extract commit subject from PR commit message"
145+
fi
146+
if [ -z "${commit_body}" ]; then
147+
on_error "Couldn't extract commit body from PR commit message"
148+
fi
149+
150+
# Squash and merge the PR with the extracted subject and body:
151+
if ! gh pr merge "${pr_number}" --admin --squash --subject "${commit_subject}" --body "${commit_body}"; then
152+
on_error "Failed to merge PR #${pr_number}"
153+
fi
154+
155+
echo "Successfully merged PR #${pr_number}"
156+
exit ${SUCCESS}
145157
}
146158

147159
# Call main with all command-line arguments:
148-
main "$@"
160+
main "${@}"

0 commit comments

Comments
 (0)