forked from harness/harness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
183 lines (166 loc) · 4.25 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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package handler
import (
"encoding/json"
"net/http"
"github.com/drone/drone/plugin/remote"
"github.com/drone/drone/server/datastore"
"github.com/drone/drone/server/sync"
"github.com/drone/drone/shared/model"
"github.com/goji/context"
"github.com/zenazn/goji/web"
)
// GetUserCurrent accepts a request to retrieve the
// currently authenticated user from the datastore
// and return in JSON format.
//
// GET /api/user
//
func GetUserCurrent(c web.C, w http.ResponseWriter, r *http.Request) {
var user = ToUser(c)
if user == nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
// return private data for the currently authenticated
// user, specifically, their auth token.
data := struct {
*model.User
Token string `json:"token"`
}{user, user.Token}
json.NewEncoder(w).Encode(&data)
}
// PutUser accepts a request to update the currently
// authenticated User profile.
//
// PUT /api/user
//
func PutUser(c web.C, w http.ResponseWriter, r *http.Request) {
var ctx = context.FromC(c)
var user = ToUser(c)
if user == nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
// unmarshal the repository from the payload
defer r.Body.Close()
in := model.User{}
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
// update the user email
if len(in.Email) != 0 {
user.SetEmail(in.Email)
}
// update the user full name
if len(in.Name) != 0 {
user.Name = in.Name
}
// update the database
if err := datastore.PutUser(ctx, user); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(user)
}
// GetRepos accepts a request to get the currently
// authenticated user's repository list from the datastore,
// encoded and returned in JSON format.
//
// GET /api/user/repos
//
func GetUserRepos(c web.C, w http.ResponseWriter, r *http.Request) {
var ctx = context.FromC(c)
var user = ToUser(c)
if user == nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
repos, err := datastore.GetRepoList(ctx, user)
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
json.NewEncoder(w).Encode(&repos)
}
// GetUserFeed accepts a request to get the user's latest
// build feed, across all repositories, from the datastore.
// The results are encoded and returned in JSON format.
//
// GET /api/user/feed
//
func GetUserFeed(c web.C, w http.ResponseWriter, r *http.Request) {
var ctx = context.FromC(c)
var user = ToUser(c)
if user == nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
repos, err := datastore.GetCommitListUser(ctx, user)
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
json.NewEncoder(w).Encode(&repos)
}
// GetUserActivity accepts a request to get the user's latest
// build activity, across all repositories, from the datastore.
// The results are encoded and returned in JSON format.
//
// GET /api/user/activity?limit=:limit&offset=:offset
//
func GetUserActivity(c web.C, w http.ResponseWriter, r *http.Request) {
var ctx = context.FromC(c)
var limit = ToLimit(r)
var offset = ToOffset(r)
var user = ToUser(c)
if user == nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
repos, err := datastore.GetCommitListActivity(ctx, user, limit, offset)
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
json.NewEncoder(w).Encode(&repos)
}
// PostUserSync accepts a request to post user sync
//
// POST /api/user/sync
//
func PostUserSync(c web.C, w http.ResponseWriter, r *http.Request) {
var ctx = context.FromC(c)
var user = ToUser(c)
if user == nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
var remote = remote.Lookup(user.Remote)
if remote == nil {
w.WriteHeader(http.StatusNotFound)
return
}
if user.Syncing {
w.WriteHeader(http.StatusConflict)
return
}
// Request a new token and update
user_token, err := remote.GetToken(user)
if user_token != nil {
user.Access = user_token.AccessToken
user.Secret = user_token.RefreshToken
user.TokenExpiry = user_token.Expiry
} else if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
user.Syncing = true
if err := datastore.PutUser(ctx, user); err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
go sync.SyncUser(ctx, user, remote)
w.WriteHeader(http.StatusNoContent)
return
}