fedora-csb-system-manager
  1#!/usr/bin/env bash
  2# Convert Jira issues to org-mode TODOs
  3
  4set -euo pipefail
  5
  6usage() {
  7    cat <<EOF
  8Usage: jira-to-todo [options] [ISSUE-KEY...]
  9
 10Convert Jira issues to org-mode TODO format and optionally add to todos.org.
 11
 12Options:
 13  --jql QUERY        Use JQL query to find issues
 14  --output FILE      Output file (default: stdout)
 15  --section SECTION  Section in todos.org (Work, Projects, Systems, Personal)
 16  --add              Add to ~/desktop/org/todos.org
 17  --state STATE      TODO state (TODO, NEXT, STRT) (default: TODO)
 18  --priority N       Org priority [#1] through [#5]
 19  -h, --help         Show this help
 20
 21Examples:
 22  # Convert single issue
 23  jira-to-todo SRVKP-1234
 24
 25  # Convert my sprint issues and add to Work section
 26  jira-to-todo --jql "sprint in openSprints() AND assignee = currentUser()" --add --section Work
 27
 28  # Convert and save to file
 29  jira-to-todo SRVKP-1234 SRVKP-5678 --output /tmp/todos.org
 30EOF
 31    exit 1
 32}
 33
 34# Default options
 35JQL=""
 36OUTPUT=""
 37SECTION=""
 38ADD_TO_FILE=false
 39STATE="TODO"
 40PRIORITY=""
 41ISSUES=()
 42
 43# Parse options
 44while [[ $# -gt 0 ]]; do
 45    case "$1" in
 46        --jql)
 47            JQL="$2"
 48            shift 2
 49            ;;
 50        --output)
 51            OUTPUT="$2"
 52            shift 2
 53            ;;
 54        --section)
 55            SECTION="$2"
 56            shift 2
 57            ;;
 58        --add)
 59            ADD_TO_FILE=true
 60            shift
 61            ;;
 62        --state)
 63            STATE="$2"
 64            shift 2
 65            ;;
 66        --priority)
 67            PRIORITY="[#$2]"
 68            shift 2
 69            ;;
 70        -h|--help)
 71            usage
 72            ;;
 73        -*)
 74            echo "Unknown option: $1"
 75            usage
 76            ;;
 77        *)
 78            ISSUES+=("$1")
 79            shift
 80            ;;
 81    esac
 82done
 83
 84# Get issues from JQL or arguments
 85if [[ -n "$JQL" ]]; then
 86    mapfile -t ISSUE_KEYS < <(jira issue list --jql "$JQL" --plain 2>/dev/null | awk '{print $1}')
 87elif [[ ${#ISSUES[@]} -gt 0 ]]; then
 88    ISSUE_KEYS=("${ISSUES[@]}")
 89else
 90    echo "Error: Provide ISSUE-KEY or --jql QUERY"
 91    usage
 92fi
 93
 94if [[ ${#ISSUE_KEYS[@]} -eq 0 ]]; then
 95    echo "No issues found"
 96    exit 1
 97fi
 98
 99# Convert priority from Jira to org (if not specified)
100jira_to_org_priority() {
101    local jira_priority="$1"
102    case "$jira_priority" in
103        Blocker|Critical) echo "[#1]" ;;
104        Major|High) echo "[#2]" ;;
105        Medium) echo "[#3]" ;;
106        Minor) echo "[#4]" ;;
107        Trivial) echo "[#5]" ;;
108        *) echo "[#3]" ;;  # Default to medium
109    esac
110}
111
112# Generate TODO entry for an issue
113generate_todo() {
114    local issue_key="$1"
115
116    # Fetch issue details
117    local issue_data
118    issue_data=$(jira issue view "$issue_key" --plain 2>/dev/null) || {
119        echo "Warning: Could not fetch $issue_key" >&2
120        return 1
121    }
122
123    # Parse fields (basic extraction - adjust based on actual format)
124    local summary status jira_priority type
125    summary=$(echo "$issue_data" | grep -i "summary" | head -1 | sed 's/.*:[[:space:]]*//' || echo "Unknown")
126    status=$(echo "$issue_data" | grep -i "status" | head -1 | sed 's/.*:[[:space:]]*//' | awk '{print $1}' || echo "Unknown")
127    jira_priority=$(echo "$issue_data" | grep -i "priority" | head -1 | sed 's/.*:[[:space:]]*//' | awk '{print $1}' || echo "Medium")
128    type=$(echo "$issue_data" | grep -i "type" | head -1 | sed 's/.*:[[:space:]]*//' | awk '{print $1}' || echo "Task")
129
130    # Determine org priority
131    local org_priority
132    if [[ -n "$PRIORITY" ]]; then
133        org_priority="$PRIORITY"
134    else
135        org_priority=$(jira_to_org_priority "$jira_priority")
136    fi
137
138    # Determine TODO state based on Jira status
139    local todo_state="$STATE"
140    if [[ "$STATE" == "TODO" ]]; then
141        case "$status" in
142            "In Progress"|"Code Review"|"QE Review") todo_state="STRT" ;;
143            "To Do") todo_state="TODO" ;;
144            "Done") todo_state="DONE" ;;
145            *) todo_state="TODO" ;;
146        esac
147    fi
148
149    # Generate TODO entry
150    cat <<EOF
151** $todo_state $org_priority $issue_key: $summary
152:PROPERTIES:
153:CREATED:       [$(date +'%Y-%m-%d %a %H:%M')]
154:JIRA:          https://issues.redhat.com/browse/$issue_key
155:JIRA_KEY:      $issue_key
156:JIRA_STATUS:   $status
157:JIRA_PRIORITY: $jira_priority
158:JIRA_TYPE:     $type
159:CATEGORY:      work
160:END:
161
162Jira: [[https://issues.redhat.com/browse/$issue_key][$issue_key]]
163Type: $type | Status: $status | Priority: $jira_priority
164
165EOF
166}
167
168# Generate all TODOs
169generate_all() {
170    for issue_key in "${ISSUE_KEYS[@]}"; do
171        generate_todo "$issue_key"
172    done
173}
174
175# Output to file or stdout
176if [[ "$ADD_TO_FILE" == true ]]; then
177    TODOS_FILE="$HOME/desktop/org/todos.org"
178
179    if [[ ! -f "$TODOS_FILE" ]]; then
180        echo "Error: $TODOS_FILE not found"
181        exit 1
182    fi
183
184    if [[ -z "$SECTION" ]]; then
185        echo "Error: --section required when using --add"
186        exit 1
187    fi
188
189    # Create temporary file with new TODOs
190    TMPFILE=$(mktemp)
191    generate_all > "$TMPFILE"
192
193    # Use org-manager to add to section (if available)
194    if command -v ~/.config/claude/skills/Org/tools/org-manager &>/dev/null; then
195        # TODO: Implement adding to specific section
196        # For now, just append to file and let user refile
197        echo "Adding ${#ISSUE_KEYS[@]} TODOs to inbox..."
198        cat "$TMPFILE" >> "$HOME/desktop/org/inbox.org"
199        echo "TODOs added to inbox.org. Please refile to appropriate sections."
200    else
201        # Fallback: just show the TODOs
202        echo "Generated TODOs (add manually to $SECTION section):"
203        cat "$TMPFILE"
204    fi
205
206    rm "$TMPFILE"
207
208elif [[ -n "$OUTPUT" ]]; then
209    generate_all > "$OUTPUT"
210    echo "TODOs written to: $OUTPUT"
211else
212    generate_all
213fi