Skip to content

Extension metadata updates #23

Extension metadata updates

Extension metadata updates #23

Workflow file for this run

name: Autolaber
on:
# pull_request_target is dangerous but necessary to assign labels to the pull request.
pull_request_target:
# Only label on initial open so as to not trigger a whole lot unnecessarily
# and also allow humans to override without their changes getting overwritten
# on the next commit.
types: [opened]
jobs:
label-pull-request:
runs-on: ubuntu-latest
# Disabled by default for forks since this is probably not useful.
if: ${{ github.repository == 'TurboWarp/extensions' }}
permissions:
pull-requests: write
steps:
# This is a sensitive workflow because we have write permissions for pull-requests but we are
# processing remote code that we can't trust. Be careful not to place any trust in the contents
# of the pull request.
- name: Assign labels
run: |
LABEL_NEW_EXTENSION="pr: new extension"
LABEL_CHANGE_EXTENSION="pr: change existing extension"
LABEL_OTHER="pr: other"
# grep doesn't have good multiline support and installing pcregrep through apt is slow, so
# make our own tiny regex checker tool.
cat > matches <<EOF
#!/usr/bin/env python3
import sys
import re
pattern = sys.argv[1]
file = sys.argv[2]
with open(file, 'r') as f:
contents = f.read()
if re.search(pattern, contents, re.MULTILINE):
sys.exit(0)
else:
sys.exit(1)
EOF
chmod +x matches
got_any_specific_label=false
if [[ "$BASE_REF" == "master" ]]; then
echo "Downloading pull request diff"
# Download just the diff so it is harder to accidentally run any code from the pull request.
gh pr diff --repo "$GH_REPO" "$PR_NUMBER" | tee pr.diff
if ./matches "^--- /dev/null\n\+\+\+ b/extensions/" pr.diff; then
# Example:
# --- /dev/null
# +++ b/extensions/DangoCat/extension.js
echo "Adding label: $LABEL_NEW_EXTENSION"
gh pr edit --repo "$GH_REPO" "$PR_NUMBER" --add-label "$LABEL_NEW_EXTENSION"
got_any_specific_label=true
elif ./matches "^\+\+\+ b/extensions/" pr.diff; then
# Example:
# --- a/extensions/DangoCat/extension.js
# +++ b/extensions/DangoCat/extension.js
echo "Adding label: $LABEL_CHANGE_EXTENSION"
gh pr edit --repo "$GH_REPO" "$PR_NUMBER" --add-label "$LABEL_CHANGE_EXTENSION"
got_any_specific_label=true
fi
else
echo "Unusual base ref: $BASE_REF"
fi
# Any PR that didn't get a specific label will go into other, for a human to look at.
if [[ "$got_any_specific_label" == "false" ]]; then
echo "Adding label: $LABEL_OTHER"
gh pr edit --repo "$GH_REPO" "$PR_NUMBER" --add-label "$LABEL_OTHER"
fi
env:
PR_NUMBER: "${{ github.event.number }}"
BASE_REF: "${{ github.base_ref }}"
GH_TOKEN: "${{ github.token }}"
GH_REPO: "${{ github.repository }}"