Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 159 additions & 0 deletions .github/workflows/scripts/rate_limit_contributions
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
#!/usr/bin/env 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 prevent contributors from opening too many "Good First PR"s.
#
# Usage: rate_limit_contributions <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 #

# Resolve the pull request number:
pr_number="$1"

# Threshold of open "Good First PR"s above which to post comment:
max_good_first_prs=5

# GitHub API base URL:
github_api_url="https://api.github.com"

# Repository owner and name:
repo_owner="stdlib-js"
repo_name="stdlib"


# FUNCTIONS #

# Error handler.
#
# $1 - error status
on_error() {
echo 'ERROR: An error was encountered during execution.' >&2
exit "$1"
}

# Prints a success message.
print_success() {
echo 'Success!' >&2
}

# Performs a GitHub API request.
#
# $1 - HTTP method (GET or POST)
# $2 - API endpoint
# $3 - data for POST 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 "ERROR: POST request requires data."
on_error 1
fi
;;
*)
echo "ERROR: Invalid HTTP method: ${method}."
on_error 1
;;
esac
}

# Main execution sequence.
main() {
local num_good_first_prs
local good_first_pr
local pr_details
local pr_author
local user_prs

if [ -z "${pr_number}" ]; then
echo "ERROR: Pull request number is required." >&2
on_error 1
fi

# Fetch pull request details:
pr_details=$(github_api "GET" "/repos/${repo_owner}/${repo_name}/pulls/${pr_number}")
good_first_pr=$(echo "${pr_details}" | jq -r '.labels | any(.name == "Good First PR")')

pr_author=$(echo "${pr_details}" | jq -r '.user.login')

# Fetch other PRs of the same user:
user_prs=$(github_api "GET" "/search/issues?q=state%3Aopen+author%3A${pr_author}+type%3Apr")

# Count number of PRs labeled "Good First PR":
num_good_first_prs=$(echo "${user_prs}" | jq -r '.items | map( select( .labels | any( .name == "Good First PR" ))) | length')

if [ "${num_good_first_prs}" -gt "${max_good_first_prs}" ]; then
# Post a comment on the PR:
comment="Hello! 👋

We've noticed that you've been opening a number of PRs addressing good first issues. Thank you for your interest and enthusiasm!

Now that you've made a few contributions, we suggest no longer working on good first issues. Instead, we encourage you to prioritize cleaning up any PRs which have yet to be merged and then proceed to work on more involved tasks.

Not only does this ensure that other new contributors can work on things and get ramped up on all things stdlib, it also ensures that you can spend your time on more challenging problems. 🚀

For ideas for future PRs, feel free to search the codebase for TODOs and FIXMEs and be sure to check out other open issues on the issue tracker. Cheers!"

if ! github_api "POST" "/repos/${repo_owner}/${repo_name}/issues/${pr_number}/comments" "{\"body\":$(echo "${comment}" | jq -R -s -c .)}"; then
echo "Failed to post comment on PR."
on_error 1
fi

exit 1
else
echo "Number of PRs below threshold."
print_success
exit 0
fi
}

main
70 changes: 70 additions & 0 deletions .github/workflows/too_many_good_first_prs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#/
# @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.
#/

# Workflow name:
name: too_many_good_first_prs

# Workflow triggers:
on:
pull_request_target:
types:
- opened

# Workflow jobs:
jobs:

# Define a job which encourages contributors opening too many "Good First PRs" to tackle more challenging problems:
check_num_good_first_prs:

# Define job name:
name: 'Check if contributor has opened too many "Good First PRs"'

# Only run this job if the pull request did not have label `automated-pr`:
if: contains(github.event.pull_request.labels.*.name, 'automated-pr') == false

# Define job permissions:
permissions:
contents: read
pull-requests: write

# Define the type of virtual host machine:
runs-on: ubuntu-latest

# Define the sequence of job steps:
steps:
# Checkout the repository:
- name: 'Checkout repository'
# Pin action to full length commit SHA
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
# Specify whether to remove untracked files before checking out the repository:
clean: true

# Limit clone depth to the most recent commit:
fetch-depth: 1

# Specify whether to download Git-LFS files:
lfs: false
timeout-minutes: 10

# Prevent contributors from opening too many "Good First PR"s:
- name: 'Prevent contributors from opening too many "Good First PR"s'
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
. "$GITHUB_WORKSPACE/.github/workflows/scripts/rate_limit_contributions" $PR_NUMBER