1 #!/usr/bin/env bash
2 #
3 # Fetches release notes / changelog for a dependency hosted on GitHub, so a
4 # dependency-update PR review can reason about breaking changes.
5 #
6 # Usage:
7 # ./scripts/get_dependency_releases.sh <owner/repo> [changelog_path]
8 #
9 # Examples:
10 # ./scripts/get_dependency_releases.sh expressjs/multer
11 # ./scripts/get_dependency_releases.sh nodeca/js-yaml CHANGELOG.md
12 #
13 # Behaviour:
14 # - Always prints the most recent GitHub Releases (tag, date, body) for the
15 # repo. Many packages publish their changelog as GitHub Releases.
16 # - If a changelog_path is given (e.g. CHANGELOG.md, History.md), also prints
17 # that file's raw contents from the repo's default branch — useful for
18 # packages that keep a CHANGELOG file instead of (or in addition to)
19 # GitHub Releases.
20 #
21 # Security notes:
22 # - Read-only. Talks ONLY to api.github.com via `gh api`, using the job's
23 # read-only GH_TOKEN. There is no way to point it at an arbitrary host.
24 # - <owner/repo> and <changelog_path> come from UNTRUSTED PR content, so they
25 # are strictly validated below before being interpolated into any API path.
26 # This prevents path traversal / request smuggling / SSRF.
27 #
28
29 set -euo pipefail
30
31 SLUG="${1:-}"
32 CHANGELOG_PATH="${2:-}"
33 RELEASE_LIMIT=40
34
35 if [[ -z "$SLUG" ]]; then
36 echo "Error: missing <owner/repo> argument" >&2
37 echo "Usage: $0 <owner/repo> [changelog_path]" >&2
38 exit 1
39 fi
40
41 # Strict slug validation: exactly "owner/repo", each segment limited to the
42 # characters GitHub allows in owner/repo names. Rejects anything with "..",
43 # slashes beyond the single separator, protocol prefixes, query strings, etc.
44 if ! [[ "$SLUG" =~ ^[A-Za-z0-9._-]+/[A-Za-z0-9._-]+$ ]]; then
45 echo "Error: invalid repo slug '$SLUG' (expected owner/repo)" >&2
46 exit 1
47 fi
48
49 echo "=== GitHub Releases for $SLUG (most recent $RELEASE_LIMIT) ==="
50 # Some repos have no GitHub Releases (changelog-only) or may be inaccessible.
51 # Capture via command substitution so a raw error body (e.g. a 404 JSON blob)
52 # never leaks to stdout; emit a clean note and continue to the changelog file.
53 if releases=$(gh api "repos/${SLUG}/releases?per_page=${RELEASE_LIMIT}" \
54 --jq '.[] | "----------\ntag: \(.tag_name)\ndate: \(.published_at)\n\n\(.body // "(no release body)")\n"' 2>/dev/null) \
55 && [[ -n "$releases" ]]; then
56 printf '%s\n' "$releases"
57 else
58 echo "(no GitHub Releases found, or the repo is not accessible)"
59 fi
60
61 if [[ -n "$CHANGELOG_PATH" ]]; then
62 # Validate the changelog path: a simple relative path, no traversal, no
63 # leading slash, no scheme. Keep it to filename-ish segments.
64 if ! [[ "$CHANGELOG_PATH" =~ ^[A-Za-z0-9._/-]+$ ]] || [[ "$CHANGELOG_PATH" == *".."* ]] || [[ "$CHANGELOG_PATH" == /* ]]; then
65 echo "Error: invalid changelog path '$CHANGELOG_PATH'" >&2
66 exit 1
67 fi
68
69 echo ""
70 echo "=== Changelog file: ${SLUG}/${CHANGELOG_PATH} (truncated to 200 KB) ==="
71 # `Accept: application/vnd.github.raw` returns the file content directly,
72 # avoiding base64 handling. Capture first (so a 404 JSON body can't leak),
73 # then cap the output so a huge changelog can't blow up the context.
74 if changelog=$(gh api "repos/${SLUG}/contents/${CHANGELOG_PATH}" \
75 -H "Accept: application/vnd.github.raw" 2>/dev/null) && [[ -n "$changelog" ]]; then
76 printf '%s' "$changelog" | head -c 200000
77 else
78 echo "(changelog file not found at that path on the default branch)"
79 fi
80 fi