add(taskwarrior): pull implementation for new tasks

This implements the pull functionality for new tasks, which are not yet
in the local taskwarrior instance created. WIP for #3.
This commit is contained in:
2023-10-21 12:43:41 +02:00
parent 3ed3c53854
commit ba9683f290
5 changed files with 113 additions and 14 deletions

View File

@@ -2,6 +2,7 @@ package gitw
import (
"errors"
"fmt"
"gitea.yves-biener.de/yves-biener/gitwarrior/internal/gitea"
"gitea.yves-biener.de/yves-biener/gitwarrior/internal/taskwarrior"
@@ -52,38 +53,115 @@ func (project *Project) Fetch() error {
}
}
project.issues = append(project.issues, Issue{
issue: issue,
comments: issue_comments,
git_issue: issue,
comments: issue_comments,
})
}
return nil
}
// TODO: tasks should include the corresponding time's of the git related issues and milestones
func (project *Project) merge() error {
// TODO: run merge to update the local taskwarrior tasks into the state of the
// issues and milestones
var tasks []taskwarrior.Task
var filter taskwarrior.Filter
// TODO: merge milestones
for _, milestone := range project.milestones {
filter.Reset()
filter.IncludeGitId(milestone.Id)
filter.IncludeGitType(taskwarrior.MILESTONE)
git_tasks, err := taskwarrior.GetTasks(filter)
if err != nil {
return err
}
var task taskwarrior.Task
if len(git_tasks) > 1 {
return errors.New("Git milestone id was at least used twice in taskwarrior tasks.")
} else if len(git_tasks) == 0 {
// NOTE: ignore closed milestones which do not have a taskwarrior task
if milestone.State == string(gitea.CLOSED) {
continue
}
// NOTE: this milestone does not yet exist
task = taskwarrior.NewTask(
milestone.Title,
project.repository.Name,
milestone.Id,
taskwarrior.MILESTONE,
)
task.AppendComment(milestone.Description, milestone.Updated_at)
fmt.Printf("\tCreated milestone: '%s'\n", task.Description)
tasks = append(tasks, task)
} else {
// NOTE: there is exactly one git_task
task = milestone.Merge(git_tasks[0])
fmt.Printf("\tUpdated milestone: '%s'\n", task.Description)
tasks = append(tasks, task)
}
}
// NOTE: merge tasks
// TODO: link milestones into tasks
for _, issue := range project.issues {
filter.Reset()
filter.IncludeGitId(issue.git_issue.Id)
filter.IncludeGitType(taskwarrior.ISSUE)
git_tasks, err := taskwarrior.GetTasks(filter)
if err != nil {
return err
}
var task taskwarrior.Task
if len(git_tasks) > 1 {
return errors.New("Git issue id was at least used twice in taskwarrior tasks.")
} else if len(git_tasks) == 0 {
// NOTE: ignore closed issues which do not have a taskwarrior task
if issue.git_issue.State == string(gitea.CLOSED) {
continue
}
// NOTE: this task does not yet exist
task = taskwarrior.NewTask(
issue.git_issue.Title,
project.repository.Name,
issue.git_issue.Id,
taskwarrior.ISSUE,
issue.git_issue.Labels...,
)
for _, comment := range issue.comments {
task.AppendComment(comment.Body, comment.Updated_at)
}
fmt.Printf("\tCreated task: '%s'\n", task.Description)
tasks = append(tasks, task)
} else {
// NOTE: there is excactly one git_task
task = issue.merge(git_tasks[0])
fmt.Printf("\tUpdated task: '%s'\n", task.Description)
tasks = append(tasks, task)
}
}
// TODO: dry-run switch
if false {
return taskwarrior.UpdateTasks(tasks)
}
return nil
}
type Issue struct {
issue gitea.Issue
comments []gitea.Comment
git_issue gitea.Issue
comments []gitea.Comment
}
func (i *Issue) into() (taskwarrior.Task, error) {
// TODO: identify if issue is already an existing task
var filter taskwarrior.Filter
filter.IncludeGitId(i.issue.Id)
filter.IncludeGitId(i.git_issue.Id)
filter.IncludeGitType(taskwarrior.ISSUE)
tasks, err := taskwarrior.GetTasks(filter)
if err != nil {
// this means that a task for this issue does not exist yet
return taskwarrior.NewTask(
i.issue.Title,
i.issue.Repository.Name,
i.issue.Id,
i.git_issue.Title,
i.git_issue.Repository.Name,
i.git_issue.Id,
taskwarrior.ISSUE,
i.issue.Labels...,
i.git_issue.Labels...,
), nil
} else {
// this means that a task exists and it needs to be merged
@@ -94,6 +172,7 @@ func (i *Issue) into() (taskwarrior.Task, error) {
}
}
// TODO: implement merging of git issue or milestone into a taskwarrior task
func (i *Issue) merge(task taskwarrior.Task) taskwarrior.Task {
// TODO: issue values into task:
// - is the issue more recent than the task?