summaryrefslogtreecommitdiffstats
path: root/model/gitlab.go
blob: 9adcac1894660cf8f7a5acbe01aa09c2bac23b01 (plain)
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
// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
// See License.txt for license information.

package model

import (
	"encoding/json"
	"io"
	"strconv"
	"strings"
)

const (
	USER_AUTH_SERVICE_GITLAB = "gitlab"
)

type GitLabUser struct {
	Id       int64  `json:"id"`
	Username string `json:"username"`
	Email    string `json:"email"`
	Name     string `json:"name"`
}

func UserFromGitLabUser(glu *GitLabUser) *User {
	user := &User{}
	user.Username = glu.Username
	splitName := strings.Split(glu.Name, " ")
	if len(splitName) == 2 {
		user.FirstName = splitName[0]
		user.LastName = splitName[1]
	} else if len(splitName) >= 2 {
		user.FirstName = splitName[0]
		user.LastName = strings.Join(splitName[1:], " ")
	} else {
		user.FirstName = glu.Name
	}
	user.Email = glu.Email
	user.AuthData = strconv.FormatInt(glu.Id, 10)
	user.AuthService = USER_AUTH_SERVICE_GITLAB

	return user
}

func GitLabUserFromJson(data io.Reader) *GitLabUser {
	decoder := json.NewDecoder(data)
	var glu GitLabUser
	err := decoder.Decode(&glu)
	if err == nil {
		return &glu
	} else {
		return nil
	}
}

func (glu *GitLabUser) GetAuthData() string {
	return strconv.FormatInt(glu.Id, 10)
}