Skip to content

Instantly share code, notes, and snippets.

@rvagg
Created March 10, 2025 09:09
Show Gist options
  • Save rvagg/e8d609e1fc357f2a030e3b611c747812 to your computer and use it in GitHub Desktop.
Save rvagg/e8d609e1fc357f2a030e3b611c747812 to your computer and use it in GitHub Desktop.
Bash script to generate a markdown link for the Lotus CHANGELOG for a PR
#!/bin/bash
## lotus-pr.sh
# This script checks if the current branch has an open pull request on the Filecoin Lotus repository.
# If a PR is found, it will output the PR number and a markdown link for CHANGELOG.md.
# If no PR is found, it will predict the next PR number based on the latest PR in the repository.
# The script requires the 'jq' command-line JSON processor and 'xclip' or 'pbcopy' for copying to clipboard.
REPO="filecoin-project/lotus"
GITHUB_API="https://api.github.com"
if [ -n "$GITHUB_TOKEN" ]; then
AUTH_HEADER="Authorization: token $GITHUB_TOKEN"
else
echo "Warning: No GITHUB_TOKEN found. API rate limits will be more restrictive."
AUTH_HEADER=""
fi
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
github_api_get() {
local endpoint="$1"
local url="${GITHUB_API}/${endpoint}"
if [ -n "$AUTH_HEADER" ]; then
curl -s -H "$AUTH_HEADER" "$url"
else
curl -s "$url"
fi
}
# Check if current branch has an open PR
check_existing_pr() {
echo "Checking if branch '$CURRENT_BRANCH' has an open pull request..."
pr_data=$(github_api_get "repos/${REPO}/pulls?head=${REPO%/*}:${CURRENT_BRANCH}&state=open")
# Check if the response contains any PRs
if [ "$(echo "$pr_data" | jq 'length')" -gt 0 ]; then
pr_number=$(echo "$pr_data" | jq '.[0].number')
echo "Found existing PR #$pr_number for branch '$CURRENT_BRANCH'"
format_pr_link "$pr_number"
return 0
else
echo "No open PR found for branch '$CURRENT_BRANCH'"
return 1
fi
}
# Get the highest number from issues (which includes PRs) and increment it
predict_next_number() {
echo "Predicting next number for PR or issue..."
# Get the latest issue/PR (they share the same number space)
latest=$(github_api_get "repos/${REPO}/issues?state=all&sort=created&direction=desc&per_page=1")
if [ -z "$latest" ] || [ "$latest" = "[]" ]; then
echo "Error: Could not fetch latest issue/PR data"
exit 1
fi
latest_number=$(echo "$latest" | jq '.[0].number')
next_number=$((latest_number + 1))
echo "Latest issue/PR number is #$latest_number, next number will likely be #$next_number"
format_pr_link "$next_number"
}
# Attempt to copy text to clipboard using xclip, pbcopy, wl-copy, or win32yank
copy_to_clipboard() {
local text="$1"
local copied=false
# Try xclip (Linux)
if command -v xclip >/dev/null 2>&1; then
echo "$text" | xclip -selection clipboard >/dev/null 2>&1 && copied=true
fi
# Try pbcopy (macOS)
if [ "$copied" = false ] && command -v pbcopy >/dev/null 2>&1; then
echo "$text" | pbcopy >/dev/null 2>&1 && copied=true
fi
# Try wl-copy (Wayland on Linux)
if [ "$copied" = false ] && command -v wl-copy >/dev/null 2>&1; then
echo "$text" | wl-copy >/dev/null 2>&1 && copied=true
fi
# Try win32yank (WSL on Windows)
if [ "$copied" = false ] && command -v win32yank >/dev/null 2>&1; then
echo "$text" | win32yank -i >/dev/null 2>&1 && copied=true
fi
if [ "$copied" = true ]; then
echo "The link has been copied to your clipboard"
else
echo "Could not copy to clipboard - clipboard utilities not found"
fi
}
# Format the PR link for CHANGELOG.md
format_pr_link() {
local pr_number="$1"
link="([filecoin-project/lotus#${pr_number}](https://github.com/filecoin-project/lotus/pull/${pr_number}))"
echo -e "\nMarkdown link for CHANGELOG.md:"
echo "$link"
echo -e ""
copy_to_clipboard "$link"
}
if ! check_existing_pr; then
predict_next_number
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment