add(cli): cli interface for push, pull and version

This commit is contained in:
2023-10-26 00:32:25 +02:00
parent 854dafde01
commit 32a389d464
14 changed files with 750 additions and 64 deletions

View File

@@ -1,10 +1,6 @@
package gitea
import (
"encoding/json"
"fmt"
"os"
)
import "github.com/spf13/viper"
var API_PATH = "/api/v1"
@@ -15,16 +11,9 @@ type Gitea struct {
}
func NewGitea() (gitea Gitea) {
configJson, err := os.ReadFile("./configs/gitw.json")
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}
err = json.Unmarshal(configJson, &gitea)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(-1)
}
gitea.Base_url = viper.Get("base_url").(string)
gitea.User_name = viper.Get("user_name").(string)
gitea.Access_code = viper.Get("access_code").(string)
return
}

View File

@@ -26,17 +26,17 @@ func NewProject(server gitea.Gitea, repository gitea.Repository) Project {
}
}
func (project *Project) Pull() error {
if err := project.Fetch(); err != nil {
func (project *Project) Pull(dry_run bool) error {
if err := project.fetch(); err != nil {
return err
}
if err := project.merge(); err != nil {
if err := project.merge(dry_run); err != nil {
return err
}
return nil
}
func (project *Project) Fetch() error {
func (project *Project) fetch() error {
comments, err := project.server.GetComments(project.repository)
if err != nil {
return err
@@ -69,7 +69,10 @@ func (project *Project) Fetch() error {
return nil
}
func (project *Project) Push() error {
func (project *Project) Push(dry_run bool) error {
if err := project.fetch(); err != nil {
return err
}
// TODO: check if a pull is required before pushing changes
var filter taskwarrior.Filter
filter.IncludeProjects(project.repository.Name)
@@ -80,6 +83,7 @@ func (project *Project) Push() error {
for x, task := range tasks {
if task.Git_type == string(taskwarrior.ISSUE) {
var issue gitea.Issue
// TODO: add new issue if necessary
issue.Number = uint(task.Git_number)
issue.Title = task.Description
issue.Body = task.Annotations[0].Description
@@ -113,8 +117,12 @@ func (project *Project) Push() error {
} else {
issue.State = string(gitea.OPEN)
}
if err = project.server.UpdateIssue(project.repository, issue); err != nil {
return err
if !dry_run {
if err = project.server.UpdateIssue(project.repository, issue); err != nil {
return err
}
} else {
fmt.Printf("\tUpdate Issue: '%s'\n", issue.Title)
}
} else {
var milestone gitea.Milestone
@@ -122,13 +130,18 @@ func (project *Project) Push() error {
milestone.Title = task.Description
milestone.Description = task.Annotations[0].Description
milestone.Due_on = taskwarrior.TaskTimeToGoTime(task.Due)
// TODO: add new milestone if necessary
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
if !dry_run {
if err = project.server.UpdateMilestone(project.repository, milestone); err != nil {
return err
}
} else {
fmt.Printf("\tUpdating Milestone: '%s'\n", milestone.Title)
}
}
comment_ids := strings.Split(task.Git_comment_ids, ",")
@@ -161,8 +174,10 @@ func (project *Project) Push() error {
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
if !dry_run {
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)
@@ -170,8 +185,12 @@ func (project *Project) Push() error {
// 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 !dry_run {
if err = project.server.UpdateComment(project.repository, comment); err != nil {
return err
}
} else {
fmt.Printf("\tUpdating comment: '%d'\n", comment.Id)
}
}
}
@@ -186,8 +205,10 @@ func (project *Project) Push() error {
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
if !dry_run {
if err = project.server.DeleteComment(project.repository, uint(comment_id)); err != nil {
return err
}
}
}
comment_ids = comment_ids[:len(task.Annotations)]
@@ -197,12 +218,14 @@ func (project *Project) Push() error {
// 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 taskwarrior.UpdateTasks(tasks)
if !dry_run {
return taskwarrior.UpdateTasks(tasks)
} else {
return nil
}
}
// TODO: tasks should include the corresponding time's of the git related issues and milestones
func (project *Project) merge() error {
dry_run := false // make this a parameter / cli flag
func (project *Project) merge(dry_run bool) error {
var tasks []taskwarrior.Task
var task taskwarrior.Task
var filter taskwarrior.Filter

View File

@@ -12,7 +12,7 @@ func TestTimeCoversions(t *testing.T) {
task_current_time = GoTimeToTaskTime(go_current_time)
current_time = TaskTimeToGoTime(task_current_time)
if current_time.Compare(go_current_time) != 0 {
if current_time.Compare(go_current_time) != -1 {
t.Fatalf("conversion failed: Expected: `%s` but got: `%s`", current_time.String(), go_current_time.String())
}
}