-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjira.sh
More file actions
executable file
·1290 lines (1108 loc) · 43.7 KB
/
jira.sh
File metadata and controls
executable file
·1290 lines (1108 loc) · 43.7 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
set -euo pipefail
# --- jira.sh ---
# Unified script for Jira ticket operations, field discovery, and template management.
#
# Requirements:
# - Bash 4.0+ (for modern bash features)
# - curl: For Jira API requests
# - jq: For JSON processing
# - yq: For YAML template processing
# - base64: For credential encoding (provided by coreutils)
#
# Usage:
# ./jira.sh [--debug|--verbose|--quiet] <command> [options]
#
# Commands:
# create, create-from-template, update
# list-fields, list-issue-types, show-required
# generate-template, fetch-metadata
# --------------------------------------------------
# Source the reusable functions
# shellcheck source=functions.sh
source "$(dirname "$0")/functions.sh"
# shellcheck source=functions-jira.sh
source "$(dirname "$0")/functions-jira.sh"
# --- Configuration ---
# Cache directory for metadata
CACHE_DIR="${HOME}/.jira-cache"
# --- Helper Functions ---
# Get cache file path for a project and issue type
get_cache_file() {
local project="$1"
local issue_type="$2"
echo "${CACHE_DIR}/${project}-${issue_type}.json"
}
# Fetch create metadata from Jira for a specific project and issue type
fetch_metadata_from_jira() {
local project="$1"
local issue_type="$2"
log_verbose "Fetching create metadata for project ${project}, issue type ${issue_type}..."
local api_endpoint="/rest/api/2/issue/createmeta"
api_endpoint="${api_endpoint}?projectKeys=${project}&issuetypeNames=${issue_type}&expand=projects.issuetypes.fields"
# Make the API call using common function
if ! jira_api_get "${api_endpoint}"; then
return 1
fi
if [ -z "${jira_response_body}" ]; then
log_error "Received empty response from Jira."
return 1
fi
echo "${jira_response_body}"
}
# Cache metadata to file
cache_metadata() {
local project="$1"
local issue_type="$2"
local metadata="$3"
local cache_file
cache_file=$(get_cache_file "${project}" "${issue_type}")
# Create cache directory if it doesn't exist with restricted permissions
if [ ! -d "${CACHE_DIR}" ]; then
if ! mkdir -p -m 700 "${CACHE_DIR}"; then
log_error "Failed to create cache directory: ${CACHE_DIR}"
return 1
fi
log_debug "Created cache directory: ${CACHE_DIR}"
else
# Ensure existing directory has correct permissions (owner only)
chmod 700 "${CACHE_DIR}" 2>/dev/null || true
fi
# Write metadata to cache file with restricted permissions (owner read/write only)
# Use umask 077 to ensure file is created with 600 permissions
if ! (umask 077 && echo "${metadata}" > "${cache_file}"); then
log_error "Failed to write metadata to cache file: ${cache_file}"
return 1
fi
# Explicitly set permissions as backup (in case umask didn't work)
chmod 600 "${cache_file}" 2>/dev/null || true
log_info "Metadata cached to: ${cache_file}"
return 0
}
# Get cached metadata or fetch if not available
get_metadata() {
local project="$1"
local issue_type="$2"
local force_refresh="${3:-false}"
local cache_file
cache_file=$(get_cache_file "${project}" "${issue_type}")
# If force refresh or cache doesn't exist, fetch from Jira
if [ "${force_refresh}" = "true" ] || [ ! -f "${cache_file}" ]; then
log_debug "Fetching metadata from Jira..."
local metadata
if ! metadata=$(fetch_metadata_from_jira "${project}" "${issue_type}"); then
return 1
fi
cache_metadata "${project}" "${issue_type}" "${metadata}"
echo "${metadata}"
else
log_debug "Using cached metadata from: ${cache_file}"
cat "${cache_file}"
fi
}
# Extract required fields from metadata
get_required_fields() {
local metadata="$1"
# Extract fields that are required
echo "${metadata}" | jq -r '
.projects[0].issuetypes[0].fields
| to_entries[]
| select(.value.required == true)
| {key: .key, name: .value.name, schema: .value.schema.type, allowedValues: .value.allowedValues}
'
}
# Validate that all required fields are present in the payload
validate_required_fields() {
local project="$1"
local issue_type="$2"
local payload="$3"
log_debug "Validating required fields..."
local metadata
if ! metadata=$(get_metadata "${project}" "${issue_type}"); then
log_error "Failed to get metadata for validation."
return 1
fi
local required_fields
required_fields=$(get_required_fields "${metadata}")
if [ -z "${required_fields}" ]; then
log_debug "No required fields found or unable to parse metadata."
return 0
fi
local missing_fields=()
while IFS= read -r field_json; do
local field_key
field_key=$(echo "${field_json}" | jq -r '.key')
local field_name
field_name=$(echo "${field_json}" | jq -r '.name')
# Check if the field exists in the payload
if ! echo "${payload}" | jq -e ".fields.${field_key}" > /dev/null 2>&1; then
missing_fields+=("${field_key} (${field_name})")
log_debug "Missing required field: ${field_key}"
fi
done <<< "$(echo "${required_fields}" | jq -c '.')"
if [ "${#missing_fields[@]}" -gt 0 ]; then
log_error "Missing required fields:"
for field in "${missing_fields[@]}"; do
log_error " - ${field}"
done
return 1
fi
log_debug "All required fields are present."
return 0
}
usage() {
cat <<EOF
Unified script for Jira ticket operations, field discovery, and template management.
Usage:
./jira.sh [global-options] <command> [options]
Global Options:
--help Show this help message
--log-level <LEVEL> Set log level: DEBUG, VERBOSE, INFO, ERROR (default: INFO)
--debug Shortcut for --log-level DEBUG (most verbose)
--verbose Shortcut for --log-level VERBOSE
--quiet Shortcut for --log-level ERROR (only errors)
Commands:
Issue Operations:
create <PROJECT> <SUMMARY> <DESCRIPTION> [options]
Create a minimal ticket with basic fields.
Options: --type <TYPE>, --skip-validation
create-from-template <PROJECT> <YAML_FILE> [options]
Create a ticket from a YAML template.
Options: --skip-validation, --interactive, --no-interactive
update <TICKET_KEY> <YAML_FILE> [options]
Update an existing ticket with a YAML template.
Options: --interactive, --no-interactive
Field Discovery:
list-fields [<PROJECT> <ISSUE_TYPE>] [options]
List available Jira fields.
Without PROJECT/ISSUE_TYPE: Lists all global fields (exploration only)
With PROJECT/ISSUE_TYPE: Lists fields for that specific combination
Options: --filter <string>
list-issue-types <PROJECT>
List available issue types for a project.
show-required <PROJECT> <ISSUE_TYPE>
Show required fields for a project/issue type.
Template Management:
generate-template <PROJECT> <ISSUE_TYPE> <OUTPUT_FILE> [options]
Generate a YAML template with available fields.
Options: --required-fields, --filter <string> (repeatable), --update
Metadata Management:
fetch-metadata <PROJECT> <ISSUE_TYPE> [options]
Fetch and cache field metadata for a project/issue type.
Options: --refresh
Examples:
# Create a ticket
jira.sh create PROJ "Fix login bug" "Users cannot log in" --type Bug
# Create from interactive template
jira.sh create-from-template PROJ story-template.yml
# Update ticket with interactive template
jira.sh update PROJ-123 update-template.yml
# List all global fields (for exploration)
jira.sh list-fields --filter "Story"
# List fields for specific project/issue type
jira.sh list-fields PROJ Story
# List issue types for a project
jira.sh list-issue-types PROJ
# Generate template with only required fields
jira.sh generate-template PROJ Story template.yml --required-fields
# Generate template with multiple specific fields
jira.sh generate-template PROJ Story template.yml --filter "Sprint" --filter "Story Points"
# Show required fields
jira.sh show-required PROJ Story
For detailed help on any command:
jira.sh <command> --help
Note: Templates support interactive placeholders:
- {{PROMPT: text}} and {{INPUT: text}} for single-line input
- {{PROMPT_MULTI: text}} and {{INPUT_MULTI: text}} for multi-line input
Multi-line input: type 'END' on a new line when finished
EOF
}
# --- Commands ---
cmd_create() {
if [ "$#" -lt 3 ]; then
log_error "Usage: ./jira.sh create <PROJECT> <SUMMARY> <DESCRIPTION> [--type <TYPE>] [--skip-validation]"
exit 1
fi
local project="$1"
local summary="$2"
local description="$3"
shift 3
local issue_type="Task"
local skip_validation="false"
# Parse optional arguments
while [ "$#" -gt 0 ]; do
case "$1" in
--type)
if [ -z "$2" ]; then
log_error "Error: --type requires a value."
return 1
fi
issue_type="$2"
shift 2
;;
--skip-validation)
skip_validation="true"
shift
;;
*)
log_error "Error: Unknown option '$1'"
echo "Usage: ./jira.sh create <PROJECT> <SUMMARY> <DESCRIPTION> [--type <TYPE>] [--skip-validation]"
return 1
;;
esac
done
log_info "Creating ${issue_type} in project ${project}..."
log_verbose "Summary: ${summary}"
log_verbose "Description: ${description}"
# Build JSON payload
local json_payload
json_payload=$(jq -n \
--arg project "${project}" \
--arg summary "${summary}" \
--arg description "${description}" \
--arg issuetype "${issue_type}" \
'{
fields: {
project: {
key: $project
},
summary: $summary,
description: $description,
issuetype: {
name: $issuetype
}
}
}')
log_debug "JSON payload created."
log_debug "Payload: ${json_payload}"
# Validate required fields unless skipped
if [ "${skip_validation}" = "false" ]; then
if ! validate_required_fields "${project}" "${issue_type}" "${json_payload}"; then
log_error "Validation failed. Use --skip-validation to bypass, or check required fields with:"
log_error " ./jira.sh show-required ${project} ${issue_type}"
exit 1
fi
else
log_info "Skipping validation as requested."
fi
# Make the API call using common function
if ! jira_api_post "/rest/api/2/issue" "${json_payload}"; then
exit 1
fi
# Extract ticket key from response
local ticket_key
ticket_key=$(echo "${jira_response_body}" | jq -r '.key')
log_info "Successfully created ticket ${ticket_key}!"
log_info "View it here: ${jira_base_url}/browse/${ticket_key}"
}
cmd_create_from_template() {
if [ "$#" -lt 2 ]; then
log_error "Usage: ./jira.sh create-from-template <PROJECT> <YAML_FILE> [--skip-validation] [--interactive|--no-interactive]"
exit 1
fi
local project="$1"
local yaml_file="$2"
shift 2
local skip_validation="false"
local interactive="auto"
# Parse optional arguments
while [ "$#" -gt 0 ]; do
case "$1" in
--skip-validation)
skip_validation="true"
shift
;;
--interactive)
interactive="true"
shift
;;
--no-interactive)
interactive="false"
shift
;;
*)
log_error "Error: Unknown option '$1'"
echo "Usage: ./jira.sh create-from-template <PROJECT> <YAML_FILE> [--skip-validation] [--interactive|--no-interactive]"
return 1
;;
esac
done
log_info "Creating ticket in project ${project} from template ${yaml_file}."
# Validate YAML file using common function
if ! validate_yaml_file "${yaml_file}"; then
exit 1
fi
# Read the YAML payload from the file and convert to JSON
local yaml_content
yaml_content=$(yq -o=json '.' "${yaml_file}")
log_debug "Read and converted YAML payload from file."
# Build the final JSON payload with project key
local json_payload
json_payload=$(echo "${yaml_content}" | jq --arg project "${project}" '.fields.project = {key: $project}')
log_debug "JSON payload created with project key."
# Auto-detect interactive mode if not explicitly set
if [ "${interactive}" = "auto" ]; then
# Check if template contains placeholders
if echo "${json_payload}" | jq -e '.. | select(type == "string" and test("^\\{\\{(PROMPT|INPUT):"))' > /dev/null 2>&1; then
# Check if we're in an interactive terminal
if [ -t 0 ]; then
interactive="true"
log_verbose "Auto-detected interactive placeholders in template. Enabling interactive mode."
else
interactive="false"
log_error "Template contains interactive placeholders but stdin is not a terminal."
log_error "Either run in an interactive terminal or use --no-interactive to skip prompts."
exit 1
fi
else
interactive="false"
fi
fi
# Process interactive placeholders if needed
if [ "${interactive}" = "true" ]; then
if ! json_payload=$(process_interactive_template "${json_payload}" "true"); then
exit 1
fi
elif [ "${interactive}" = "false" ]; then
if ! json_payload=$(process_interactive_template "${json_payload}" "false"); then
exit 1
fi
fi
log_debug "Final payload: ${json_payload}"
# Extract issue type from the payload for validation
local issue_type
issue_type=$(echo "${json_payload}" | jq -r '.fields.issuetype.name // "Task"')
# Validate required fields unless skipped
if [ "${skip_validation}" = "false" ]; then
if ! validate_required_fields "${project}" "${issue_type}" "${json_payload}"; then
log_error "Validation failed. Use --skip-validation to bypass, or check required fields with:"
log_error " ./jira.sh show-required ${project} ${issue_type}"
exit 1
fi
else
log_info "Skipping validation as requested."
fi
log_verbose "Creating ticket in project: ${project}"
log_verbose "Using template file: ${yaml_file}"
# Make the API call using common function
if ! jira_api_post "/rest/api/2/issue" "${json_payload}"; then
exit 1
fi
# Extract ticket key from response
local ticket_key
ticket_key=$(echo "${jira_response_body}" | jq -r '.key')
log_info "Successfully created ticket ${ticket_key}!"
log_info "View it here: ${jira_base_url}/browse/${ticket_key}"
}
cmd_update() {
if [ "$#" -lt 2 ]; then
log_error "Usage: ./jira.sh update <TICKET_KEY> <YAML_FILE> [--interactive|--no-interactive]"
exit 1
fi
local ticket_key="$1"
local yaml_file="$2"
shift 2
local interactive="auto"
# Parse optional arguments
while [ "$#" -gt 0 ]; do
case "$1" in
--interactive)
interactive="true"
shift
;;
--no-interactive)
interactive="false"
shift
;;
*)
log_error "Error: Unknown option '$1'"
echo "Usage: ./jira.sh update <TICKET_KEY> <YAML_FILE> [--interactive|--no-interactive]"
return 1
;;
esac
done
log_info "Updating ticket ${ticket_key} with data from ${yaml_file}."
# Validate YAML file using common function
if ! validate_yaml_file "${yaml_file}"; then
exit 1
fi
# Read the YAML payload from the file and convert to JSON
local json_payload
json_payload=$(yq -o=json '.' "$yaml_file")
log_debug "Read and converted YAML payload from file."
# Auto-detect interactive mode if not explicitly set
if [ "${interactive}" = "auto" ]; then
# Check if template contains placeholders
if echo "${json_payload}" | jq -e '.. | select(type == "string" and test("^\\{\\{(PROMPT|INPUT):"))' > /dev/null 2>&1; then
# Check if we're in an interactive terminal
if [ -t 0 ]; then
interactive="true"
log_verbose "Auto-detected interactive placeholders in template. Enabling interactive mode."
else
interactive="false"
log_error "Template contains interactive placeholders but stdin is not a terminal."
log_error "Either run in an interactive terminal or use --no-interactive to skip prompts."
exit 1
fi
else
interactive="false"
fi
fi
# Process interactive placeholders if needed
if [ "${interactive}" = "true" ]; then
if ! json_payload=$(process_interactive_template "${json_payload}" "true"); then
exit 1
fi
elif [ "${interactive}" = "false" ]; then
if ! json_payload=$(process_interactive_template "${json_payload}" "false"); then
exit 1
fi
fi
log_verbose "Updating ticket: ${ticket_key}"
log_verbose "Using template file: ${yaml_file}"
log_debug "Final payload: ${json_payload}"
# Make the API call using common function
if ! jira_api_put "/rest/api/2/issue/${ticket_key}" "${json_payload}"; then
exit 1
fi
log_info "Successfully updated ticket ${ticket_key}!"
log_info "View it here: ${jira_base_url}/browse/${ticket_key}"
}
cmd_list_fields() {
local project=""
local issue_type=""
local filter_name=""
# Parse positional and optional arguments
while [ "$#" -gt 0 ]; do
case "$1" in
--filter)
if [ -z "$2" ]; then
log_error "Error: --filter requires a value."
return 1
fi
filter_name="$2"
shift 2
;;
--help)
echo "Usage: ./jira.sh list-fields [<PROJECT> <ISSUE_TYPE>] [--filter <string>]"
echo ""
echo "List available Jira fields."
echo ""
echo "Without PROJECT/ISSUE_TYPE: Lists all global Jira fields (exploration only)"
echo "With PROJECT/ISSUE_TYPE: Lists fields available for that specific combination"
echo ""
echo "Options:"
echo " --filter <string> Filter fields by name (case-insensitive)"
return 0
;;
-*)
log_error "Error: Unknown option '$1'"
echo "Usage: ./jira.sh list-fields [<PROJECT> <ISSUE_TYPE>] [--filter <string>]"
return 1
;;
*)
# Positional arguments
if [ -z "${project}" ]; then
project="$1"
shift
elif [ -z "${issue_type}" ]; then
issue_type="$1"
shift
else
log_error "Error: Too many positional arguments"
echo "Usage: ./jira.sh list-fields [<PROJECT> <ISSUE_TYPE>] [--filter <string>]"
return 1
fi
;;
esac
done
# Validate that if project is provided, issue_type must also be provided
if [ -n "${project}" ] && [ -z "${issue_type}" ]; then
log_error "Error: When PROJECT is specified, ISSUE_TYPE must also be specified"
echo "Usage: ./jira.sh list-fields [<PROJECT> <ISSUE_TYPE>] [--filter <string>]"
return 1
fi
if [ -n "${filter_name}" ]; then
log_info "Filtering fields by name (case-insensitive): '${filter_name}'"
fi
local fields_json
# If project and issue type are provided, use metadata (project-specific fields)
if [ -n "${project}" ] && [ -n "${issue_type}" ]; then
log_info "Fetching fields for project ${project}, issue type ${issue_type}..."
local metadata
if ! metadata=$(get_metadata "${project}" "${issue_type}"); then
log_error "Failed to get metadata. Try fetching it first:"
log_error " ./jira.sh fetch-metadata ${project} ${issue_type}"
exit 1
fi
# Extract fields from metadata
local jq_query='
.projects[0].issuetypes[0].fields
| to_entries[]
| {id: .key, name: .value.name, schema: (.value.schema.type // "none"), items: (.value.schema.items // "none"), required: .value.required, allowedValues: (if .value.allowedValues then [.value.allowedValues[] | (.name // .value // .id // "" | tostring)] else [] end)}
'
if [ -n "${filter_name}" ]; then
jq_query='
.projects[0].issuetypes[0].fields
| to_entries[]
| select(.value.name | test($filter; "i"))
| {id: .key, name: .value.name, schema: (.value.schema.type // "none"), items: (.value.schema.items // "none"), required: .value.required, allowedValues: (if .value.allowedValues then [.value.allowedValues[] | (.name // .value // .id // "" | tostring)] else [] end)}
'
fields_json=$(jq --arg filter "${filter_name}" "${jq_query}" <<< "${metadata}")
else
fields_json=$(jq "${jq_query}" <<< "${metadata}")
fi
else
# No project/issue type - list all global fields
log_info "Fetching all global Jira fields (for exploration only)..."
log_verbose "Note: Use 'list-fields <PROJECT> <ISSUE_TYPE>' for project-specific fields"
# Make the API call using common function
if ! jira_api_get "/rest/api/2/field"; then
exit 1
fi
local body="${jira_response_body}"
if [ -z "${body}" ]; then
log_error "Received empty response from Jira."
exit 1
fi
log_debug "Response body is not empty. Parsing with jq..."
local jq_query
jq_query='
.[]
| {id, name, schema: (.schema.type // "none"), items: (.schema.items // "none"), allowedValues: (if .allowedValues then [.allowedValues[] | (.name // .value // .id // "" | tostring)] else [] end)}
'
if [ -n "${filter_name}" ]; then
jq_query='
.[]
| select(.name | test($filter; "i"))
| {id, name, schema: (.schema.type // "none"), items: (.schema.items // "none"), allowedValues: (if .allowedValues then [.allowedValues[] | (.name // .value // .id // "" | tostring)] else [] end)}
'
fields_json=$(jq --arg filter "${filter_name}" "${jq_query}" <<< "${body}")
else
fields_json=$(jq "${jq_query}" <<< "${body}")
fi
fi
if [ -z "${fields_json}" ]; then
log_info "No fields found matching the criteria."
return 0
fi
# Output the fields
echo "${fields_json}"
}
cmd_list_issue_types() {
if [ "$#" -ne 1 ]; then
log_error "Usage: ./jira.sh list-issue-types <PROJECT>"
exit 1
fi
local project="$1"
log_info "Fetching issue types for project ${project}..."
# Call Jira API to get project metadata including issue types
local api_endpoint="/rest/api/2/issue/createmeta?projectKeys=${project}&expand=projects.issuetypes"
if ! jira_api_get "${api_endpoint}"; then
log_error "Failed to fetch issue types for project ${project}"
exit 1
fi
local body="${jira_response_body}"
if [ -z "${body}" ]; then
log_error "Received empty response from Jira."
exit 1
fi
# Check if project exists
local project_count
project_count=$(echo "${body}" | jq -r '.projects | length')
if [ "${project_count}" -eq 0 ]; then
log_error "Project '${project}' not found or you don't have permission to access it."
exit 1
fi
log_info "Available issue types for project ${project}:"
echo ""
# Extract issue types with name, id, and description
# Format: Display the name prominently, and if the name differs from what needs to be used in templates, show both
echo "${body}" | jq -r '
.projects[0].issuetypes[]
| {
name: .name,
id: .id,
description: .description,
subtask: .subtask
}
| if .subtask then
" • \(.name) (ID: \(.id)) [SUBTASK]"
else
" • \(.name) (ID: \(.id))"
end,
if .description and .description != "" then
" Description: \(.description)"
else
empty
end,
" Usage in templates: issuetype: {name: '\''\(.name)'\''}"
'
echo ""
log_info "Note: Use the exact name shown above in templates and --type flag."
log_info " Names are case-sensitive (e.g., 'Story' not 'story')."
}
cmd_show_required() {
if [ "$#" -ne 2 ]; then
log_error "Usage: ./jira.sh show-required <PROJECT> <ISSUE_TYPE>"
exit 1
fi
local project="$1"
local issue_type="$2"
log_info "Required fields for project ${project}, issue type ${issue_type}:"
echo ""
local metadata
if ! metadata=$(get_metadata "${project}" "${issue_type}"); then
log_error "Failed to get metadata. Try fetching it first:"
log_error " ./jira.sh fetch-metadata ${project} ${issue_type}"
exit 1
fi
local required_fields
required_fields=$(get_required_fields "${metadata}")
if [ -z "${required_fields}" ]; then
log_info "No required fields found (or only standard fields required)."
return 0
fi
# Display required fields in a readable format
echo "${required_fields}" | jq -r '
"Field Key: \(.key)",
"Field Name: \(.name)",
"Field Type: \(.schema)",
(if .key == "project" then
"Note: Automatically set to \(.allowedValues[0].key // .allowedValues[0].name // .allowedValues[0].value // "")"
elif .key == "issuetype" then
"Note: Automatically set to \(.allowedValues[0].name // .allowedValues[0].value // "")"
elif .allowedValues and (.allowedValues | length > 0) then
"Allowed Values: \(.allowedValues | map((.name // .value // .id // "") | tostring) | join(", "))"
else
""
end),
"---"
'
}
cmd_generate_template() {
if [ "$#" -lt 3 ]; then
log_error "Usage: ./jira.sh generate-template <PROJECT> <ISSUE_TYPE> <OUTPUT_FILE> [options]"
exit 1
fi
local project="$1"
local issue_type="$2"
local output_file="$3"
shift 3
local required_fields="false"
local filters=()
local update_existing="false"
# Parse optional arguments
while [ "$#" -gt 0 ]; do
case "$1" in
--required-fields)
required_fields="true"
shift
;;
--required-only)
# Backward compatibility alias for --required-fields
log_verbose "Note: --required-only is deprecated, use --required-fields instead"
required_fields="true"
shift
;;
--filter)
if [ -z "$2" ]; then
log_error "Error: --filter requires a value."
return 1
fi
filters+=("$2")
shift 2
;;
--update)
update_existing="true"
shift
;;
--add-to-existing)
# Backward compatibility alias for --update
log_verbose "Note: --add-to-existing is deprecated, use --update instead"
update_existing="true"
shift
;;
*)
log_error "Error: Unknown option '$1'"
echo "Usage: ./jira.sh generate-template <PROJECT> <ISSUE_TYPE> <OUTPUT_FILE> [options]"
return 1
;;
esac
done
log_info "Generating template for project ${project}, issue type ${issue_type}..."
if [ "${#filters[@]}" -gt 0 ]; then
log_info "Filtering fields by name (case-insensitive): ${filters[*]}"
fi
# Check if output file already exists
local is_updating="false"
if [ -f "${output_file}" ]; then
if [ "${update_existing}" = "true" ]; then
log_info "Will add missing fields to existing template '${output_file}'"
is_updating="true"
else
log_error "Output file already exists: ${output_file}"
log_error "Use --update to update it, or choose a different name."
exit 1
fi
fi
local metadata
if ! metadata=$(get_metadata "${project}" "${issue_type}"); then
log_error "Failed to get metadata. Try fetching it first:"
log_error " ./jira.sh fetch-metadata ${project} ${issue_type}"
exit 1
fi
# Extract all fields or only required fields, with optional name filtering
local fields_json
# Build filter condition for jq
local filter_condition=""
if [ "${#filters[@]}" -gt 0 ]; then
# Build OR expression: (.value.name | test("filter1"; "i")) or (.value.name | test("filter2"; "i")) ...
local filter_parts=()
for filter in "${filters[@]}"; do
filter_parts+=("(.value.name | test(\"${filter}\"; \"i\"))")
done
# Join with " or "
filter_condition="| select($(IFS=' or '; echo "${filter_parts[*]}"))"
fi
if [ "${required_fields}" = "true" ]; then
log_verbose "Including only required fields..."
fields_json=$(echo "${metadata}" | jq -c "
.projects[0].issuetypes[0].fields
| to_entries[]
| select(.value.required == true)
${filter_condition}
| {key: .key, name: .value.name, schema: .value.schema, required: .value.required, allowedValues: .value.allowedValues}
")
else
log_verbose "Including all available fields..."
fields_json=$(echo "${metadata}" | jq -c "
.projects[0].issuetypes[0].fields
| to_entries[]
${filter_condition}
| {key: .key, name: .value.name, schema: .value.schema, required: .value.required, allowedValues: .value.allowedValues}
")
fi
if [ -z "${fields_json}" ]; then
log_error "No fields found matching the criteria."
exit 1
fi
# Get existing fields if updating
local existing_fields=""
if [ "${is_updating}" = "true" ]; then
log_debug "Reading existing fields from ${output_file}..."
if ! existing_fields=$(yq '.fields | keys' "${output_file}" 2>/dev/null); then
log_error "Failed to read existing fields from ${output_file}"
log_error "The file may not be a valid YAML template."
exit 1
fi
log_debug "Existing fields: ${existing_fields}"
else
# Initialize the YAML file
echo "# YAML template for creating ${issue_type} in project ${project}" > "${output_file}"
echo "# Generated by jira.sh" >> "${output_file}"
echo "" >> "${output_file}"
echo "fields:" >> "${output_file}"
fi
local fields_added=0
# Process each field and add to YAML
while IFS= read -r field_json; do
local field_key
field_key=$(echo "${field_json}" | jq -r '.key')
# If updating, check if field already exists
if [ "${is_updating}" = "true" ]; then
if echo "${existing_fields}" | grep -q "\"${field_key}\""; then
log_debug "Field '${field_key}' already exists in template. Skipping."
continue
fi
fi
local field_name
field_name=$(echo "${field_json}" | jq -r '.name')
local schema_type
schema_type=$(echo "${field_json}" | jq -r '.schema.type // "string"')
local is_required
is_required=$(echo "${field_json}" | jq -r '.required // false')
# Determine a sensible default value based on schema type
# Use interactive placeholders by default - users can change to static if desired
local default_value
# Check if this is a multi-line field based on Jira schema information
# This is more reliable than pattern matching on field names
local is_multiline_field="false"
# Check for textarea custom fields
local custom_type
custom_type=$(echo "${field_json}" | jq -r '.schema.custom // ""')
if [[ "${custom_type}" == *"textarea"* ]]; then
is_multiline_field="true"
log_debug "Auto-detected multi-line field: ${field_name} (textarea custom field)"
fi
# Check for standard system fields that are multi-line
local system_type
system_type=$(echo "${field_json}" | jq -r '.schema.system // ""')
if [[ "${system_type}" == "description" ]] || [[ "${system_type}" == "environment" ]] || [[ "${system_type}" == "comment" ]]; then
is_multiline_field="true"
log_debug "Auto-detected multi-line field: ${field_name} (system field: ${system_type})"
fi