Github2ticket

From BITPlan Wiki
Jump to navigation Jump to search

github2ticket

creates a JSON / WikiSON based list of issues from github projects

to analyze the results https://jqplay.org/ is a useful platform e.g. try filter

.[] | [{number,title,state}] | sort_by(.number)

example

github2ticket SmartDataAnalytics/OpenResearch -r | jq .

usage

usage [-h|--help]

-h | --help: show this usage
-r | --raw: raw json result
-f | --filter: filter fields number,title and state
-s |--state [state (open/closed)]: only issues with the given state
-w |---wiki: results as WikiSon Tickelist

source code

#!/bin/zsh
# get the github tickets for a given project
# WF 2021-12-11

#ansi colors
#http://www.csc.uvic.ca/~sae/seng265/fall04/tips/s265s047-tips/bash-using-colors.html
blue='\033[0;34m'
red='\033[0;31m'
green='\033[0;32m' # '\e[1;32m' is too bright for white bg.
endColor='\033[0m'

#
# a colored message
#   params:
#     1: l_color - the color of the message
#     2: l_msg - the message to display
#
color_msg() {
  local l_color="$1"
  local l_msg="$2"
  echo -e "${l_color}$l_msg${endColor}"
}

#
# error
#
# show the given error message on stderr and exit
#
#   params:
#     1: l_msg - the error message to display
#
error() {
  local l_msg="$1"
  # use ansi red for error
  color_msg $red "Error:" 1>&2
  color_msg $red "\t$l_msg" 1>&2
  exit 1
}


# show usage
#
usage() {
  echo "$0 [-h|--help]"
  echo ""
  echo "-h | --help: show this usage"
  echo "-r | --raw: raw json result"
  echo "-f | --filter: filter fields number,title and state"
  echo "-s |--state [state (open/closed)]: only issues with the given state"
  echo "-w |---wiki: results as WikiSon Tickelist"
  exit 1
}

# get the github ticketlist for the given repository
#
# see https://docs.github.com/en/rest/overview/resources-in-the-rest-api
ticketlist() {
  local l_repo="$1"
  local l_state="$2"
  curl -H "Accept: application/vnd.github.v3+json"  -si "https://api.github.com/repos/$l_repo/issues?state=$l_state" | gawk '
BEGIN { doprint=0 }
/^X-GitHub-Request-Id:/ { doprint=1;next}
{
  if (doprint) print
}'
}

# filter a tickelist
simpleticketlist() {
  local l_repo="$1"
  local l_state="$2"
  ticketlist $l_repo $l_state | jq '.[] | [{number,title,state}] | sort_by(.number)'
}

wikiticketlist() {
  local l_repo="$1"
  local l_state="$2"
  local l_repoid=$(echo "$l_repo" | cut -f2 -d "/")
  IFS=$'\n'.
  if [ "$l_state" = "closed" ]
  then
    marker="{{Done}}"
  else
    marker=""
  fi
  simpleticketlist $l_repo $l_state | jq -c -r '.[]' | while read line
  do
    num=$(echo "$line" | jq -r '.number' )
    title=$(echo "$line" | jq -r '.title' )
    echo "{{Ticket|$num|$title|$l_repoid}}$marker"
    # echo $title
  done
}

option="--simple"
repo=""
repoid=""
state="open"
while [  "$1" != ""  ]
do
  option="$1"
  case $option in
    -h|--help) usage;;
    -r|--raw) ;;
    -f|--filter) ;;
    -s|--state)
      shift
      state="$1"
      ;;
    -w|---wiki) ;;
    *) repo=$option;;
  esac
  shift
done
if [ "$repo" = "" ]
then
  usage
  exit 1
fi
case $option in
  -h|--help) usage;;
  -r|--raw) ticketlist $repo $state;;
  -f|--filter) simpleticketlist $repo $state;;
  -w|---wiki) wikiticketlist $repo $state;;
esac