Skip to content

Commit

Permalink
[chore] Allow adding component labels through comments (#15719)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
evan-bradley committed Nov 1, 2022
1 parent 49c5c48 commit 6e66d7a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 13 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/add-labels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:

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

runs-on: ubuntu-latest
steps:
Expand All @@ -20,3 +20,4 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ISSUE: ${{ github.event.issue.number }}
COMMENT: ${{ github.event.comment.body }}
SENDER: ${{ github.event.sender.login }}
60 changes: 53 additions & 7 deletions .github/workflows/scripts/add-labels.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,61 @@
#
#

if [ -z "${ISSUE}" ] || [ -z "${COMMENT}" ]; then
set -euo pipefail

if [[ -z "${ISSUE:-}" || -z "${COMMENT:-}" || -z "${SENDER:-}" ]]; then
echo "At least one of ISSUE, COMMENT, or SENDER has not been set, please ensure each is set."
exit 0
fi

declare -A labels
labels["/good-first-issue"]="good first issue"
labels["/help-wanted"]="help wanted"

CUR_DIRECTORY=$(dirname "$0")

if [ -n "${labels["${COMMENT}"]}" ] ; then
gh issue edit "${ISSUE}" --add-label "${labels["${COMMENT}"]}"
if [[ ${COMMENT:0:6} != "/label" ]]; then
echo "Comment is not a label comment, exiting."
exit 0
fi

declare -A COMMON_LABELS
COMMON_LABELS["good-first-issue"]="good first issue"
COMMON_LABELS["help-wanted"]="help wanted"

LABELS=$(echo "${COMMENT}" | sed -E 's%^/label%%')

for LABEL_REQ in ${LABELS}; do
LABEL=$(echo "${LABEL_REQ}" | sed -E s/^[+-]?//)
SHOULD_ADD=true

if [[ "${LABEL_REQ:0:1}" = "-" ]]; then
SHOULD_ADD=false
fi

if [[ -v COMMON_LABELS["${LABEL}"] ]]; then
if [[ ${SHOULD_ADD} = true ]]; then
gh issue edit "${ISSUE}" --add-label "${COMMON_LABELS["${LABEL}"]}"
else
gh issue edit "${ISSUE}" --remove-label "${COMMON_LABELS["${LABEL}"]}"
fi
continue
fi

# Grep exits with status code 1 if there are no matches,
# so we manually set RESULT to 0 if nothing is found.
RESULT=$(grep -c "${LABEL}" .github/CODEOWNERS || true)

if [[ ${RESULT} = 0 ]]; then
echo "\"${LABEL}\" doesn't correspond to a component, skipping."
continue
fi

if [[ ${SHOULD_ADD} = true ]]; then
gh issue edit "${ISSUE}" --add-label "${LABEL}"

# Labels added by a GitHub Actions workflow don't trigger other workflows
# by design, so we have to manually ping code owners here.
COMPONENT="${LABEL}" ISSUE=${ISSUE} SENDER="${SENDER}" bash "${CUR_DIRECTORY}/ping-codeowners.sh"
else
gh issue edit "${ISSUE}" --remove-label "${LABEL}"
fi
done


18 changes: 13 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,22 @@ in general try to follow them.

## Adding Labels via Comments

In order to facilitate proper label usage and to empower Code Owners, you are able to add the following labels to issues via comments.
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).

| Label | Generating Comment |
The following general labels are supported:

| Label | Label in Comment |
|--------------------|--------------------|
| `good first issue` | /good-first-issue |
| `help wanted` | /help-wanted |
| `good first issue` | `good-first-issue` |
| `help wanted` | `help-wanted` |

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.

Example label comment:

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

## Becoming a Code Owner

Expand Down

0 comments on commit 6e66d7a

Please sign in to comment.