open-code-review/install.sh
kite 533b526b4c
Some checks are pending
CI / cross-compile (arm64, darwin) (push) Waiting to run
CI / cross-compile (arm64, linux) (push) Waiting to run
CI / cross-compile (arm64, windows) (push) Waiting to run
CI / test (push) Waiting to run
CI / cross-compile (amd64, darwin) (push) Waiting to run
CI / cross-compile (amd64, windows) (push) Waiting to run
CodeQL Advanced / Analyze (go) (push) Waiting to run
CodeQL Advanced / Analyze (actions) (push) Waiting to run
CodeQL Advanced / Analyze (javascript-typescript) (push) Waiting to run
Deploy Pages / build (push) Waiting to run
Deploy Pages / deploy (push) Blocked by required conditions
chore: add SPDX license headers and automated verification (#740)
* chore: add SPDX license headers to all source files

Add Apache-2.0 SPDX license identifiers and copyright notices to all
tracked .go, .sh, .js, .mjs, .ts, and .tsx source files.

Introduce scripts/verify-license.sh and scripts/add-license.sh for
automated verification and bulk addition of license headers. Integrate
the check into CI (ci.yml) and the Makefile (license-check target as
a prerequisite of the existing check target).

This satisfies the OpenSSF Best Practices Badge requirements for
copyright_per_file and license_per_file.

* fix: restore execute permissions on scripts

* docs: add license header instructions to CONTRIBUTING guides

* docs: add license header instructions to pages contributing guides

* fix(pages): strip unclosed HTML comment markers to satisfy CodeQL

* fix: apply code review suggestions for license scripts

- Fix portability: detect macOS vs Linux stat for permission copy
- Fix has_header: check both SPDX and copyright (match verify logic)
- Fix is_ignored: match on path boundaries to avoid false positives
- Fix year extraction: use consistent pipeline across both scripts
- Fix Bash 3.2 compat: quote array length expansion for set -u

* fix(pages): use loop-until-clean for HTML comment stripping (CodeQL)

* fix(pages): use split/join instead of replace to avoid CodeQL false positive

CodeQL's js/incomplete-multi-character-sanitization rule flags any
.replace() that removes multi-character sequences like '<!--...-->',
regardless of context. The data here comes from readFileSync on the
project's own index.html (no untrusted input), making this a false
positive. Using split(regex).join('') achieves the same result without
triggering the taint-tracking rule.
2026-08-05 21:26:27 +08:00

104 lines
3.7 KiB
Bash
Executable file

#!/bin/sh
# SPDX-License-Identifier: Apache-2.0
# Copyright 2026 alibaba/open-code-review Contributors
# Install the ocr (Open Code Review) CLI from GitHub releases.
# curl -fsSL https://raw.githubusercontent.com/alibaba/open-code-review/main/install.sh | sh
# Prefer to inspect first:
# curl -fsSL https://raw.githubusercontent.com/alibaba/open-code-review/main/install.sh -o install.sh
# less install.sh && sh install.sh
# Env: OCR_INSTALL_DIR (default /usr/local/bin), OCR_VERSION (default latest).
set -eu
main() {
REPO="alibaba/open-code-review"
BIN="ocr"
ASSET_PREFIX="opencodereview"
INSTALL_DIR="${OCR_INSTALL_DIR:-/usr/local/bin}"
VERSION="${OCR_VERSION:-}"
command -v curl >/dev/null 2>&1 || err "curl is required"
os="$(uname -s | tr '[:upper:]' '[:lower:]')"
case "$os" in
darwin|linux) ;;
*) err "unsupported OS: $os (on Windows use: irm https://raw.githubusercontent.com/alibaba/open-code-review/main/install.ps1 | iex)" ;;
esac
arch="$(uname -m)"
case "$arch" in
x86_64|amd64) arch="amd64" ;;
arm64|aarch64) arch="arm64" ;;
*) err "unsupported architecture: $arch" ;;
esac
if [ -z "$VERSION" ]; then
release_json="$(curl -fsSL "https://api.github.com/repos/$REPO/releases/latest")" ||
err "failed to fetch latest release info from github api"
VERSION="$(printf '%s' "$release_json" |
sed -n 's/.*"tag_name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' | head -n1)"
[ -n "$VERSION" ] || err "could not resolve latest release tag"
fi
asset="${ASSET_PREFIX}-${os}-${arch}"
base="https://github.com/$REPO/releases/download/$VERSION"
tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' INT TERM EXIT
printf 'downloading %s %s (%s/%s)...\n' "$BIN" "$VERSION" "$os" "$arch"
curl -fsSL -o "$tmp/$asset" "$base/$asset" || err "download failed: $base/$asset"
curl -fsSL -o "$tmp/sha256sum.txt" "$base/sha256sum.txt" || err "sha256sum.txt download failed"
want="$(awk -v a="$asset" '$2 == a {print tolower($1)}' "$tmp/sha256sum.txt")"
[ -n "$want" ] || err "no checksum entry for $asset in sha256sum.txt"
got="$(sha256 "$tmp/$asset" | awk '{print tolower($1)}')"
[ "$got" = "$want" ] || err "checksum mismatch for $asset (got $got, want $want)"
install_binary "$tmp/$asset" "$INSTALL_DIR" "$BIN"
printf 'installed %s %s -> %s\n' "$BIN" "$VERSION" "$INSTALL_DIR/$BIN"
post_install_path_notice "$BIN" "$INSTALL_DIR"
}
# Install the staged binary (mode 0755), escalating with sudo only when needed.
# Using install(1) under sudo gives the binary root ownership in system dirs.
install_binary() {
src="$1"
dir="$2"
bin="$3"
if mkdir -p "$dir" 2>/dev/null && [ -w "$dir" ]; then
install -m 0755 "$src" "$dir/$bin"
elif command -v sudo >/dev/null 2>&1; then
printf 'note: %s is not writable; escalating with sudo\n' "$dir"
sudo mkdir -p "$dir"
sudo install -m 0755 "$src" "$dir/$bin"
else
err "$dir is not writable and sudo is unavailable; set OCR_INSTALL_DIR to a writable path"
fi
}
post_install_path_notice() {
bin="$1"
install_dir="$2"
case ":$PATH:" in
*":$install_dir:"*) ;;
*) printf 'note: %s is not on your PATH; add it or run %s/%s directly\n' "$install_dir" "$install_dir" "$bin"; return ;;
esac
command -v "$bin" >/dev/null 2>&1 || printf 'note: open a new shell so %s resolves on PATH\n' "$bin"
}
# Print the SHA-256 of a file, preferring shasum (macOS) over sha256sum (Linux).
sha256() {
if command -v shasum >/dev/null 2>&1; then
shasum -a 256 "$1"
elif command -v sha256sum >/dev/null 2>&1; then
sha256sum "$1"
else
err "shasum or sha256sum is required for checksum verification"
fi
}
err() { printf 'error: %s\n' "$1" >&2; exit 1; }
main "$@"