From 5044258532f9113d02a3b38ec64c5a975ee445e8 Mon Sep 17 00:00:00 2001 From: Delan Azabani Date: Wed, 14 Feb 2024 15:11:36 +0800 Subject: [PATCH] Add tool to bury drafts for failed releases --- README.md | 5 ++++ tools/bury-old-drafts.sh | 52 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100755 tools/bury-old-drafts.sh diff --git a/README.md b/README.md index b5583e7..dcf5c11 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,7 @@ # servo-nightly-builds Repository to host Servo nightly builds using Github Releases. + +## How to bury drafts for failed releases +```sh +$ tools/bury-old-drafts.sh servo/servo-nightly-builds +``` diff --git a/tools/bury-old-drafts.sh b/tools/bury-old-drafts.sh new file mode 100755 index 0000000..3487805 --- /dev/null +++ b/tools/bury-old-drafts.sh @@ -0,0 +1,52 @@ +#!/usr/bin/env zsh +# usage: tools/bury-old-drafts.sh +# requires: zsh, gh, jq, rg +set -euo pipefail +missing() { >&2 echo "fatal: $1 not found"; exit 1; } +> /dev/null command -v gh || missing gh +> /dev/null command -v jq || missing jq +> /dev/null command -v rg || missing rg + +set -- +org_repo_slug=servo/servo-nightly-builds +now=$(date +\%s) +result=$(mktemp) +page=1 +while :; do + gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" \ + /repos/"$org_repo_slug"'/releases?per_page=100&page='$page > $result + length=$(< $result jq length) + if [ $length -eq 0 ]; then + break + fi + for draft in $(< $result jq '.[] | select(.draft) | .id'); do + created_at=$(< $result jq -r '.[] | select(.id == '"$draft"') | .created_at') + created_at=$(date +\%s --date="$created_at") + age=$((now - created_at)) + # Ignore young drafts, because their release builds may still be running + if [ $age -lt 86400 ]; then + >&2 echo "Warning: ignoring release $draft, which is only $age seconds old" + else + set -- "$@" "$draft" + fi + done + >&2 echo "Page $page has $length releases; found $# drafts so far" + page=$((page+1)) +done +for draft; do + # Mark as prerelease and unmark as draft + set -- PATCH "/repos/$org_repo_slug/releases/$draft" -F prerelease=true -F draft=false + echo "$@" + if ! gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" \ + --method "$@" > $result; then + < $result jq + >&2 printf 'Delete release? [y/N] ' + read -r yn + if [ "$yn" = y ]; then + set -- DELETE "/repos/$org_repo_slug/releases/$draft" + echo "$@" + gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" \ + --method "$@" + fi + fi +done