Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Copy open github issues between two repositories. Just copy.
#!/usr/bin/env bash
set -euo pipefail
ME=''
GITHUB_API_TOKEN=${GITHUB_API_TOKEN:-}
ACTION=false
CURL=${CURL:-curl}
JQ=${JQ:-jq}
usage()
{
cat <<USAGE
Copy issues from one Github repository into another.
Usage: '$0' [options] src dst
Options are:
-a Action! Default is dry-run, i. e. only show what would be done
-h, -? This help message
Environment variables:
GITHUB_API_TOKEN - exactly what it reads. Required.
USAGE
exit 42
}
while getopts h?a opt; do
case $opt in
a) ACTION=true;;
*) usage;;
esac
done
shift $((OPTIND - 1))
SRC="$1"
DST="$2"
_request()
{
$CURL -s -S -f \
-H "Authorization: token $GITHUB_API_TOKEN" \
-H 'Accept: application/vnd.github.v3+json' \
"$@"
}
_POST()
{
_request -X POST \
-H 'Content-Type: application/json' \
"$@"
}
_GET()
{
_request -X GET \
"$@"
}
_list_issues()
{
local repo="$1"
_GET "https://api.github.com/repos/$repo/issues" \
| $JQ -c '.[] | select(.state == "open") | {assignees: .assignees, title: .title, body: .body}'
}
_create_issue()
{
local repo="$1"
local issue="$2"
_POST \
-d "$issue" \
"https://api.github.com/repos/$repo/issues"
}
_list_issues "$SRC" \
| while read -r issue; do
if $ACTION; then
_create_issue "$DST" "$issue"
else
echo "$issue"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment