|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# @license Apache-2.0 |
| 4 | +# |
| 5 | +# Copyright (c) 2025 The Stdlib Authors. |
| 6 | +# |
| 7 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +# you may not use this file except in compliance with the License. |
| 9 | +# You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | + |
| 19 | +# Script to check whether a PR references an issue with label "Good First Issue". |
| 20 | +# |
| 21 | +# Usage: references_good_first_issue <pr_number> |
| 22 | +# |
| 23 | +# Arguments: |
| 24 | +# |
| 25 | +# pr_number Pull request number. |
| 26 | +# |
| 27 | +# Environment variables: |
| 28 | +# |
| 29 | +# GITHUB_TOKEN GitHub token for authentication. |
| 30 | + |
| 31 | +# Ensure that the exit status of pipelines is non-zero in the event that at least one of the commands in a pipeline fails: |
| 32 | +set -o pipefail |
| 33 | + |
| 34 | + |
| 35 | +# VARIABLES # |
| 36 | + |
| 37 | +# Resolve the pull request number: |
| 38 | +pr_number="$1" |
| 39 | + |
| 40 | +# GitHub API base URL: |
| 41 | +github_api_url="https://api.github.com" |
| 42 | + |
| 43 | +# Repository owner and name: |
| 44 | +repo_owner="stdlib-js" |
| 45 | +repo_name="stdlib" |
| 46 | + |
| 47 | + |
| 48 | +# FUNCTIONS # |
| 49 | + |
| 50 | +# Error handler. |
| 51 | +# |
| 52 | +# $1 - error status |
| 53 | +on_error() { |
| 54 | + echo 'ERROR: An error was encountered during execution.' >&2 |
| 55 | + exit "$1" |
| 56 | +} |
| 57 | + |
| 58 | +# Performs a GitHub API request. |
| 59 | +# |
| 60 | +# $1 - HTTP method (GET or POST) |
| 61 | +# $2 - API endpoint |
| 62 | +# $3 - data for POST requests |
| 63 | +github_api() { |
| 64 | + local method="$1" |
| 65 | + local endpoint="$2" |
| 66 | + local data="$3" |
| 67 | + |
| 68 | + # Initialize an array to hold curl headers: |
| 69 | + local headers=() |
| 70 | + |
| 71 | + # If GITHUB_TOKEN is set, add the Authorization header: |
| 72 | + if [ -n "${GITHUB_TOKEN}" ]; then |
| 73 | + headers+=("-H" "Authorization: token ${GITHUB_TOKEN}") |
| 74 | + fi |
| 75 | + |
| 76 | + # Determine the HTTP method and construct the curl command accordingly... |
| 77 | + case "${method}" in |
| 78 | + GET) |
| 79 | + curl -s "${headers[@]}" "${github_api_url}${endpoint}" |
| 80 | + ;; |
| 81 | + POST) |
| 82 | + # For POST requests, always set the Content-Type header: |
| 83 | + headers+=("-H" "Content-Type: application/json") |
| 84 | + |
| 85 | + # If data is provided, include it in the request: |
| 86 | + if [ -n "${data}" ]; then |
| 87 | + curl -s -X POST "${headers[@]}" -d "${data}" "${github_api_url}${endpoint}" |
| 88 | + else |
| 89 | + # Handle cases where POST data is required but not provided: |
| 90 | + echo "ERROR: POST request requires data." |
| 91 | + on_error 1 |
| 92 | + fi |
| 93 | + ;; |
| 94 | + *) |
| 95 | + echo "ERROR: Invalid HTTP method: ${method}." |
| 96 | + on_error 1 |
| 97 | + ;; |
| 98 | + esac |
| 99 | +} |
| 100 | + |
| 101 | +# Main execution sequence. |
| 102 | +main() { |
| 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 |
| 113 | + fi |
| 114 | + |
| 115 | + # Fetch pull request details: |
| 116 | + pr_details=$(github_api "GET" "/repos/${repo_owner}/${repo_name}/pulls/${pr_number}") |
| 117 | + pr_body=$(echo "${pr_details}" | jq -r '.body') |
| 118 | + |
| 119 | + issue_numbers=$(echo "${pr_body}" | grep -Ei '#[0-9]+' | grep -oEi '[0-9]+' | sort | uniq) |
| 120 | + if [ -z "${issue_numbers}" ]; then |
| 121 | + echo 'false' |
| 122 | + exit 0 |
| 123 | + fi |
| 124 | + |
| 125 | + for issue in ${issue_numbers}; do |
| 126 | + issue_details=$(github_api "GET" "/repos/${repo_owner}/${repo_name}/issues/${issue}") |
| 127 | + |
| 128 | + bool=$(echo "${issue_details}" | jq '.labels | any(.name == "Good First Issue")') |
| 129 | + if [ "${bool}" == 'true' ]; then |
| 130 | + echo 'true' |
| 131 | + exit 0 |
| 132 | + fi |
| 133 | + done |
| 134 | + |
| 135 | + echo 'false' |
| 136 | + exit 0 |
| 137 | +} |
| 138 | + |
| 139 | +main |
0 commit comments