|
| 1 | +name: Label PR |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: |
| 6 | + - opened |
| 7 | + - synchronize |
| 8 | + - reopened |
| 9 | + |
| 10 | +jobs: |
| 11 | + label: |
| 12 | + name: Apply labels from changesets |
| 13 | + # Skip fork PRs — GITHUB_TOKEN is read-only there and the labeling would fail. |
| 14 | + if: github.event.pull_request.head.repo.full_name == github.repository |
| 15 | + runs-on: ubuntu-latest |
| 16 | + permissions: |
| 17 | + pull-requests: write |
| 18 | + contents: read |
| 19 | + steps: |
| 20 | + - uses: actions/checkout@v4 |
| 21 | + with: |
| 22 | + fetch-depth: 0 |
| 23 | + - name: Determine and apply label |
| 24 | + env: |
| 25 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 26 | + PR_NUM: ${{ github.event.pull_request.number }} |
| 27 | + BASE_SHA: ${{ github.event.pull_request.base.sha }} |
| 28 | + run: | |
| 29 | + set -euo pipefail |
| 30 | +
|
| 31 | + CHANGESETS=$(git diff --name-only --diff-filter=A "$BASE_SHA" HEAD -- '.changeset/*.md' | grep -v 'README\.md$' || true) |
| 32 | +
|
| 33 | + if [ -z "$CHANGESETS" ]; then |
| 34 | + TARGET="no-changelog" |
| 35 | + else |
| 36 | + HAS_APP=0 |
| 37 | + HAS_THEME=0 |
| 38 | + HAS_OTHER=0 |
| 39 | + while IFS= read -r FILE; do |
| 40 | + [ -z "$FILE" ] && continue |
| 41 | + FRONTMATTER=$(awk '/^---$/{c++; if (c==2) exit; next} c==1' "$FILE") |
| 42 | + while IFS= read -r PKG; do |
| 43 | + case "$PKG" in |
| 44 | + @shopify/app|@shopify/create-app) HAS_APP=1 ;; |
| 45 | + @shopify/theme) HAS_THEME=1 ;; |
| 46 | + @shopify/*) HAS_OTHER=1 ;; |
| 47 | + esac |
| 48 | + done < <(echo "$FRONTMATTER" | grep -oE "'@shopify/[a-z-]+'" | tr -d "'") |
| 49 | + done <<< "$CHANGESETS" |
| 50 | +
|
| 51 | + AREA_COUNT=$((HAS_APP + HAS_THEME + HAS_OTHER)) |
| 52 | + if [ "$AREA_COUNT" -eq 1 ] && [ "$HAS_APP" -eq 1 ]; then |
| 53 | + TARGET="Area: @shopify/app" |
| 54 | + elif [ "$AREA_COUNT" -eq 1 ] && [ "$HAS_THEME" -eq 1 ]; then |
| 55 | + TARGET="Area: @shopify/theme" |
| 56 | + else |
| 57 | + TARGET="Area: @shopify/cli" |
| 58 | + fi |
| 59 | + fi |
| 60 | +
|
| 61 | + echo "Target label: $TARGET" |
| 62 | +
|
| 63 | + # Idempotently keep only the target label from the managed set. |
| 64 | + for L in "no-changelog" "Area: @shopify/app" "Area: @shopify/theme" "Area: @shopify/cli"; do |
| 65 | + if [ "$L" = "$TARGET" ]; then |
| 66 | + gh pr edit "$PR_NUM" --add-label "$L" |
| 67 | + else |
| 68 | + gh pr edit "$PR_NUM" --remove-label "$L" 2>/dev/null || true |
| 69 | + fi |
| 70 | + done |
0 commit comments