Created
March 21, 2025 16:21
-
-
Save dotysan/c05b4ca5f452b1cc1953258e6d7c5c34 to your computer and use it in GitHub Desktop.
Move all items from one GitHub Project to another.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env bash | |
# | |
# Move all items from one GitHub Project to another. | |
# | |
# edit these as needed | |
OWNER=example | |
SRC_PROJ_NUM=1 | |
DST_PROJ_NUM=2 | |
main() { | |
SRC_ITEMS=$(get_proj_items "$SRC_PROJ_NUM") | |
# jq . <<<"$SRC_ITEMS" | |
local src_title=$(get_proj_title "$SRC_PROJ_NUM") | |
local dst_title=$(get_proj_title "$DST_PROJ_NUM") | |
local item_id url title item_json | |
while read -r item_id url title | |
do echo "Moving item $item_id ($title)..." | |
item_json=$(get_item_json "$item_id") | |
# jq . <<<"$item_json" | |
# --add-label will fail if you haven't already created that label in the repo | |
gh issue edit "$url" --add-project="$dst_title" --add-label="$src_title" | |
# TODO: also copy fields (using graphql?) | |
# once we've safely copied over all metadata/fields... | |
gh issue edit "$url" --remove-project="$src_title" | |
echo "Item $item_id moved." | |
done < <(get_items) | |
} | |
#---------------------------------------------------------------------- | |
get_proj_items() { | |
gh project item-list "$1" --owner="$OWNER" --format=json | |
} | |
get_proj_title() { | |
gh project view "$1" --owner="$OWNER" --format=json --jq=.title | |
} | |
get_items() { | |
jq --raw-output ' | |
.items[] |"\(.id) \(.content.url) \(.title)" | |
' <<<"$SRC_ITEMS" | |
} | |
get_item_json() { | |
jq ".items[] |select(.id==\"$1\")" <<<"$SRC_ITEMS" | |
} | |
#====================================================================== | |
if [ "${BASH_VERSINFO[0]}" != "5" ] | |
then | |
echo 'This script only tested in The Bourne-Again Shell v5.' | |
exit 1 | |
fi >&2 | |
PS4func() { | |
local lineno="$1" | |
local i f='' | |
local c="\033[0;36m" y="\033[0;33m" n="\033[0m" | |
local d=$((${#FUNCNAME[@]}-2)) | |
if [[ $lineno == 1 ]] | |
then lineno=0 | |
fi | |
for ((i=d; i>0; i--)) | |
do printf -v f "%s%s()" "$f" "${FUNCNAME[i]}" | |
done | |
printf "$y%s:%04d$c%s$n " "${BASH_SOURCE[1]##*/}" "$lineno" "$f" | |
} | |
set -o errexit | |
set -o nounset | |
set -o pipefail | |
# poor man's __main__ | |
return 2>/dev/null ||: | |
if [[ "${DEBUG:-}" ]] | |
then | |
PS4='\r$(PS4func $LINENO)' | |
set -o xtrace | |
fi | |
main | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment