|
| 1 | +#!/bin/bash |
| 2 | +# ---------------------------------------------------------------------------- |
| 3 | +# Copyright 2026 The Netty Project |
| 4 | +# |
| 5 | +# The Netty Project licenses this file to you under the Apache License, |
| 6 | +# version 2.0 (the "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at: |
| 8 | +# |
| 9 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | +# License for the specific language governing permissions and limitations |
| 15 | +# under the License. |
| 16 | +# ---------------------------------------------------------------------------- |
| 17 | +# |
| 18 | +# Checks whether a GitHub user has signed the Netty ICLA by looking them up in |
| 19 | +# the ICLA spreadsheet (column "Github user name"). |
| 20 | +# |
| 21 | +# Usage: |
| 22 | +# scripts/check_icla.sh <github-user | PR number | PR URL> [...] |
| 23 | +# |
| 24 | +# A PR number or URL is resolved to the PR author via the GitHub CLI (gh); a PR |
| 25 | +# number refers to netty/netty unless GITHUB_REPOSITORY is set. |
| 26 | +# |
| 27 | +# The sheet is fetched as CSV. If it is not publicly readable, the script falls |
| 28 | +# back to a gcloud access token (`gcloud auth login --enable-gdrive-access` first). You can also set |
| 29 | +# ICLA_SHEET_TOKEN to an OAuth access token with Drive read access. |
| 30 | +# |
| 31 | +# Environment: |
| 32 | +# ICLA_SHEET_ID spreadsheet id (default: the Netty ICLA sheet) |
| 33 | +# ICLA_SHEET_GID sheet tab gid (default: 0) |
| 34 | +# ICLA_SHEET_TOKEN OAuth access token (optional) |
| 35 | +# ICLA_SHEET_CSV path to a local CSV export, skips the download (optional) |
| 36 | +# |
| 37 | +# Exit status: 0 = all users signed, 1 = at least one user not found, 2 = error. |
| 38 | +set -e |
| 39 | + |
| 40 | +SHEET_ID="${ICLA_SHEET_ID:-1csGlaBCltss7AZ6zWktPFS8BDeOvumf-6aqcMct_bSw}" |
| 41 | +SHEET_GID="${ICLA_SHEET_GID:-0}" |
| 42 | +REPO="${GITHUB_REPOSITORY:-netty/netty}" |
| 43 | +COLUMN="Github user name" |
| 44 | + |
| 45 | +if [ "$#" -lt 1 ]; then |
| 46 | + echo "Usage: $0 <github-user | PR number | PR URL> [...]" >&2 |
| 47 | + exit 2 |
| 48 | +fi |
| 49 | + |
| 50 | +csv="$(mktemp)" |
| 51 | +trap 'rm -f "$csv"' EXIT |
| 52 | + |
| 53 | +download() { |
| 54 | + # $1 = optional bearer token |
| 55 | + local url="https://docs.google.com/spreadsheets/d/${SHEET_ID}/export?format=csv&gid=${SHEET_GID}" |
| 56 | + local args=(-sSL -o "$csv" -w '%{http_code}') |
| 57 | + [ -n "$1" ] && args+=(-H "Authorization: Bearer $1") |
| 58 | + last_error="" |
| 59 | + local code |
| 60 | + if ! code="$(curl "${args[@]}" "$url" 2>&1)"; then |
| 61 | + last_error="curl failed: $code" |
| 62 | + return 1 |
| 63 | + fi |
| 64 | + if [ "$code" != "200" ]; then |
| 65 | + last_error="HTTP $code" |
| 66 | + return 1 |
| 67 | + fi |
| 68 | + # A login redirect returns HTML with a 200 status, so verify we got CSV. |
| 69 | + if head -c 100 "$csv" | grep -qi '<html\|<!doctype'; then |
| 70 | + last_error="got a login page instead of CSV (no access)" |
| 71 | + return 1 |
| 72 | + fi |
| 73 | +} |
| 74 | + |
| 75 | +fetch_sheet() { |
| 76 | + if [ -n "$ICLA_SHEET_CSV" ]; then |
| 77 | + cp "$ICLA_SHEET_CSV" "$csv" |
| 78 | + return 0 |
| 79 | + fi |
| 80 | + if [ -n "$ICLA_SHEET_TOKEN" ]; then |
| 81 | + download "$ICLA_SHEET_TOKEN" && return 0 |
| 82 | + else |
| 83 | + download "" && return 0 |
| 84 | + if command -v gcloud >/dev/null 2>&1; then |
| 85 | + local token |
| 86 | + token="$(gcloud auth print-access-token 2>/dev/null)" || token="" |
| 87 | + [ -n "$token" ] && download "$token" && return 0 |
| 88 | + fi |
| 89 | + fi |
| 90 | + echo "Could not download the ICLA sheet (${last_error:-unknown error})." >&2 |
| 91 | + echo "The sheet is not publicly readable. Either:" >&2 |
| 92 | + echo " - run 'gcloud auth login --enable-gdrive-access' (the default gcloud token has no Drive scope)," >&2 |
| 93 | + echo " - set ICLA_SHEET_TOKEN to an OAuth token with Drive read access, or" >&2 |
| 94 | + echo " - export the sheet as CSV and set ICLA_SHEET_CSV=/path/to/file.csv" >&2 |
| 95 | + return 1 |
| 96 | +} |
| 97 | + |
| 98 | +resolve_user() { |
| 99 | + local arg="$1" pr |
| 100 | + if [[ "$arg" =~ ^[0-9]+$ ]]; then |
| 101 | + pr="$arg" |
| 102 | + elif [[ "$arg" =~ ^https?://github\.com/([^/]+/[^/]+)/pull/([0-9]+) ]]; then |
| 103 | + REPO="${BASH_REMATCH[1]}" |
| 104 | + pr="${BASH_REMATCH[2]}" |
| 105 | + else |
| 106 | + # Plain user name, tolerate a leading '@'. |
| 107 | + echo "${arg#@}" |
| 108 | + return 0 |
| 109 | + fi |
| 110 | + gh pr view "$pr" --repo "$REPO" --json author --jq '.author.login' |
| 111 | +} |
| 112 | + |
| 113 | +fetch_sheet || exit 2 |
| 114 | + |
| 115 | +status=0 |
| 116 | +for arg in "$@"; do |
| 117 | + if ! user="$(resolve_user "$arg")" || [ -z "$user" ]; then |
| 118 | + echo "ERROR: could not resolve a GitHub user from '$arg'" >&2 |
| 119 | + status=2 |
| 120 | + continue |
| 121 | + fi |
| 122 | + |
| 123 | + set +e |
| 124 | + ICLA_USER="$user" ICLA_COLUMN="$COLUMN" python3 - "$csv" <<'EOF' |
| 125 | +import csv, os, re, sys |
| 126 | +
|
| 127 | +user = os.environ["ICLA_USER"].strip().lower() |
| 128 | +column = os.environ["ICLA_COLUMN"].strip().lower() |
| 129 | +
|
| 130 | +def normalize(value): |
| 131 | + value = value.strip().lower() |
| 132 | + value = re.sub(r"^https?://(www\.)?github\.com/", "", value) |
| 133 | + return value.lstrip("@").strip("/ ") |
| 134 | +
|
| 135 | +with open(sys.argv[1], newline="", encoding="utf-8-sig") as f: |
| 136 | + reader = csv.DictReader(f) |
| 137 | + fields = {(name or "").strip().lower(): name for name in (reader.fieldnames or [])} |
| 138 | + if column not in fields: |
| 139 | + print("ERROR: column '%s' not found, available: %s" % (column, ", ".join(fields)), file=sys.stderr) |
| 140 | + sys.exit(2) |
| 141 | + key = fields[column] |
| 142 | + for row in reader: |
| 143 | + if normalize(row.get(key) or "") == user: |
| 144 | + print("OK: %s has signed the ICLA" % os.environ["ICLA_USER"]) |
| 145 | + sys.exit(0) |
| 146 | +
|
| 147 | +print("MISSING: %s was not found in the ICLA sheet" % os.environ["ICLA_USER"]) |
| 148 | +sys.exit(1) |
| 149 | +EOF |
| 150 | + rc=$? |
| 151 | + set -e |
| 152 | + # Keep the most severe status: 2 (error) > 1 (missing) > 0 (ok). |
| 153 | + [ "$rc" -gt "$status" ] && status=$rc |
| 154 | +done |
| 155 | +exit "$status" |