Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
Sync progress, error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
falconandy committed Aug 3, 2018
1 parent 71dd6c0 commit 341fe4c
Show file tree
Hide file tree
Showing 36 changed files with 351 additions and 155 deletions.
5 changes: 4 additions & 1 deletion actions/assign_user_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ func (a *AssignUserAction) Execute() error {
item := items[itemIndex]
item.SetAssigned(user)
item.SetModified(utils.GetCurrentTimestamp())
item.Save()
err := item.Save()
if err != nil {
return err
}
}
}

Expand Down
5 changes: 4 additions & 1 deletion actions/change_status_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ func (a *ChangeStatusAction) Execute() error {
} else {
item.SetArchived(true)
}
item.Save()
err := item.Save()
if err != nil {
return err
}
}
}

Expand Down
15 changes: 12 additions & 3 deletions actions/change_tag_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,27 @@ func (a *ChangeTagAction) Execute() error {
itemTags := item.Tags()
itemTags = utils.RenameItemIgnoreCase(itemTags, a.oldTag, a.newTag)
item.SetTags(itemTags)
item.Save()
err := item.Save()
if err != nil {
return err
}
}

tagIdeas := ideasTags[a.oldTag]
for _, idea := range tagIdeas {
ideaTags := idea.Tags()
ideaTags = utils.RenameItemIgnoreCase(ideaTags, a.oldTag, a.newTag)
idea.SetTags(ideaTags)
idea.Save()
err := idea.Save()
if err != nil {
return err
}
}

backlog.NewTimelineGenerator(a.root).RenameTimeline(a.oldTag, a.newTag)
err = backlog.NewTimelineGenerator(a.root).RenameTimeline(a.oldTag, a.newTag)
if err != nil {
return err
}

fmt.Printf("Tag '%s' changed to '%s'. Sync to regenerate files.\n", a.oldTag, a.newTag)

Expand Down
7 changes: 5 additions & 2 deletions actions/create_backlog_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (a *CreateBacklogAction) Execute() error {
return nil
}

git.SetUpstream()
_ = git.SetUpstream()

err := os.MkdirAll(backlogDir, 0777)
if err != nil {
Expand All @@ -56,7 +56,10 @@ func (a *CreateBacklogAction) Execute() error {
return err
}
overview.SetTitle(a.backlogName)
overview.UpdateLinks("archive", filepath.Join(backlogDir, backlog.ArchiveFileName), a.root.Root(), a.root.Root())
err = overview.UpdateLinks("archive", filepath.Join(backlogDir, backlog.ArchiveFileName), a.root.Root(), a.root.Root())
if err != nil {
return err
}
overview.SetCreated(utils.GetCurrentTimestamp())
return overview.Save()
}
10 changes: 5 additions & 5 deletions actions/create_idea_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ func (a *CreateIdeaAction) Execute() error {

if !a.simulate {
return idea.Save()
} else {
rootDir := filepath.Dir(filepath.Dir(ideaPath))
fmt.Println(strings.TrimPrefix(ideaPath, rootDir))
fmt.Print(string(idea.Content()))
return nil
}

rootDir := filepath.Dir(filepath.Dir(ideaPath))
fmt.Println(strings.TrimPrefix(ideaPath, rootDir))
fmt.Print(string(idea.Content()))
return nil
}
12 changes: 6 additions & 6 deletions actions/create_item_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ func (a *CreateItemAction) Execute() error {

if !a.simulate {
return item.Save()
} else {
itemPath, _ := filepath.Abs(itemPath)
rootDir := filepath.Dir(filepath.Dir(itemPath))
fmt.Println(strings.TrimPrefix(itemPath, rootDir))
fmt.Print(string(item.Content()))
return nil
}

itemPath, _ = filepath.Abs(itemPath)
rootDir := filepath.Dir(filepath.Dir(itemPath))
fmt.Println(strings.TrimPrefix(itemPath, rootDir))
fmt.Print(string(item.Content()))
return nil
}
6 changes: 2 additions & 4 deletions actions/create_user_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,8 @@ func (a *CreateUserAction) Execute() error {

if userList.AddUser(name, email) {
return userList.Save()
} else {
fmt.Printf("Can't add the user '%s'\n", name)
return nil
}
return nil

fmt.Printf("Can't add the user '%s'\n", name)
return nil
}
10 changes: 8 additions & 2 deletions actions/delete_tag_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,21 @@ func (a *DeleteTagAction) Execute() error {
itemTags = utils.RemoveItemIgnoreCase(itemTags, a.tag)
item.SetTags(itemTags)
item.ClearTimeline()
item.Save()
err := item.Save()
if err != nil {
return err
}
}

tagIdeas := ideasTags[a.tag]
for _, idea := range tagIdeas {
ideaTags := idea.Tags()
ideaTags = utils.RemoveItemIgnoreCase(ideaTags, a.tag)
idea.SetTags(ideaTags)
idea.Save()
err := idea.Save()
if err != nil {
return err
}
}

fmt.Printf("Tag '%s' deleted. Sync to regenerate files.\n", a.tag)
Expand Down
36 changes: 29 additions & 7 deletions actions/sync_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strings"
)

const AttemptCount = 10

type SyncAction struct {
root *backlog.BacklogsStructure
author string
Expand All @@ -33,8 +35,12 @@ func (a *SyncAction) Execute() error {
return err
}

attempts := 10
attempts := AttemptCount
for attempts > 0 {
if attempts < AttemptCount {
fmt.Printf("Attempt %d\n", AttemptCount-attempts+1)
}

attempts--

err := NewSyncItemsStep(a.root).Execute()
Expand Down Expand Up @@ -73,6 +79,7 @@ func (a *SyncAction) Execute() error {
}

if a.testMode {
fmt.Println("OK")
return nil
}

Expand All @@ -81,6 +88,7 @@ func (a *SyncAction) Execute() error {
return err
}
if ok {
fmt.Println("OK")
return nil
}
}
Expand All @@ -92,15 +100,21 @@ func (a *SyncAction) syncToGit() (bool, error) {
if err != nil {
return false, err
}
git.Commit("sync", a.author) // TODO commit message
fmt.Println("git commit")
err = git.Commit("sync", a.author) // TODO commit message
if err != nil {
return false, fmt.Errorf("can't commit: %v", err)
}
err = git.Fetch()
if err != nil {
return false, fmt.Errorf("can't fetch: %v", err)
}
fmt.Println("git merge")
mergeOutput, mergeErr := git.Merge()
if mergeErr != nil {
status, _ := git.Status()
if !strings.Contains(status, "Your branch is based on 'origin/master', but the upstream is gone.") {
fmt.Println("Auto-resolving merge conflicts")
conflictFiles, conflictErr := git.ConflictFiles()
hasConflictItems := false
for _, fileName := range conflictFiles {
Expand All @@ -116,18 +130,26 @@ func (a *SyncAction) syncToGit() (bool, error) {
}
if conflictErr != nil || hasConflictItems {
fmt.Println(mergeOutput)
git.AbortMerge()
_ = git.AbortMerge()
return false, fmt.Errorf("can't merge: %v", mergeErr)
}
for _, conflictFile := range conflictFiles {
git.CheckoutOurVersion(conflictFile)
git.Add(conflictFile)
err = git.CheckoutOurVersion(conflictFile)
if err != nil {
return false, err
}

err := git.Add(conflictFile)
if err != nil {
return false, err
}
fmt.Printf("Remote changes to %s are ignored\n", conflictFile)
}
git.CommitNoEdit(a.author)
return false, nil
err = git.CommitNoEdit(a.author)
return false, err
}
}
fmt.Println("git push")
err = git.Push()
if err != nil {
return false, fmt.Errorf("can't push: %v", err)
Expand Down
28 changes: 19 additions & 9 deletions actions/sync_ideas_step.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ func NewSyncIdeasStep(root *backlog.BacklogsStructure, cfg *config.Config, userL
}

func (s *SyncIdeasStep) Execute() error {
fmt.Println("Generating idea pages")

_, itemsTags, ideasTags, overviews, err := backlog.ItemsAndIdeasTags(s.root)
if err != nil {
return err
Expand All @@ -36,7 +38,11 @@ func (s *SyncIdeasStep) Execute() error {
return err
}

s.sendNewCommentsForIdeas(ideas)
fmt.Println("Send new comments for ideas")
err = s.sendNewCommentsForIdeas(ideas)
if err != nil {
return err
}

ideasByRank := make(map[string][]*backlog.BacklogIdea)
var ranks []string
Expand All @@ -61,7 +67,7 @@ func (s *SyncIdeasStep) Execute() error {

lines := []string{"# Ideas", ""}
lines = append(lines,
fmt.Sprintf(utils.JoinMarkdownLinks(backlog.MakeStandardLinks(s.root.Root(), s.root.Root())...)))
utils.JoinMarkdownLinks(backlog.MakeStandardLinks(s.root.Root(), s.root.Root())...))
lines = append(lines, "")
for _, rank := range ranks {
if rank != "" {
Expand Down Expand Up @@ -98,9 +104,15 @@ func (s *SyncIdeasStep) updateIdea(tagsDir string, idea *backlog.BacklogIdea, id
idea.SetAuthor(author)
idea.SetTags(nil)
idea.SetText(idea.Text())
idea.Save()
err = idea.Save()
if err != nil {
return err
}
}
err := idea.UpdateLinks(s.root.Root())
if err != nil {
return err
}
idea.UpdateLinks(s.root.Root())

ideaTags := make(map[string]struct{})
NextTag:
Expand Down Expand Up @@ -135,12 +147,10 @@ NextTag:
itemsLines = append(itemsLines, backlog.BacklogView{}.WriteMarkdownItemsWithProjectAndStatus(overviews, items, filepath.Dir(idea.Path()), tagsDir, s.userList)...)
}
idea.SetFooter(itemsLines)
idea.Save()

return nil
return idea.Save()
}

func (s *SyncIdeasStep) sendNewCommentsForIdeas(ideas []*backlog.BacklogIdea) {
func (s *SyncIdeasStep) sendNewCommentsForIdeas(ideas []*backlog.BacklogIdea) error {
userList := backlog.NewUserList(s.root.UsersDirectory())
var mailSender *utils.MailSender
if s.cfg.SmtpServer != "" {
Expand All @@ -151,7 +161,7 @@ func (s *SyncIdeasStep) sendNewCommentsForIdeas(ideas []*backlog.BacklogIdea) {
for i := range ideas {
commented[i] = ideas[i]
}
sendNewComments(commented, func(idea backlog.Commented, to []string, comment []string) (me string, err error) {
return sendNewComments(commented, func(idea backlog.Commented, to []string, comment []string) (me string, err error) {
return sendComment(userList, comment, idea.Title(), s.author, to, mailSender, s.cfg, s.root.Root(), idea.Path())
})
}
Loading

0 comments on commit 341fe4c

Please sign in to comment.