add: push changes to gitea server after merging local changes

This implementation has no configuration yet to authorize accordingly.
WIP for #3 and #4.
This commit is contained in:
2023-10-24 22:17:37 +02:00
parent d2aeb7ffe4
commit 0cb4190540
6 changed files with 217 additions and 43 deletions

View File

@@ -68,9 +68,86 @@ func (project *Project) Fetch() error {
return nil
}
func (project *Project) Push() error {
// TODO: check if a pull is required before pushing changes
var filter taskwarrior.Filter
filter.IncludeProjects(project.repository.Name)
tasks, err := taskwarrior.GetTasks(filter)
if err != nil {
return err
}
for _, task := range tasks {
if task.Git_type == string(taskwarrior.ISSUE) {
var issue gitea.Issue
issue.Id = uint(task.Git_id)
issue.Title = task.Description
issue.Body = task.Annotations[0].Description
if len(task.Due) > 0 {
issue.Due_date = taskwarrior.TaskTimeToGoTime(task.Due)
} else {
issue.Due_date = time.Unix(0, 0)
}
filter.Reset()
filter.IncludeGitType(taskwarrior.MILESTONE)
filter.IncludeProjects(project.repository.Name)
milestones, err := taskwarrior.GetTasks(filter)
if err != nil {
return err
}
found := false
for _, milestone := range milestones {
for _, dep := range milestone.Depends {
if task.Uuid == dep {
issue.Milestone.Id = uint(milestone.Git_id)
found = true
break
}
}
if found {
break
}
}
if task.Status == "completed" {
issue.State = string(gitea.CLOSED)
} else {
issue.State = string(gitea.OPEN)
}
if err = project.server.UpdateIssue(project.repository, issue); err != nil {
return err
}
} else {
var milestone gitea.Milestone
milestone.Id = uint(task.Git_id)
milestone.Title = task.Description
milestone.Description = task.Annotations[0].Description
milestone.Due_on = taskwarrior.TaskTimeToGoTime(task.Due)
if task.Status == "completed" {
milestone.State = string(gitea.CLOSED)
} else {
milestone.State = string(gitea.OPEN)
}
if err = project.server.UpdateMilestone(project.repository, milestone); err != nil {
return err
}
}
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 {
return err
}
}
}
return nil
}
// TODO: tasks should include the corresponding time's of the git related issues and milestones
func (project *Project) merge() error {
dry_run := true // make this a parameter / cli flag
dry_run := false // make this a parameter / cli flag
var tasks []taskwarrior.Task
var task taskwarrior.Task
var filter taskwarrior.Filter
@@ -208,32 +285,26 @@ func (issue *Issue) MergeTask(task taskwarrior.Task) taskwarrior.Task {
task.Status = diff.Prompt(task.Status, issue.git_issue.State, task.Status)
}
}
if len(task.Annotations) != len(issue.comments) {
var annotations []string
annotations = append(annotations, issue.git_issue.Body)
for _, annotation := range task.Annotations {
annotations = append(annotations, annotation.Description)
// 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)
}
var comments []string
for _, comment := range issue.comments {
comments = append(comments, comment.Body)
}
// TODO: provide options for theirs, mine and manual edit of the task annotations
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 {
continue
}
var comments []string
for _, comment := range issue.comments {
comments = append(comments, comment.Body)
}
// TODO: how should the user manually enter the values?
// TODO: provide options for theirs, mine and manual edit of the task annotations
// annotation_joined := prompt(strings.Join(annotations, "\n\n"), strings.Join(comments, "\n\n"), strings.Join(annotations, "\n\n"))
} else {
for i := range issue.comments {
// check the modification times?
annotation := task.Annotations[i]
comment := issue.comments[i]
modification_time := taskwarrior.TaskTimeToGoTime(annotation.Entry)
if comment.Updated_at.After(modification_time) {
annotation.Description = comment.Body
annotation.Entry = taskwarrior.GoTimeToTaskTime(comment.Updated_at)
} else {
annotation.Description = diff.Prompt(annotation.Description, comment.Body, annotation.Description)
}
task.Annotations[i] = annotation
if i < len(task.Annotations) {
task.Annotations[i].Description = description
} else {
task.AppendComment(description, time.Now().In(time.Local))
}
}
} else {