mirror of
https://github.com/oSoWoSo/DistroHopper.git
synced 2026-12-06 04:43:46 +00:00
111 lines
No EOL
4.8 KiB
Bash
111 lines
No EOL
4.8 KiB
Bash
#!/usr/bin/env bash
|
|
# Generate docs/upstream-quickemu.md: a digest of merged PRs, open PRs and
|
|
# open issues in upstream quickemu. Called by .github/workflows/track-upstream.yml.
|
|
#
|
|
# The generated file keeps YAML front matter with the newest merged_at it has
|
|
# seen, so each run only lists PRs merged since the previous run.
|
|
set -euo pipefail
|
|
|
|
UPSTREAM_REPO="${UPSTREAM_REPO:-quickemu-project/quickemu}"
|
|
UPSTREAM_BRANCH="${UPSTREAM_BRANCH:-master}"
|
|
OUT_FILE="${UPSTREAM_TRACK_OUT:-docs/upstream-quickemu.md}"
|
|
LOOKBACK_DAYS="${UPSTREAM_LOOKBACK_DAYS:-7}"
|
|
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
|
cd "${REPO_ROOT}"
|
|
|
|
if [ -n "${GH_TOKEN:-}" ]; then
|
|
export GH_TOKEN
|
|
elif [ -n "${GITHUB_TOKEN:-}" ]; then
|
|
export GH_TOKEN="${GITHUB_TOKEN}"
|
|
fi
|
|
|
|
mkdir -p "$(dirname "${OUT_FILE}")"
|
|
|
|
# Newest merged_at recorded on the previous run, if any.
|
|
LAST_MERGED_AT=""
|
|
if [ -f "${OUT_FILE}" ]; then
|
|
LAST_MERGED_AT="$(sed -n 's/^last_merged_at:[[:space:]]*//p' "${OUT_FILE}" | head -n1 | tr -d '[:space:]')"
|
|
fi
|
|
# Guard against a malformed or placeholder marker from a previous run.
|
|
case "${LAST_MERGED_AT}" in
|
|
[0-9][0-9][0-9][0-9]-*) ;;
|
|
*) LAST_MERGED_AT="" ;;
|
|
esac
|
|
LAST="${LAST_MERGED_AT:-0000-00-00T00:00:00Z}"
|
|
|
|
NOW="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
|
|
|
|
# Search matches by day; look back one extra day and filter precisely with jq.
|
|
if [ -n "${LAST_MERGED_AT}" ]; then
|
|
SINCE_DAY="$(date -u -d "${LAST_MERGED_AT} 1 day ago" +%Y-%m-%d)"
|
|
else
|
|
SINCE_DAY="$(date -u -d "${NOW} ${LOOKBACK_DAYS} days ago" +%Y-%m-%d)"
|
|
fi
|
|
|
|
# Upstream HEAD, so the digest always records which commit it describes.
|
|
HEAD_SHA="$(gh api "repos/${UPSTREAM_REPO}/commits/${UPSTREAM_BRANCH}" --jq '.sha')"
|
|
|
|
OPEN_ISSUES="$(gh api --paginate \
|
|
"repos/${UPSTREAM_REPO}/issues?state=open&per_page=100&sort=created&direction=desc")"
|
|
OPEN_PRS="$(gh api --paginate \
|
|
"repos/${UPSTREAM_REPO}/pulls?state=open&per_page=100&sort=created&direction=desc")"
|
|
MERGED_PRS="$(gh api --paginate -X GET \
|
|
-f q="repo:${UPSTREAM_REPO} is:pr is:merged merged:>=${SINCE_DAY}" \
|
|
"search/issues")"
|
|
|
|
# --- Build parseable JSON for rendering ------------------------------------
|
|
TMP_DIR="$(mktemp -d)"
|
|
TMP="${TMP_DIR}/digest.json"
|
|
trap 'rm -rf "${TMP_DIR}"' EXIT
|
|
|
|
# Keep the payloads in files; the OpenAPI responses are too big for argv.
|
|
printf '%s' "${OPEN_ISSUES}" | jq '[.[] | select(.pull_request == null)]' > "${TMP_DIR}/issues.json"
|
|
printf '%s' "${OPEN_PRS}" > "${TMP_DIR}/prs.json"
|
|
printf '%s' "${MERGED_PRS}" | jq --arg last "$LAST" \
|
|
'.items | [.[] | select((.pull_request.merged_at // "") > $last)]' > "${TMP_DIR}/merged.json"
|
|
|
|
jq -n \
|
|
--slurpfile issues "${TMP_DIR}/issues.json" \
|
|
--slurpfile prs "${TMP_DIR}/prs.json" \
|
|
--slurpfile merged "${TMP_DIR}/merged.json" \
|
|
'{open_issues: ($issues | add), open_prs: ($prs | add), merged_prs: ($merged | add)}' > "${TMP}"
|
|
|
|
MERGED_COUNT="$(jq '.merged_prs | length' "${TMP}")"
|
|
if [ "${MERGED_COUNT}" -gt 0 ]; then
|
|
NEXT_LAST="$(jq -r '[.merged_prs[].pull_request.merged_at] | max' "${TMP}")"
|
|
else
|
|
NEXT_LAST="${LAST}"
|
|
fi
|
|
|
|
# --- Render -----------------------------------------------------------------
|
|
{
|
|
printf -- '---\n'
|
|
printf 'last_checked: %s\n' "${NOW}"
|
|
printf 'last_merged_at: %s\n' "${NEXT_LAST}"
|
|
printf -- '---\n'
|
|
printf '\n'
|
|
printf '# Upstream quickemu tracker\n'
|
|
printf '\n'
|
|
printf 'Tracking [%s](https://github.com/%s) (%s commit `%.12s`).\n' \
|
|
"${UPSTREAM_REPO}" "${UPSTREAM_REPO}" "${UPSTREAM_BRANCH}" "${HEAD_SHA}"
|
|
printf '\n'
|
|
printf '_Auto-generated by `.github/workflows/track-upstream.yml` — do not edit._\n'
|
|
printf '\n## Recently merged PRs\n'
|
|
printf '\n_%s new PR merged since the last check._\n' "${MERGED_COUNT}"
|
|
printf '\n| PR | Title | Merged | Author |\n|---|------|--------|--------|\n'
|
|
jq -r '.merged_prs | sort_by(.pull_request.merged_at) | reverse | .[] |
|
|
"| [#\(.number)](\(.html_url)) | \(.title | gsub("[|]"; "\\|")) | \(.pull_request.merged_at[0:10]) | \(.user.login) |"' "${TMP}"
|
|
printf '\n## Open pull requests (%s)\n' "$(jq '.open_prs | length' "${TMP}")"
|
|
printf '\n| PR | Title | Opened | Author |\n|---|------|--------|--------|\n'
|
|
jq -r '.open_prs | .[] |
|
|
"| [#\(.number)](\(.html_url)) | \(.title | gsub("[|]"; "\\|")) | \(.created_at[0:10]) | \(.user.login) |"' "${TMP}"
|
|
printf '\n## Open issues (%s)\n' "$(jq '.open_issues | length' "${TMP}")"
|
|
printf '\n| Issue | Title | Opened | Author |\n|---|------|--------|--------|\n'
|
|
jq -r '.open_issues | .[] |
|
|
"| [#\(.number)](\(.html_url)) | \(.title | gsub("[|]"; "\\|")) | \(.created_at[0:10]) | \(.user.login) |"' "${TMP}"
|
|
} > "${OUT_FILE}"
|
|
|
|
echo "Updated ${OUT_FILE}"
|
|
echo "Merged PRs since last check: ${MERGED_COUNT}"
|
|
echo "Open PRs: $(jq '.open_prs | length' "${TMP}")"
|
|
echo "Open issues: $(jq '.open_issues | length' "${TMP}")" |