Skip to content

Commit

Permalink
Add download PRs and generate PR changelog scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
newhoggy committed Jun 2, 2023
1 parent ce27d86 commit 522f8c5
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 0 deletions.
40 changes: 40 additions & 0 deletions scripts/download-prs.sh
@@ -0,0 +1,40 @@
#!/bin/bash

(yq --version | grep https://github.com/mikefarah/yq/ > /dev/null) || {
echo "Please install yq from https://github.com/mikefarah/yq/" > /dev/stderr
exit 1
}

if [ "$#" -ne 1 ]; then
echo "Usage: $0 repository" >&2
echo "Example: $0 input-output-hk/cardano-node" >&2
exit 1
fi

if [[ "$(uname -s)" == "Darwin" ]]; then
date_cmd=gdate
else
date_cmd=date
fi

repository="$1"

root_dir="$HOME/.cardano-updates"
out_dir="$root_dir/$repository"

mkdir -p "$out_dir"

temp_json_file="$(mktemp).json"

# Requires the default repository to be set in gh
max_pr_number="$(gh pr list --state all --limit 1 --json number | jq '.[0].number // 1000')"

echo "Downloading up to $max_pr_number PRs"

gh pr list --repo "$repository" \
-L "$max_pr_number" \
--state all \
--json number,title,author,createdAt,closedAt,files,mergedAt,baseRefName,url,body \
> "$temp_json_file"

cat "$temp_json_file" | yq -P > "$out_dir/download.yaml"
95 changes: 95 additions & 0 deletions scripts/generate-pr-changelogs.sh
@@ -0,0 +1,95 @@
#!/usr/bin/env bash

repository="$1"
subdir="$2"
commit_range="$3"

root_dir="$HOME/.cardano-updates"
work_dir="$root_dir/$repository"
work_subdir="$work_dir/$subdir"
download_file="$work_dir/download.yaml"

trim() {
local trimmed=$1
# Remove leading whitespace
trimmed=${trimmed##+([[:space:]])}
# Remove trailing whitespace
trimmed=${trimmed%%+([[:space:]])}
printf '%s' "$trimmed"
}

extract_changelog() {
local in_changelog=false

# Read from stdin
while IFS= read -r line; do
trimmed_line=$(trim "$line")

echo ">>>> $line ]"

if [[ $trimmed_line == "# Changelog" ]]; then
in_changelog=true
elif [[ $trimmed_line == "# "* ]]; then
in_changelog=false
fi

if [ "$in_changelog" = true ]; then
echo "$line"
fi
done
}

select_notable() {
yq -o json \
| jq -r '
["feature", "bugfix"] as $notable
| if length == 0 then
true
elif .[0] | [.type] | flatten | any([.] | inside($notable)) then
true
else
halt_error(1)
end
' \
> /dev/null 2> /dev/null
}

temp_changelog_yaml="$(mktemp)"

for pr_number in $(
git log --merges --oneline "$commit_range" \
| sed 's|^[0-9a-z]\+ Merge pull request #\([0-9]\+\) .*$|\1|g'
); do
cat "$download_file" | yq -o json | jq -r "$(
cat <<-JQ
.[] | select(.number == $pr_number) | .body
JQ
)" \
| awk '/# Changelog/{flag=1;next}/^#/{flag=0}flag' \
| awk '/^```yaml/{flag=1;next}/^```/{flag=0}flag' \
> "$temp_changelog_yaml"

if cat "$temp_changelog_yaml" | select_notable; then
if [ -s "$temp_changelog_yaml" ]; then
cat "$temp_changelog_yaml" \
| yq -o json \
| jq -r "$(
cat <<-JQ
.[]
| ([.type] | flatten | join(", ")) as \$type_string
| "- \(.description | gsub("^[[:space:]]+|[[:space:]]+$"; "") | gsub("\n"; "\n "))\n (\(\$type_string); \(.compatibility))"
JQ
)"
else
echo "- <missing changelog>"
fi

cat "$download_file" | yq -o json | jq -r "$(
cat <<-JQ
.[] | select(.number == $pr_number) | " [PR \(.number)](\(.url))"
JQ
)"

echo
fi
done

0 comments on commit 522f8c5

Please sign in to comment.