From ecb3f3f687411854b50035cee7feb93e6dd5b582 Mon Sep 17 00:00:00 2001 From: George BV Date: Tue, 22 Feb 2022 23:10:44 +0300 Subject: [PATCH] feat: added sync statuses #5 NOTE: in spec yaml file added new option --- clickup/mirror_task.go | 2 +- clickup/sync_mirror_task.go | 154 +++++++++++++++++++----------------- clickup/sync_preferences.go | 28 +++++++ 3 files changed, 110 insertions(+), 74 deletions(-) diff --git a/clickup/mirror_task.go b/clickup/mirror_task.go index 5c3f4c8..6442c58 100644 --- a/clickup/mirror_task.go +++ b/clickup/mirror_task.go @@ -95,7 +95,7 @@ type MirrorTask struct { DestroyedAt *Timestamp } -func (t *MirrorTask) GetTask(ctx context.Context) *Task { +func (t *MirrorTask) GetOrigTask(ctx context.Context) *Task { if t.Task != nil { return t.Task } diff --git a/clickup/sync_mirror_task.go b/clickup/sync_mirror_task.go index bd61be0..e1a6087 100644 --- a/clickup/sync_mirror_task.go +++ b/clickup/sync_mirror_task.go @@ -73,13 +73,13 @@ func (s *mirrorTaskSyncer) Sync(ctx context.Context, opts *SyncPreferences, oldT if mirror.MirrorTaskRef.ID == task.ID { for idx := range rules.syncedRules { rule := rules.syncedRules[idx] - s.applyChangesToMirrorTask(ctx, mirror, rule, oldTask, task) + s.applyChangesToMirrorTask(ctx, mirror, rule, oldTask, task, opts.GlobalMirrorTaskStatuses) } } // если среди всех зеркальныйх заданий текущая задача является исходной то // сохраняем listID в котром находится зеркальная задача - if task.ID == mirror.GetTask(ctx).ID { + if task.ID == mirror.GetOrigTask(ctx).ID { listOfMirrorTaskLists[mirror.GetMirrorTask(ctx).ListRef.ID] = true } } @@ -271,7 +271,8 @@ func (s *mirrorTaskSyncer) applyChangesToOriginalTask(ctx context.Context, mirro } func (s *mirrorTaskSyncer) applyChangesToMirrorTask(ctx context.Context, mirror *MirrorTask, - spec MirrorTaskSpecification, oldTask, task *Task) { + spec MirrorTaskSpecification, oldTask, task *Task, statuses MirrorTaskStatuses, +) { if task.IsDeletedOrHidden() { s.sendComment(ctx, task.ID, "FYI changes have been made to a mirror task that is DELETED or HIDDEN - nothing will be updated in original tasks and UNLINK MIRROR TASK", @@ -280,14 +281,14 @@ func (s *mirrorTaskSyncer) applyChangesToMirrorTask(ctx context.Context, mirror return } - if mirror.GetTask(ctx).IsDeletedOrHidden() { + if mirror.GetOrigTask(ctx).IsDeletedOrHidden() { s.sendComment(ctx, task.ID, "UNLINK MIRROR TASK: the original task has been DELETED or HIDDEN", spec.CondAdd.IfAssignedToMemberEmail) s.destroyMirrorTask(ctx, mirror, "original task has been DELETED or HIDDEN") return } - origTask := mirror.GetTask(ctx) + origTask := mirror.GetOrigTask(ctx) if !origTask.Exists() { s.log.Warn("handle task for mirror task - orig task was nil (why?)", zap.String("orig_task_id", mirror.TaskRef.ID), zap.String("mirror_task_id", mirror.TaskRef.ID), @@ -316,83 +317,90 @@ func (s *mirrorTaskSyncer) applyChangesToMirrorTask(ctx context.Context, mirror } // visibility status - if mirror.GetMirrorTask(ctx).IsDeletedOrHidden() && !mirror.GetTask(ctx).IsDeletedOrHidden() { + if mirror.GetMirrorTask(ctx).IsDeletedOrHidden() && !mirror.GetOrigTask(ctx).IsDeletedOrHidden() { fmt.Fprintf(commentText, "- mirror task has removed. TODO: remove from the mirror?\n") needToSendComment = true - } // TODO: description change - // estimate - totalEstimate := task.TotalEstimate() - if origTask.TimeEstimateMs != nil && totalEstimate == 0 { - // removed time estimate - needToUpdateTask = true - updTask.TimeEstimateMs = -1 - fmt.Fprintf(commentText, "- orig task will be updated - time estimate will be removed\n") - needToSendComment = true - } else if origTask.TimeEstimateMs != nil && totalEstimate != 0 && totalEstimate != *origTask.TimeEstimateMs { - // changed estimate from to - needToUpdateTask = true - updTask.TimeEstimateMs = totalEstimate - fmt.Fprintf(commentText, "- orig task will be updated - time estimate will be sets to %s\n", msHuman(totalEstimate)) - needToSendComment = true - } - if origTask.TimeEstimateMs == nil && totalEstimate > 0 { - // added estimate + if origTaskStatus := statuses.SetStatusToOrigTaskIfExists(mirror.GetMirrorTask(ctx).StatusName); origTaskStatus != "" { + // will be set to original task the status needToUpdateTask = true - updTask.TimeEstimateMs = totalEstimate - fmt.Fprintf(commentText, "- orig task will be updated - time estimate will be sets to %s\n", msHuman(totalEstimate)) - needToSendComment = true - } + updTask.StatusName = strings.ToLower(origTaskStatus) + } + + if statuses.AllowedSyncEstimate(mirror.GetMirrorTask(ctx).StatusName) { + // estimate + totalEstimate := task.TotalEstimate() + if origTask.TimeEstimateMs != nil && totalEstimate == 0 { + // removed time estimate + needToUpdateTask = true + updTask.TimeEstimateMs = -1 + fmt.Fprintf(commentText, "- orig task will be updated - time estimate will be removed\n") + needToSendComment = true + } else if origTask.TimeEstimateMs != nil && totalEstimate != 0 && totalEstimate != *origTask.TimeEstimateMs { + // changed estimate from to + needToUpdateTask = true + updTask.TimeEstimateMs = totalEstimate + fmt.Fprintf(commentText, "- orig task will be updated - time estimate will be sets to %s\n", msHuman(totalEstimate)) + needToSendComment = true + } + if origTask.TimeEstimateMs == nil && totalEstimate > 0 { + // added estimate + needToUpdateTask = true + updTask.TimeEstimateMs = totalEstimate + fmt.Fprintf(commentText, "- orig task will be updated - time estimate will be sets to %s\n", msHuman(totalEstimate)) + needToSendComment = true + } - // due date - if origTask.DueDateAt != nil && task.DueDateAt == nil { - // removed duedate - needToUpdateTask = true - updTask.DueDate = -1 - fmt.Fprintf(commentText, "- orig task will be updated - time due date will be removed\n") - needToSendComment = true - } - if origTask.DueDateAt != nil && task.DueDateAt != nil && - (*origTask.DueDateAt).AsTime().Unix() != (*task.DueDateAt).AsTime().Unix() { - // changed duedate from to - needToUpdateTask = true - updTask.DueDate = (*task.DueDateAt).AsTime().Unix() * 1000 - fmt.Fprintf(commentText, "- orig task will be updated - time due date will be sets to %s\n", (*task.DueDateAt).AsTime().Format(time.RFC3339)) - needToSendComment = true - } - if origTask.DueDateAt == nil && task.DueDateAt != nil { - // added duedate - needToUpdateTask = true - updTask.DueDate = (*task.DueDateAt).AsTime().Unix() * 1000 - fmt.Fprintf(commentText, "- orig task will be updated - time due date will be sets to %s\n", (*task.DueDateAt).AsTime().Format(time.RFC3339)) - needToSendComment = true - } + // due date + if origTask.DueDateAt != nil && task.DueDateAt == nil { + // removed duedate + needToUpdateTask = true + updTask.DueDate = -1 + fmt.Fprintf(commentText, "- orig task will be updated - time due date will be removed\n") + needToSendComment = true + } + if origTask.DueDateAt != nil && task.DueDateAt != nil && + (*origTask.DueDateAt).AsTime().Unix() != (*task.DueDateAt).AsTime().Unix() { + // changed duedate from to + needToUpdateTask = true + updTask.DueDate = (*task.DueDateAt).AsTime().Unix() * 1000 + fmt.Fprintf(commentText, "- orig task will be updated - time due date will be sets to %s\n", (*task.DueDateAt).AsTime().Format(time.RFC3339)) + needToSendComment = true + } + if origTask.DueDateAt == nil && task.DueDateAt != nil { + // added duedate + needToUpdateTask = true + updTask.DueDate = (*task.DueDateAt).AsTime().Unix() * 1000 + fmt.Fprintf(commentText, "- orig task will be updated - time due date will be sets to %s\n", (*task.DueDateAt).AsTime().Format(time.RFC3339)) + needToSendComment = true + } - // start date - if origTask.StartDateAt != nil && task.StartDateAt == nil { - // removed startdate - needToUpdateTask = true - updTask.StartDate = -1 - fmt.Fprintf(commentText, "- orig task will be updated - time start date will be removed\n") - needToSendComment = true - } - if origTask.StartDateAt != nil && task.StartDateAt != nil && - (*origTask.StartDateAt).AsTime().Unix() != (*task.StartDateAt).AsTime().Unix() { - // changed startdate from to - needToUpdateTask = true - updTask.StartDate = (*task.StartDateAt).AsTime().Unix() * 1000 - fmt.Fprintf(commentText, "- orig task will be updated - time start date will be sets to %s\n", (*task.StartDateAt).AsTime().Format(time.RFC3339)) - needToSendComment = true - } - if origTask.StartDateAt == nil && task.StartDateAt != nil { - // added startdate - needToUpdateTask = true - updTask.StartDate = (*task.StartDateAt).AsTime().Unix() * 1000 - fmt.Fprintf(commentText, "- orig task will be updated - time start date will be sets to %s\n", (*task.StartDateAt).AsTime().Format(time.RFC3339)) - needToSendComment = true + // start date + if origTask.StartDateAt != nil && task.StartDateAt == nil { + // removed startdate + needToUpdateTask = true + updTask.StartDate = -1 + fmt.Fprintf(commentText, "- orig task will be updated - time start date will be removed\n") + needToSendComment = true + } + if origTask.StartDateAt != nil && task.StartDateAt != nil && + (*origTask.StartDateAt).AsTime().Unix() != (*task.StartDateAt).AsTime().Unix() { + // changed startdate from to + needToUpdateTask = true + updTask.StartDate = (*task.StartDateAt).AsTime().Unix() * 1000 + fmt.Fprintf(commentText, "- orig task will be updated - time start date will be sets to %s\n", (*task.StartDateAt).AsTime().Format(time.RFC3339)) + needToSendComment = true + } + if origTask.StartDateAt == nil && task.StartDateAt != nil { + // added startdate + needToUpdateTask = true + updTask.StartDate = (*task.StartDateAt).AsTime().Unix() * 1000 + fmt.Fprintf(commentText, "- orig task will be updated - time start date will be sets to %s\n", (*task.StartDateAt).AsTime().Format(time.RFC3339)) + needToSendComment = true + } } if needToUpdateMirrorTask { diff --git a/clickup/sync_preferences.go b/clickup/sync_preferences.go index 4680a42..cf8f9a3 100644 --- a/clickup/sync_preferences.go +++ b/clickup/sync_preferences.go @@ -21,6 +21,34 @@ func ParseSyncPreferences(in io.Reader) (*SyncPreferences, error) { type SyncPreferences struct { MirrorTaskRules []MirrorTaskSpecification `yaml:"mirror_task_rules"` + // ASSERTS: all mirror tasks have the same status life cycle + GlobalMirrorTaskStatuses MirrorTaskStatuses `yaml:"global_mirror_task_statuses"` +} + +type MirrorTaskStatuses map[string]MirrorTaskStatus + +func (s MirrorTaskStatuses) AllowedSyncEstimate(mirrorTaskStatus string) bool { + mirrorTaskStatus = strings.ToLower(mirrorTaskStatus) + rule, exists := s[mirrorTaskStatus] + if !exists { + return false + } + return rule.SyncEstimateAndDueDateToOrigTask +} + +// returns "" if nothing needs to be done +func (s MirrorTaskStatuses) SetStatusToOrigTaskIfExists(mirrorTaskStatus string) string { + mirrorTaskStatus = strings.ToLower(mirrorTaskStatus) + rule, exists := s[mirrorTaskStatus] + if !exists { + return "" + } + return rule.SetStatusToOriginalTask +} + +type MirrorTaskStatus struct { + SyncEstimateAndDueDateToOrigTask bool `yaml:"sync_estimate"` + SetStatusToOriginalTask string `yaml:"orig_task_status"` } func (s *SyncPreferences) AllUsedTeamIDs() []string {