-
Notifications
You must be signed in to change notification settings - Fork 2
/
event_handler.go
137 lines (123 loc) · 3.55 KB
/
event_handler.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package handlers
import (
"errors"
"net/http"
"github.com/labstack/echo/v4"
"github.com/rs/zerolog"
kerr "github.com/krok-o/krok/errors"
"github.com/krok-o/krok/pkg/krok/providers"
"github.com/krok-o/krok/pkg/models"
)
// EventHandlerDependencies defines the dependencies for the vcs token handler provider.
type EventHandlerDependencies struct {
Logger zerolog.Logger
EventsStorer providers.EventsStorer
}
// EventHandler is a handler taking care of vcs token related api calls.
type EventHandler struct {
EventHandlerDependencies
}
var _ providers.EventHandler = &EventHandler{}
// NewEventHandler creates a new event handler.
func NewEventHandler(deps EventHandlerDependencies) *EventHandler {
return &EventHandler{
EventHandlerDependencies: deps,
}
}
// List handles the list rest event.
// swagger:operation POST /events/{repoid} listEvents
// List events for a repository.
// ---
// produces:
// - application/json
// parameters:
// - name: repoid
// in: path
// description: 'The ID of the repository to list events for.'
// required: true
// type: integer
// format: int
// responses:
// '200':
// schema:
// type: array
// items:
// "$ref": "#/definitions/Event"
// '400':
// description: 'invalid repository id'
// schema:
// "$ref": "#/responses/Message"
// '500':
// description: 'failed to list events'
// schema:
// "$ref": "#/responses/Message"
func (r *EventHandler) List() echo.HandlerFunc {
return func(c echo.Context) error {
opts := &models.ListOptions{}
if err := c.Bind(opts); err != nil {
// if we don't have anything to bind, just ignore opts.
opts = nil
}
n, err := GetParamAsInt("repoid", c)
if err != nil {
apiError := kerr.APIError("invalid id", http.StatusBadRequest, nil)
return c.JSON(http.StatusBadRequest, apiError)
}
ctx := c.Request().Context()
list, err := r.EventsStorer.ListEventsForRepository(ctx, n, opts)
if err != nil {
r.Logger.Debug().Err(err).Msg("Event List failed.")
return c.JSON(http.StatusInternalServerError, kerr.APIError("failed to list events", http.StatusInternalServerError, err))
}
return c.JSON(http.StatusOK, list)
}
}
// Get a specific event.
// swagger:operation GET /event/{id} getEvent
// Get a specific event.
// ---
// produces:
// - application/json
// parameters:
// - name: id
// in: path
// description: 'The ID of the event to retrieve'
// required: true
// type: integer
// format: int
// responses:
// '200':
// schema:
// "$ref": "#/definitions/Event"
// '400':
// description: 'invalid event id'
// schema:
// "$ref": "#/responses/Message"
// '404':
// description: 'event not found'
// schema:
// "$ref": "#/responses/Message"
// '500':
// description: 'failed to get event'
// schema:
// "$ref": "#/responses/Message"
func (r *EventHandler) Get() echo.HandlerFunc {
return func(c echo.Context) error {
n, err := GetParamAsInt("id", c)
if err != nil {
apiError := kerr.APIError("invalid id", http.StatusBadRequest, nil)
return c.JSON(http.StatusBadRequest, apiError)
}
ctx := c.Request().Context()
// Get the event from store.
event, err := r.EventsStorer.GetEvent(ctx, n)
if err != nil {
if errors.Is(err, kerr.ErrNotFound) {
return c.JSON(http.StatusNotFound, kerr.APIError("event not found", http.StatusNotFound, err))
}
apiError := kerr.APIError("failed to get event", http.StatusInternalServerError, err)
return c.JSON(http.StatusInternalServerError, apiError)
}
return c.JSON(http.StatusOK, event)
}
}