-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path.bash_trello_specific
108 lines (99 loc) · 3.71 KB
/
.bash_trello_specific
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
# vi: set ft=bash:
TRELLO_CONFIG_PATH="$HOME/.config/trello"
TRELLO_CREDS_FILE="${TRELLO_CONFIG_PATH}/credentials"
TRELLO_BASE_URL="${TRELLO_BASE_URL:-https://api.trello.com/1}"
# LITERALLY EVERY Trello CLI sucks. Which makes no fucking sense since the Trello API
# is so well documented.
#
# The most-starred CLI, mheap/trello, is completely designed for human interaction, is poorly tested
# (the only test that exists is for add_card, which is crazy), and there are tons of bugs
# lurking around translating card titles to IDs, boards to IDs, etc.
#
# So I'll just do it my damn self and use the Trello API directly.
trello() {
endpoint="$1"
query_params="$2"
method="${3:-GET}"
_first_time_setup() {
test -d "$TRELLO_CONFIG_PATH" || mkdir -p "$TRELLO_CONFIG_PATH"
appKey=$(get_password "Trello API Key")
if test -z "$appKey"
then
log_error "Unable to retrieve Trello API Key from 1Password"
return 1
fi
token=$(get_password "Trello API Token")
if test -z "$token"
then
log_error "Unable to retrieve Trello token from 1Password"
return 1
fi
printf '{"app_key": "%s", "token": "%s"}' "$appKey" "$token" > "$TRELLO_CREDS_FILE"
echo 'done' > "${TRELLO_CONFIG_PATH}/initialized"
}
_first_time_setup_complete() {
test -e "${TRELLO_CONFIG_PATH}/initialized"
}
_first_time_setup_complete || { _first_time_setup || return 1; }
localAppKey=$(jq -r '.app_key' "$TRELLO_CREDS_FILE")
localToken=$(jq -r '.token' "$TRELLO_CREDS_FILE")
endpoint=$(sed 's#^/##' <<< "$endpoint")
url="${TRELLO_BASE_URL}/${endpoint}?key=$localAppKey&token=$localToken"
if test -n "$query_params" && ( test "${method,,}" == "post" || test "${method,,}" == "put" )
then
curl -sS --json "$query_params" -H "Content-Type: application/json" -H "Accept: application/json" -X "${method}" "${url}"
else
url="${url}&${query_params}"
curl -sS -H "Content-Type: application/json" -H "Accept: application/json" -X "${method}" "${url}"
fi
echo
}
trello_list_boards() {
trello "/members/me/boards" | jq -r '[.[] | { name: .name, id: .id }]'
}
trello_board_id_by_name() {
trello "/members/me/boards" | jq --arg board "$1" -r '.[] | select(.name == $board) | .id'
}
trello_list_cards_in_board() {
trello "/boards/$1/cards"
}
trello_list_lists_in_board() {
trello "/boards/$1/lists"
}
trello_get_card() {
trello "/cards/$1"
}
trello_get_board_labels() {
board="$1"
boardID="$(trello_board_id_by_name "$board")" || return 1
trello "/boards/$boardID/labels" | jq -r '[.[] | {name: .name, id: .id}]'
}
trello_get_list_in_board_by_list_name() {
board="$1"
list="$2"
boardID="$(trello_board_id_by_name "$board")" || return 1
trello_list_lists_in_board "$boardID" | jq --arg listName "$list" -r '.[] | select(.name == $listName)'
}
trello_get_label_in_board_by_name() {
board="$1"
name="$2"
trello_get_board_labels "$board" | jq --arg label "$name" -r '.[] | select(.name == $label)'
}
trello_add_card_with_due_date_and_labels() {
name="$1"
board="$2"
list_name="$3"
due_date="$4"
labels="$5"
description="$6"
listID="$(trello_get_list_in_board_by_list_name "$board" "$list_name" | jq -r '.id')" || return 1
due_date="$(date -d "$due_date" +"%Y-%m-%d")" || return 1
labelIDs=""
for label in $(tr ',' '\n' <<< "$labels")
do labelIDs="$labelIDs,$(trello_get_label_in_board_by_name "$board" "$label" | jq '.id')" || return 1
done
labelIDs="$(sed 's/^,//' <<< "$labelIDs")"
>&2 echo "===> Adding card: $name [board: $board, list_id: $listID, due date: $due_date, labels: $labelIDs]"
trello "/cards" "$(printf '{"name":"%s","desc":"%s","due":"%s","idList":"%s","idLabels":[%s],"pos":"bottom"}' \
"$name" "$description" "$due_date" "$listID" "$labelIDs")" "POST"
}