forked from harness/gitness
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.go
109 lines (98 loc) · 2.81 KB
/
create.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
// 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 builds
import (
"net/http"
"github.com/drone/drone/core"
"github.com/drone/drone/handler/api/render"
"github.com/drone/drone/handler/api/request"
"github.com/drone/go-scm/scm"
"github.com/go-chi/chi"
)
// HandleCreate returns an http.HandlerFunc that processes http
// requests to create a build for the specified commit.
func HandleCreate(
repos core.RepositoryStore,
commits core.CommitService,
triggerer core.Triggerer,
) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var (
ctx = r.Context()
namespace = chi.URLParam(r, "owner")
name = chi.URLParam(r, "name")
sha = r.FormValue("commit")
branch = r.FormValue("branch")
user, _ = request.UserFrom(ctx)
)
repo, err := repos.FindName(ctx, namespace, name)
if err != nil {
render.NotFound(w, err)
return
}
// if the user does not provide a branch, assume the
// default repository branch.
if branch == "" {
branch = repo.Branch
}
// expand the branch to a git reference.
ref := scm.ExpandRef(branch, "refs/heads")
var commit *core.Commit
if sha != "" {
commit, err = commits.Find(ctx, user, repo.Slug, sha)
} else {
commit, err = commits.FindRef(ctx, user, repo.Slug, ref)
}
if err != nil {
render.NotFound(w, err)
return
}
hook := &core.Hook{
Trigger: user.Login,
Event: core.EventCustom,
Link: commit.Link,
Timestamp: commit.Author.Date,
Title: "", // we expect this to be empty.
Message: commit.Message,
Before: commit.Sha,
After: commit.Sha,
Ref: ref,
Source: branch,
Target: branch,
Author: commit.Author.Login,
AuthorName: commit.Author.Name,
AuthorEmail: commit.Author.Email,
AuthorAvatar: commit.Author.Avatar,
Sender: user.Login,
Params: map[string]string{},
}
for key, value := range r.URL.Query() {
if key == "access_token" ||
key == "commit" ||
key == "branch" {
continue
}
if len(value) == 0 {
continue
}
hook.Params[key] = value[0]
}
result, err := triggerer.Trigger(r.Context(), repo, hook)
if err != nil {
render.InternalError(w, err)
} else {
render.JSON(w, result, 200)
}
}
}