summaryrefslogtreecommitdiff
path: root/bin/manage-fail-reasons
blob: eaa7ff519bc40e49b2cd5d59d10226b9f7b2e6b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/sh

# shellcheck source=../lib/load-configuration
. "${0%/*}/../lib/load-configuration"

# shellcheck disable=SC2016
usage() {
  >&2 echo ''
  >&2 echo 'manage-fail-reasons $action [parameters]: manage the list of fail reasons'
  >&2 echo ''
  >&2 echo 'possible actions:'
  >&2 echo '  list               list installed fail reasons'
  >&2 echo '  check <build-log>  check a build log for fail reasons (file or URL)'
  exit 1
}

if [ $# -eq 0 ]; then
  usage
fi

case "$1" in
  'list')
    # shellcheck disable=SC2016
    fail_reason_identifiers=$(
      {
        printf 'SELECT `fail_reasons`.`id`,name,`fail_reasons`.`severity`,replace(to_base64(`fail_reasons`.`identifier`),"\\n","")'
        printf ' FROM `fail_reasons` ORDER BY `fail_reasons`.`severity` ASC, `fail_reasons`.id ASC'
      } | \
        mysql_run_query
    )
    printf '%s\n' "${fail_reason_identifiers}" | \
      while read -r reason_id name severity identifier; do
        ident=$(printf '%s' "${identifier}" | base64 -d -)
        printf "%s %s %s  %s\n" "$reason_id" "$name" "$severity" "$ident"
      done
  ;;
  'check')
    shift
    if [ $# -ne 1 ]; then
      >&2 printf '"check" expects 1 parameter (a path to a build log), %s were given\n' "$#"
      usage
    fi
    case "$1" in
       http*)
         tmp_file=$(mktemp 'tmp.manage-fail-reasons.XXXXXXXXXX' --tmpdir)
         trap 'rm "${tmp_file}"' EXIT
         wget -q -O "$tmp_file" "$1"
         filelog="$tmp_file"
         ;;
       *)
         filelog="$1"
         ;;
    esac
    fail_reason_identifiers=$(
      {
        printf 'SELECT id,severity,name,replace(to_base64(`fail_reasons`.`identifier`),"\\n","")'
        printf ' FROM `fail_reasons` ORDER BY `fail_reasons`.`severity` ASC, `fail_reasons`.id ASC'
      } | \
        mysql_run_query
    )
    printf '%s\n' "${fail_reason_identifiers}" | \
      while read -r id severity name identifier; do
        if zgrep -qx "\s*$(
                printf '%s' "${identifier}" | \
                  base64 -d
              )\s*" \
              "$filelog"; then
           echo 'match'
          ident=$(printf '%s' "${identifier}" | base64 -d -)
          printf "$name ($id, $ident) matches with severity $severity\n"
        fi
      done
      zcat "$filelog" | tail -n 4
  ;;
  *)
    >&2 printf 'unknown action "%s"\n' "$1"
    usage
  ;;
esac