0

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.

Killerz0ne
  • 254
  • 1
  • 2
  • 12
  • There are two questions in here and I think both are duplicates. First, `"${SEVERITY}_Alerts"=...` would need a `declare` or `eval` to work. Have you considered using associative arrays instead? See [Dynamic variable names in Bash](https://stackoverflow.com/q/16553089/6770384). Second, to print an aligned table use `printf` or `column -t`. See [How can I align the columns of tables in Bash?](https://stackoverflow.com/q/12768907/6770384). – Socowi May 24 '21 at 13:33
  • If you can point me to a link or an example, I'll try it. I just went with my basic knowledge in bash. – Killerz0ne May 24 '21 at 13:35
  • I am not trying to align the table. I am trying to make into a markdown table. I'll update the post with the intended design – Killerz0ne May 24 '21 at 13:55

1 Answers1

0
  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

Killerz0ne
  • 254
  • 1
  • 2
  • 12