-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.yml
112 lines (96 loc) · 3.86 KB
/
action.yml
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
name: "Auto Label Pulls"
description: "Automated labeling for Pull Requests based on the target branch"
author: shiftEscape
branding:
icon: "tag"
color: "purple"
inputs:
token:
description: "Access token with restrictive `repo` permissions"
required: true
config-path:
description: "Path of JSON file containing branch names and their corresponding labels"
required: true
runs:
using: "composite"
steps:
- name: Checkout latest code
uses: actions/checkout@v2
- name: Verify Event, Action and Author
if: ${{ github.event_name != 'pull_request' || github.event.action != 'opened' || github.actor == 'dependabot[bot]' }}
shell: bash
run: |
echo "Event is not a 'pull request' of type 'opened' or author is 'dependabot'. Exiting the workflow."
exit 0
- name: Validate and Parse Config File
id: validate-and-parse
shell: bash
run: |
# Start here
CONFIGURATION_PATH="${{ inputs.config-path }}"
# Check if the configuration file exists
if [ ! -f $CONFIGURATION_PATH ]; then
echo "The config file '$CONFIGURATION_PATH' does not exist. Exiting the workflow."
exit 1
fi
# Try to parse the JSON file to validate if it's a correct JSON
JSON_CONFIG=$(cat $CONFIGURATION_PATH)
if ! echo $JSON_CONFIG | jq empty > /dev/null 2>&1; then
echo "Invalid JSON format in '$CONFIGURATION_PATH'. Exiting the workflow."
exit 1
fi
# Check if the JSON is not empty
if [ $(echo $JSON_CONFIG | jq 'length') -eq 0 ]; then
echo "The JSON file '$CONFIGURATION_PATH' is empty. Exiting the workflow."
exit 1
fi
# Set the ENV
echo "CONFIGURATION_PATH=$CONFIGURATION_PATH" >> "$GITHUB_OUTPUT"
- name: Verify PR Base Branch Config Match
id: verify-base-branch
shell: bash
run: |
BASE_BRANCH=${{ github.event.pull_request.base.ref }}
CONFIGURATION_PATH="${{ steps.validate-and-parse.outputs.CONFIGURATION_PATH }}"
JSON_CONFIG=$(cat $CONFIGURATION_PATH)
# Check if the base branch is in the keys of the JSON file
if ! echo $JSON_CONFIG | jq -e --arg key "$BASE_BRANCH" 'has($key)' > /dev/null; then
echo "Base branch '$BASE_BRANCH' not found in config file '$CONFIGURATION_PATH'"
exit 1
fi
# Get the value of the key in JSON_CONFIG
VALUE=$(echo $JSON_CONFIG | jq -r --arg key "$BASE_BRANCH" '.[$key]')
# Set the ENV
echo "TO_ASSIGN_LABEL=$VALUE" >> "$GITHUB_OUTPUT"
- name: Verify Label
id: verify-label
shell: bash
env:
GH_TOKEN: ${{ inputs.token }}
run: |
OWNER="${{ github.event.pull_request.head.repo.owner.login }}"
REPO="${{ github.event.pull_request.head.repo.name }}"
PR_NUMBER="${{ github.event.pull_request.number }}"
LABEL_NAME="${{ steps.verify-base-branch.outputs.TO_ASSIGN_LABEL }}"
ENCODE_LABEL="$(printf "%s" "$LABEL_NAME" | jq -s -R -r @uri)"
# Check if the label name exists
if ! gh api repos/$OWNER/$REPO/labels/$ENCODE_LABEL -f status=200 >/dev/null; then
RANDOM_COLOR=$(openssl rand -hex 3)
# Add the label to the pull request
gh api repos/$OWNER/$REPO/labels \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-f name="$LABEL_NAME" \
-f color="$RANDOM_COLOR"
fi
- name: Assign Label to PR
id: assign-label-to-pr
shell: bash
env:
GH_TOKEN: ${{ inputs.token }}
run: |
PR_NUMBER="${{ github.event.pull_request.number }}"
LABEL_NAME="${{ steps.verify-base-branch.outputs.TO_ASSIGN_LABEL }}"
# Assign label to PR
gh issue edit "$PR_NUMBER" --add-label "$LABEL_NAME"