-
Notifications
You must be signed in to change notification settings - Fork 786
/
user_details.go
98 lines (78 loc) · 2.11 KB
/
user_details.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
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
96
97
98
package users
import (
"fmt"
"github.com/jenkins-x/jx/v2/pkg/kube/naming"
"github.com/jenkins-x/jx-logging/pkg/log"
v1 "github.com/jenkins-x/jx/v2/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx/v2/pkg/client/clientset/versioned"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type UserDetailService struct {
jxClient versioned.Interface
namespace string
}
func NewUserDetailService(jxClient versioned.Interface, namespace string) UserDetailService {
return UserDetailService{
jxClient: jxClient,
namespace: namespace,
}
}
func (this *UserDetailService) CreateOrUpdateUser(u *v1.UserDetails) error {
if u == nil || u.Login == "" {
return fmt.Errorf("Unable to get or create user, nil or missing login")
}
log.Logger().Infof("CreateOrUpdateUser: %s <%s>", u.Login, u.Email)
id := naming.ToValidName(u.Login)
// check for an existing user by email
user, err := this.jxClient.JenkinsV1().Users(this.namespace).Get(id, metav1.GetOptions{})
if err != nil {
// we get an error when not found
log.Logger().Info("Unable to find user: " + id + " -- " + err.Error())
}
if user != nil && err == nil {
changed := false
existing := &user.User
if existing.Email != u.Email {
existing.Email = u.Email
changed = true
}
if existing.AvatarURL != u.AvatarURL {
existing.AvatarURL = u.AvatarURL
changed = true
}
if existing.URL != u.URL {
existing.URL = u.URL
changed = true
}
if existing.Name != u.Name {
existing.Name = u.Name
changed = true
}
if existing.Login != u.Login {
existing.Login = u.Login
changed = true
}
if changed {
log.Logger().Infof("Updating modified user: %s", existing.Email)
_, err = this.jxClient.JenkinsV1().Users(this.namespace).Update(user)
if err != nil {
return err
}
} else {
log.Logger().Infof("Existing user found: %s", existing.Email)
}
} else {
user = &v1.User{
ObjectMeta: metav1.ObjectMeta{
Name: id,
},
Spec: *u,
}
log.Logger().Infof("Adding missing user: %s", id)
_, err = this.jxClient.JenkinsV1().Users(this.namespace).Create(user)
if err != nil {
return err
}
}
return nil
}