-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trello.go
176 lines (165 loc) · 3.67 KB
/
trello.go
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
package main
import (
"fmt"
"github.com/adlio/trello"
)
var err error
type TrelloConnect struct {
TrelloClient *trello.Client
TrelloBoard *trello.Board
TrelloListe []*trello.List
Config *ConfigBot
TrelloBoardId string
LastUsedCard *trello.Card
}
func NewTrelloConnect(config *ConfigBot) *TrelloConnect {
tC := TrelloConnect{Config: config}
tC.TrelloBoardId = config.TrelloAzubiBoard
return &tC
}
// Funktion zum Hinzufügen einer Trello-Karte
func (t *TrelloConnect) AddTrelloCard(title string, description string) {
if !t.Config.UseTrello {
return
}
// Eine neue Trello-Karte erstellen
card := &trello.Card{
Name: title,
Desc: description,
}
fmt.Printf("Card Inhalt %s", GetJSONString(card))
// Trello-Karte hinzufügen
trelloListen, err := t.GetLists()
if err != nil {
LogError("Trello Liste", err)
return
}
if trelloListen == nil {
LogError("Konnte Trello Liste nicht laden", err)
return
}
list := trelloListen[0]
listC, _ := t.TrelloClient.GetList(list.ID, trello.Defaults())
err = listC.AddCard(card, trello.Defaults())
if err != nil {
LogError("Trello Liste", err)
return
}
// Erfolgsmeldung anzeigen
fmt.Println("Trello-Karte erfolgreich hinzugefügt!")
}
func (t *TrelloConnect) MoveCardToNextList(description string, response string) string {
if t.TrelloClient == nil {
t.LoginToTrelloBoard()
}
board, err := t.GetBoard()
if err != nil {
fmt.Println(err)
return ""
}
if board == nil {
return ""
}
lists, err := board.GetLists(trello.Defaults())
if err != nil {
fmt.Println(err)
return ""
}
if lists == nil {
return ""
}
t.LastUsedCard = nil
for i, list := range lists {
if i == 0 {
cards, err := list.GetCards(trello.Defaults())
if err != nil {
fmt.Println(err)
return ""
}
for _, card := range cards {
t.LastUsedCard = card
break
}
}
if i == 1 {
if t.LastUsedCard == nil {
break
}
t.LastUsedCard.Desc = description
t.LastUsedCard.AddComment(response, trello.Defaults())
t.LastUsedCard.MoveToList(list.ID, trello.Defaults())
}
}
return ""
}
func (t *TrelloConnect) GetTodoFromList() string {
if t.TrelloClient == nil {
t.LoginToTrelloBoard()
}
board, err := t.GetBoard()
if err != nil {
fmt.Println(err)
return ""
}
if board == nil {
return ""
}
lists, err := board.GetLists(trello.Defaults())
if err != nil {
fmt.Println(err)
return ""
}
if lists == nil {
return ""
}
for _, list := range lists {
cards, err := list.GetCards(trello.Defaults())
if err != nil {
fmt.Println(err)
return ""
}
for _, card := range cards {
t.LastUsedCard = card
return card.Name
}
}
return ""
}
// Funktion zum Login in ein Trello-Board
func (t *TrelloConnect) LoginToTrelloBoard() *trello.Client {
if !t.Config.UseTrello {
return nil
}
if t.TrelloClient != nil {
return t.TrelloClient
}
return trello.NewClient(t.Config.TrelloKey, t.Config.TrelloSecret)
}
func (t *TrelloConnect) GetBoard() (*trello.Board, error) {
t.TrelloClient = t.LoginToTrelloBoard()
if t.TrelloClient == nil {
fmt.Println("Kein Client forhanden")
return nil, nil
}
fmt.Printf("Versuche Board %s zu finden.", t.TrelloBoardId)
t.TrelloBoard, err = t.TrelloClient.GetBoard(t.TrelloBoardId, trello.Defaults())
if err != nil {
fmt.Println("No Board found for " + t.TrelloBoardId)
fmt.Println(err.Error())
return nil, err
}
return t.TrelloBoard, nil
}
func (t *TrelloConnect) GetLists() ([]*trello.List, error) {
trelloBoard, err := t.GetBoard()
if err != nil {
fmt.Println(err.Error())
return nil, err
}
trelloListe, err := trelloBoard.GetLists(trello.Defaults())
if err != nil {
fmt.Println(err.Error())
return nil, err
}
return trelloListe, nil
}