Skip to content

Instantly share code, notes, and snippets.

@dradtke
Created June 22, 2018 21:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dradtke/d01fe0eafca503854c2a5ee3ae088058 to your computer and use it in GitHub Desktop.
Save dradtke/d01fe0eafca503854c2a5ee3ae088058 to your computer and use it in GitHub Desktop.
A script for importing GitHub issues into a Fossil repository
#!/bin/bash
# +o histexpand is to avoid issues with ! showing up in ticket descriptions.
# The rest are just good Bash script practice.
set -eu -o pipefail +o histexpand
if [[ $# -lt 2 ]]; then
echo "usage: $0 <user> <repo>"
exit 1
fi
REPO_OWNER="$1"
REPO_NAME="$2"
github () {
API_ENDPOINT="https://api.github.com"
ACCEPT_HEADER="Accept: application/vnd.github.v3+json"
if [[ ! -z "${GITHUB_TOKEN:-}" ]]; then
RESULT=$(curl -s -H "${ACCEPT_HEADER}" -H "Authorization: token ${GITHUB_TOKEN}" "${API_ENDPOINT}$1")
elif [[ ! -z "${GITHUB_USERNAME:-}" ]]; then
RESULT=$(curl -s -H "${ACCEPT_HEADER}" -u "${GITHUB_USERNAME}" "${API_ENDPOINT}$1")
else
echo "One of GITHUB_TOKEN or GITHUB_USERNAME must be set. See https://developer.github.com/v3/?#authentication" >&2
exit 1
fi
if [[ ! -z $(echo "${RESULT}" | jq '.message? | select(.!=null)') ]]; then
echo "${RESULT}" >&2
exit 1
fi
echo "${RESULT}"
}
for issue in $(github "/repos/${REPO_OWNER}/${REPO_NAME}/issues" | jq --raw-output --compact-output '.[] | @base64'); do
_jq () {
echo "${issue}" | base64 --decode | jq --raw-output $@
}
ticket_args=()
ticket_args+=(title "'$(_jq '.title')'")
ticket_args+=(type "'GitHub Issue'")
# Convert markdown to HTML, then remove special characters.
body=$(_jq '.body' | pandoc --from markdown --to html | sed $'s/\r/\\\\r/' | awk 1 ORS='\\n' | sed $'s/\'/\\\\\'/g')
body+="<br><br><center><small>Imported from <a href=\"$(_jq '.html_url')\">GitHub</a></small></center>"
ticket_args+=(comment "'${body}'")
state=$(_jq --raw-output '.state')
status=$(tr '[:lower:]' '[:upper:]' <<< ${state:0:1})${state:1} # Capitalize the first letter, so e.g. "open" becomes "Open"
ticket_args+=(status "'${status}'")
email=$(github "/users/$(_jq .user.login)" | jq --raw-output '.email | select(.!=null)')
if [[ ! -z "${email}" ]]; then
ticket_args+=(private_contact "'${email}'")
fi
echo "${ticket_args[@]}" | xargs fossil ticket add --quote
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment