Skip to content

Instantly share code, notes, and snippets.

@ichernev
Created October 19, 2016 05:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ichernev/5ad304aa3d489b54fcdff289025dbd8b to your computer and use it in GitHub Desktop.
Save ichernev/5ad304aa3d489b54fcdff289025dbd8b to your computer and use it in GitHub Desktop.
Helper script that pushes a markdown file as a gist after linkifying issues
#!/bin/bash
set -e
set -o pipefail
REPO="moment/moment"
function json_escape(){
echo -n "$1" | python -c 'import json,sys; print json.dumps(sys.stdin.read())'
}
if [ $# -lt 1 ]; then
echo "$0 FILE" >&2
exit 1
fi
filename="$1"
shift
do_linkify=1
while [ $# -gt 0 ]; do
case $1 in
'--skip-linkify')
do_linkify=0
;;
*)
echo "unknown option" >&2
exit 1
esac
shift
done
description="$(head -n1 $filename)"
description=${description### }
basename=$(basename $filename)
if [ "$do_linkify" = 1 ]; then
cp "$filename" "${filename}.bac"
sed -i \
-e 's_#\([0-9][0-9]*\)_[#\1](https://github.com/'$REPO'/issues/\1)_g' \
-e 's_\[\([0-9]*\)\]_[#\1](https://github.com/'$REPO'/pull/\1)_g' \
$filename
fi
echo "$description"
cat > /tmp/abc <<EOF
quit with error ':cq'
basename: $basename
desc: $description
$(cat $filename)
EOF
if ! vim /tmp/abc; then
if [ "$do_linkify" = 1 ]; then
cp "$filename" "${filename}.new"
mv "${filename}.bac" "$filename"
fi
exit
fi
curl -H "Authorization: token $GITHUB_TOKEN" \
-H "Content-Type: application/json" \
-X POST \
-d "
{
\"description\":\"$description\",
\"public\":true,
\"files\": {
\"$basename\": {
\"content\": $(json_escape "$(cat $filename)")
}
}
}" \
"https://api.github.com/gists"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment