-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtw-description
More file actions
executable file
·247 lines (208 loc) · 6.97 KB
/
tw-description
File metadata and controls
executable file
·247 lines (208 loc) · 6.97 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
#!/usr/bin/env bash
#ddev-generated
#annertech-ddev
## Description: Update Teamwork task description
## Usage: tw-description [-t task-id]
## Example: "ddev tw-description" (uses task ID from branch)
## Example: "ddev tw-description -t 18126193"
set -eo pipefail
# Color output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
echo_red() { echo -e "${RED}$1${NC}" >&2; }
echo_green() { echo -e "${GREEN}$1${NC}"; }
echo_yellow() { echo -e "${YELLOW}$1${NC}"; }
TASK_ID_OVERRIDE=""
# Parse flags
while [[ "$1" == -* ]]; do
case "$1" in
-t)
TASK_ID_OVERRIDE="$2"
shift 2
;;
*)
echo_red "Unknown option: $1"
exit 1
;;
esac
done
# Get Teamwork API key from environment
LIB_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/_lib"
. "$LIB_DIR/tw-curl.sh"
TW_API_KEY="${TEAMWORK_API_KEY:-}"
DOMAIN="${TEAMWORK_DOMAIN:-}"
if [[ -z "$TW_API_KEY" || -z "$DOMAIN" ]]; then
echo_red "Error: TEAMWORK_API_KEY and TEAMWORK_DOMAIN environment variables must be set"
exit 1
fi
# Use override if provided, otherwise extract from branch, otherwise prompt
if [[ -n "$TASK_ID_OVERRIDE" ]]; then
TASK_ID="$TASK_ID_OVERRIDE"
else
branch_name=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "")
if [[ $branch_name =~ T-([0-9]+) ]]; then
TASK_ID="${BASH_REMATCH[1]}"
echo_yellow "Using task ID from branch: T-${TASK_ID}"
else
prev_branch=$(git rev-parse --abbrev-ref @{-1} 2>/dev/null)
if [[ -n "$prev_branch" && "$prev_branch" != "@{-1}" && $prev_branch =~ T-([0-9]+) ]]; then
TASK_ID="${BASH_REMATCH[1]}"
echo_yellow "No task ID in current branch. Found T-${TASK_ID} in previous branch: $prev_branch"
read -p "Continue with T-${TASK_ID}? (Y/n): " confirm
confirm=${confirm:-Y}
if [[ "$confirm" =~ ^[Nn] ]]; then
read -e -p "Enter task ID: " TASK_ID
if [[ -z "$TASK_ID" ]]; then
echo_red "Error: Task ID is required."
exit 1
fi
fi
else
echo_yellow "No task ID found in branch name."
read -e -p "Enter task ID: " TASK_ID
if [[ -z "$TASK_ID" ]]; then
echo_red "Error: Task ID is required."
exit 1
fi
fi
fi
fi
# Validate task ID is numeric
if ! [[ "$TASK_ID" =~ ^[0-9]+$ ]]; then
echo_red "Error: Task ID must be numeric"
exit 1
fi
# Prompt for MR link
echo_yellow "Enter MR link:"
read -e -r MR_LINK
if [ -z "$MR_LINK" ]; then
echo_red "Error: MR link is required"
exit 1
fi
# Prompt for deploy state - pre-fill default checklist items for inline editing
echo_yellow "Edit deploy state checklist (arrow keys to edit, clear a line to remove it, empty line to finish):"
DEPLOY_ITEMS=()
for default_item in "* [ ] Deployed to Dev" "* [ ] Deployed to Production"; do
read -e -i "$default_item" -r line
[ -n "$line" ] && DEPLOY_ITEMS+=("$line")
done
echo_yellow "Add more state items (empty line to finish):"
while IFS= read -r line; do
[ -z "$line" ] && break
DEPLOY_ITEMS+=("$line")
done
DEPLOY_STATE=$(printf '%s\n' "${DEPLOY_ITEMS[@]}")
# Prompt for deployment steps (checklist, optional)
DEPLOY_STEPS_ITEMS=()
echo_yellow "Enter deployment steps (arrow keys to edit, empty line to finish, leave blank to skip):"
while IFS= read -r line; do
[ -z "$line" ] && break
# Auto-prefix with checkbox if not already
[[ "$line" == "* [ ]"* ]] || line="* [ ] $line"
DEPLOY_STEPS_ITEMS+=("$line")
done
if [ ${#DEPLOY_STEPS_ITEMS[@]} -eq 0 ]; then
DEPLOY_STEPS="_no manual steps required/provided_"
else
DEPLOY_STEPS=$(printf '%s\n' "${DEPLOY_STEPS_ITEMS[@]}")
fi
# Prompt for test notes (multiline, optional)
TEST_NOTES=""
echo_yellow "Enter test notes/plan (empty line to finish, leave blank to skip):"
while IFS= read -r line; do
[ -z "$line" ] && break
if [ -n "$TEST_NOTES" ]; then
TEST_NOTES="${TEST_NOTES}
${line}"
else
TEST_NOTES="$line"
fi
done
if [ -z "$TEST_NOTES" ]; then
TEST_NOTES="_no test notes provided_"
fi
# Fetch Dev URL if upstream provider is platform or upsun
UPSUN_DEV_URL=""
ENV_FILE=".ddev/.env.anner"
if [ -f "$ENV_FILE" ]; then
DEV_UPSTREAM_PROVIDER=$(grep -E '^DEV_UPSTREAM_PROVIDER=' "$ENV_FILE" | cut -d= -f2 | tr -d '"' | tr -d "'" || true)
if [[ "$DEV_UPSTREAM_PROVIDER" == "platform" || "$DEV_UPSTREAM_PROVIDER" == "upsun" ]]; then
read -e -r -p "$(echo -e "${YELLOW}Include Dev environment URL in test notes? [Y/n]: ${NC}")" include_url
include_url="${include_url:-Y}"
if [[ "$include_url" =~ ^[Yy]$ ]]; then
echo_yellow "Fetching Dev environment URL..."
UPSUN_DEV_URL=$(ddev exec upsun url -1 -e dev 2>/dev/null || echo "")
if [ -n "$UPSUN_DEV_URL" ]; then
echo_green "Found Dev URL: ${UPSUN_DEV_URL}"
TEST_NOTES="Dev URL: ${UPSUN_DEV_URL}
${TEST_NOTES}"
else
echo_yellow "No Dev URL found, skipping."
fi
fi
fi
fi
# Get existing task description
echo_yellow "Fetching existing task description..."
# Check if jq is available
if ! command -v jq &> /dev/null; then
echo_red "Error: jq is required for this command"
echo_yellow "Install with: apt-get install jq (or brew install jq on macOS)"
exit 1
fi
# Fetch the task
TASK_RESPONSE=$(tw_curl -s \
"https://${DOMAIN}/tasks/${TASK_ID}.json")
# Debug: Check if we got a valid response
if [ -z "$TASK_RESPONSE" ]; then
echo_red "Error: Failed to fetch task from API"
exit 1
fi
# Extract description - try different possible field names
EXISTING_DESC=$(echo "$TASK_RESPONSE" | jq -r '.task.description // .["todo-item"].description // ""')
if [ -z "$EXISTING_DESC" ] || [ "$EXISTING_DESC" == "null" ]; then
echo_yellow "No existing description found, creating new one."
EXISTING_DESC=""
else
echo_yellow "Found existing description (${#EXISTING_DESC} characters)"
# Add separator if there's existing content
EXISTING_DESC="${EXISTING_DESC}
---
"
fi
# Build new section to append
NEW_SECTION="## Deployment notes
- Date: $(date +%Y-%m-%d)
- MR: ${MR_LINK}
- Branch: \`${branch_name}\`
#### State
${DEPLOY_STATE}
#### Testing
${TEST_NOTES}
#### Deployment steps
${DEPLOY_STEPS}
#### Approval
* [ ] Code Review and approved
* [ ] Internal QA successful
* [ ] Client acceptance (or N/A)"
# Combine existing and new
FULL_DESCRIPTION="${EXISTING_DESC}${NEW_SECTION}"
# Escape description for JSON
DESCRIPTION_JSON=$(echo "$FULL_DESCRIPTION" | jq -Rs .)
# Make API call
echo_yellow "Updating task $TASK_ID..."
RESPONSE=$(tw_curl -s -w "\n%{http_code}" -X PUT \
-H "Content-Type: application/json" \
-d "{\"task\":{\"description\":${DESCRIPTION_JSON}}}" \
"https://${DOMAIN}/tasks/${TASK_ID}.json")
HTTP_CODE=$(echo "$RESPONSE" | tail -n1)
if [ "$HTTP_CODE" -eq 200 ] || [ "$HTTP_CODE" -eq 204 ]; then
echo_green "✓ Task description updated successfully"
echo_green "https://${DOMAIN}/tasks/${TASK_ID}"
else
echo_red "✗ Failed to update task (HTTP $HTTP_CODE)"
echo "$RESPONSE" | head -n-1
exit 1
fi