-
Notifications
You must be signed in to change notification settings - Fork 2
/
checkout.go
73 lines (62 loc) · 1.92 KB
/
checkout.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
package handler
import (
"github.com/go-git/go-git/v5/plumbing"
"github.com/harbourrocks/harbour/pkg/harbourscm/common"
"github.com/harbourrocks/harbour/pkg/harbourscm/github"
"github.com/harbourrocks/harbour/pkg/harbourscm/worker"
"github.com/harbourrocks/harbour/pkg/httphelper"
"github.com/harbourrocks/harbour/pkg/logconfig"
"net/http"
"net/url"
)
type CheckoutRequestModel struct {
SCMId string `json:"scm_id"`
CallbackURL string `json:"callback_url"`
State string `json:"state"`
Commit string `json:"commit"`
}
type CheckoutHandler struct {
Github chan worker.GithubCheckoutTask
}
// Checkout clones the specified repository from a connected source control manager
func (h CheckoutHandler) Checkout(w http.ResponseWriter, r *http.Request) {
log := logconfig.GetLogReq(r)
requestModel := CheckoutRequestModel{}
err := httphelper.ReadRequest(r, w, &requestModel)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
scmProvider, second, third := common.DecomposeRepositoryId(requestModel.SCMId)
if second == "" || third == "" {
log.Warn("Missing second and third of scmId")
w.WriteHeader(http.StatusBadRequest)
return
}
_, err = url.Parse(requestModel.CallbackURL)
if err != nil {
log.WithField("callbackUrl", requestModel.CallbackURL).Warn("Invalid callback url")
w.WriteHeader(http.StatusBadRequest)
return
}
commitHash := plumbing.NewHash(requestModel.Commit)
switch scmProvider {
case "gh":
// test github connection
_, err := github.GenerateTokenForOrganization(r.Context(), second)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
// queue task
h.Github <- worker.GithubCheckoutTask{
OrganizationLogin: second,
Repository: third,
CallbackUrl: requestModel.CallbackURL,
State: requestModel.State,
Commit: commitHash,
Ctx: r.Context(),
}
}
w.WriteHeader(http.StatusAccepted)
}