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

Commit

Permalink
Pivotal import supports user creation
Browse files Browse the repository at this point in the history
  • Loading branch information
falconandy committed Aug 2, 2018
1 parent 1f4da4a commit 10070aa
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 69 deletions.
64 changes: 1 addition & 63 deletions actions/sync_users_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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, ", "))
Expand Down Expand Up @@ -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
}
18 changes: 16 additions & 2 deletions backlog/csv_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion backlog/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
70 changes: 67 additions & 3 deletions backlog/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package backlog

import (
"fmt"
"github.com/mreider/agilemarkdown/git"
"github.com/mreider/agilemarkdown/utils"
"io/ioutil"
"os"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
2 changes: 2 additions & 0 deletions commands/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ func findRootDirectory() (string, error) {
newDir := filepath.Dir(dir)
if newDir == dir {
dir = ""
} else {
dir = newDir
}
}
if dir == "" {
Expand Down

0 comments on commit 10070aa

Please sign in to comment.