Permalink
Newer
100644
116 lines (104 sloc)
2.94 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.
14
15
package builds
16
17
import (
18
"net/http"
19
20
"github.com/drone/drone/core"
21
"github.com/drone/drone/handler/api/render"
22
"github.com/drone/drone/handler/api/request"
23
"github.com/drone/go-scm/scm"
24
25
"github.com/go-chi/chi"
26
)
27
28
// HandleCreate returns an http.HandlerFunc that processes http
29
// requests to create a build for the specified commit.
30
func HandleCreate(
32
repos core.RepositoryStore,
33
commits core.CommitService,
34
triggerer core.Triggerer,
35
) http.HandlerFunc {
36
return func(w http.ResponseWriter, r *http.Request) {
37
var (
38
ctx = r.Context()
39
namespace = chi.URLParam(r, "owner")
40
name = chi.URLParam(r, "name")
46
repo, err := repos.FindName(ctx, namespace, name)
47
if err != nil {
48
render.NotFound(w, err)
49
return
50
}
52
owner, err := users.Find(ctx, repo.UserID)
53
if err != nil {
54
render.NotFound(w, err)
55
return
56
}
57
58
// if the user does not provide a branch, assume the
59
// default repository branch.
60
if branch == "" {
61
branch = repo.Branch
62
}
63
// expand the branch to a git reference.
64
ref := scm.ExpandRef(branch, "refs/heads")
65
71
}
72
if err != nil {
73
render.NotFound(w, err)
74
return
75
}
76
77
hook := &core.Hook{
78
Trigger: user.Login,
80
Link: commit.Link,
81
Timestamp: commit.Author.Date,
82
Title: "", // we expect this to be empty.
83
Message: commit.Message,
89
Author: commit.Author.Login,
90
AuthorName: commit.Author.Name,
91
AuthorEmail: commit.Author.Email,
92
AuthorAvatar: commit.Author.Avatar,
94
Params: map[string]string{},
95
}
96
97
for key, value := range r.URL.Query() {
98
if key == "access_token" ||
99
key == "commit" ||
100
key == "branch" {
101
continue
102
}
103
if len(value) == 0 {
104
continue
105
}
106
hook.Params[key] = value[0]