Created
May 18, 2017 19:25
-
-
Save ip1981/ebe63873cd2f4cf9cee3138b790e91de to your computer and use it in GitHub Desktop.
Copy open github issues between two repositories. Just copy.
This file contains hidden or 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 | |
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