Permalink
Newer
100644
108 lines (99 sloc)
2.77 KB
1
// Copyright 2019 Drone IO, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
22
"github.com/drone/drone/handler/api/render"
23
"github.com/drone/drone/handler/api/request"
24
25
"github.com/go-chi/chi"
26
)
27
28
// HandleRetry returns an http.HandlerFunc that processes http
29
// requests to retry and re-execute a build.
30
func HandleRetry(
31
repos core.RepositoryStore,
32
builds core.BuildStore,
33
triggerer core.Triggerer,
34
) http.HandlerFunc {
35
return func(w http.ResponseWriter, r *http.Request) {
36
var (
37
namespace = chi.URLParam(r, "owner")
38
name = chi.URLParam(r, "name")
39
user, _ = request.UserFrom(r.Context())
40
)
41
number, err := strconv.ParseInt(chi.URLParam(r, "number"), 10, 64)
42
if err != nil {
43
render.BadRequest(w, err)
44
return
45
}
46
repo, err := repos.FindName(r.Context(), namespace, name)
47
if err != nil {
48
render.NotFound(w, err)
49
return
50
}
51
prev, err := builds.FindNumber(r.Context(), repo.ID, number)
52
if err != nil {
53
render.NotFound(w, err)
54
return
55
}
56
57
switch prev.Status {
58
case core.StatusBlocked:
59
render.BadRequestf(w, "cannot start a blocked build")
60
return
61
case core.StatusDeclined:
62
render.BadRequestf(w, "cannot start a declined build")
63
return
64
}
65
66
hook := &core.Hook{
67
Trigger: user.Login,
68
Event: prev.Event,
69
Action: prev.Action,
70
Link: prev.Link,
71
Timestamp: prev.Timestamp,
72
Title: prev.Title,
73
Message: prev.Message,
74
Before: prev.Before,
75
After: prev.After,
76
Ref: prev.Ref,
78
Source: prev.Source,
79
Target: prev.Target,
80
Author: prev.Author,
81
AuthorName: prev.AuthorName,
82
AuthorEmail: prev.AuthorEmail,
83
AuthorAvatar: prev.AuthorAvatar,
87
Sender: prev.Sender,
88
Params: map[string]string{},
89
}
90
91
for key, value := range r.URL.Query() {
92
if key == "access_token" {
93
continue
94
}
95
if len(value) == 0 {
96
continue
97
}
98
hook.Params[key] = value[0]
99
}
100
101
result, err := triggerer.Trigger(r.Context(), repo, hook)
102
if err != nil {
103
render.InternalError(w, err)
104
} else {
105
render.JSON(w, result, 200)
106
}
107
}
108
}