Add misspell, gocritic, and revive

This commit is contained in:
eikendev 2023-04-01 20:00:58 +02:00
parent 5e640800fe
commit 2c20e42a21
No known key found for this signature in database
GPG key ID: A1BDB1B28C8EF694
30 changed files with 79 additions and 43 deletions

View file

@ -1,3 +1,4 @@
// Package alertmanager provides definitions and functionality related to Alertmanager notifications.
package alertmanager
import (
@ -11,12 +12,14 @@ import (
"github.com/pushbits/server/internal/model"
)
type AlertmanagerHandler struct {
// Handler holds information for processing alerts received via Alertmanager.
type Handler struct {
DP api.NotificationDispatcher
Settings AlertmanagerHandlerSettings
Settings HandlerSettings
}
type AlertmanagerHandlerSettings struct {
// HandlerSettings represents the settings for processing alerts received via Alertmanager.
type HandlerSettings struct {
TitleAnnotation string
MessageAnnotation string
}
@ -33,7 +36,7 @@ type AlertmanagerHandlerSettings struct {
// @Success 200 {object} []model.Notification
// @Failure 500,404,403 ""
// @Router /alert [post]
func (h *AlertmanagerHandler) CreateAlert(ctx *gin.Context) {
func (h *Handler) CreateAlert(ctx *gin.Context) {
application := authentication.GetApplication(ctx)
log.L.Printf("Sending alert notification for application %s.", application.Name)
@ -52,7 +55,7 @@ func (h *AlertmanagerHandler) CreateAlert(ctx *gin.Context) {
}
notification.ID = messageID
notification.UrlEncodedID = url.QueryEscape(messageID)
notification.URLEncodedID = url.QueryEscape(messageID)
notifications[i] = notification
}
ctx.JSON(http.StatusOK, &notifications)

View file

@ -30,7 +30,7 @@ func (h *ApplicationHandler) generateToken(compat bool) string {
func (h *ApplicationHandler) registerApplication(ctx *gin.Context, a *model.Application, u *model.User) error {
log.L.Printf("Registering application %s.", a.Name)
channelID, err := h.DP.RegisterApplication(a.ID, a.Name, a.Token, u.MatrixID)
channelID, err := h.DP.RegisterApplication(a.ID, a.Name, u.MatrixID)
if success := SuccessOrAbort(ctx, http.StatusInternalServerError, err); !success {
return err
}

View file

@ -323,7 +323,7 @@ func TestApi_DeleteApplication(t *testing.T) {
}
// GetApplicationHandler creates and returns an application handler
func getApplicationHandler(c *configuration.Configuration) (*ApplicationHandler, error) {
func getApplicationHandler(_ *configuration.Configuration) (*ApplicationHandler, error) {
dispatcher := &mockups.MockDispatcher{}
return &ApplicationHandler{

View file

@ -1,3 +1,4 @@
// Package api provides definitions and functionality related to the API.
package api
import (
@ -27,7 +28,7 @@ type Database interface {
// The Dispatcher interface for relaying notifications.
type Dispatcher interface {
RegisterApplication(id uint, name, token, user string) (string, error)
RegisterApplication(id uint, name, user string) (string, error)
DeregisterApplication(a *model.Application, u *model.User) error
UpdateApplication(a *model.Application, behavior *configuration.RepairBehavior) error
}

View file

@ -4,18 +4,20 @@ import (
"github.com/gin-gonic/gin"
)
type idInURI struct {
// IDInURI is used to retrieve an ID from a context.
type IDInURI struct {
ID uint `uri:"id" binding:"required"`
}
type messageIdInURI struct {
// messageIDInURI is used to retrieve an message ID from a context.
type messageIDInURI struct {
MessageID string `uri:"messageid" binding:"required"`
}
// RequireIDInURI returns a Gin middleware which requires an ID to be supplied in the URI of the request.
func RequireIDInURI() gin.HandlerFunc {
return func(ctx *gin.Context) {
var requestModel idInURI
var requestModel IDInURI
if err := ctx.BindUri(&requestModel); err != nil {
return
@ -28,7 +30,7 @@ func RequireIDInURI() gin.HandlerFunc {
// RequireMessageIDInURI returns a Gin middleware which requires an messageID to be supplied in the URI of the request.
func RequireMessageIDInURI() gin.HandlerFunc {
return func(ctx *gin.Context) {
var requestModel messageIdInURI
var requestModel messageIDInURI
if err := ctx.BindUri(&requestModel); err != nil {
return

View file

@ -59,7 +59,7 @@ func (h *NotificationHandler) CreateNotification(ctx *gin.Context) {
}
notification.ID = messageID
notification.UrlEncodedID = url.QueryEscape(messageID)
notification.URLEncodedID = url.QueryEscape(messageID)
ctx.JSON(http.StatusOK, &notification)
}

View file

@ -10,6 +10,7 @@ import (
"github.com/gin-gonic/gin"
)
// SuccessOrAbort is a convenience function to write a HTTP status code based on a given error.
func SuccessOrAbort(ctx *gin.Context, code int, err error) bool {
if err != nil {
// If we know the error force error code
@ -24,13 +25,13 @@ func SuccessOrAbort(ctx *gin.Context, code int, err error) bool {
return err == nil
}
func isCurrentUser(ctx *gin.Context, ID uint) bool {
func isCurrentUser(ctx *gin.Context, id uint) bool {
user := authentication.GetUser(ctx)
if user == nil {
return false
}
if user.ID != ID {
if user.ID != id {
ctx.AbortWithError(http.StatusForbidden, errors.New("only owner can delete application"))
return false
}