Skip to content

Instantly share code, notes, and snippets.

@philroche
Created August 19, 2024 14:41
Show Gist options
  • Save philroche/949e87c7a9343c7da9da86d0d94ef8ea to your computer and use it in GitHub Desktop.
Save philroche/949e87c7a9343c7da9da86d0d94ef8ea to your computer and use it in GitHub Desktop.
Helpful script to summarize the commits and files changes between two git SHAs or branches or tags using the github API and updating a wolfi version update pull request with the summary.
#!/bin/bash -eu
# Helpful script to summarize the commits and files changes between two git SHAs or branches or tags using the github API and updating a wolfi version update pull request with the summary.
# This is very specific to wolfi version update PRs.
# Usage: ./github-compare-summary-update-pr.sh <wolfi version update PR URL>
# Example: ./github-compare-summary-update-pr.sh "https://github.com/wolfi-dev/os/pull/26617"
base_url="https://api.github.com/repos/"
# get the repo as the first argument
version_bump_pull_request_url=$1
# parse the owner, repository name and pull request number from the pull request URL
# example pull request URL: https://github.com/wolfi-dev/os/pull/26610
# Parse the owner, repository name, and pull request number
owner=$(echo "${version_bump_pull_request_url}" | awk -F'/' '{print $4}')
repo=$(echo "${version_bump_pull_request_url}" | awk -F'/' '{print $5}')
pr_number=$(echo "${version_bump_pull_request_url}" | awk -F'/' '{print $7}')
# Output the parsed values
echo "Pull Request: ${version_bump_pull_request_url}"
echo "Owner: ${owner}"
echo "Repository: ${repo}"
echo "Pull Request Number: ${pr_number}"
echo "https://api.github.com/repos/${owner}/${repo}/pulls/${pr_number}"
# use the guthub cli to get the pull request details
pull_request_data=$(gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
/repos/${owner}/${repo}/pulls/${pr_number})
# get the diff URL for this pull request using the diff_url field in the pull_request_data json
diff_url=$(echo "${pull_request_data}" | jq --raw-output '.diff_url')
echo "Diff URL: ${diff_url}"
diff_data=$(gh api \
-H "X-GitHub-Api-Version: 2022-11-28" "${diff_url}")
# Parse the repository name from the diff data by parsing `repository:` line from the diff data
upstream_repository_url=$(echo "${diff_data}" | grep --max-count=1 "repository:" | awk '{print $2}')
# parse the owner, repository name upstream repository URL
# example pull request URL: https://github.com/Homebrew/brew
# Parse the owner, repository name
upstream_owner=$(echo "${upstream_repository_url}" | awk -F'/' '{print $4}')
upstream_repo=$(echo "${upstream_repository_url}" | awk -F'/' '{print $5}')
# Output the parsed values
echo "Upstream repository URL: ${upstream_repository_url}"
echo "Upstream owner: ${upstream_owner}"
echo "Upstream repository: ${upstream_repo}"
from_sha=$(echo "${diff_data}" | grep --max-count=1 --only-matching --extended-regexp "^\-\s+expected-commit:\s+([a-f0-9])+" | sed -E 's/-\s+expected-commit:\s+([a-f0-9]+)/\1/' )
to_sha=$(echo "${diff_data}" | grep --max-count=1 --only-matching --extended-regexp "^\+\s+expected-commit:\s+([a-f0-9])+" | sed -E 's/\+\s+expected-commit:\s+([a-f0-9]+)/\1/' )
echo "From SHA: ${from_sha}"
echo "To SHA: ${to_sha}"
compare_url="${base_url}${upstream_owner}/${upstream_repo}/compare/${from_sha}...${to_sha}"
echo "Compare URL: ${compare_url}"
# Fetch the data from the GitHub API
data=$(curl -s "${compare_url}")
# create a temporary file to store the PR comment body
temp_pr_comment_body_file=$(mktemp --tmpdir=/tmp --suffix=".md" pr_comment_body_file_XXXXXX)
# Print the total number of files changes
echo "Total files changed: $(echo "$data" | jq --raw-output '.files | length')" | tee --append ${temp_pr_comment_body_file}
# Print the summary of the file changes - total changes, additions, deletions
echo "$data" | jq --raw-output '
.files | reduce .[] as $item (
{"changes": 0, "additions": 0, "deletions": 0};
.changes += ($item.changes // 0) |
.additions += ($item.additions // 0) |
.deletions += ($item.deletions // 0)
) | "\nTotal changes: \(.changes)\nTotal additions: \(.additions)\nTotal deletions: \(.deletions)"' | tee --append ${temp_pr_comment_body_file}
# Print the details of the file changes
echo "$data" | jq --raw-output '.files[] | "\n* \(.filename)\n\tChanges: \(.changes)\n\tAdditions: \(.additions)\n\tDeletions: \(.deletions)"' | tee --append ${temp_pr_comment_body_file}
# Print the total number of commits
echo -e "\nTotal commits: $(echo "$data" | jq --raw-output '.total_commits')" | tee --append ${temp_pr_comment_body_file}
echo "$data" | jq --raw-output '.commits[] | "\n* \(.commit.message | split("\n") | first)\n\n\t\(.commit.author.name) (\(.commit.author.email)) @ \(.commit.author.date)\n"' | tee --append ${temp_pr_comment_body_file}
# print the human readable github compare URL by removing ``
echo -e "\nGitHub compare URL: https://github.com/${upstream_owner}/${upstream_repo}/compare/${from_sha}...${to_sha}" | tee --append ${temp_pr_comment_body_file}
gh pr comment "${pr_number}" --repo "${owner}/${repo}" --body-file ${temp_pr_comment_body_file}
rm ${temp_pr_comment_body_file}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment