Last active
June 12, 2017 12:12
-
-
Save stigok/fe8056d2672eaa49fdc5490dd10438d0 to your computer and use it in GitHub Desktop.
Create new issue in git repository on Gogs server instance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
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
https://blog.stigok.com/post/cli-create-new-issue-for-git-repo-on-private-gogs-server-from-command-line