|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +project_root="${PWD}" |
| 5 | +tickets_dir="${project_root}/.memento/tickets" |
| 6 | +statuses=("next" "in-progress" "done") |
| 7 | +ticket_name_raw="${1:-}" |
| 8 | + |
| 9 | +sanitize() { |
| 10 | + printf '%s' "$1" \ |
| 11 | + | tr -d '\000-\037\177' \ |
| 12 | + | sed -E \ |
| 13 | + -e 's/\.+/-/g' \ |
| 14 | + -e 's#[/\\]+#-#g' \ |
| 15 | + -e 's/[^a-zA-Z0-9_-]/-/g' \ |
| 16 | + -e 's/-{2,}/-/g' \ |
| 17 | + -e 's/^-+//; s/-+$//' \ |
| 18 | + | cut -c1-100 |
| 19 | +} |
| 20 | + |
| 21 | +rand_hex() { |
| 22 | + od -An -N3 -tx1 /dev/urandom | tr -d ' \n' |
| 23 | +} |
| 24 | + |
| 25 | +iso_now_utc() { |
| 26 | + date -u +"%Y-%m-%dT%H:%M:%SZ" |
| 27 | +} |
| 28 | + |
| 29 | +local_now() { |
| 30 | + date +"%Y-%m-%d %H:%M:%S" |
| 31 | +} |
| 32 | + |
| 33 | +json_escape() { |
| 34 | + # Escape backslashes and double quotes for JSON strings |
| 35 | + printf '%s' "$1" | sed -e 's/\\/\\\\/g' -e 's/"/\\"/g' |
| 36 | +} |
| 37 | + |
| 38 | +find_existing_ticket() { |
| 39 | + local name="$1" |
| 40 | + local p |
| 41 | + for s in "${statuses[@]}"; do |
| 42 | + p="${tickets_dir}/${s}/${name}.md" |
| 43 | + if [ -f "$p" ]; then |
| 44 | + printf '%s\n' "$p" |
| 45 | + return 0 |
| 46 | + fi |
| 47 | + done |
| 48 | + return 1 |
| 49 | +} |
| 50 | + |
| 51 | +ticket_name="$ticket_name_raw" |
| 52 | +if [ -n "$ticket_name" ]; then |
| 53 | + ticket_name="$(sanitize "$ticket_name")" |
| 54 | +fi |
| 55 | + |
| 56 | +timestamp="$(iso_now_utc)" |
| 57 | +local_time="$(local_now)" |
| 58 | + |
| 59 | +if [ -z "$ticket_name" ]; then |
| 60 | + ticket_name="session-$(date +%F)-$(rand_hex)" |
| 61 | +fi |
| 62 | + |
| 63 | +ticket_path="" |
| 64 | +if existing_path="$(find_existing_ticket "$ticket_name")"; then |
| 65 | + is_new=false |
| 66 | + session_section=$(cat <<EOF |
| 67 | +
|
| 68 | +--- |
| 69 | +
|
| 70 | +## Session Entry - ${local_time} |
| 71 | +
|
| 72 | +### Summary |
| 73 | +<!-- AI_SUMMARY_START --> |
| 74 | +[AI will generate a comprehensive summary here based on recent work, changes made, and next steps] |
| 75 | +<!-- AI_SUMMARY_END --> |
| 76 | +
|
| 77 | +### Context |
| 78 | +``` |
| 79 | +No automatic context |
| 80 | +``` |
| 81 | +EOF |
| 82 | +) |
| 83 | + printf '%s' "$session_section" >> "$existing_path" |
| 84 | + ticket_path="$existing_path" |
| 85 | +else |
| 86 | + is_new=true |
| 87 | + ticket_path="${tickets_dir}/next/${ticket_name}.md" |
| 88 | + mkdir -p "$(dirname "$ticket_path")" |
| 89 | + cat > "$ticket_path" <<EOF |
| 90 | +# ${ticket_name} |
| 91 | +
|
| 92 | +## Description |
| 93 | +Session-based ticket created for AI summarization. |
| 94 | +
|
| 95 | +## Tasks |
| 96 | +- [ ] Review session context |
| 97 | +- [ ] Define specific tasks based on work completed |
| 98 | +
|
| 99 | +## Session Entry - ${local_time} |
| 100 | +
|
| 101 | +### Summary |
| 102 | +<!-- AI_SUMMARY_START --> |
| 103 | +[AI will generate a comprehensive summary here based on recent work, changes made, and next steps] |
| 104 | +<!-- AI_SUMMARY_END --> |
| 105 | +
|
| 106 | +### Context |
| 107 | +``` |
| 108 | +No automatic context |
| 109 | +``` |
| 110 | +
|
| 111 | +--- |
| 112 | +Created: ${timestamp} |
| 113 | +EOF |
| 114 | +fi |
| 115 | + |
| 116 | +if [ "$is_new" = true ]; then |
| 117 | + message="Created new ticket: ${ticket_name}" |
| 118 | +else |
| 119 | + message="Updated existing ticket: ${ticket_name}" |
| 120 | +fi |
| 121 | + |
| 122 | +tn_esc="$(json_escape "$ticket_name")" |
| 123 | +tp_esc="$(json_escape "$ticket_path")" |
| 124 | +msg_esc="$(json_escape "$message")" |
| 125 | + |
| 126 | +printf '{\n' |
| 127 | +printf ' "success": true,\n' |
| 128 | +printf ' "ticketName": "%s",\n' "$tn_esc" |
| 129 | +printf ' "ticketPath": "%s",\n' "$tp_esc" |
| 130 | +printf ' "isNew": %s,\n' "$is_new" |
| 131 | +printf ' "message": "%s"\n' "$msg_esc" |
| 132 | +printf '}\n' |
| 133 | + |
| 134 | + |
0 commit comments