-
Notifications
You must be signed in to change notification settings - Fork 2
/
vcs_token_handler.go
72 lines (62 loc) · 2.08 KB
/
vcs_token_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
package handlers
import (
"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"
)
// VCSTokenHandlerDependencies defines the dependencies for the vcs token handler provider.
type VCSTokenHandlerDependencies struct {
Logger zerolog.Logger
TokenProvider providers.PlatformTokenProvider
}
// VCSTokenHandler is a handler taking care of vcs token related api calls.
type VCSTokenHandler struct {
VCSTokenHandlerDependencies
}
var _ providers.VCSTokenHandler = &VCSTokenHandler{}
// NewVCSTokenHandler creates a new vcs token handler.
func NewVCSTokenHandler(deps VCSTokenHandlerDependencies) *VCSTokenHandler {
return &VCSTokenHandler{
VCSTokenHandlerDependencies: deps,
}
}
// Create handles the Create rest event.
// swagger:operation POST /vcs-token createVcsToken
// Create a new token for a platform like Github, Gitlab, Gitea...
// ---
// consumes:
// - application/json
// parameters:
// - name: secret
// in: body
// required: true
// schema:
// "$ref": "#/definitions/VCSToken"
// responses:
// '200':
// description: 'OK setting successfully create'
// '400':
// description: 'invalid json payload'
// schema:
// "$ref": "#/responses/Message"
// '500':
// description: 'failed to create secret'
// schema:
// "$ref": "#/responses/Message"
func (r *VCSTokenHandler) Create() echo.HandlerFunc {
return func(c echo.Context) error {
vcsToken := &models.VCSToken{}
if err := c.Bind(vcsToken); err != nil {
r.Logger.Debug().Err(err).Msg("Failed to bind vcs token.")
return c.JSON(http.StatusBadRequest, kerr.APIError("failed to bind vcs token", http.StatusBadRequest, err))
}
if err := r.TokenProvider.SaveTokenForPlatform(vcsToken.Token, vcsToken.VCS); err != nil {
r.Logger.Debug().Err(err).Msg("VCS token creation failed.")
return c.JSON(http.StatusInternalServerError, kerr.APIError("VCS token creation failed", http.StatusInternalServerError, err))
}
return c.NoContent(http.StatusCreated)
}
}