Skip to content

Commit

Permalink
feat(gh-edit): move gh-edit to gh extension
Browse files Browse the repository at this point in the history
  • Loading branch information
nobe4 committed Nov 30, 2023
1 parent cd7d301 commit cd43382
Showing 1 changed file with 1 addition and 127 deletions.
128 changes: 1 addition & 127 deletions bin/commands/gh-edit
Original file line number Diff line number Diff line change
@@ -1,129 +1,3 @@
#!/usr/bin/env bash
#/ Usage: gh-edit [FLAGS] [URL [FILE]]
#/
#/ Automatically sync a local file change with a field on GitHub.
#/ It's like https://cli.github.com/manual/gh_gist_edit but better:
#/ - Update on save
#/ - Support more fields
#/
#/ Requirements:
#/ - gh
#/
#/ Flags:
#/ -h Show this help
#/
#/ Arguments:
#/ FILE Path to local file to use.
#/ !Warning! The file content's will be replaced by the comment.
#/
#/ URL URL to the item to edit
#/ Support for
#/ Issue
#/ https://github.com/owner/repo/issues/123
#/ https://github.com/owner/repo/issues/123#issuecomment-456
#/ PR
#/ https://github.com/owner/repo/pull/123
#/ https://github.com/owner/repo/pull/123#issuecomment-456
#/ https://github.com/owner/repo/pull/123#discussion_r456
#/ Gist
#/ https://gist.github.com/owner/123
#/
#/ Warning: Support only for the first file.
#/ The link one gets from the UI doesn't always match what's in the
#/ UI, especially in the case of special characters:
#/ - UI
#/ https://gist.github.com/owner/123#file-file-with-a-space-and
#/ - API
#/ gh api "/gists/123" -q '.files'
#/ "file with a space and &[])": { ... }

set -e

show_help() { grep ^#/ <"${0}" | cut -c4-; }
[[ "$*" == "-h" ]] && show_help && exit 0

input="${1}"

if [[ "${input}" == "" ]]; then
default="$(pbpaste | grep 'https://github.com/.\+' || true)"

echo "Enter a comment URL to sync"
if [[ "${default}" != "" ]]; then
echo "or <CR> to use: '${default}'"
fi

printf '> '
read -r input
fi

regexp_github="https://github.com/([^/]+)/([^/]+)/([^/]+)/([^#]+)#?([^0-9_\-]+)?[_-]?r?([0-9]+)?"
regexp_gist="https://gist.github.com/([^/]+)/(.+)"

if [[ "${input}" =~ ${regexp_github} ]]; then
owner="${BASH_REMATCH[1]}"
repo="${BASH_REMATCH[2]}"
type="${BASH_REMATCH[3]}"
id="${BASH_REMATCH[4]}"
field="${BASH_REMATCH[5]:-body}"
field_id="${BASH_REMATCH[6]}"
file="${2:-$(mktemp -d)/${owner}-${repo}-${type}-${id}.md}"
elif [[ "${input}" =~ ${regexp_gist} ]]; then
type="gist"
owner="${BASH_REMATCH[1]}"
id="${BASH_REMATCH[2]}"
field="first_file"
field_id="$(gh api "/gists/${id}" -q '.files.[].filename' | head -n1)"
file="${2:-$(mktemp -d)/${field_id}}"
else
echo "${input} didn't match"
exit 1
fi

# https://docs.github.com/en/rest/issues/issues
get_issues_body(){ gh api "/repos/${owner}/${repo}/issues/${id}" -q '.body'; }
set_issues_body(){ gh api "/repos/${owner}/${repo}/issues/${id}" -F "body=@${file}"; }

# https://docs.github.com/en/rest/issues/comments
get_issues_issuecomment(){ gh api "/repos/${owner}/${repo}/issues/comments/${field_id}" -q '.body'; }
set_issues_issuecomment(){ gh api "/repos/${owner}/${repo}/issues/comments/${field_id}" -F "body=@${file}" ; }
get_pull_issuecomment(){ get_issues_issuecomment; }
set_pull_issuecomment(){ set_issues_issuecomment; }

# https://docs.github.com/en/rest/pulls/pulls
get_pull_body(){ gh api "/repos/${owner}/${repo}/pulls/${id}" -q '.body'; }
set_pull_body(){ gh api "/repos/${owner}/${repo}/pulls/${id}" -F "body=@${file}"; }

# https://docs.github.com/en/rest/pulls/comments
get_pull_discussion(){ gh api "/repos/${owner}/${repo}/pulls/comments/${field_id}" -q '.body'; }
set_pull_discussion(){ gh api "/repos/${owner}/${repo}/pulls/comments/${field_id}" -F "body=@${file}"; }

# https://docs.github.com/en/rest/gists/gists
get_gist_first_file(){ gh api "/gists/${id}" -q ".files.\"${field_id}\".content"; }
set_gist_first_file(){ gh api "/gists/${id}" -F "files[${field_id}][content]=@${file}"; }

"get_${type}_${field}" > "${file}"

(
# I tried using entr here, but it didn't behave well with the background
# job. It started a new process that wouldn't get killed by the kill at the
# end of the file. Playing with the flags (esp. -r and -z) didn't result in
# a better state.
last_update="$(ls -l "${file}")"
while true; do
sleep 1
new_update="$(ls -l "$file")"
if [ "${new_update}" != "${last_update}" ]; then
"set_${type}_${field}"
last_update="${new_update}"
fi
done
) 1>/dev/null 2>&1 &
watch_pid="${!}"

${EDITOR} "${file}"

echo "Waiting a sec for exit save"
sleep 1

kill "${watch_pid}"
wait "${watch_pid}" 2>/dev/null
gh edit $@

0 comments on commit cd43382

Please sign in to comment.