18
18
19
19
# Script to check whether a PR references an issue with label "Good First Issue".
20
20
#
21
- # Usage: references_good_first_issue PR_NUMBER
21
+ # Usage: references_good_first_issue <pr_number>
22
22
#
23
23
# Arguments:
24
24
#
25
- # PR_NUMBER Pull request number.
25
+ # pr_number Pull request number.
26
26
#
27
27
# Environment variables:
28
28
#
@@ -34,19 +34,15 @@ set -o pipefail
34
34
35
35
# VARIABLES #
36
36
37
- # Get the pull request number:
37
+ # Resolve the pull request number:
38
38
pr_number=" $1 "
39
39
40
40
# GitHub API base URL:
41
- GITHUB_API_URL =" https://api.github.com"
41
+ github_api_url =" https://api.github.com"
42
42
43
43
# Repository owner and name:
44
- REPO_OWNER=" stdlib-js"
45
- REPO_NAME=" stdlib"
46
-
47
- # Exit codes:
48
- SUCCESS=0
49
- ERROR=1
44
+ repo_owner=" stdlib-js"
45
+ repo_name=" stdlib"
50
46
51
47
52
48
# FUNCTIONS #
@@ -59,7 +55,7 @@ on_error() {
59
55
exit " $1 "
60
56
}
61
57
62
- # Makes GitHub API requests .
58
+ # Performs a GitHub API request .
63
59
#
64
60
# $1 - HTTP method (GET or POST)
65
61
# $2 - API endpoint
@@ -73,66 +69,71 @@ github_api() {
73
69
local headers=()
74
70
75
71
# If GITHUB_TOKEN is set, add the Authorization header:
76
- if [ -n " $GITHUB_TOKEN " ]; then
77
- headers+=(" -H" " Authorization: token $GITHUB_TOKEN " )
72
+ if [ -n " ${ GITHUB_TOKEN} " ]; then
73
+ headers+=(" -H" " Authorization: token ${ GITHUB_TOKEN} " )
78
74
fi
79
75
80
76
# Determine the HTTP method and construct the curl command accordingly...
81
- case " $method " in
77
+ case " ${ method} " in
82
78
GET)
83
- curl -s " ${headers[@]} " " $GITHUB_API_URL$ endpoint "
79
+ curl -s " ${headers[@]} " " ${github_api_url}${ endpoint} "
84
80
;;
85
81
POST)
86
82
# For POST requests, always set the Content-Type header:
87
83
headers+=(" -H" " Content-Type: application/json" )
88
84
89
85
# If data is provided, include it in the request:
90
- if [ -n " $data " ]; then
91
- curl -s -X POST " ${headers[@]} " -d " $data " " $GITHUB_API_URL$ endpoint "
86
+ if [ -n " ${ data} " ]; then
87
+ curl -s -X POST " ${headers[@]} " -d " ${ data} " " ${github_api_url}${ endpoint} "
92
88
else
93
89
# Handle cases where POST data is required but not provided:
94
- echo " POST request requires data."
95
- on_error $ERROR
90
+ echo " ERROR: POST request requires data."
91
+ on_error 1
96
92
fi
97
93
;;
98
94
* )
99
- echo " Invalid HTTP method: $method "
100
- on_error $ERROR
95
+ echo " ERROR: Invalid HTTP method: ${ method} . "
96
+ on_error 1
101
97
;;
102
98
esac
103
99
}
104
100
105
101
# Main execution sequence.
106
102
main () {
107
- if [ -z " $pr_number " ]; then
108
- echo " ERROR: Pull request number is required" >&2
109
- on_error $ERROR
103
+ local issue_numbers
104
+ local issue_details
105
+ local pr_details
106
+ local pr_body
107
+ local issue
108
+ local bool
109
+
110
+ if [ -z " ${pr_number} " ]; then
111
+ echo " ERROR: Pull request number is required." >&2
112
+ on_error 1
110
113
fi
111
114
112
115
# Fetch pull request details:
113
- pr_details=$( github_api " GET" " /repos/$REPO_OWNER /$REPO_NAME /pulls/$pr_number " )
114
- pr_title=$( echo " $pr_details " | jq -r ' .title' )
115
- pr_body=$( echo " $pr_details " | jq -r ' .body' )
116
+ pr_details=$( github_api " GET" " /repos/${repo_owner} /${repo_name} /pulls/${pr_number} " )
117
+ pr_body=$( echo " ${pr_details} " | jq -r ' .body' )
116
118
117
- issue_numbers=$( echo $ pr_body | grep -Ei ' #[0-9]+' | grep -oEi ' [0-9]+' | sort | uniq)
118
- if [ -z " $issue_numbers " ]; then
119
+ issue_numbers=$( echo " ${ pr_body} " | grep -Ei ' #[0-9]+' | grep -oEi ' [0-9]+' | sort | uniq)
120
+ if [ -z " ${ issue_numbers} " ]; then
119
121
echo ' false'
120
- exit $SUCCESS
122
+ exit 0
121
123
fi
122
124
123
- for issue in $issue_numbers ; do
124
- issue_details=$( github_api " GET" " /repos/$REPO_OWNER / $REPO_NAME /issues/$issue " )
125
+ for issue in ${ issue_numbers} ; do
126
+ issue_details=$( github_api " GET" " /repos/${repo_owner} / ${repo_name} /issues/${ issue} " )
125
127
126
- bool=$( echo $ issue_details | jq ' .labels | any(.name == "Good First Issue")' )
127
- if [ " $bool " == ' true' ]; then
128
+ bool=$( echo " ${ issue_details} " | jq ' .labels | any(.name == "Good First Issue")' )
129
+ if [ " ${ bool} " == ' true' ]; then
128
130
echo ' true'
129
- exit $SUCCESS
131
+ exit 0
130
132
fi
131
133
done
132
134
133
135
echo ' false'
134
- exit $SUCCESS
136
+ exit 0
135
137
}
136
138
137
- # Call main with all command-line arguments:
138
- main " $@ "
139
+ main
0 commit comments