Skip to content

Instantly share code, notes, and snippets.

@mislav
Created February 5, 2014 17:37
Show Gist options
  • Save mislav/8829156 to your computer and use it in GitHub Desktop.
Save mislav/8829156 to your computer and use it in GitHub Desktop.
Script to migrate releases from CHANGELOG.md to GitHub Releases
#!/bin/bash
# Usage: OAUTH_TOKEN="..." backfill-releases CHANGELOG.md [<project-title>]
set -e
log="${1?}"
project_name="${2}"
repo="$(git config remote.origin.url | grep -oE 'github\.com[/:][^/]+/[^/]+' | sed 's/\.git$//' | cut -d/ -f2-3)"
[ -n "${project_name}" ] || project_name="${repo#*/}"
tags=()
tag_exists() {
local t
if [ -z "$tags" ]; then
tags=( $(git ls-remote --tags origin | cut -d/ -f3) )
fi
for t in "${tags[@]}"; do [ "$t" = "$1" ] && return 0; done
return 1
}
extract_entry() {
awk -v "tag=$1" '
/^#### / {
if ($2 == tag) {
show = 1
next
}
else if (show) exit
}
{ if (show) print }
' "$log"
}
create_release() {
ruby --disable-gems -rjson -e "
puts JSON::generate(
tag_name: 'v${1}',
name: '${project_name} ${1}',
body: ARGF.read.strip
)
" | curl -fsS -X POST https://api.github.com/repos/${repo}/releases \
-H "Authorization: token ${OAUTH_TOKEN}" -d @-
}
for entry in $(grep '^#### ' "$log" | awk '{print $2}'); do
if tag_exists "v${entry}"; then
if extract_entry "$entry" | create_release "$entry" >/dev/null; then
printf "."
else
printf "\nCreating release v${entry} failed.\n" >&2
fi
else
echo "warning: no \`v${entry}' tag exists" >&2
fi
done
printf "\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment