From 10b6d42347421af8951d09c84b01e42b8e1654e1 Mon Sep 17 00:00:00 2001 From: Peter Dulacka Date: Tue, 31 May 2022 10:20:58 +0200 Subject: [PATCH] Fix Go linter issues, add Latte linter to the CI --- .gitlab-ci.yml | 19 ++++++++++++++----- Beam/Makefile | 2 +- Beam/go/cmd/tracker/Makefile | 4 ++-- Beam/go/cmd/tracker/config.go | 6 +++--- .../cmd/tracker/controller/event_producer.go | 5 +++++ .../controller/kafka_event_producer.go | 2 ++ .../controller/pubsub_event_producer.go | 12 +++++++----- Beam/go/cmd/tracker/main.go | 4 ++-- Mailer/Makefile | 5 ++++- Makefile | 3 +++ 10 files changed, 43 insertions(+), 19 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 1068a3b49..2c88e08ce 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -16,7 +16,7 @@ syntax:7.4: sniffer: stage: test - image: remp/php-ci:7.4.15 + image: remp/php-ci:7.4.29 tags: - docker script: @@ -25,16 +25,25 @@ sniffer: phpstan: stage: test - image: remp/php-ci:7.4.15 + image: remp/php-ci:7.4.29 tags: - docker script: - make composer-install - make phpstan +latte_lint: + stage: test + image: remp/php-ci:7.4.29 + tags: + - docker + script: + - make composer-install + - make latte-lint + tests: stage: test - image: remp/php-ci:7.4.15 + image: remp/php-ci:7.4.29 tags: - docker script: @@ -42,9 +51,9 @@ tests: - make copy-env - make phpunit services: - - name: mysql:5.7.21 + - name: mysql:8.0.29 alias: mysql - - name: redis:3.2 + - name: redis:6.2 alias: redis variables: MYSQL_DATABASE: remp_test diff --git a/Beam/Makefile b/Beam/Makefile index a1fd6f605..7826fb9cd 100644 --- a/Beam/Makefile +++ b/Beam/Makefile @@ -20,7 +20,7 @@ lint: lint-lib @cd $(SUB_SEGMENTS) && make lint vet-lib: - @go vet $$(go list ./... | grep -vE '(/vendor|/cmd)') + @go vet $$(go list go/... | grep -vE '(/vendor|/cmd)') vet: vet-lib @cd $(SUB_TRACKER) && make vet diff --git a/Beam/go/cmd/tracker/Makefile b/Beam/go/cmd/tracker/Makefile index fced732e2..32a5ae028 100644 --- a/Beam/go/cmd/tracker/Makefile +++ b/Beam/go/cmd/tracker/Makefile @@ -48,8 +48,8 @@ install: gen lint: set -e; \ - for package in $$(go list ./... | grep -vE '(/app|/design|/swagger|/client|/tool|/test)'); \ + for package in $$(go list ./... | grep -vE '(/app|/design|/swagger|/client|/tool|/test|/data)'); \ do golint -set_exit_status=true $$package; done vet: - go vet $$(go list ./... | grep -vE '(/app|/design|/swagger|/client|/tool|/test)') \ No newline at end of file + go vet $$(go list ./... | grep -vE '(/app|/design|/swagger|/client|/tool|/test|/data)') \ No newline at end of file diff --git a/Beam/go/cmd/tracker/config.go b/Beam/go/cmd/tracker/config.go index a45f63879..4eed058e3 100644 --- a/Beam/go/cmd/tracker/config.go +++ b/Beam/go/cmd/tracker/config.go @@ -17,10 +17,10 @@ type Config struct { InternalHosts string `envconfig:"internal_hosts" required:"false"` - TimespentLimit int `envconfig:"timespent_limit" required:"false" default:0` + TimespentLimit int `envconfig:"timespent_limit" required:"false" default:"0"` BrokerImpl string `envconfig:"broker_impl" required:"false" default:"kafka"` - PubSubProjectId string `envconfig:"pubsub_project_id" required:"false"` - PubSubTopicId string `envconfig:"pubsub_topic_id" required:"false"` + PubSubProjectID string `envconfig:"pubsub_project_id" required:"false"` + PubSubTopicID string `envconfig:"pubsub_topic_id" required:"false"` } diff --git a/Beam/go/cmd/tracker/controller/event_producer.go b/Beam/go/cmd/tracker/controller/event_producer.go index 417875582..4cf9ee2c9 100644 --- a/Beam/go/cmd/tracker/controller/event_producer.go +++ b/Beam/go/cmd/tracker/controller/event_producer.go @@ -5,8 +5,13 @@ import ( "time" ) +// EventProducer provides a method to produce and publish the tracked events as messages. +// +// The messages are published to the configured message queue implementation. type EventProducer interface { + // Produce generates a message and passes it to the message queue broker. Produce(ctx context.Context, table string, time time.Time, data map[string]any) error + // Close closes any channels or connections that are opened by the producer. Close() error } diff --git a/Beam/go/cmd/tracker/controller/kafka_event_producer.go b/Beam/go/cmd/tracker/controller/kafka_event_producer.go index c8218c3d9..d365473bc 100644 --- a/Beam/go/cmd/tracker/controller/kafka_event_producer.go +++ b/Beam/go/cmd/tracker/controller/kafka_event_producer.go @@ -52,6 +52,7 @@ func NewKafkaEventProducer(brokerAddrs []string, saslConfig *SaslConfig) (*Kafka }, nil } +// Produce generates a message in the influx-data format and passes it to the configured Kafka instance. func (kep KafkaEventProducer) Produce(ctx context.Context, table string, time time.Time, data map[string]any) error { p, err := influxClient.NewPoint(table, nil, data, time) if err != nil { @@ -64,6 +65,7 @@ func (kep KafkaEventProducer) Produce(ctx context.Context, table string, time ti return nil } +// Close closes the connection to the Kafka event producer. func (kep KafkaEventProducer) Close() error { return kep.EventProducer.Close() } diff --git a/Beam/go/cmd/tracker/controller/pubsub_event_producer.go b/Beam/go/cmd/tracker/controller/pubsub_event_producer.go index 4d19c1b6d..a42236b7c 100644 --- a/Beam/go/cmd/tracker/controller/pubsub_event_producer.go +++ b/Beam/go/cmd/tracker/controller/pubsub_event_producer.go @@ -10,26 +10,26 @@ import ( "github.com/pkg/errors" ) -// Implementation of EventProducer +// PubSubEventProducer is a Cloud PubSub implementation of EventProducer interface. type PubSubEventProducer struct { client pubsub.Client topic pubsub.Topic } // NewPubSubEventProducer creates new PubSubEventProducer -func NewPubSubEventProducer(ctx context.Context, projectId string, topicId string) (*PubSubEventProducer, error) { - client, err := pubsub.NewClient(ctx, projectId) +func NewPubSubEventProducer(ctx context.Context, projectID string, topicID string) (*PubSubEventProducer, error) { + client, err := pubsub.NewClient(ctx, projectID) if err != nil { return nil, err } - topic := client.Topic(topicId) + topic := client.Topic(topicID) ok, err := topic.Exists(ctx) if err != nil { return nil, err } if !ok { - return nil, errors.Errorf("PubSub: topic %q doesn't exist.", topicId) + return nil, errors.Errorf("PubSub: topic %q doesn't exist.", topicID) } return &PubSubEventProducer{ @@ -38,6 +38,7 @@ func NewPubSubEventProducer(ctx context.Context, projectId string, topicId strin }, nil } +// Produce generates a message and passes it to the configured Cloud Pub/Sub interface. func (psep PubSubEventProducer) Produce(ctx context.Context, table string, time time.Time, data map[string]any) error { p, err := influxClient.NewPoint(table, nil, data, time) if err != nil { @@ -58,6 +59,7 @@ func (psep PubSubEventProducer) Produce(ctx context.Context, table string, time return nil } +// Close closes the connection with the Cloud Pub/Sub client. func (psep PubSubEventProducer) Close() error { return psep.client.Close() } diff --git a/Beam/go/cmd/tracker/main.go b/Beam/go/cmd/tracker/main.go index edb7d48e4..bebb760bf 100644 --- a/Beam/go/cmd/tracker/main.go +++ b/Beam/go/cmd/tracker/main.go @@ -192,8 +192,8 @@ func newProducer(ctx context.Context, config *Config, service *goa.Service) (con return producer, nil case "pubsub": - service.LogInfo("connecting to pubsub", "projectID", config.PubSubProjectId, "topicID", config.PubSubTopicId) - producer, err := controller.NewPubSubEventProducer(ctx, config.PubSubProjectId, config.PubSubTopicId) + service.LogInfo("connecting to pubsub", "projectID", config.PubSubProjectID, "topicID", config.PubSubTopicID) + producer, err := controller.NewPubSubEventProducer(ctx, config.PubSubProjectID, config.PubSubTopicID) if err != nil { return nil, err } diff --git a/Mailer/Makefile b/Mailer/Makefile index 5f13843df..1ed5e8d12 100644 --- a/Mailer/Makefile +++ b/Mailer/Makefile @@ -31,4 +31,7 @@ syntax: find ${PHP_FOLDERS} -name "*.php" -print0 | xargs -0 -n1 -P8 php -l phpstan: - php vendor/bin/phpstan analyse --configuration=.phpstan.neon --level=1 --memory-limit=1G app tests extensions + php vendor/bin/phpstan analyse --level=1 --memory-limit=1G app tests extensions + +latte-lint: + php vendor/bin/latte-lint app extensions diff --git a/Makefile b/Makefile index 68767f1b5..6658880fe 100644 --- a/Makefile +++ b/Makefile @@ -46,6 +46,9 @@ phpstan: lint: cd $(SUB_BEAM) && make lint +latte-lint: + cd $(SUB_MAILER) && make latte-lint + vet: cd $(SUB_BEAM) && make vet