Skip to content

Instantly share code, notes, and snippets.

@stigok stigok/gogs-issue
Last active Jun 12, 2017

Embed
What would you like to do?
Create new issue in git repository on Gogs server instance
#!/bin/bash
# Create an issue on a Gogs repo specified by pwd's git remote
# Exit codes:
# 1 : Missing env
# 2 : Curl error
# 3 : JSON parsing error
# Get a key at https://<gogs-server-url>/user/settings/applications
api_server="$GOGS_SERVER_URL"
api_key="$GOGS_SERVER_API_KEY"
# Fail if missing env
if [ -z "$api_server" ]; then
echo >&2 'GOGS_SERVER_URL is an empty string'
exit 1
fi
if [ -z "$api_key" ]; then
echo >&2 'GOGS_SERVER_API_KEY is an empty string'
exit 1
fi
set -e
repo="$(pwd)"
remote_url="$(git remote get-url origin | grep -oE '[a-z]+/[a-z]+')"
org="$(echo $remote_url | cut -d '/' -f1)"
name="$(echo $remote_url | cut -d '/' -f2)"
title="${@}"
body="you can do it"
# Print issue list when no args
if [ -z "$title" ]; then
result=$(curl -s \
-H 'Content-Type: application/json' \
"${api_server}/api/v1/repos/$org/$name/issues?token=$api_key")
if [ $? -ne 0 ]; then
>&2 echo "Failed to list issues (curl exited with $?)"
exit 2
fi
node -e "const obj = JSON.parse('$result'); obj.forEach(x => console.log(x.number, x.title)); if (!obj.length) console.log('No issues in repo')" && exit 0
exit 3
fi
# Create a new issue with args as title
result=$(curl -s \
-H 'Content-Type: application/json' \
-d "{\"title\":\"$title\",\"body\": \"$body\", \"labels\": []}" \
-X POST "${api_server}/api/v1/repos/$org/$name/issues?token=$api_key" \
| jsonprop number)
if [ $? -ne 0 ]; then
>&2 echo "Failed to create new issue (curl exited with $?)"
exit 2
fi
# Print URL of the new issue
echo $api_server/$remote_url/issues/$result
@stigok

This comment has been minimized.

Copy link
Owner Author

commented Jun 12, 2017

Depends on Node.js for JSON parsing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.