aboutsummaryrefslogtreecommitdiffstats
path: root/bug/person.go
blob: 449e2262335c405548dcf9386f5c27bce47d505e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package bug

import (
	"errors"
	"fmt"
	"strings"

	"github.com/MichaelMure/git-bug/repository"
	"github.com/MichaelMure/git-bug/util/text"
)

type Person struct {
	Name      string `json:"name"`
	Email     string `json:"email"`
	Login     string `json:"login"`
	AvatarUrl string `json:"avatar_url"`
}

// GetUser will query the repository for user detail and build the corresponding Person
func GetUser(repo repository.Repo) (Person, error) {
	name, err := repo.GetUserName()
	if err != nil {
		return Person{}, err
	}
	if name == "" {
		return Person{}, errors.New("User name is not configured in git yet. Please use `git config --global user.name \"John Doe\"`")
	}

	email, err := repo.GetUserEmail()
	if err != nil {
		return Person{}, err
	}
	if email == "" {
		return Person{}, errors.New("User name is not configured in git yet. Please use `git config --global user.email johndoe@example.com`")
	}

	return Person{Name: name, Email: email}, nil
}

// Match tell is the Person match the given query string
func (p Person) Match(query string) bool {
	query = strings.ToLower(query)

	return strings.Contains(strings.ToLower(p.Name), query) ||
		strings.Contains(strings.ToLower(p.Login), query)
}

func (p Person) Validate() error {
	if text.Empty(p.Name) && text.Empty(p.Login) {
		return fmt.Errorf("either name or login should be set")
	}

	if strings.Contains(p.Name, "\n") {
		return fmt.Errorf("name should be a single line")
	}

	if !text.Safe(p.Name) {
		return fmt.Errorf("name is not fully printable")
	}

	if strings.Contains(p.Login, "\n") {
		return fmt.Errorf("login should be a single line")
	}

	if !text.Safe(p.Login) {
		return fmt.Errorf("login is not fully printable")
	}

	if strings.Contains(p.Email, "\n") {
		return fmt.Errorf("email should be a single line")
	}

	if !text.Safe(p.Email) {
		return fmt.Errorf("email is not fully printable")
	}

	if p.AvatarUrl != "" && !text.ValidUrl(p.AvatarUrl) {
		return fmt.Errorf("avatarUrl is not a valid URL")
	}

	return nil
}

func (p Person) DisplayName() string {
	switch {
	case p.Name == "" && p.Login != "":
		return p.Login
	case p.Name != "" && p.Login == "":
		return p.Name
	case p.Name != "" && p.Login != "":
		return fmt.Sprintf("%s (%s)", p.Name, p.Login)
	}

	panic("invalid person data")
}