Skip to content

Commit 6e66d7a

Browse files
authored
[chore] Allow adding component labels through comments (open-telemetry#15719)
This adds the ability to add and remove component labels using comments, which will allow issue openers to add the label corresponding to a component to ping the code owners, and will enable code owners to update the component labels on an issue if necessary. There are two relatively minor caveats with the way the add-labels.sh script is currently implemented. - Labels and comments are added separately, so adding a lot of labels can make things noisy. This can be fixed with additional processing, but I figure this is an uncommon case, so I opted to instead keep the script simple. - Adding a component label that already is applied will ping the code owners again.
1 parent 49c5c48 commit 6e66d7a

File tree

3 files changed

+68
-13
lines changed

3 files changed

+68
-13
lines changed

.github/workflows/add-labels.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ on:
55

66
jobs:
77
add-labels:
8-
if: ${{ !github.event.issue.pull_request && (github.event.comment.body == '/help-wanted' || github.event.comment.body == '/good-first-issue' ) }}
8+
if: ${{ !github.event.issue.pull_request && startsWith(github.event.comment.body, '/label') }}
99

1010
runs-on: ubuntu-latest
1111
steps:
@@ -20,3 +20,4 @@ jobs:
2020
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2121
ISSUE: ${{ github.event.issue.number }}
2222
COMMENT: ${{ github.event.comment.body }}
23+
SENDER: ${{ github.event.sender.login }}

.github/workflows/scripts/add-labels.sh

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,61 @@
1616
#
1717
#
1818

19-
if [ -z "${ISSUE}" ] || [ -z "${COMMENT}" ]; then
19+
set -euo pipefail
20+
21+
if [[ -z "${ISSUE:-}" || -z "${COMMENT:-}" || -z "${SENDER:-}" ]]; then
22+
echo "At least one of ISSUE, COMMENT, or SENDER has not been set, please ensure each is set."
2023
exit 0
2124
fi
2225

23-
declare -A labels
24-
labels["/good-first-issue"]="good first issue"
25-
labels["/help-wanted"]="help wanted"
26-
26+
CUR_DIRECTORY=$(dirname "$0")
2727

28-
if [ -n "${labels["${COMMENT}"]}" ] ; then
29-
gh issue edit "${ISSUE}" --add-label "${labels["${COMMENT}"]}"
28+
if [[ ${COMMENT:0:6} != "/label" ]]; then
29+
echo "Comment is not a label comment, exiting."
30+
exit 0
3031
fi
32+
33+
declare -A COMMON_LABELS
34+
COMMON_LABELS["good-first-issue"]="good first issue"
35+
COMMON_LABELS["help-wanted"]="help wanted"
36+
37+
LABELS=$(echo "${COMMENT}" | sed -E 's%^/label%%')
38+
39+
for LABEL_REQ in ${LABELS}; do
40+
LABEL=$(echo "${LABEL_REQ}" | sed -E s/^[+-]?//)
41+
SHOULD_ADD=true
42+
43+
if [[ "${LABEL_REQ:0:1}" = "-" ]]; then
44+
SHOULD_ADD=false
45+
fi
46+
47+
if [[ -v COMMON_LABELS["${LABEL}"] ]]; then
48+
if [[ ${SHOULD_ADD} = true ]]; then
49+
gh issue edit "${ISSUE}" --add-label "${COMMON_LABELS["${LABEL}"]}"
50+
else
51+
gh issue edit "${ISSUE}" --remove-label "${COMMON_LABELS["${LABEL}"]}"
52+
fi
53+
continue
54+
fi
55+
56+
# Grep exits with status code 1 if there are no matches,
57+
# so we manually set RESULT to 0 if nothing is found.
58+
RESULT=$(grep -c "${LABEL}" .github/CODEOWNERS || true)
59+
60+
if [[ ${RESULT} = 0 ]]; then
61+
echo "\"${LABEL}\" doesn't correspond to a component, skipping."
62+
continue
63+
fi
64+
65+
if [[ ${SHOULD_ADD} = true ]]; then
66+
gh issue edit "${ISSUE}" --add-label "${LABEL}"
67+
68+
# Labels added by a GitHub Actions workflow don't trigger other workflows
69+
# by design, so we have to manually ping code owners here.
70+
COMPONENT="${LABEL}" ISSUE=${ISSUE} SENDER="${SENDER}" bash "${CUR_DIRECTORY}/ping-codeowners.sh"
71+
else
72+
gh issue edit "${ISSUE}" --remove-label "${LABEL}"
73+
fi
74+
done
75+
76+

CONTRIBUTING.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,22 @@ in general try to follow them.
160160

161161
## Adding Labels via Comments
162162

163-
In order to facilitate proper label usage and to empower Code Owners, you are able to add the following labels to issues via comments.
163+
In order to facilitate proper label usage and to empower Code Owners, you are able to add labels to issues via comments. To add a label through a comment, post a new comment on an issue starting with `/label`, followed by a space-separated list of your desired labels. Supported labels come from the table below, or correspond to a component defined in the [CODEOWNERS file](.github/CODEOWNERS).
164164

165-
| Label | Generating Comment |
165+
The following general labels are supported:
166+
167+
| Label | Label in Comment |
166168
|--------------------|--------------------|
167-
| `good first issue` | /good-first-issue |
168-
| `help wanted` | /help-wanted |
169+
| `good first issue` | `good-first-issue` |
170+
| `help wanted` | `help-wanted` |
171+
172+
To delete a label, prepend the label with `-`. Note that you must make a new comment to modify labels; you cannot edit an existing comment.
173+
174+
Example label comment:
169175

170-
Currently, labels can only be created via comment, not deleted. You must make a new comment; you cannot edit an existing comment.
176+
```
177+
/label receiver/prometheus help-wanted -exporter/prometheus
178+
```
171179

172180
## Becoming a Code Owner
173181

0 commit comments

Comments
 (0)