forked from harness/harness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repos.go
390 lines (324 loc) · 10.3 KB
/
repos.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package handler
import (
"fmt"
"net/http"
"github.com/drone/drone/pkg/channel"
"github.com/drone/drone/pkg/database"
. "github.com/drone/drone/pkg/model"
"github.com/drone/go-bitbucket/bitbucket"
"github.com/drone/go-github/github"
"launchpad.net/goyaml"
)
// Display a Repository dashboard.
func RepoDashboard(w http.ResponseWriter, r *http.Request, u *User, repo *Repo) error {
branch := r.FormValue("branch")
// get a list of all branches
branches, err := database.ListBranches(repo.ID)
if err != nil {
return err
}
// if no branch is provided then we'll
// want to use a default value.
if len(branch) == 0 {
branch = repo.DefaultBranch()
}
// get a list of recent commits for the
// repository and specific branch
commits, err := database.ListCommits(repo.ID, branch)
if err != nil {
return err
}
// get a token that can be exchanged with the
// websocket handler to authorize listening
// for a stream of changes for this repository
token := channel.Create(repo.Slug)
data := struct {
User *User
Repo *Repo
Branches []*Commit
Commits []*Commit
Branch string
Token string
}{u, repo, branches, commits, branch, token}
return RenderTemplate(w, "repo_dashboard.html", &data)
}
func RepoAddGithub(w http.ResponseWriter, r *http.Request, u *User) error {
settings := database.SettingsMust()
teams, err := database.ListTeams(u.ID)
if err != nil {
return err
}
data := struct {
User *User
Teams []*Team
Settings *Settings
}{u, teams, settings}
// if the user hasn't linked their GitHub account
// render a different template
if len(u.GithubToken) == 0 {
return RenderTemplate(w, "github_link.html", &data)
}
// otherwise display the template for adding
// a new GitHub repository.
return RenderTemplate(w, "github_add.html", &data)
}
func RepoAddBitbucket(w http.ResponseWriter, r *http.Request, u *User) error {
settings := database.SettingsMust()
teams, err := database.ListTeams(u.ID)
if err != nil {
return err
}
data := struct {
User *User
Teams []*Team
Settings *Settings
}{u, teams, settings}
// if the user hasn't linked their Bitbucket account
// render a different template
if len(u.BitbucketToken) == 0 {
return RenderTemplate(w, "bitbucket_link.html", &data)
}
// otherwise display the template for adding
// a new Bitbucket repository.
return RenderTemplate(w, "bitbucket_add.html", &data)
}
func RepoCreateGithub(w http.ResponseWriter, r *http.Request, u *User) error {
teamName := r.FormValue("team")
owner := r.FormValue("owner")
name := r.FormValue("name")
// get the github settings from the database
settings := database.SettingsMust()
// create the GitHub client
client := github.New(u.GithubToken)
client.ApiUrl = settings.GitHubApiUrl
githubRepo, err := client.Repos.Find(owner, name)
if err != nil {
return fmt.Errorf("Unable to find GitHub repository %s/%s.", owner, name)
}
repo, err := NewGitHubRepo(settings.GitHubDomain, owner, name, githubRepo.Private)
if err != nil {
return err
}
repo.UserID = u.ID
repo.Private = githubRepo.Private
// if the user chose to assign to a team account
// we need to retrieve the team, verify the user
// has access, and then set the team id.
if len(teamName) > 0 {
team, err := database.GetTeamSlug(teamName)
if err != nil {
return fmt.Errorf("Unable to find Team %s.", teamName)
}
// user must be an admin member of the team
if ok, _ := database.IsMemberAdmin(u.ID, team.ID); !ok {
return fmt.Errorf("Invalid permission to access Team %s.", teamName)
}
repo.TeamID = team.ID
}
// if the repository is private we'll need
// to upload a github key to the repository
if repo.Private {
// name the key
keyName := fmt.Sprintf("%s@%s", repo.Owner, settings.Domain)
// create the github key, or update if one already exists
_, err := client.RepoKeys.CreateUpdate(owner, name, repo.PublicKey, keyName)
if err != nil {
return fmt.Errorf("Unable to add Public Key to your GitHub repository.")
}
} else {
}
// create a hook so that we get notified when code
// is pushed to the repository and can execute a build.
link := fmt.Sprintf("%s://%s/hook/github.com?id=%s", settings.Scheme, settings.Domain, repo.Slug)
// add the hook
if _, err := client.Hooks.CreateUpdate(owner, name, link); err != nil {
return fmt.Errorf("Unable to add Hook to your GitHub repository.")
}
// Save to the database
if err := database.SaveRepo(repo); err != nil {
return fmt.Errorf("Error saving repository to the database. %s", err)
}
return RenderText(w, http.StatusText(http.StatusOK), http.StatusOK)
}
func RepoCreateBitbucket(w http.ResponseWriter, r *http.Request, u *User) error {
teamName := r.FormValue("team")
owner := r.FormValue("owner")
name := r.FormValue("name")
// get the bitbucket settings from the database
settings := database.SettingsMust()
// create the Bitbucket client
client := bitbucket.New(
settings.BitbucketKey,
settings.BitbucketSecret,
u.BitbucketToken,
u.BitbucketSecret,
)
bitbucketRepo, err := client.Repos.Find(owner, name)
if err != nil {
return fmt.Errorf("Unable to find Bitbucket repository %s/%s.", owner, name)
}
repo, err := NewBitbucketRepo(owner, name, bitbucketRepo.Private)
if err != nil {
return err
}
repo.UserID = u.ID
repo.Private = bitbucketRepo.Private
// if the user chose to assign to a team account
// we need to retrieve the team, verify the user
// has access, and then set the team id.
if len(teamName) > 0 {
team, err := database.GetTeamSlug(teamName)
if err != nil {
return fmt.Errorf("Unable to find Team %s.", teamName)
}
// user must be an admin member of the team
if ok, _ := database.IsMemberAdmin(u.ID, team.ID); !ok {
return fmt.Errorf("Invalid permission to access Team %s.", teamName)
}
repo.TeamID = team.ID
}
// if the repository is private we'll need
// to upload a bitbucket key to the repository
if repo.Private {
// name the key
keyName := fmt.Sprintf("%s@%s", repo.Owner, settings.Domain)
// create the bitbucket key, or update if one already exists
_, err := client.RepoKeys.CreateUpdate(owner, name, repo.PublicKey, keyName)
if err != nil {
return fmt.Errorf("Unable to add Public Key to your Bitbucket repository: %s", err)
}
} else {
}
// create a hook so that we get notified when code
// is pushed to the repository and can execute a build.
link := fmt.Sprintf("%s://%s/hook/bitbucket.org?id=%s", settings.Scheme, settings.Domain, repo.Slug)
// add the hook
if _, err := client.Brokers.CreateUpdate(owner, name, link, bitbucket.BrokerTypePost); err != nil {
return fmt.Errorf("Unable to add Hook to your Bitbucket repository. %s", err.Error())
}
// Save to the database
if err := database.SaveRepo(repo); err != nil {
return fmt.Errorf("Error saving repository to the database. %s", err)
}
return RenderText(w, http.StatusText(http.StatusOK), http.StatusOK)
}
// Repository Settings
func RepoSettingsForm(w http.ResponseWriter, r *http.Request, u *User, repo *Repo) error {
// get the list of teams
teams, err := database.ListTeams(u.ID)
if err != nil {
return err
}
data := struct {
Repo *Repo
User *User
Teams []*Team
Owner *User
Team *Team
}{Repo: repo, User: u, Teams: teams}
// get the repo owner
if repo.TeamID > 0 {
data.Team, err = database.GetTeam(repo.TeamID)
if err != nil {
return err
}
}
// get the team owner
data.Owner, err = database.GetUser(repo.UserID)
if err != nil {
return err
}
return RenderTemplate(w, "repo_settings.html", &data)
}
// Repository Params (YAML parameters) Form
func RepoParamsForm(w http.ResponseWriter, r *http.Request, u *User, repo *Repo) error {
data := struct {
Repo *Repo
User *User
Textarea string
}{repo, u, ""}
if repo.Params != nil && len(repo.Params) != 0 {
raw, _ := goyaml.Marshal(&repo.Params)
data.Textarea = string(raw)
}
return RenderTemplate(w, "repo_params.html", &data)
}
func RepoBadges(w http.ResponseWriter, r *http.Request, u *User, repo *Repo) error {
// hostname from settings
hostname := database.SettingsMust().URL().String()
data := struct {
Repo *Repo
User *User
Host string
}{repo, u, hostname}
return RenderTemplate(w, "repo_badges.html", &data)
}
func RepoKeys(w http.ResponseWriter, r *http.Request, u *User, repo *Repo) error {
data := struct {
Repo *Repo
User *User
}{repo, u}
return RenderTemplate(w, "repo_keys.html", &data)
}
// Updates an existing repository.
func RepoUpdate(w http.ResponseWriter, r *http.Request, u *User, repo *Repo) error {
switch r.FormValue("action") {
case "params":
repo.Params = map[string]string{}
if err := goyaml.Unmarshal([]byte(r.FormValue("params")), &repo.Params); err != nil {
return err
}
default:
repo.URL = r.FormValue("URL")
repo.Disabled = len(r.FormValue("Disabled")) == 0
repo.DisabledPullRequest = len(r.FormValue("DisabledPullRequest")) == 0
repo.Private = len(r.FormValue("Private")) > 0
repo.Privileged = u.Admin && len(r.FormValue("Privileged")) > 0
// value of "" indicates the currently authenticated user
// should be set as the administrator.
if len(r.FormValue("Owner")) == 0 {
repo.UserID = u.ID
repo.TeamID = 0
} else {
// else the user has chosen a team
team, err := database.GetTeamSlug(r.FormValue("Owner"))
if err != nil {
return err
}
// verify the user is a member of the team
if member, _ := database.IsMemberAdmin(u.ID, team.ID); !member {
return fmt.Errorf("Forbidden")
}
// set the team ID
repo.TeamID = team.ID
}
}
// save the page
if err := database.SaveRepo(repo); err != nil {
return err
}
http.Redirect(w, r, r.URL.Path, http.StatusSeeOther)
return nil
}
// Deletes a specific repository.
func RepoDeleteForm(w http.ResponseWriter, r *http.Request, u *User, repo *Repo) error {
data := struct {
Repo *Repo
User *User
}{repo, u}
return RenderTemplate(w, "repo_delete.html", &data)
}
// Deletes a specific repository.
func RepoDelete(w http.ResponseWriter, r *http.Request, u *User, repo *Repo) error {
// the user must confirm their password before deleting
password := r.FormValue("password")
if err := u.ComparePassword(password); err != nil {
return RenderError(w, err, http.StatusBadRequest)
}
// delete the repo
if err := database.DeleteRepo(repo.ID); err != nil {
return err
}
http.Redirect(w, r, "/dashboard", http.StatusSeeOther)
return nil
}