-
Notifications
You must be signed in to change notification settings - Fork 3
/
export.go
202 lines (164 loc) · 4.95 KB
/
export.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
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
package main
import (
"fmt"
"io"
"log"
"net/http"
"regexp"
"time"
trello "github.com/jnormington/go-trello"
dropbox "github.com/tj/go-dropbox"
)
var dateLayout = "2006-01-02T15:04:05.000Z"
var safeFileNameRegexp = regexp.MustCompile(`[^a-zA-Z0-9_.]+`)
// Card holds all the attributes needed for migrating a complete card from Trello to Clubhouse
type Card struct {
Name string `json:"name"`
Desc string `json:"desc"`
Labels []string `json:"labels"`
DueDate *time.Time `json:"due_date"`
IDCreator string `json:"id_creator"`
IDOwners []string `json:"id_owners"`
CreatedAt *time.Time `json:"created_at"`
Comments []Comment `json:"comments"`
Tasks []Task `json:"checklists"`
Position float32 `json:"position"`
ShortURL string `json:"url"`
Attachments map[string]string `json:"attachments"`
}
// Task builds a basic object based off trello.Task
type Task struct {
Completed bool `json:"completed"`
Description string `json:"description"`
}
// Comment builds a basic object based off trello.Comment
type Comment struct {
Text string
IDCreator string
CreatorName string
CreatedAt *time.Time
}
// ProcessCardsForExporting takes *[]trello.Card, *TrelloOptions and builds up a Card
// which consists of calling other functions to make the api calls to Trello
// for the relevant attributes of a card returns *[]Card
func ProcessCardsForExporting(crds *[]trello.Card, opts *TrelloOptions) *[]Card {
var cards []Card
for _, card := range *crds {
var c Card
c.Name = card.Name
c.Desc = card.Desc
c.Labels = getLabelsFlattenFromCard(&card)
c.DueDate = parseDateOrReturnNil(card.Due)
c.IDCreator, c.CreatedAt, c.Comments = getCommentsAndCardCreator(&card)
c.Tasks = getCheckListsForCard(&card)
c.Position = card.Pos
c.ShortURL = card.ShortUrl
c.IDOwners = card.IdMembers
if opts.ProcessImages {
c.Attachments = downloadCardAttachmentsUploadToDropbox(&card)
}
cards = append(cards, c)
}
return &cards
}
func getCommentsAndCardCreator(card *trello.Card) (string, *time.Time, []Comment) {
var creator string
var createdAt *time.Time
var comments []Comment
actions, err := card.Actions()
if err != nil {
fmt.Println("Error: Querying the actions for:", card.Name, "ignoring...", err)
}
for _, a := range actions {
if a.Type == "commentCard" && a.Data.Text != "" {
c := Comment{
Text: a.Data.Text,
IDCreator: a.MemberCreator.Id,
CreatorName: a.MemberCreator.FullName,
CreatedAt: parseDateOrReturnNil(a.Date),
}
comments = append(comments, c)
} else if a.Type == "createCard" {
creator = a.MemberCreator.Id
createdAt = parseDateOrReturnNil(a.Date)
}
}
return creator, createdAt, comments
}
func getCheckListsForCard(card *trello.Card) []Task {
var tasks []Task
checklists, err := card.Checklists()
if err != nil {
fmt.Println("Error: Occurred querying checklists for:", card.Name, "ignoring...", err)
}
for _, cl := range checklists {
for _, i := range cl.CheckItems {
var completed bool
if i.State == "complete" {
completed = true
}
t := Task{
Completed: completed,
Description: fmt.Sprintf("%s - %s", cl.Name, i.Name),
}
tasks = append(tasks, t)
}
}
return tasks
}
func getLabelsFlattenFromCard(card *trello.Card) []string {
var labels []string
for _, l := range card.Labels {
labels = append(labels, l.Name)
}
return labels
}
func parseDateOrReturnNil(strDate string) *time.Time {
d, err := time.Parse(dateLayout, strDate)
if err != nil {
//If the date isn't parseable from trello api just return nil
return nil
}
return &d
}
func downloadCardAttachmentsUploadToDropbox(card *trello.Card) map[string]string {
sharedLinks := map[string]string{}
d := dropbox.New(dropbox.NewConfig(dropboxToken))
attachments, err := card.Attachments()
if err != nil {
log.Fatal(err)
}
for i, f := range attachments {
name := safeFileNameRegexp.ReplaceAllString(f.Name, "_")
path := fmt.Sprintf("/trello/%s/%s/%d%s%s", card.IdList, card.Id, i, "_", name)
io := downloadTrelloAttachment(&f)
_, err := d.Files.Upload(&dropbox.UploadInput{
Path: path,
Mode: dropbox.WriteModeAdd,
Reader: io,
Mute: true,
})
io.Close()
if err != nil {
fmt.Printf("Error occurred uploading file to dropbox continuing... %s\n", err)
} else {
// Must be success created a shared url
s := dropbox.CreateSharedLinkInput{path, false}
out, err := d.Sharing.CreateSharedLink(&s)
if err != nil {
fmt.Printf("Error occurred sharing file on dropbox continuing... %s\n", err)
} else {
sharedLinks[name] = out.URL
}
}
}
return sharedLinks
}
func downloadTrelloAttachment(attachment *trello.Attachment) io.ReadCloser {
resp, err := http.Get(attachment.Url)
// defer resp.Body.Close()
if err != nil {
log.Fatalf("Error in download Trello attachment %s\n", err)
}
return resp.Body
}