mod(taskwarrior): complete push implementation

Currently the merging process is not working entirely as I would
expect. There are no checks for actual changes between the local version
and the server version. WIP for #3. Configuration file created with
corresponding values like, user name and access code, which are - for
obvious reasons - not commited.
This commit is contained in:
2023-10-25 17:14:54 +02:00
parent 0cb4190540
commit 854dafde01
8 changed files with 160 additions and 47 deletions

View File

@@ -3,6 +3,7 @@ package gitw
import (
"errors"
"fmt"
"strconv"
"strings"
"time"
@@ -76,10 +77,10 @@ func (project *Project) Push() error {
if err != nil {
return err
}
for _, task := range tasks {
for x, task := range tasks {
if task.Git_type == string(taskwarrior.ISSUE) {
var issue gitea.Issue
issue.Id = uint(task.Git_id)
issue.Number = uint(task.Git_number)
issue.Title = task.Description
issue.Body = task.Annotations[0].Description
if len(task.Due) > 0 {
@@ -98,7 +99,7 @@ func (project *Project) Push() error {
for _, milestone := range milestones {
for _, dep := range milestone.Depends {
if task.Uuid == dep {
issue.Milestone.Id = uint(milestone.Git_id)
issue.Milestone.Id = uint(milestone.Git_number)
found = true
break
}
@@ -117,7 +118,7 @@ func (project *Project) Push() error {
}
} else {
var milestone gitea.Milestone
milestone.Id = uint(task.Git_id)
milestone.Id = uint(task.Git_number)
milestone.Title = task.Description
milestone.Description = task.Annotations[0].Description
milestone.Due_on = taskwarrior.TaskTimeToGoTime(task.Due)
@@ -130,19 +131,73 @@ func (project *Project) Push() error {
return err
}
}
comment_ids := strings.Split(task.Git_comment_ids, ",")
for i, annotation := range task.Annotations {
if i == 0 { // that the body of the issue / milestone
continue
}
var comment gitea.Comment
comment.Id = uint(task.Git_id)
comment.Body = annotation.Description
if err = project.server.UpdateComment(project.repository, comment); err != nil {
comment_id, err := strconv.Atoi(comment_ids[i])
if err != nil {
return err
}
if comment_id == 0 {
continue
}
if comment_id < 0 {
// create new comment
var issue gitea.Issue
found_issue := false
for _, git_issue := range project.issues {
if git_issue.git_issue.Number == uint(task.Git_number) {
issue = git_issue.git_issue
found_issue = true
break
}
}
if !found_issue {
return errors.New("Could not find corresponding issue for annotation to create new comment on gitea.")
}
comment.Body = annotation.Description
comment.Created_at = taskwarrior.TaskTimeToGoTime(annotation.Entry)
fmt.Printf("\tAdd comment to '%s'#'%d'\n", issue.Title, issue.Number)
if comment, err = project.server.NewComment(project.repository, issue, comment); err != nil {
return err
}
// update the corresponding id from -1 to the one assigned by git
comment_ids[i] = fmt.Sprintf("%d", comment.Id)
} else {
// update existing comment
comment.Id = uint(comment_id)
comment.Body = annotation.Description
if err = project.server.UpdateComment(project.repository, comment); err != nil {
return err
}
}
}
if len(task.Annotations) < len(comment_ids) {
// there have been annotations removed, hence the corresponding comments need to be removed as well
for i := len(task.Annotations); i < len(comment_ids); i += 1 {
comment_id, err := strconv.Atoi(comment_ids[i])
if err != nil {
return err
}
if comment_id < 0 {
return errors.New("Tried to delete comment with id < 0")
}
fmt.Printf("\tRemoving comment\n")
if err = project.server.DeleteComment(project.repository, uint(comment_id)); err != nil {
return err
}
}
comment_ids = comment_ids[:len(task.Annotations)]
}
// update task
tasks[x].Git_comment_ids = strings.Join(comment_ids, ",")
// as we update the task we also update the last update time as well
tasks[x].Last_gitw_update = taskwarrior.GoTimeToTaskTime(time.Now().In(time.Local))
}
return nil
return taskwarrior.UpdateTasks(tasks)
}
// TODO: tasks should include the corresponding time's of the git related issues and milestones
@@ -155,7 +210,7 @@ func (project *Project) merge() error {
// NOTE: merge tasks
for _, issue := range project.issues {
filter.Reset()
filter.IncludeGitId(issue.git_issue.Id)
filter.IncludeGitNumber(issue.git_issue.Number)
filter.IncludeGitType(taskwarrior.ISSUE)
git_tasks, err := taskwarrior.GetTasks(filter)
if err != nil {
@@ -197,7 +252,7 @@ func (project *Project) merge() error {
// TODO: merge milestones
for _, milestone := range project.milestones {
filter.Reset()
filter.IncludeGitId(milestone.Id)
filter.IncludeGitNumber(milestone.Id)
filter.IncludeGitType(taskwarrior.MILESTONE)
git_tasks, err := taskwarrior.GetTasks(filter)
if err != nil {
@@ -213,12 +268,12 @@ func (project *Project) merge() error {
// NOTE: this milestone does not yet exist
task = milestone.IntoTask(project.repository)
for _, issue := range project.issues {
if issue.git_issue.State == "closed" || issue.git_issue.Milestone.Id != milestone.Id {
if issue.git_issue.State == string(gitea.CLOSED) || issue.git_issue.Milestone.Id != milestone.Id {
continue
}
// link to the corresponding task
filter.Reset()
filter.IncludeGitId(issue.git_issue.Id)
filter.IncludeGitNumber(issue.git_issue.Number)
filter.IncludeGitType(taskwarrior.ISSUE)
tasks, err := taskwarrior.GetTasks(filter)
if err != nil {
@@ -258,14 +313,14 @@ func (issue *Issue) IntoTask(repository gitea.Repository) (task taskwarrior.Task
task = taskwarrior.NewTask(
issue.git_issue.Title,
repository.Name,
issue.git_issue.Id,
issue.git_issue.Number,
taskwarrior.ISSUE,
issue.git_issue.Labels...,
)
task.Entry = taskwarrior.GoTimeToTaskTime(issue.git_issue.Created_at)
task.Modified = taskwarrior.GoTimeToTaskTime(issue.git_issue.Updated_at)
for _, comment := range issue.comments {
task.AppendComment(comment.Body, comment.Updated_at)
task.AppendComment(int(comment.Id), comment.Body, comment.Updated_at)
}
return
}
@@ -280,14 +335,13 @@ func (issue *Issue) MergeTask(task taskwarrior.Task) taskwarrior.Task {
if task.Description != issue.git_issue.Title {
task.Description = diff.Prompt(task.Description, issue.git_issue.Title, task.Description)
}
if task.Status == "completed" || issue.git_issue.State == "closed" {
if !(task.Status == "completed" && issue.git_issue.State == "closed" || task.Status != "completed" && issue.git_issue.State != "closed") {
if task.Status == "completed" || issue.git_issue.State == string(gitea.CLOSED) {
if !(task.Status == "completed" && issue.git_issue.State == string(gitea.CLOSED) || task.Status != "completed" && issue.git_issue.State != string(gitea.CLOSED)) {
task.Status = diff.Prompt(task.Status, issue.git_issue.State, task.Status)
}
}
// TODO: try to automatically derive the correct required changes
var annotations []string
annotations = append(annotations, issue.git_issue.Body)
for _, annotation := range task.Annotations {
annotations = append(annotations, annotation.Description)
}
@@ -295,7 +349,9 @@ func (issue *Issue) MergeTask(task taskwarrior.Task) taskwarrior.Task {
for _, comment := range issue.comments {
comments = append(comments, comment.Body)
}
// TODO: provide options for theirs, mine and manual edit of the task annotations
// FIXME: provide options for theirs, mine and manual edit of the task annotations
// FIXME: prompted for no right reason
// FIXME: changes the task incorrectly
annotation_joined := diff.Prompt(strings.Join(annotations, "\n\n"), strings.Join(comments, "\n\n"), strings.Join(annotations, "\n\n"))
for i, description := range strings.Split(annotation_joined, "\n\n") {
if len(description) == 0 {
@@ -304,13 +360,13 @@ func (issue *Issue) MergeTask(task taskwarrior.Task) taskwarrior.Task {
if i < len(task.Annotations) {
task.Annotations[i].Description = description
} else {
task.AppendComment(description, time.Now().In(time.Local))
task.AppendComment(-1, description, time.Now().In(time.Local))
}
}
} else {
// NOTE: there are no modifications between the last received update and the current version, hence we just accept theirs fully
task.Description = issue.git_issue.Title
if issue.git_issue.State == "closed" {
if issue.git_issue.State == string(gitea.CLOSED) {
// otherwise do not update the value
task.Status = "completed"
task.End = taskwarrior.GoTimeToTaskTime(issue.git_issue.Closed_at)
@@ -322,7 +378,7 @@ func (issue *Issue) MergeTask(task taskwarrior.Task) taskwarrior.Task {
task.Annotations[i].Description = comment.Body
task.Annotations[i].Entry = taskwarrior.GoTimeToTaskTime(comment.Updated_at)
} else {
task.AppendComment(comment.Body, comment.Updated_at)
task.AppendComment(-1, comment.Body, comment.Updated_at)
}
}
}