| 1 |
#!/usr/bin/env bash |
| 2 |
# |
| 3 |
# Copyright The OpenTelemetry Authors |
| 4 |
# SPDX-License-Identifier: Apache-2.0 |
| 5 |
# |
| 6 |
# |
| 7 |
|
| 8 |
set -euo pipefail |
| 9 |
|
| 10 |
if [[ -z "${ISSUE:-}" || -z "${COMMENT:-}" || -z "${SENDER:-}" ]]; then |
| 11 |
echo "At least one of ISSUE, COMMENT, or SENDER has not been set, please ensure each is set." |
| 12 |
exit 0 |
| 13 |
fi |
| 14 |
|
| 15 |
CUR_DIRECTORY=$(dirname "$0") |
| 16 |
|
| 17 |
if [[ ${COMMENT:0:6} != "/label" ]]; then |
| 18 |
echo "Comment is not a label comment, exiting." |
| 19 |
exit 0 |
| 20 |
fi |
| 21 |
|
| 22 |
# key: label in comment |
| 23 |
# value: actual label |
| 24 |
declare -A COMMON_LABELS |
| 25 |
COMMON_LABELS["arm64"]="arm64" |
| 26 |
COMMON_LABELS["good-first-issue"]="good first issue" |
| 27 |
COMMON_LABELS["help-wanted"]="help wanted" |
| 28 |
COMMON_LABELS["discussion-needed"]="discussion-needed" |
| 29 |
COMMON_LABELS["os:macos"]="os:macos" |
| 30 |
COMMON_LABELS["os:windows"]="os:windows" |
| 31 |
COMMON_LABELS["waiting-for-author"]="waiting-for-author" |
| 32 |
COMMON_LABELS["waiting-for-codeowners"]="waiting-for-codeowners" |
| 33 |
COMMON_LABELS["bug"]="bug" |
| 34 |
COMMON_LABELS["priority:p0"]="priority:p0" |
| 35 |
COMMON_LABELS["priority:p1"]="priority:p1" |
| 36 |
COMMON_LABELS["priority:p2"]="priority:p2" |
| 37 |
COMMON_LABELS["priority:p3"]="priority:p3" |
| 38 |
COMMON_LABELS["stale"]="Stale" |
| 39 |
|
| 40 |
LABELS=$(echo "${COMMENT}" | sed -E 's%^/label%%') |
| 41 |
|
| 42 |
for LABEL_REQ in ${LABELS}; do |
| 43 |
LABEL=$(echo "${LABEL_REQ}" | sed -E s/^[+-]?//) |
| 44 |
# Trim newlines from label that would cause matching to fail |
| 45 |
LABEL=$(echo "${LABEL}" | tr -d '\n') |
| 46 |
|
| 47 |
SHOULD_ADD=true |
| 48 |
if [[ "${LABEL_REQ:0:1}" = "-" ]]; then |
| 49 |
SHOULD_ADD=false |
| 50 |
fi |
| 51 |
|
| 52 |
if [[ -v COMMON_LABELS["${LABEL}"] ]]; then |
| 53 |
if [[ ${SHOULD_ADD} = true ]]; then |
| 54 |
gh issue edit "${ISSUE}" --add-label "${COMMON_LABELS["${LABEL}"]}" |
| 55 |
else |
| 56 |
gh issue edit "${ISSUE}" --remove-label "${COMMON_LABELS["${LABEL}"]}" |
| 57 |
fi |
| 58 |
continue |
| 59 |
fi |
| 60 |
|
| 61 |
# Grep exits with status code 1 if there are no matches, |
| 62 |
# so we manually set RESULT to 0 if nothing is found. |
| 63 |
RESULT=$(grep -c "${LABEL}" .github/CODEOWNERS || true) |
| 64 |
|
| 65 |
if [[ ${RESULT} = 0 ]]; then |
| 66 |
echo "\"${LABEL}\" doesn't correspond to a component, skipping." |
| 67 |
continue |
| 68 |
fi |
| 69 |
|
| 70 |
if [[ ${SHOULD_ADD} = true ]]; then |
| 71 |
gh issue edit "${ISSUE}" --add-label "${LABEL}" |
| 72 |
|
| 73 |
# Labels added by a GitHub Actions workflow don't trigger other workflows |
| 74 |
# by design, so we have to manually ping code owners here. |
| 75 |
COMPONENT="${LABEL}" ISSUE=${ISSUE} SENDER="${SENDER}" bash "${CUR_DIRECTORY}/ping-codeowners-issues.sh" |
| 76 |
else |
| 77 |
gh issue edit "${ISSUE}" --remove-label "${LABEL}" |
| 78 |
fi |
| 79 |
done |
| 80 |
|
| 81 |
|