Debian Codegraph @

.github/workflows/scripts/ping-codeowners-on-new-issue.sh

raw
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 "${TITLE:-}" || -z "${BODY:-}" || -z "${OPENER:-}" ]]; then
11 echo "Missing one of ISSUE, TITLE, BODY, or OPENER, please ensure all are set."
12 exit 0
13 fi
14
15 LABELS_COMMENT='See [Adding Labels via Comments](https://github.com/open-telemetry/opentelemetry-collector/blob/main/CONTRIBUTING.md#adding-labels-via-comments) if you do not have permissions to add labels yourself.'
16 CUR_DIRECTORY=$(dirname "$0")
17 LABELS=""
18 PING_LINES=""
19 declare -A PINGED_COMPONENTS
20
21 TITLE_COMPONENT=$(echo "${TITLE}" | (grep -oE "\[.+\]" || true) | sed -E 's/\[(.+)\]/\1/' | sed -E 's%^(.+)/(.+)\1%\1/\2%')
22
23 COMPONENTS_SECTION_START=$( (echo "${BODY}" | grep -n '### Component(s)' | awk '{ print $1 }' | grep -oE '[0-9]+') || echo '-1' )
24 BODY_COMPONENTS=""
25
26 if [[ "${COMPONENTS_SECTION_START}" != '-1' ]]; then
27 BODY_COMPONENTS=$(echo "${BODY}" | sed -n $((COMPONENTS_SECTION_START+2))p)
28 fi
29
30 if [[ -n "${TITLE_COMPONENT}" && ! ("${TITLE_COMPONENT}" =~ " ") ]]; then
31 CODEOWNERS=$(COMPONENT="${TITLE_COMPONENT}" "${CUR_DIRECTORY}/get-codeowners.sh" || true)
32
33 if [[ -n "${CODEOWNERS}" ]]; then
34 PING_LINES+="- ${TITLE_COMPONENT}: ${CODEOWNERS}\n"
35 PINGED_COMPONENTS["${TITLE_COMPONENT}"]=1
36
37 if (( "${#TITLE_COMPONENT}" <= 50 )); then
38 LABELS+="${TITLE_COMPONENT}"
39 else
40 echo "'${TITLE_COMPONENT}' exceeds GitHub's 50-character limit, skipping adding a label"
41 fi
42 fi
43 fi
44
45 for COMPONENT in ${BODY_COMPONENTS}; do
46 # Comments are delimited by ', ' and the for loop separates on spaces, so remove the extra comma.
47 COMPONENT=${COMPONENT//,/}
48
49 CODEOWNERS=$(COMPONENT="${COMPONENT}" "${CUR_DIRECTORY}/get-codeowners.sh" || true)
50
51 if [[ -n "${CODEOWNERS}" ]]; then
52 if [[ -v PINGED_COMPONENTS["${COMPONENT}"] ]]; then
53 continue
54 fi
55
56 PING_LINES+="- ${COMPONENT}: ${CODEOWNERS}\n"
57 PINGED_COMPONENTS["${COMPONENT}"]=1
58
59 if (( "${#COMPONENT}" > 50 )); then
60 echo "'${COMPONENT}' exceeds GitHub's 50-character limit on labels, skipping adding a label"
61 continue
62 fi
63
64 if [[ -n "${LABELS}" ]]; then
65 LABELS+=","
66 fi
67 LABELS+="${COMPONENT}"
68 fi
69 done
70
71 if [[ -v PINGED_COMPONENTS[@] ]]; then
72 echo "The issue was associated with components:" "${!PINGED_COMPONENTS[@]}"
73 else
74 echo "No related components were given"
75 fi
76
77 if [[ -n "${LABELS}" ]]; then
78 # Notes on this call:
79 # 1. Labels will be deduplicated by the GitHub CLI.
80 # 2. The call to edit the issue will fail if any of the
81 # labels doesn't exist. We can be reasonably sure that
82 # all labels will exist since they come from a known set.
83 echo "Adding the following labels: ${LABELS//,/ /}"
84 gh issue edit "${ISSUE}" --add-label "${LABELS}" || true
85 else
86 echo "No labels were found to add"
87 fi
88
89 if [[ -n "${PING_LINES}" ]]; then
90 # Notes on this call:
91 # 1. Adding labels above will not trigger the ping-codeowners flow,
92 # since GitHub Actions disallows triggering a workflow from a
93 # workflow, so we have to ping code owners here.
94 # 2. The GitHub CLI only offers multiline strings through file input,
95 # so we provide the comment through stdin.
96 # 3. The PING_LINES variable must be directly put into the echo string
97 # to get the newlines to render correctly, using string formatting
98 # causes the newlines to be interpreted literally.
99 echo -e "Pinging code owners:\n${PING_LINES}"
100 echo -e "Pinging code owners:\n${PING_LINES}\n" "${LABELS_COMMENT}" \
101 | gh issue comment "${ISSUE}" -F -
102 else
103 echo "No code owners were found to ping"
104 fi