| 1 | #!/usr/bin/env bash |
| 2 | set -euo pipefail |
| 3 | IFS=$'\n\t' |
| 4 | |
| 5 | auth="" |
| 6 | #auth="-u kubuxu:$GH_TOKEN" |
| 7 | org=ipfs |
| 8 | repo=go-ipfs |
| 9 | arch_repo=go-ipfs-archived |
| 10 | api_repo="repos/$org/$repo" |
| 11 | |
| 12 | exclusions=( |
| 13 | 'master' |
| 14 | 'release' |
| 15 | 'feat/zcash' |
| 16 | 'feat/ai-mirror' |
| 17 | ) |
| 18 | |
| 19 | gh_api_next() { |
| 20 | links=$(grep '^Link:' | sed -e 's/Link: //' -e 's/, /\n/g') |
| 21 | echo "$links" | grep '; rel="next"' >/dev/null || return |
| 22 | link=$(echo "$links" | grep '; rel="next"' | sed -e 's/^<//' -e 's/>.*//') |
| 23 | |
| 24 | curl $auth -f -sD >(gh_api_next) "$link" |
| 25 | } |
| 26 | |
| 27 | gh_api() { |
| 28 | curl $auth -f -sD >(gh_api_next) "https://api.github.com/$1" | jq -s '[.[] | .[]]' |
| 29 | } |
| 30 | |
| 31 | pr_branches() { |
| 32 | gh_api "$api_repo/pulls" | jq -r '.[].head.label | select(test("^ipfs:"))' \ |
| 33 | | sed 's/^ipfs://' |
| 34 | } |
| 35 | |
| 36 | origin_refs() { |
| 37 | format=${1-'%(refname:short)'} |
| 38 | |
| 39 | git for-each-ref --format "$format" refs/remotes/origin | sed 's|^origin/||' |
| 40 | } |
| 41 | |
| 42 | active_branches() { |
| 43 | origin_refs '%(refname:short) %(committerdate:unix)' |awk \ |
| 44 | ' BEGIN { monthAgo = systime() - 31*24*60*60 } |
| 45 | { if ($2 > monthAgo) print $1 } |
| 46 | ' |
| 47 | } |
| 48 | |
| 49 | git remote add archived "git@github.com:$org/$arch_repo.git" || true |
| 50 | |
| 51 | branches_to_move="$(cat <(active_branches) <(pr_branches) <((IFS=$'\n'; echo "${exclusions[*]}")) | sort -u | comm - <(origin_refs | sort) -13)" |
| 52 | |
| 53 | echo "================" |
| 54 | printf "%s\n" "$branches_to_move" |
| 55 | echo "================" |
| 56 | |
| 57 | echo "Please confirm move of above branches [y/N]:" |
| 58 | |
| 59 | read line |
| 60 | case $line in |
| 61 | [Yy]|[Yy][Ee][Ss]) ;; |
| 62 | *) exit 1 ;; |
| 63 | esac |
| 64 | |
| 65 | |
| 66 | printf "%s\n" "$branches_to_move" | \ |
| 67 | while read -r ref; do |
| 68 | git push archived "origin/$ref:refs/heads/$ref/$(date --rfc-3339=date)" |
| 69 | git push origin --delete "$ref" |
| 70 | done |
| 71 |