1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

fix: server error when passing empty heartbeats slice

fix: do not allow to set id for diagnostics inputs
chore: remove authentication for diagnostics endpoint from swagger docs
This commit is contained in:
Ferdinand Mütsch 2022-05-13 16:12:18 +02:00
parent 09d1124794
commit 4bd58789f4
7 changed files with 20 additions and 75 deletions

View File

@ -204,7 +204,7 @@ See our [Swagger API Documentation](https://wakapi.dev/swagger-ui).
### Generating Swagger docs ### Generating Swagger docs
```bash ```bash
$ go get -u github.com/swaggo/swag/cmd/swag $ go install github.com/swaggo/swag/cmd/swag@latest
$ swag init -o static/docs $ swag init -o static/docs
``` ```

View File

@ -36,7 +36,6 @@ func (h *DiagnosticsApiHandler) RegisterRoutes(router *mux.Router) {
// @Tags diagnostics // @Tags diagnostics
// @Accept json // @Accept json
// @Param diagnostics body models.Diagnostics true "A single diagnostics object sent by WakaTime CLI" // @Param diagnostics body models.Diagnostics true "A single diagnostics object sent by WakaTime CLI"
// @Security ApiKeyAuth
// @Success 201 // @Success 201
// @Router /plugins/errors [post] // @Router /plugins/errors [post]
func (h *DiagnosticsApiHandler) Post(w http.ResponseWriter, r *http.Request) { func (h *DiagnosticsApiHandler) Post(w http.ResponseWriter, r *http.Request) {

View File

@ -19,5 +19,6 @@ func NewDiagnosticsService(diagnosticsRepo repositories.IDiagnosticsRepository)
} }
func (srv *DiagnosticsService) Create(diagnostics *models.Diagnostics) (*models.Diagnostics, error) { func (srv *DiagnosticsService) Create(diagnostics *models.Diagnostics) (*models.Diagnostics, error) {
diagnostics.ID = 0
return srv.repository.Insert(diagnostics) return srv.repository.Insert(diagnostics)
} }

View File

@ -54,6 +54,10 @@ func (srv *HeartbeatService) Insert(heartbeat *models.Heartbeat) error {
} }
func (srv *HeartbeatService) InsertBatch(heartbeats []*models.Heartbeat) error { func (srv *HeartbeatService) InsertBatch(heartbeats []*models.Heartbeat) error {
if len(heartbeats) == 0 {
return nil
}
hashes := datastructure.NewSet[string]() hashes := datastructure.NewSet[string]()
// https://github.com/muety/wakapi/issues/139 // https://github.com/muety/wakapi/issues/139

View File

@ -1,22 +1,14 @@
// GENERATED BY THE COMMAND ABOVE; DO NOT EDIT // Package docs GENERATED BY SWAG; DO NOT EDIT
// This file was generated by swaggo/swag // This file was generated by swaggo/swag
package docs package docs
import ( import "github.com/swaggo/swag"
"bytes"
"encoding/json"
"strings"
"github.com/alecthomas/template" const docTemplate = `{
"github.com/swaggo/swag"
)
var doc = `{
"schemes": {{ marshal .Schemes }}, "schemes": {{ marshal .Schemes }},
"swagger": "2.0", "swagger": "2.0",
"info": { "info": {
"description": "{{.Description}}", "description": "{{escape .Description}}",
"title": "{{.Title}}", "title": "{{.Title}}",
"contact": { "contact": {
"name": "Ferdinand Mütsch", "name": "Ferdinand Mütsch",
@ -612,11 +604,6 @@ var doc = `{
}, },
"/plugins/errors": { "/plugins/errors": {
"post": { "post": {
"security": [
{
"ApiKeyAuth": []
}
],
"consumes": [ "consumes": [
"application/json" "application/json"
], ],
@ -1341,9 +1328,6 @@ var doc = `{
"machine_name_id": { "machine_name_id": {
"type": "string" "type": "string"
}, },
"modified_at": {
"type": "string"
},
"project": { "project": {
"type": "string" "type": "string"
}, },
@ -1697,49 +1681,18 @@ var doc = `{
} }
}` }`
type swaggerInfo struct {
Version string
Host string
BasePath string
Schemes []string
Title string
Description string
}
// SwaggerInfo holds exported Swagger Info so clients can modify it // SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo = swaggerInfo{ var SwaggerInfo = &swag.Spec{
Version: "1.0", Version: "1.0",
Host: "", Host: "",
BasePath: "/api", BasePath: "/api",
Schemes: []string{}, Schemes: []string{},
Title: "Wakapi API", Title: "Wakapi API",
Description: "REST API to interact with [Wakapi](https://wakapi.dev)\n\n## Authentication\nSet header `Authorization` to your API Key encoded as Base64 and prefixed with `Basic`\n**Example:** `Basic ODY2NDhkNzQtMTljNS00NTJiLWJhMDEtZmIzZWM3MGQ0YzJmCg==`", Description: "REST API to interact with [Wakapi](https://wakapi.dev)\n\n## Authentication\nSet header `Authorization` to your API Key encoded as Base64 and prefixed with `Basic`\n**Example:** `Basic ODY2NDhkNzQtMTljNS00NTJiLWJhMDEtZmIzZWM3MGQ0YzJmCg==`",
} InfoInstanceName: "swagger",
SwaggerTemplate: docTemplate,
type s struct{}
func (s *s) ReadDoc() string {
sInfo := SwaggerInfo
sInfo.Description = strings.Replace(sInfo.Description, "\n", "\\n", -1)
t, err := template.New("swagger_info").Funcs(template.FuncMap{
"marshal": func(v interface{}) string {
a, _ := json.Marshal(v)
return string(a)
},
}).Parse(doc)
if err != nil {
return doc
}
var tpl bytes.Buffer
if err := t.Execute(&tpl, sInfo); err != nil {
return doc
}
return tpl.String()
} }
func init() { func init() {
swag.Register(swag.Name, &s{}) swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo)
} }

View File

@ -596,11 +596,6 @@
}, },
"/plugins/errors": { "/plugins/errors": {
"post": { "post": {
"security": [
{
"ApiKeyAuth": []
}
],
"consumes": [ "consumes": [
"application/json" "application/json"
], ],
@ -1325,9 +1320,6 @@
"machine_name_id": { "machine_name_id": {
"type": "string" "type": "string"
}, },
"modified_at": {
"type": "string"
},
"project": { "project": {
"type": "string" "type": "string"
}, },

View File

@ -166,8 +166,6 @@ definitions:
type: string type: string
machine_name_id: machine_name_id:
type: string type: string
modified_at:
type: string
project: project:
type: string type: string
time: time:
@ -808,8 +806,6 @@ paths:
responses: responses:
"201": "201":
description: "" description: ""
security:
- ApiKeyAuth: []
summary: Push a new diagnostics object summary: Push a new diagnostics object
tags: tags:
- diagnostics - diagnostics