|  | 1 | +#!/usr/bin/env bash
 | 
|  | 2 | +
 | 
|  | 3 | +# prints to stderr
 | 
|  | 4 | +function echoerr() { echo "$@" 1>&2; }
 | 
|  | 5 | +
 | 
|  | 6 | +# help dialog
 | 
|  | 7 | +if [ "$#" -lt 5 ]; then
 | 
|  | 8 | +    echoerr "Usage: $0 ff-version begin-commit end-commit gitlab-audit-issue reviewers..."
 | 
|  | 9 | +    echoerr ""
 | 
|  | 10 | +    echoerr "Writes a CSV to stdout of Bugzilla issues to triage for a particular Firefox version. This"
 | 
|  | 11 | +    echoerr "script performs a union of the labeled Bugzilla issues in Mozilla's issue tracker and the"
 | 
|  | 12 | +    echoerr "labeled commits in the provided commit range"
 | 
|  | 13 | +    echoerr
 | 
|  | 14 | +    echoerr "    ff-version             rapid-release Firefox version to audit"
 | 
|  | 15 | +    echoerr "    begin-commit           starting gecko-dev commit of this Firefox version"
 | 
|  | 16 | +    echoerr "    end-commit             ending gecko-dev commit of this Firefox version"
 | 
|  | 17 | +    echoerr "    gitlab-audit-issue     tor-browser-spec Gitlab issue number for this audit"
 | 
|  | 18 | +    echoerr "    reviewers...           space-separated list of reviewers responsible for this audit"
 | 
|  | 19 | +    echoerr ""
 | 
|  | 20 | +    echoerr "Example:"
 | 
|  | 21 | +    echoerr ""
 | 
|  | 22 | +    echoerr "$0 116 FIREFOX_ESR_115_BASE FIREFOX_116_0_3_RELEASE 40064 richard pierov henry"
 | 
|  | 23 | +    exit 1
 | 
|  | 24 | +fi
 | 
|  | 25 | +
 | 
|  | 26 | +# set -x
 | 
|  | 27 | +set -e
 | 
|  | 28 | +
 | 
|  | 29 | +
 | 
|  | 30 | +# Ensure various required tools are available
 | 
|  | 31 | +function check_exists() {
 | 
|  | 32 | +    local cmd=$1
 | 
|  | 33 | +    if ! which ${cmd} > /dev/null ; then
 | 
|  | 34 | +        echoerr "missing ${cmd} dependency"
 | 
|  | 35 | +        exit 1
 | 
|  | 36 | +    fi
 | 
|  | 37 | +}
 | 
|  | 38 | +
 | 
|  | 39 | +check_exists git
 | 
|  | 40 | +check_exists jq
 | 
|  | 41 | +check_exists mktemp
 | 
|  | 42 | +check_exists perl
 | 
|  | 43 | +check_exists printf
 | 
|  | 44 | +check_exists sed
 | 
|  | 45 | +check_exists sort
 | 
|  | 46 | +check_exists touch
 | 
|  | 47 | +check_exists uniq
 | 
|  | 48 | +check_exists wget
 | 
|  | 49 | +
 | 
|  | 50 | +# Assign arguments to named variables
 | 
|  | 51 | +firefox_version=$1
 | 
|  | 52 | +git_begin=$2
 | 
|  | 53 | +git_end=$3
 | 
|  | 54 | +audit_issue=$4
 | 
|  | 55 | +reviewers="${@:5}"
 | 
|  | 56 | +
 | 
|  | 57 | +# Check valid Firefox version
 | 
|  | 58 | +if ! [[ "${firefox_version}" =~ ^[1-9][0-9]{2}$ ]]; then
 | 
|  | 59 | +    echoerr "invalid Firefox version (probably)"
 | 
|  | 60 | +    exit 1
 | 
|  | 61 | +fi
 | 
|  | 62 | +
 | 
|  | 63 | +# Check valid Gitlab issue number
 | 
|  | 64 | +if ! [[ "${audit_issue}" =~ ^[1-9][0-9]{4}$ ]]; then
 | 
|  | 65 | +    echoerr "invalid gitlab audit issue number (probably)"
 | 
|  | 66 | +    exit 1
 | 
|  | 67 | +fi
 | 
|  | 68 | +
 | 
|  | 69 | +#
 | 
|  | 70 | +# Encoding/Decoding Functions
 | 
|  | 71 | +#
 | 
|  | 72 | +
 | 
|  | 73 | +# escape " and \
 | 
|  | 74 | +function json_escape() {
 | 
|  | 75 | +    local input="$1"
 | 
|  | 76 | +    echo "${input}" | sed 's/["\]/\\"/g'
 | 
|  | 77 | +}
 | 
|  | 78 | +
 | 
|  | 79 | +
 | 
|  | 80 | +# un-escape \"
 | 
|  | 81 | +function jq_unescape() {
 | 
|  | 82 | +    local input="$1"
 | 
|  | 83 | +    echo "${input}" | sed 's/\\"/"/g'
 | 
|  | 84 | +}
 | 
|  | 85 | +
 | 
|  | 86 | +# change quotes to double-quotes
 | 
|  | 87 | +function csv_escape() {
 | 
|  | 88 | +    local input="$1"
 | 
|  | 89 | +    echo "${input}" | sed 's/"/""/g'
 | 
|  | 90 | +}
 | 
|  | 91 | +
 | 
|  | 92 | +# we need to urlencode the strings used in the new issue link
 | 
|  | 93 | +function url_encode() {
 | 
|  | 94 | +    local input="$1"
 | 
|  | 95 | +    echo "${input}" | perl -MURI::Escape -wlne 'print uri_escape $_'
 | 
|  | 96 | +}
 | 
|  | 97 | +
 | 
|  | 98 | +
 | 
|  | 99 | +#
 | 
|  | 100 | +# Create temp json files
 | 
|  | 101 | +#
 | 
|  | 102 | +git_json=$(mktemp -t git-audit-${firefox_version}-XXXXXXXXXXX.json)
 | 
|  | 103 | +bugzilla_json=$(mktemp -t bugzilla-audit-${firefox_version}-XXXXXXXXXXX.json)
 | 
|  | 104 | +union_json=$(mktemp -t union-audit-${firefox_version}-XXXXXXXXXXX.json)
 | 
|  | 105 | +touch "${git_json}"
 | 
|  | 106 | +touch "${bugzilla_json}"
 | 
|  | 107 | +touch "${union_json}"
 | 
|  | 108 | +
 | 
|  | 109 | +function json_cleanup {
 | 
|  | 110 | +    rm -f "${git_json}"
 | 
|  | 111 | +    rm -f "${bugzilla_json}"
 | 
|  | 112 | +    rm -f "${union_json}"
 | 
|  | 113 | +}
 | 
|  | 114 | +trap json_cleanup EXIT
 | 
|  | 115 | +
 | 
|  | 116 | +#
 | 
|  | 117 | +# Generate Git Commit Triage List
 | 
|  | 118 | +#
 | 
|  | 119 | +
 | 
|  | 120 | +# Try and extract bug id and summary from git log
 | 
|  | 121 | +# Mozilla's commits are not always 100% consistently named, so this
 | 
|  | 122 | +# regex is a bit flexible to handle various inputs such as:
 | 
|  | 123 | +# "Bug 1234 -", "Bug 1234:", "Bug Bug 1234 -", "[Bug 1234] -", " bug 1234 -".
 | 
|  | 124 | +sed_extract_id_summary="s/^[[ ]*[bug –-]+ ([1-9][0-9]*)[]:\., –-]*(.*)\$/\\1 \\2/pI"
 | 
|  | 125 | +
 | 
|  | 126 | +# Generate a json array of objects in the same format as bugzilla: {id: number, summary: string}
 | 
|  | 127 | +printf "[\n" >> "${git_json}"
 | 
|  | 128 | +
 | 
|  | 129 | +first_object=true
 | 
|  | 130 | +git log --format='%s' $git_begin..$git_end  \
 | 
|  | 131 | +| sed -En "${sed_extract_id_summary}" \
 | 
|  | 132 | +| sort -h \
 | 
|  | 133 | +| uniq \
 | 
|  | 134 | +| while IFS= read -r line; do
 | 
|  | 135 | +    read -r id summary <<< "${line}"
 | 
|  | 136 | +    summary=$(json_escape "${summary}")
 | 
|  | 137 | +
 | 
|  | 138 | +    # json does not allow trailing commas
 | 
|  | 139 | +    if [[ "${first_object}" = true ]]; then
 | 
|  | 140 | +        first_object=false
 | 
|  | 141 | +    else
 | 
|  | 142 | +        printf ",\n" >> "${git_json}"
 | 
|  | 143 | +    fi
 | 
|  | 144 | +
 | 
|  | 145 | +    printf "  { \"id\": %s, \"summary\": \"%s\" }" ${id} "${summary}" >> "${git_json}"
 | 
|  | 146 | +done
 | 
|  | 147 | +printf "\n]\n" >> "${git_json}"
 | 
|  | 148 | +
 | 
|  | 149 | +#
 | 
|  | 150 | +# Download Bugzilla Triage List
 | 
|  | 151 | +#
 | 
|  | 152 | +
 | 
|  | 153 | +# search for:
 | 
|  | 154 | +# + Product is NOT "Thunderbird,Calander,Chat Core,MailNews Core" (&f1=product&n1=1&o1=anyexact&v1=Thunderbird%2CCalendar%2CChat%20Core%2CMailNews%20Core). AND
 | 
|  | 155 | +# + Target Milestone contains "${firefox_version}" (115 Branch or Firefox 115) (&f2=target_milestone&o2=substring&v2=${firefox_version}).
 | 
|  | 156 | +# "&limit=0" shows all matching bugs.
 | 
|  | 157 | +
 | 
|  | 158 | +query_tail="&f1=product&n1=1&o1=anyexact&v1=Thunderbird%2CCalendar%2CChat%20Core%2CMailNews%20Core&f2=target_milestone&o2=substring&v2=${firefox_version}&limit=0"
 | 
|  | 159 | +
 | 
|  | 160 | +bugzilla_query="https://bugzilla.mozilla.org/buglist.cgi?${query_tail}"
 | 
|  | 161 | +bugzilla_json_query="https://bugzilla.mozilla.org/rest/bug?include_fields=id,summary${query_tail}"
 | 
|  | 162 | +
 | 
|  | 163 | +wget "${bugzilla_json_query}" -O ${bugzilla_json}
 | 
|  | 164 | +
 | 
|  | 165 | +
 | 
|  | 166 | +#
 | 
|  | 167 | +# Create Union of these two sets of issues
 | 
|  | 168 | +#
 | 
|  | 169 | +
 | 
|  | 170 | +# bugzilla array is actually on a root object: { bugs: [...] }
 | 
|  | 171 | +jq -s '[ (.[0].bugs)[], (.[1])[] ] | group_by(.id) | map(.[0])' "${bugzilla_json}" "${git_json}" > "${union_json}"
 | 
|  | 172 | +
 | 
|  | 173 | +#
 | 
|  | 174 | +# Generate Triage CSV
 | 
|  | 175 | +#
 | 
|  | 176 | +
 | 
|  | 177 | +echo "\"Review\",,\"Bugzilla Bug\""
 | 
|  | 178 | +
 | 
|  | 179 | +jq '. | sort_by(.id)[] | "\(.id)|\(.summary)"' ${union_json} \
 | 
|  | 180 | +| while IFS='|' read -r id summary; do
 | 
|  | 181 | +
 | 
|  | 182 | +    # bugzilla info
 | 
|  | 183 | +    id="${id:1}"
 | 
|  | 184 | +    summary="${summary:0:-1}"
 | 
|  | 185 | +    summary=$(jq_unescape "${summary}")
 | 
|  | 186 | +    # short summary for gitlab issue title
 | 
|  | 187 | +    [[ ${#summary} -gt 80 ]] && summary_short="${summary:0:77}..." || summary_short="${summary}"
 | 
|  | 188 | +
 | 
|  | 189 | +    # filter out some issue types that we never care about
 | 
|  | 190 | +    skip_issue=false
 | 
|  | 191 | +
 | 
|  | 192 | +    # skip `[wpt-sync] Sync PR`
 | 
|  | 193 | +    if [[ "${summary}" =~ ^\[wpt-sync\]\ Sync\ PR.*$ ]]; then
 | 
|  | 194 | +        skip_issue=true
 | 
|  | 195 | +    # skip `Crash in [@` and variants
 | 
|  | 196 | +    elif [[ "${summary}" =~ ^Crash[esin\ ]*\ \[\@.*$ ]]; then
 | 
|  | 197 | +        skip_issue=true
 | 
|  | 198 | +    # skip `Assertion failuire: `
 | 
|  | 199 | +    elif [[ "${summary}" =~ ^Assertion\ failure:\ .*$ ]]; then
 | 
|  | 200 | +        skip_issue=true
 | 
|  | 201 | +    # skip `Hit MOZ_CRASH`
 | 
|  | 202 | +    elif [[ "${summary}" =~ ^Hit\ MOZ_CRASH.*$ ]]; then
 | 
|  | 203 | +        skip_issue=true
 | 
|  | 204 | +    fi
 | 
|  | 205 | +
 | 
|  | 206 | +    if [[ "${skip_issue}" = true ]]; then
 | 
|  | 207 | +        echoerr "Skipped Bugzilla ${id}: ${summary_short}"
 | 
|  | 208 | +    else
 | 
|  | 209 | +        csv_summary=$(csv_escape "${summary}")
 | 
|  | 210 | +
 | 
|  | 211 | +        # parent issue
 | 
|  | 212 | +        bugzilla_url="https://bugzilla.mozilla.org/show_bug.cgi?id=${id}"
 | 
|  | 213 | +        # review issue title
 | 
|  | 214 | +        new_issue_title=$(url_encode "Review Mozilla ${id}: ${summary_short}")
 | 
|  | 215 | +        # review issue description + labeling (14.0 stable, FF128-esr, Next)
 | 
|  | 216 | +        new_issue_description=$(url_encode "### Bugzilla: ${bugzilla_url}")%0A$(url_encode "/label ~\"14.0 stable\" ~FF128-esr ~Next")%0A$(url_encode "/relate tpo/applications/tor-browser-spec#${audit_issue}")%0A%0A$(url_encode "<!-- briefly describe why this issue needs further review -->")%0A
 | 
|  | 217 | +        # url which create's new issue with title and description pre-populated
 | 
|  | 218 | +        new_issue_url="https://gitlab.torproject.org/tpo/applications/tor-browser/-/issues/new?issue[title]=${new_issue_title}&issue[description]=${new_issue_description}"
 | 
|  | 219 | +
 | 
|  | 220 | +        # this link will start the creation of a new gitlab issue to review
 | 
|  | 221 | +        create_issue=$(csv_escape "=HYPERLINK(\"${new_issue_url}\", \"New Issue\")")
 | 
|  | 222 | +        bugzilla_link=$(csv_escape "=HYPERLINK(\"${bugzilla_url}\", \"Bugzilla ${id}: ${csv_summary}\")")
 | 
|  | 223 | +
 | 
|  | 224 | +        echo "FALSE,\"${create_issue}\",\"${bugzilla_link}\","
 | 
|  | 225 | +    fi
 | 
|  | 226 | +done
 | 
|  | 227 | +
 | 
|  | 228 | +echo
 | 
|  | 229 | +echo "\"Triaged by:\""
 | 
|  | 230 | +for reviewer in $reviewers; do
 | 
|  | 231 | +    reviewer=$(csv_escape "${reviewer}")
 | 
|  | 232 | +    echo "\"FALSE\",\"${reviewer}\""
 | 
|  | 233 | +done
 | 
|  | 234 | +echo
 | 
|  | 235 | +
 | 
|  | 236 | +bugzilla_query="=HYPERLINK(\"${bugzilla_query}\", \"Bugzilla query\")"
 | 
|  | 237 | +echo \"$(csv_escape "${bugzilla_query}")\" |