|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# @license Apache-2.0 |
| 4 | +# |
| 5 | +# Copyright (c) 2024 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 if a pull request contains the checked contributing guidelines acknowledgment checkbox. |
| 20 | +# |
| 21 | +# Usage: check_contributing_guidelines 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 | +# VARIABLES # |
| 35 | + |
| 36 | +# Get the pull request number: |
| 37 | +pr_number="$1" |
| 38 | + |
| 39 | +# Set the repository name: |
| 40 | +GITHUB_REPOSITORY="stdlib-js/stdlib" |
| 41 | + |
| 42 | +# Set the contributing guidelines link: |
| 43 | +CONTRIBUTING_LINK="https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md" |
| 44 | + |
| 45 | +# FUNCTIONS # |
| 46 | + |
| 47 | +# Error handler. |
| 48 | +# |
| 49 | +# $1 - error status |
| 50 | +on_error() { |
| 51 | + echo 'ERROR: An error was encountered during execution.' >&2 |
| 52 | + exit "$1" |
| 53 | +} |
| 54 | + |
| 55 | +# Prints a success message. |
| 56 | +print_success() { |
| 57 | + echo 'Success!' >&2 |
| 58 | +} |
| 59 | + |
| 60 | +# Main execution sequence. |
| 61 | +main() { |
| 62 | + echo "Checking contributing guidelines acknowledgment for PR #${pr_number}..." |
| 63 | + |
| 64 | + # Fetch the pull request body: |
| 65 | + if ! pr_body=$(curl -s -H "Authorization: token ${GITHUB_TOKEN}" \ |
| 66 | + "https://api.github.com/repos/${GITHUB_REPOSITORY}/pulls/${pr_number}" | \ |
| 67 | + jq -r .body); then |
| 68 | + echo "Failed to fetch PR body." |
| 69 | + on_error 1 |
| 70 | + fi |
| 71 | + |
| 72 | + # Check for the contributing guidelines checkbox: |
| 73 | + if echo "${pr_body}" | grep -qE '^\s*-\s*\[x\]\s*Read,\s*understood,\s*and\s*followed\s*the\s*\[contributing\s*guidelines\]'; then |
| 74 | + echo "Contributing guidelines acknowledged." |
| 75 | + print_success |
| 76 | + exit 0 |
| 77 | + else |
| 78 | + echo "Contributing guidelines not acknowledged." |
| 79 | + |
| 80 | + # Post a comment on the PR: |
| 81 | + comment="Hello! Thank you for your contribution to stdlib. |
| 82 | +
|
| 83 | +We noticed that the contributing guidelines acknowledgment is missing from your pull request. Here's what you need to do: |
| 84 | +
|
| 85 | +1. Please read our [contributing guidelines][contributing]. |
| 86 | +2. Update your pull request description to include this checked box: |
| 87 | +
|
| 88 | + \`- [x] Read, understood, and followed the [contributing guidelines](https://github.com/stdlib-js/stdlib/blob/develop/CONTRIBUTING.md)\` |
| 89 | +
|
| 90 | +This acknowledgment confirms that you've read the guidelines, which include: |
| 91 | +
|
| 92 | +- The developer's certificate of origin |
| 93 | +- Your agreement to license your contributions under the project's terms |
| 94 | +
|
| 95 | +We can't review or accept contributions without this acknowledgment. |
| 96 | +
|
| 97 | +Thank you for your understanding and cooperation. We look forward to reviewing your contribution! |
| 98 | +
|
| 99 | +[contributing]: ${CONTRIBUTING_LINK}" |
| 100 | + |
| 101 | + if ! curl -s -X POST \ |
| 102 | + -H "Authorization: token ${GITHUB_TOKEN}" \ |
| 103 | + -H "Accept: application/vnd.github.v3+json" \ |
| 104 | + "https://api.github.com/repos/${GITHUB_REPOSITORY}/issues/${pr_number}/comments" \ |
| 105 | + -d "{\"body\":$(echo "$comment" | jq -R -s -c .)}"; then |
| 106 | + echo "Failed to post comment on PR." |
| 107 | + on_error 1 |
| 108 | + fi |
| 109 | + |
| 110 | + exit 1 |
| 111 | + fi |
| 112 | +} |
| 113 | + |
| 114 | +# Set an error handler to print captured output and perform any clean-up tasks: |
| 115 | +trap 'on_error' ERR |
| 116 | + |
| 117 | +# Run main: |
| 118 | +main |
0 commit comments