-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshared-check-pr-labels-with-prefix.yml
More file actions
60 lines (53 loc) · 2.23 KB
/
shared-check-pr-labels-with-prefix.yml
File metadata and controls
60 lines (53 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
name: check-pr-labels-with-prefix
on:
workflow_call:
inputs:
labels:
description: "JSON string of labels from the pull request"
required: false
type: string
default: ${{ toJSON(github.event.pull_request.labels) }}
prefix:
description: 'Label prefix to filter (e.g., "deploy-", "test-", "run-")'
required: false
default: ""
type: string
outputs:
labels:
description: "JSON object with labels matching the prefix"
value: ${{ jobs.check-pr-labels-with-prefix.outputs.labels }}
jobs:
check-pr-labels-with-prefix:
runs-on: ubuntu-latest
# Example outputs:
# - With prefix "deploy-": {"deploy-d": {full label object}, "deploy-n1": {full label object}}
outputs:
labels: ${{ steps.filter.outputs.filtered-labels }}
steps:
- name: filter-labels-by-prefix
id: filter
env:
LABELS_JSON: ${{ inputs.labels }}
run: |
PREFIX="${{ inputs.prefix }}"
echo "Prefix: $PREFIX"
echo "Input labels: $LABELS_JSON"
# Use jq to filter labels by prefix (empty prefix returns all labels)
JSON_RESULT=$(echo "$LABELS_JSON" | \
jq --arg prefix "$PREFIX" \
'map(select(.name | startswith($prefix))) |
map({(.name): .}) |
reduce .[] as $item ({}; . + $item)')
# Ensure JSON_RESULT is never empty - default to {} if empty
if [ -z "$JSON_RESULT" ] || [ "$JSON_RESULT" = "null" ]; then
JSON_RESULT="{}"
fi
# Use <<< to pass the JSON as a single line to avoid file command issues
{
echo "filtered-labels<<EOF"
echo "$JSON_RESULT"
echo "EOF"
} >> $GITHUB_OUTPUT
- name: display-filter-result
run: |
echo "Filtered labels: ${{ steps.filter.outputs.filtered-labels }}"