-
Notifications
You must be signed in to change notification settings - Fork 785
/
helpers.go
42 lines (40 loc) · 1.15 KB
/
helpers.go
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
package gits
import (
"github.com/pkg/errors"
"os"
"os/user"
)
// EnsureUserAndEmailSetup returns the user name and email for the gitter
// lazily setting them if they are blank either from the environment variables
// `GIT_AUTHOR_NAME` and `GIT_AUTHOR_EMAIL` or using default values
func EnsureUserAndEmailSetup(gitter Gitter) (string, string, error) {
userName, _ := gitter.Username("")
userEmail, _ := gitter.Email("")
if userName == "" {
userName = os.Getenv("GIT_AUTHOR_NAME")
if userName == "" {
user, err := user.Current()
if err == nil && user != nil {
userName = user.Username
}
}
if userName == "" {
userName = "jenkins-x-bot"
}
err := gitter.SetUsername("", userName)
if err != nil {
return userName, userEmail, errors.Wrapf(err, "Failed to set the git username to %s", userName)
}
}
if userEmail == "" {
userEmail = os.Getenv("GIT_AUTHOR_EMAIL")
if userEmail == "" {
userEmail = "jenkins-x@googlegroups.com"
}
err := gitter.SetEmail("", userEmail)
if err != nil {
return userName, userEmail, errors.Wrapf(err, "Failed to set the git email to %s", userEmail)
}
}
return userName, userEmail, nil
}