-
Notifications
You must be signed in to change notification settings - Fork 414
/
hook.go
110 lines (93 loc) · 3.31 KB
/
hook.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
package api
import (
"context"
"fmt"
"net/http"
"strconv"
"time"
"github.com/gorilla/mux"
"github.com/rockbears/log"
"github.com/ovh/cds/engine/api/project"
"github.com/ovh/cds/engine/api/repositoriesmanager"
"github.com/ovh/cds/engine/api/workflow"
"github.com/ovh/cds/engine/service"
"github.com/ovh/cds/sdk"
)
func (api *API) getHookPollingVCSEvents() service.Handler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
vars := mux.Vars(r)
uuid := vars["uuid"]
vcsServerParam := vars["vcsServer"]
lastExec := time.Now()
workflowID, err := requestVarInt(r, "workflowID")
if err != nil {
return err
}
if r.Header.Get("X-CDS-Last-Execution") != "" {
if ts, err := strconv.ParseInt(r.Header.Get("X-CDS-Last-Execution"), 10, 64); err == nil {
lastExec = time.Unix(0, ts)
}
}
h, err := workflow.LoadHookByUUID(api.mustDB(), uuid)
if err != nil {
return err
}
proj, err := project.Load(ctx, api.mustDB(), h.Config[sdk.HookConfigProject].Value, nil)
if err != nil {
return err
}
tx, err := api.mustDB().Begin()
if err != nil {
return sdk.WithStack(err)
}
defer tx.Rollback() // nolint
//get the client for the repositories manager
client, err := repositoriesmanager.AuthorizedClient(ctx, tx, api.Cache, proj.Key, vcsServerParam)
if err != nil {
return err
}
if err := tx.Commit(); err != nil {
return sdk.WithStack(err)
}
//Check if the polling if disabled
if info, err := repositoriesmanager.GetPollingInfos(ctx, client, *proj); err != nil {
return sdk.WrapError(err, "cannot check if polling is enabled")
} else if info.PollingDisabled || !info.PollingSupported {
log.Info(ctx, "getHookPollingVCSEvents> %s polling is disabled", vcsServerParam)
return service.WriteJSON(w, nil, http.StatusOK)
}
events, pollingDelay, err := client.GetEvents(ctx, h.Config["repoFullName"].Value, lastExec)
if err != nil && err.Error() != "No new events" {
return sdk.WrapError(err, "Unable to get events for %s %s", proj.Key, vcsServerParam)
}
pushEvents, err := client.PushEvents(ctx, h.Config["repoFullName"].Value, events)
if err != nil {
return sdk.WithStack(err)
}
pullRequestEvents, err := client.PullRequestEvents(ctx, h.Config["repoFullName"].Value, events)
if err != nil {
return sdk.WithStack(err)
}
repoEvents := sdk.RepositoryEvents{}
for _, pushEvent := range pushEvents {
exist, errB := workflow.RunExist(api.mustDB(), h.Config[sdk.HookConfigProject].Value, workflowID, pushEvent.Commit.Hash)
if errB != nil {
return sdk.WrapError(errB, "getHookPollingVCSEvents> Cannot check existing builds for push events")
}
if !exist {
repoEvents.PushEvents = append(repoEvents.PushEvents, pushEvent)
}
}
for _, pullRequestEvent := range pullRequestEvents {
exist, errB := workflow.RunExist(api.mustDB(), h.Config[sdk.HookConfigProject].Value, workflowID, pullRequestEvent.Head.Commit.Hash)
if errB != nil {
return sdk.WrapError(errB, "getHookPollingVCSEvents> Cannot check existing builds for pull request events")
}
if !exist {
repoEvents.PullRequestEvents = append(repoEvents.PullRequestEvents, pullRequestEvent)
}
}
w.Header().Add("X-CDS-Poll-Interval", fmt.Sprintf("%.0f", pollingDelay.Seconds()))
return service.WriteJSON(w, repoEvents, http.StatusOK)
}
}