-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtw-tag-issue
More file actions
executable file
·118 lines (99 loc) · 3.5 KB
/
Copy pathtw-tag-issue
File metadata and controls
executable file
·118 lines (99 loc) · 3.5 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
#!/usr/bin/env bash
#ddev-generated
#annertech-ddev
## Description: Add tags to a Teamwork task
## Usage: tw-tag-issue [-t task-id]
## Example: "ddev tw-tag-issue" or "ddev tw-tag-issue -t 17865107"
## Aliases: tw-tag
set -eo pipefail
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=""
while [[ "$1" == -* ]]; do
case "$1" in
-t) TASK_ID_OVERRIDE="$2"; shift 2 ;;
*) echo_red "Unknown option: $1"; exit 1 ;;
esac
done
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
if ! command -v jq &> /dev/null; then
echo_red "Error: jq is required"
exit 1
fi
# Get Task ID
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
echo_yellow "No task ID found in branch name."
read -e -r -p "Enter task ID: " TASK_ID
if [[ -z "$TASK_ID" ]]; then
echo_red "Error: Task ID is required."
exit 1
fi
fi
fi
if ! [[ "$TASK_ID" =~ ^[0-9]+$ ]]; then
echo_red "Error: Task ID must be numeric"
exit 1
fi
# Fetch task and its current tags
echo_yellow "Fetching task $TASK_ID..."
TASK_RESPONSE=$(tw_curl -s "https://${DOMAIN}/tasks/${TASK_ID}.json")
EXISTING_TAGS_JSON=$(echo "$TASK_RESPONSE" | jq -c '.task.tags // .["todo-item"].tags // []')
CURRENT_TAG_NAMES=$(echo "$EXISTING_TAGS_JSON" | jq -r '.[].name' | tr '\n' ',' | sed 's/,$//')
if [[ -n "$CURRENT_TAG_NAMES" ]]; then
echo_yellow "Current tags: $CURRENT_TAG_NAMES"
else
echo_yellow "No tags currently applied."
fi
# Fetch all available tags
echo_yellow "Fetching available tags..."
TAGS_RESPONSE=$(tw_curl -s "https://${DOMAIN}/tags.json")
TAG_LIST=$(echo "$TAGS_RESPONSE" | jq -r '.tags[] | "\(.id)\t\(.name)"')
if [[ -z "$TAG_LIST" ]]; then
echo_red "No tags found."
exit 1
fi
# Select tags to add (TAB for multi-select)
SELECTION=$(echo "$TAG_LIST" | \
fzf --multi --reverse --height=50% \
--header="Select tags to add (TAB to multi-select)" \
--delimiter=$'\t' --with-nth=2)
if [[ -z "$SELECTION" ]]; then
echo_yellow "No tags selected. Aborting."
exit 0
fi
# Merge existing + selected tag IDs
SELECTED_IDS=$(echo "$SELECTION" | cut -f1 | jq -Rs '[split("\n")[] | select(length > 0) | tonumber]')
NEW_TAG_IDS=$(echo "$EXISTING_TAGS_JSON" | jq -c "map(.id) + $SELECTED_IDS | unique")
# Update task
echo_yellow "Adding tags..."
PAYLOAD=$(jq -n --argjson ids "$NEW_TAG_IDS" '{"todo-item":{"tag-ids":$ids}}')
RESPONSE=$(tw_curl -s -w "\n%{http_code}" \
-H "Content-Type: application/json" \
-X PUT \
-d "$PAYLOAD" \
"https://${DOMAIN}/tasks/${TASK_ID}.json")
HTTP_CODE=$(echo "$RESPONSE" | tail -n1)
if [[ "$HTTP_CODE" -eq 200 || "$HTTP_CODE" -eq 204 ]]; then
ADDED_NAMES=$(echo "$SELECTION" | cut -f2 | tr '\n' ',' | sed 's/,$//')
echo_green "✓ Tags added: $ADDED_NAMES"
else
echo_red "✗ Failed to update task (HTTP $HTTP_CODE)"
echo "$RESPONSE" | head -n-1
exit 1
fi