Search code examples
bashgithub-cli

creating table in github issues using gh cli


I am working with bash and gh cli to create a table for a summarized report that goes into a newly created issue. I am stuck on 2 problems:

  1. I want the table to look like a markdown table, likewise:
Severity Type Issue URL

But, the special characters do not turn into a table in the issue body. instead I get the raw input. maybe it is because I used <br> to go down a line? <br> was the only thing that worked, \n \n\r and other options didn't do it. This is a screenshot of the issue created: enter image description here 2. I have to run over many scans and I want to gather each alert severity into a group and at the end of the code to append them all together for better readability. My idea was that by knowing what is the SEVERITY I'll append it into the proper *_alerts var. I need help making these lines functional:

"${SEVERITY}_Alerts"="${${SEVERITY}_Alerts} | $SEVERITY | $alertType | URL |<br>"
REPORT="${REPORT}${High_Alerts}${Medium_Alerts}${Low_Alerts}${Informational_Alerts}"

For a better context, this is what I came up with so far:

SEVERITY="High"
High_Alerts=''
Medium_Alerts=''
Low_Alerts=''
Informational_Alerts=''
REPORT=$"###Unified zap-scan report ran at $(date) <br><br> | Severity | Type | Issue URL | <br> | ----------- | ----------- | ----------- |<br>"
for ((i = 0; i < ${#BODY[@]}; i++)); do
  line=${BODY[$i]}
  if [[ $line =~ "Alert\nAlert" ]]; then
    line=${line##*#}
    line=${line%%A*}
    SEVERITY=$line
    i=$i+1
    line=${BODY[$i]}
    alertType="${line%%\\*}"
    i=$i+2
    REPORT=$"${REPORT} | $SEVERITY | ${alertType} | URL |<br>"
    #"${SEVERITY}_Alerts"="${${SEVERITY}_Alerts} | $SEVERITY | $alertType | URL |<br>"
  elif [[ $line =~ "\nAlert" ]]; then
    i=$i+1
    line=${BODY[$i]}
    alertType="${line%%\\*}"
    i=$i+2
    REPORT=$"${REPORT} | $SEVERITY | ${alertType} | URL |<br>"
    #"${SEVERITY}_Alerts"="${${SEVERITY}_Alerts} | $SEVERITY | $alertType | URL |<br>"
  fi
done
#REPORT="${REPORT}${High_Alerts}${Medium_Alerts}${Low_Alerts}${Informational_Alerts}"

p.s.

the command to create the issue works perfect, it's the input to the body of the issue that is wrong.


Solution

    1. For the table problem, I changed any <br> into $'\r\n' accordingly.
    2. When collecting info using the GH-CLI, it will come back as JSON and as such, you need to use jq to query and get the value itself. On the same note, the value will be with double quotes and therefore I used substitution operators as such:
      URL=${URL%\"} #remove last double quote
      URL=${URL#\"} #remove first double quote
    
    1. As @Socowi mentioned in the comments, the best way to solve the array problem is associative arrays. here's a very good blog post that helped me

    p.s.

    Those double quotes broke the symlinks I tried to create so better watch out from them