forked from drone/go-scm
-
Notifications
You must be signed in to change notification settings - Fork 84
/
user.go
59 lines (50 loc) · 1.47 KB
/
user.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
// Copyright 2017 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package github
import (
"context"
"fmt"
"time"
"github.com/jenkins-x/go-scm/scm"
"github.com/jenkins-x/go-scm/scm/driver/internal/null"
)
type userService struct {
client *wrapper
}
func (s *userService) Find(ctx context.Context) (*scm.User, *scm.Response, error) {
out := new(user)
res, err := s.client.do(ctx, "GET", "user", nil, out)
return convertUser(out), res, err
}
func (s *userService) FindLogin(ctx context.Context, login string) (*scm.User, *scm.Response, error) {
path := fmt.Sprintf("users/%s", login)
out := new(user)
res, err := s.client.do(ctx, "GET", path, nil, out)
return convertUser(out), res, err
}
func (s *userService) FindEmail(ctx context.Context) (string, *scm.Response, error) {
user, res, err := s.Find(ctx)
return user.Email, res, err
}
type user struct {
ID int `json:"id"`
Login string `json:"login"`
Name string `json:"name"`
Email null.String `json:"email"`
Avatar string `json:"avatar_url"`
HTMLURL string `json:"html_url"`
Created time.Time `json:"created_at"`
Updated time.Time `json:"updated_at"`
}
func convertUser(from *user) *scm.User {
return &scm.User{
Avatar: from.Avatar,
Email: from.Email.String,
Login: from.Login,
Name: from.Name,
Link: from.HTMLURL,
Created: from.Created,
Updated: from.Updated,
}
}