Added migration of task labels

This commit is contained in:
kolaente 2019-10-20 17:53:45 +02:00
parent 71b841f2e3
commit 9dbeddc635
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
1 changed files with 40 additions and 0 deletions

40
main.go
View File

@ -45,6 +45,7 @@ func main() {
var notApplicable []string
var lastTask *sdk.ModelsTask
var taskRelations []*sdk.ModelsTaskRelation
labels := make(map[string]*sdk.ModelsLabel)
for in, l := range lines {
if l == "" {
@ -53,6 +54,7 @@ func main() {
if strings.HasPrefix(l, "#") {
lastHeadline = strings.TrimSpace(strings.ReplaceAll(l, "#", ""))
labels[lastHeadline] = &sdk.ModelsLabel{Title: lastHeadline}
continue
}
@ -120,8 +122,46 @@ func main() {
log.Println("Done migrating tasks.")
// Make sure all labels exist
outer:
for _, l := range labels {
// Check if it exists
lls, _, err := client.LabelsApi.LabelsGet(auth, map[string]interface{}{"s": l.Title})
if err != nil {
log.Fatal("Error getting label: ", err)
}
// If the result is empty, we need to create the label
if len(lls) == 0 {
newLabel, _, err := client.LabelsApi.LabelsPut(auth, *l)
if err != nil {
log.Fatal("Error creating new label: ", err)
}
labels[l.Title] = &newLabel
continue
}
// Go through the results of the search and put the right label in our label map
for _, ll := range lls {
if l.Title == ll.Title {
labels[ll.Title] = &ll
continue outer
}
}
}
// At this point, we have a map with existing labels
// Update all tasks which have a description
// Update all labels on tasks
for _, t := range tasks {
label, _ := labels[t.Labels[0].Title]
_, _, err := client.LabelsApi.TasksTaskLabelsPut(auth, t.Id, sdk.ModelsLabelTask{LabelId: label.Id})
if err != nil {
log.Fatalf("Error adding label %s to task %d: %v", label.Title, t.Id, err)
}
log.Printf("Added label %s to task %d", label.Title, t.Id)
if t.Description == "" {
continue
}