|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# Copyright The OpenTelemetry Authors |
| 4 | +# SPDX-License-Identifier: Apache-2.0 |
| 5 | +# |
| 6 | +# |
| 7 | + |
| 8 | +# This script extracts the "Area" from the issue body and adds it as a label |
| 9 | +# on newly created issues. The area from the issue body comes from |
| 10 | +# the "Area" drop-down field in the ISSUE_TEMPLATE, which is auto-generated |
| 11 | +# from the files inside model/registry. |
| 12 | + |
| 13 | +# TODO: This script can be later used to also auto-assign the correct code-owner |
| 14 | +# once that is implemented. |
| 15 | + |
| 16 | +set -euo pipefail |
| 17 | + |
| 18 | +if [[ -z "${ISSUE:-}" || -z "${BODY:-}" || -z "${OPENER:-}" ]]; then |
| 19 | + echo "Missing one of ISSUE, BODY, or OPENER, please ensure all are set." |
| 20 | + exit 0 |
| 21 | +fi |
| 22 | + |
| 23 | +LABELS="" |
| 24 | +AREAS_SECTION_START=$( (echo "${BODY}" | grep -n '### Area(s)' | awk '{ print $1 }' | grep -oE '[0-9]+') || echo '-1' ) |
| 25 | +BODY_AREAS="" |
| 26 | + |
| 27 | +if [[ "${AREAS_SECTION_START}" != '-1' ]]; then |
| 28 | + BODY_AREAS=$(echo "${BODY}" | sed -n $((AREAS_SECTION_START+2))p) |
| 29 | +fi |
| 30 | + |
| 31 | +for AREA in ${BODY_AREAS}; do |
| 32 | + # Areas are delimited by ', ' and the for loop separates on spaces, so remove the extra comma. |
| 33 | + AREA=${AREA//,/} |
| 34 | + |
| 35 | + if (( "${#AREA}" > 50 )); then |
| 36 | + echo "'${AREA}' exceeds GitHub's 50-character limit on labels, skipping adding a label" |
| 37 | + continue |
| 38 | + fi |
| 39 | + |
| 40 | + if [[ -n "${LABELS}" ]]; then |
| 41 | + LABELS+="," |
| 42 | + fi |
| 43 | + LABELS+="${AREA}" |
| 44 | +done |
| 45 | + |
| 46 | +if [[ -v PINGED_AREAS[@] ]]; then |
| 47 | + echo "The issue was associated with areas:" "${!PINGED_AREAS[@]}" |
| 48 | +else |
| 49 | + echo "No related areas were given" |
| 50 | +fi |
| 51 | + |
| 52 | +if [[ -n "${LABELS}" ]]; then |
| 53 | + # Notes on this call: |
| 54 | + # 1. Labels will be deduplicated by the GitHub CLI. |
| 55 | + # 2. The call to edit the issue will fail if any of the |
| 56 | + # labels doesn't exist. We can be reasonably sure that |
| 57 | + # all labels will exist since they come from a known set. |
| 58 | + echo "Adding the following labels: ${LABELS//,/ /}" |
| 59 | + gh issue edit "${ISSUE}" --add-label "${LABELS}" || true |
| 60 | +else |
| 61 | + echo "No labels were found to add" |
| 62 | +fi |
0 commit comments