1package bug
2
3import (
4 "errors"
5 "fmt"
6 "strings"
7
8 "github.com/MichaelMure/git-bug/repository"
9 "github.com/MichaelMure/git-bug/util/text"
10)
11
12type Person struct {
13 Name string `json:"name"`
14 Email string `json:"email"`
15 AvatarUrl string `json:"avatar_url"`
16}
17
18// GetUser will query the repository for user detail and build the corresponding Person
19func GetUser(repo repository.Repo) (Person, error) {
20 name, err := repo.GetUserName()
21 if err != nil {
22 return Person{}, err
23 }
24 if name == "" {
25 return Person{}, errors.New("User name is not configured in git yet. Please use `git config --global user.name \"John Doe\"`")
26 }
27
28 email, err := repo.GetUserEmail()
29 if err != nil {
30 return Person{}, err
31 }
32 if email == "" {
33 return Person{}, errors.New("User name is not configured in git yet. Please use `git config --global user.email johndoe@example.com`")
34 }
35
36 return Person{Name: name, Email: email}, nil
37}
38
39// Match tell is the Person match the given query string
40func (p Person) Match(query string) bool {
41 return strings.Contains(strings.ToLower(p.Name), strings.ToLower(query))
42}
43
44func (p Person) Validate() error {
45 if text.Empty(p.Name) {
46 return fmt.Errorf("name is not set")
47 }
48
49 if strings.Contains(p.Name, "\n") {
50 return fmt.Errorf("name should be a single line")
51 }
52
53 if !text.Safe(p.Name) {
54 return fmt.Errorf("name is not fully printable")
55 }
56
57 if strings.Contains(p.Email, "\n") {
58 return fmt.Errorf("email should be a single line")
59 }
60
61 if !text.Safe(p.Email) {
62 return fmt.Errorf("email is not fully printable")
63 }
64
65 if p.AvatarUrl != "" && !text.ValidUrl(p.AvatarUrl) {
66 return fmt.Errorf("avatarUrl is not a valid URL")
67 }
68
69 return nil
70}
71
72func (p Person) String() string {
73 return fmt.Sprintf("%s <%s>", p.Name, p.Email)
74}