forked from harness/harness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
135 lines (122 loc) · 3.41 KB
/
update.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
// Copyright 2019 Drone IO, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package repos
import (
"encoding/json"
"net/http"
"github.com/drone/drone/core"
"github.com/drone/drone/handler/api/render"
"github.com/drone/drone/handler/api/request"
"github.com/drone/drone/logger"
"github.com/go-chi/chi"
)
type (
repositoryInput struct {
Visibility *string `json:"visibility"`
Config *string `json:"config_path"`
Trusted *bool `json:"trusted"`
Protected *bool `json:"protected"`
IgnoreForks *bool `json:"ignore_forks"`
IgnorePulls *bool `json:"ignore_pull_requests"`
CancelPulls *bool `json:"auto_cancel_pull_requests"`
CancelPush *bool `json:"auto_cancel_pushes"`
Timeout *int64 `json:"timeout"`
Counter *int64 `json:"counter"`
}
)
// HandleUpdate returns an http.HandlerFunc that processes http
// requests to update the repository details.
func HandleUpdate(repos core.RepositoryStore) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var (
owner = chi.URLParam(r, "owner")
name = chi.URLParam(r, "name")
slug = owner + "/" + name
)
user, _ := request.UserFrom(r.Context())
repo, err := repos.FindName(r.Context(), owner, name)
if err != nil {
render.NotFound(w, err)
logger.FromRequest(r).
WithError(err).
WithField("repository", slug).
Debugln("api: repository not found")
return
}
in := new(repositoryInput)
err = json.NewDecoder(r.Body).Decode(in)
if err != nil {
render.BadRequest(w, err)
logger.FromRequest(r).
WithError(err).
WithField("repository", slug).
Debugln("api: cannot unmarshal json input")
return
}
if in.Visibility != nil {
repo.Visibility = *in.Visibility
}
if in.Config != nil {
repo.Config = *in.Config
}
if in.Protected != nil {
repo.Protected = *in.Protected
}
if in.IgnoreForks != nil {
repo.IgnoreForks = *in.IgnoreForks
}
if in.IgnorePulls != nil {
repo.IgnorePulls = *in.IgnorePulls
}
if in.CancelPulls != nil {
repo.CancelPulls = *in.CancelPulls
}
if in.CancelPush != nil {
repo.CancelPush = *in.CancelPush
}
//
// system administrator only
//
if user != nil && user.Admin {
if in.Trusted != nil {
repo.Trusted = *in.Trusted
}
if in.Timeout != nil {
repo.Timeout = *in.Timeout
}
if in.Counter != nil {
repo.Counter = *in.Counter
}
}
// // right now the only repository field that a user
// // can update is the visibility field.
// if govalidator.IsIn(in.Visibility,
// core.VisibilityInternal,
// core.VisibilityPrivate,
// core.VisibilityPublic,
// ) {
// repo.Visibility = in.Visibility
// }
err = repos.Update(r.Context(), repo)
if err != nil {
render.InternalError(w, err)
logger.FromRequest(r).
WithError(err).
WithField("repository", slug).
Warnln("api: cannot update repository")
return
}
render.JSON(w, repo, 200)
}
}