Skip to content

Commit b8c1acf

Browse files
Refactor: Extract helper function for adding field options to reduce duplication
Co-authored-by: andreasohlund <[email protected]>
1 parent 4f0b6b3 commit b8c1acf

File tree

1 file changed

+43
-48
lines changed

1 file changed

+43
-48
lines changed

tools/add-alpha-samples-to-project.sh

Lines changed: 43 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,43 @@ if [ -n "$PRIO_VALUE" ]; then
9393
fi
9494
echo ""
9595

96+
# Helper function to add an option to a single-select field
97+
add_field_option() {
98+
local FIELD_NAME=$1
99+
local FIELD_ID=$2
100+
local OPTION_VALUE=$3
101+
local PROJECT_ID=$4
102+
103+
echo "Adding '$OPTION_VALUE' option to '$FIELD_NAME' field..."
104+
105+
MUTATION=$(cat <<EOF
106+
mutation {
107+
addProjectV2SingleSelectFieldOption(input: {
108+
projectId: "$PROJECT_ID"
109+
fieldId: "$FIELD_ID"
110+
name: "$OPTION_VALUE"
111+
}) {
112+
option {
113+
id
114+
name
115+
}
116+
}
117+
}
118+
EOF
119+
)
120+
121+
ADD_OPTION_RESULT=$(gh api graphql -f query="$MUTATION" 2>&1)
122+
if [ $? -eq 0 ]; then
123+
echo " ✓ Added '$OPTION_VALUE' option to '$FIELD_NAME' field"
124+
# Extract and return the new option ID
125+
echo "$ADD_OPTION_RESULT" | jq -r '.data.addProjectV2SingleSelectFieldOption.option.id'
126+
return 0
127+
else
128+
echo " ✗ Failed to add option: $ADD_OPTION_RESULT"
129+
return 1
130+
fi
131+
}
132+
96133
# Get project ID and field IDs if custom fields are requested
97134
PROJECT_ID=""
98135
AREA_FIELD_ID=""
@@ -146,30 +183,9 @@ if [ -n "$AREA_VALUE" ] || [ -n "$PRIO_VALUE" ]; then
146183
# For single-select fields, we need to get the option ID
147184
AREA_OPTION_ID=$(echo "$FIELDS_DATA" | jq -r ".fields[] | select(.name == \"Area\") | .options[]? | select(.name == \"$AREA_VALUE\") | .id")
148185
if [ -z "$AREA_OPTION_ID" ] || [ "$AREA_OPTION_ID" = "null" ]; then
149-
echo "Adding '$AREA_VALUE' option to 'Area' field..."
150-
# Add the new option to the existing field using GraphQL
151-
MUTATION=$(cat <<EOF
152-
mutation {
153-
addProjectV2SingleSelectFieldOption(input: {
154-
projectId: "$PROJECT_ID"
155-
fieldId: "$AREA_FIELD_ID"
156-
name: "$AREA_VALUE"
157-
}) {
158-
option {
159-
id
160-
name
161-
}
162-
}
163-
}
164-
EOF
165-
)
166-
ADD_OPTION_RESULT=$(gh api graphql -f query="$MUTATION" 2>&1)
167-
if [ $? -eq 0 ]; then
168-
echo " ✓ Added '$AREA_VALUE' option to 'Area' field"
169-
# Extract the new option ID
170-
AREA_OPTION_ID=$(echo "$ADD_OPTION_RESULT" | jq -r '.data.addProjectV2SingleSelectFieldOption.option.id')
171-
else
172-
echo " ✗ Failed to add option: $ADD_OPTION_RESULT"
186+
# Add the new option to the existing field using helper function
187+
AREA_OPTION_ID=$(add_field_option "Area" "$AREA_FIELD_ID" "$AREA_VALUE" "$PROJECT_ID")
188+
if [ $? -ne 0 ] || [ -z "$AREA_OPTION_ID" ] || [ "$AREA_OPTION_ID" = "null" ]; then
173189
echo "Available options:"
174190
echo "$FIELDS_DATA" | jq -r '.fields[] | select(.name == "Area") | .options[]? | .name' | sed 's/^/ - /'
175191
AREA_VALUE=""
@@ -199,30 +215,9 @@ EOF
199215
# For single-select fields, we need to get the option ID
200216
PRIO_OPTION_ID=$(echo "$FIELDS_DATA" | jq -r ".fields[] | select(.name == \"Prio\") | .options[]? | select(.name == \"$PRIO_VALUE\") | .id")
201217
if [ -z "$PRIO_OPTION_ID" ] || [ "$PRIO_OPTION_ID" = "null" ]; then
202-
echo "Adding '$PRIO_VALUE' option to 'Prio' field..."
203-
# Add the new option to the existing field using GraphQL
204-
MUTATION=$(cat <<EOF
205-
mutation {
206-
addProjectV2SingleSelectFieldOption(input: {
207-
projectId: "$PROJECT_ID"
208-
fieldId: "$PRIO_FIELD_ID"
209-
name: "$PRIO_VALUE"
210-
}) {
211-
option {
212-
id
213-
name
214-
}
215-
}
216-
}
217-
EOF
218-
)
219-
ADD_OPTION_RESULT=$(gh api graphql -f query="$MUTATION" 2>&1)
220-
if [ $? -eq 0 ]; then
221-
echo " ✓ Added '$PRIO_VALUE' option to 'Prio' field"
222-
# Extract the new option ID
223-
PRIO_OPTION_ID=$(echo "$ADD_OPTION_RESULT" | jq -r '.data.addProjectV2SingleSelectFieldOption.option.id')
224-
else
225-
echo " ✗ Failed to add option: $ADD_OPTION_RESULT"
218+
# Add the new option to the existing field using helper function
219+
PRIO_OPTION_ID=$(add_field_option "Prio" "$PRIO_FIELD_ID" "$PRIO_VALUE" "$PROJECT_ID")
220+
if [ $? -ne 0 ] || [ -z "$PRIO_OPTION_ID" ] || [ "$PRIO_OPTION_ID" = "null" ]; then
226221
echo "Available options:"
227222
echo "$FIELDS_DATA" | jq -r '.fields[] | select(.name == "Prio") | .options[]? | .name' | sed 's/^/ - /'
228223
PRIO_VALUE=""

0 commit comments

Comments
 (0)