From 10070aaac15f26db813f1d9093813254eeb1fdf4 Mon Sep 17 00:00:00 2001 From: Andrey Sokolov Date: Thu, 2 Aug 2018 11:38:44 +0400 Subject: [PATCH] Pivotal import supports user creation --- actions/sync_users_check.go | 64 +-------------------------------- backlog/csv_import.go | 18 ++++++++-- backlog/user.go | 8 ++++- backlog/users.go | 70 +++++++++++++++++++++++++++++++++++-- commands/utils.go | 2 ++ 5 files changed, 93 insertions(+), 69 deletions(-) diff --git a/actions/sync_users_check.go b/actions/sync_users_check.go index 397c916..b35bc17 100644 --- a/actions/sync_users_check.go +++ b/actions/sync_users_check.go @@ -3,8 +3,6 @@ package actions import ( "fmt" "github.com/mreider/agilemarkdown/backlog" - "github.com/mreider/agilemarkdown/git" - "github.com/mreider/agilemarkdown/utils" "sort" "strings" ) @@ -31,7 +29,7 @@ func (ch *SyncUsersCheck) Check() (bool, error) { } unknownUsers := ch.getUnknownUsers(items, ideas) - unknownUsers = ch.resolveGitUsers(unknownUsers) + unknownUsers = ch.userList.ResolveGitUsers(unknownUsers) if len(unknownUsers) != 0 { fmt.Printf("You should resolve unknown users: %s\n", strings.Join(unknownUsers, ", ")) @@ -82,63 +80,3 @@ func (ch *SyncUsersCheck) getUnknownUsers(items []*backlog.BacklogItem, ideas [] return unknownUsers } - -func (ch *SyncUsersCheck) resolveGitUsers(unknownUsers []string) (unresolvedUsers []string) { - names, emails, _ := git.KnownUsers() - currentUserName, currentUserEmail, _ := git.CurrentUser() - names = append(names, currentUserName) - emails = append(emails, currentUserEmail) - - normalizedNames := make([]string, len(names)) - for i, name := range names { - normalizedNames[i] = utils.CollapseWhiteSpaces(strings.ToLower(name)) - } - - normalizedEmails := make([]string, len(emails)) - for i, email := range emails { - normalizedEmails[i] = utils.CollapseWhiteSpaces(strings.ToLower(email)) - } - -NextUser: - for _, user := range unknownUsers { - if ch.userList.User(user) != nil { - continue - } - - normalizedUser := utils.CollapseWhiteSpaces(strings.ToLower(user)) - for i, email := range normalizedEmails { - if email == normalizedUser { - fmt.Printf("User '%s' is associated with a git user '%s <%s>'\n", user, names[i], emails[i]) - if ch.userList.AddUser(names[i], emails[i]) { - ch.userList.Save() - } - continue NextUser - } - } - - for i, email := range normalizedEmails { - nickname := strings.SplitN(email, "@", 2)[0] - if nickname == normalizedUser { - fmt.Printf("User '%s' is associated with a git user '%s <%s>'\n", user, names[i], emails[i]) - if ch.userList.AddUser(names[i], emails[i]) { - ch.userList.Save() - } - continue NextUser - } - } - - for i, name := range normalizedNames { - if name == normalizedUser { - fmt.Printf("User '%s' is associated with a git user '%s <%s>'\n", user, names[i], emails[i]) - if ch.userList.AddUser(names[i], emails[i]) { - ch.userList.Save() - } - continue NextUser - } - } - - unresolvedUsers = append(unresolvedUsers, user) - } - - return unresolvedUsers -} diff --git a/backlog/csv_import.go b/backlog/csv_import.go index 3d42c6d..b7441bd 100644 --- a/backlog/csv_import.go +++ b/backlog/csv_import.go @@ -31,6 +31,9 @@ func NewCsvImporter(csvPath string, backlogDir string) *CsvImporter { } func (imp *CsvImporter) Import() error { + root := NewBacklogsStructure(filepath.Join(imp.backlogDir, "..")) + userList := NewUserList(root.UsersDirectory()) + csvFile, err := os.Open(imp.csvPath) if err != nil { return err @@ -48,7 +51,7 @@ func (imp *CsvImporter) Import() error { if imp.headers == nil { imp.parseHeaders(line) } else { - err := imp.createItemIfNotExists(line) + err := imp.createItemIfNotExists(line, userList) if err != nil { return err } @@ -96,7 +99,7 @@ func (imp *CsvImporter) getItemName(title string) string { return itemName } -func (imp *CsvImporter) createItemIfNotExists(line []string) error { +func (imp *CsvImporter) createItemIfNotExists(line []string, userList *UserList) error { title := imp.cellValue(line, "title") labels := delimiterRe.Split(imp.cellValue(line, "labels"), -1) itemName := imp.getItemName(title) @@ -146,6 +149,17 @@ func (imp *CsvImporter) createItemIfNotExists(line []string) error { created = utils.GetTimestamp(createdDate) } + user := userList.User(assigned) + if user == nil { + unresolvedUsers := userList.ResolveGitUsers([]string{assigned}) + if len(unresolvedUsers) > 0 { + if userList.AddUser(assigned, "") { + fmt.Printf("You should specify email for auto-created user '%s'\n", assigned) + userList.Save() + } + } + } + item.SetTitle(title) item.SetCreated(created) item.SetModified(created) diff --git a/backlog/user.go b/backlog/user.go index 7ef889c..6b148d7 100644 --- a/backlog/user.go +++ b/backlog/user.go @@ -78,6 +78,10 @@ func (u *User) Nickname() string { } func (u *User) HasEmail(email string) bool { + if email == "" { + return false + } + emails := u.Emails() for _, m := range emails { if m == email { @@ -100,7 +104,9 @@ func (u *User) AddEmailIfNotExist(email string) bool { return false } emails := u.Emails() - emails = append(emails, email) + if email != "" { + emails = append(emails, email) + } u.content.SetMetadataValue(UserEmailMetadataKey, strings.Join(emails, ", ")) u.content.SetHeader("") return true diff --git a/backlog/users.go b/backlog/users.go index dcb0613..5b56c1f 100644 --- a/backlog/users.go +++ b/backlog/users.go @@ -2,6 +2,7 @@ package backlog import ( "fmt" + "github.com/mreider/agilemarkdown/git" "github.com/mreider/agilemarkdown/utils" "io/ioutil" "os" @@ -43,9 +44,12 @@ func (ul *UserList) AddUser(name, email string) bool { name = utils.CollapseWhiteSpaces(name) email = utils.CollapseWhiteSpaces(email) - currentUser := ul.User(email) - if currentUser != nil { - return true + var currentUser *User + if email != "" { + currentUser = ul.User(email) + if currentUser != nil { + return true + } } currentUser = ul.User(name) @@ -185,3 +189,63 @@ func (ul *UserList) fixObsoleteUserFiles() error { } return nil } + +func (ul *UserList) ResolveGitUsers(unknownUsers []string) (unresolvedUsers []string) { + names, emails, _ := git.KnownUsers() + currentUserName, currentUserEmail, _ := git.CurrentUser() + names = append(names, currentUserName) + emails = append(emails, currentUserEmail) + + normalizedNames := make([]string, len(names)) + for i, name := range names { + normalizedNames[i] = utils.CollapseWhiteSpaces(strings.ToLower(name)) + } + + normalizedEmails := make([]string, len(emails)) + for i, email := range emails { + normalizedEmails[i] = utils.CollapseWhiteSpaces(strings.ToLower(email)) + } + +NextUser: + for _, user := range unknownUsers { + if ul.User(user) != nil { + continue + } + + normalizedUser := utils.CollapseWhiteSpaces(strings.ToLower(user)) + for i, email := range normalizedEmails { + if email == normalizedUser { + fmt.Printf("User '%s' is associated with a git user '%s <%s>'\n", user, names[i], emails[i]) + if ul.AddUser(names[i], emails[i]) { + ul.Save() + } + continue NextUser + } + } + + for i, email := range normalizedEmails { + nickname := strings.SplitN(email, "@", 2)[0] + if nickname == normalizedUser { + fmt.Printf("User '%s' is associated with a git user '%s <%s>'\n", user, names[i], emails[i]) + if ul.AddUser(names[i], emails[i]) { + ul.Save() + } + continue NextUser + } + } + + for i, name := range normalizedNames { + if name == normalizedUser { + fmt.Printf("User '%s' is associated with a git user '%s <%s>'\n", user, names[i], emails[i]) + if ul.AddUser(names[i], emails[i]) { + ul.Save() + } + continue NextUser + } + } + + unresolvedUsers = append(unresolvedUsers, user) + } + + return unresolvedUsers +} diff --git a/commands/utils.go b/commands/utils.go index 5b6f4c9..680e825 100644 --- a/commands/utils.go +++ b/commands/utils.go @@ -49,6 +49,8 @@ func findRootDirectory() (string, error) { newDir := filepath.Dir(dir) if newDir == dir { dir = "" + } else { + dir = newDir } } if dir == "" {