Skip to content

Commit

Permalink
UPPSF-1150 Added golangci linter and fixed linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Tsvetan Dimitrov committed Feb 20, 2020
1 parent f952bbf commit 5dd7ea8
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 98 deletions.
5 changes: 5 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ jobs:
command: |
GO111MODULE=off go get -u github.com/mattn/goveralls
GO111MODULE=off go get -u github.com/jstemmer/go-junit-report
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.18.0
wget https://raw.githubusercontent.com/Financial-Times/upp-coding-standard/v1.0.0/golangci-config/.golangci.yml
- run:
name: Make result folders
command: |
Expand All @@ -22,6 +24,9 @@ jobs:
- run:
name: Go build
command: go build -mod=readonly -v ./cmd/content-rw-elasticsearch
- run:
name: Run Linters
command: golangci-lint run --config=.golangci.yml --new-from-rev=master
- run:
name: Run tests
command: go test -race -cover -coverprofile=${CIRCLE_COVERAGE_REPORT}/coverage.out ./... | go-junit-report > ${CIRCLE_TEST_REPORTS}/junit.xml
Expand Down
8 changes: 5 additions & 3 deletions cmd/content-rw-elasticsearch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (
"github.com/Financial-Times/content-rw-elasticsearch/pkg/http"
"github.com/Financial-Times/content-rw-elasticsearch/pkg/mapper"
"github.com/Financial-Times/content-rw-elasticsearch/pkg/message"
"github.com/Financial-Times/go-logger/v2"
"github.com/Financial-Times/message-queue-gonsumer/consumer"
"github.com/jawher/mow.cli"

nethttp "net/http"
"os"
"sync"
"time"

"github.com/Financial-Times/go-logger/v2"
"github.com/Financial-Times/message-queue-gonsumer/consumer"
"github.com/jawher/mow.cli"
)

func main() {
Expand Down
10 changes: 5 additions & 5 deletions pkg/concept/concordance.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,16 @@ type Client interface {
}

type ConcordanceAPIService struct {
ConcordanceApiBaseURL string
ConcordanceAPIBaseURL string
Client Client
}

func NewConcordanceAPIService(concordanceApiBaseURL string, c Client) *ConcordanceAPIService {
return &ConcordanceAPIService{ConcordanceApiBaseURL: concordanceApiBaseURL, Client: c}
func NewConcordanceAPIService(concordanceAPIBaseURL string, c Client) *ConcordanceAPIService {
return &ConcordanceAPIService{ConcordanceAPIBaseURL: concordanceAPIBaseURL, Client: c}
}

func (c *ConcordanceAPIService) GetConcepts(tid string, ids []string) (map[string]Model, error) {
req, err := http.NewRequest(http.MethodGet, c.ConcordanceApiBaseURL+concordancesEndpoint, nil)
req, err := http.NewRequest(http.MethodGet, c.ConcordanceAPIBaseURL+concordancesEndpoint, nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -119,7 +119,7 @@ func TransformToConceptModel(concordancesResp ConcordancesResponse) map[string]M
}

func (c *ConcordanceAPIService) HealthCheck() (string, error) {
req, err := http.NewRequest(http.MethodGet, c.ConcordanceApiBaseURL+"/__gtg", nil)
req, err := http.NewRequest(http.MethodGet, c.ConcordanceAPIBaseURL+"/__gtg", nil)
if err != nil {
return "", err
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package config

import (
"fmt"
"github.com/Financial-Times/content-rw-elasticsearch/pkg/schema"
"github.com/spf13/viper"
"log"
"os"
"path"
"strings"

"github.com/Financial-Times/content-rw-elasticsearch/pkg/schema"
"github.com/spf13/viper"
)

const (
Expand Down
3 changes: 2 additions & 1 deletion pkg/es/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/Financial-Times/content-rw-elasticsearch/pkg/config"
"io/ioutil"
"reflect"
"sync"

"github.com/Financial-Times/content-rw-elasticsearch/pkg/config"

"gopkg.in/olivere/elastic.v2"
)

Expand Down
7 changes: 4 additions & 3 deletions pkg/health/healthcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package health
import (
"encoding/json"
"fmt"
status "github.com/Financial-Times/service-status-go/httphandlers"
"net/http"

status "github.com/Financial-Times/service-status-go/httphandlers"

"github.com/Financial-Times/content-rw-elasticsearch/pkg/concept"
"github.com/Financial-Times/content-rw-elasticsearch/pkg/es"
fthealth "github.com/Financial-Times/go-fthealth/v1_1"
Expand All @@ -29,11 +30,11 @@ type Service struct {
log *logger.UPPLogger
}

func NewHealthService(config *consumer.QueueConfig, esHealthService es.HealthStatus, client *http.Client, concordanceApi *concept.ConcordanceAPIService, appSystemCode string, log *logger.UPPLogger) *Service {
func NewHealthService(config *consumer.QueueConfig, esHealthService es.HealthStatus, client *http.Client, concordanceAPI *concept.ConcordanceAPIService, appSystemCode string, log *logger.UPPLogger) *Service {
consumerInstance := consumer.NewConsumer(*config, func(m consumer.Message) {}, client)
service := &Service{
ESHealthService: esHealthService,
ConcordanceAPI: concordanceApi,
ConcordanceAPI: concordanceAPI,
ConsumerInstance: consumerInstance,
HTTPClient: client,
AppSystemCode: appSystemCode,
Expand Down
2 changes: 1 addition & 1 deletion pkg/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"
)

func NewHTTPClient() *http.Client{
func NewHTTPClient() *http.Client {
return &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
Expand Down
4 changes: 2 additions & 2 deletions pkg/http/server.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package http

import (
"github.com/Financial-Times/go-logger/v2"
"net/http"
"os"
"os/signal"
"sync"
"syscall"

"github.com/Financial-Times/go-logger/v2"
)

func StartServer(log *logger.UPPLogger, serveMux *http.ServeMux, port string) {
Expand Down Expand Up @@ -35,4 +36,3 @@ func waitForSignal() {
signal.Notify(ch, syscall.SIGINT, syscall.SIGTERM)
<-ch
}

23 changes: 12 additions & 11 deletions pkg/mapper/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package mapper
import (
"encoding/base64"
"errors"
"github.com/Financial-Times/content-rw-elasticsearch/pkg/config"
"github.com/Financial-Times/content-rw-elasticsearch/pkg/schema"
"strings"
"time"

"github.com/Financial-Times/content-rw-elasticsearch/pkg/config"
"github.com/Financial-Times/content-rw-elasticsearch/pkg/schema"

"fmt"

"github.com/Financial-Times/content-rw-elasticsearch/pkg/concept"
Expand All @@ -33,17 +34,17 @@ const (

type Handler struct {
ConceptReader concept.Reader
BaseApiURL string
BaseAPIURL string
Config config.AppConfig
log *logger.UPPLogger
}

var noAnnotationErr = errors.New("no annotation to be processed")
var errNoAnnotation = errors.New("no annotation to be processed")

func NewMapperHandler(reader concept.Reader, baseApiURL string, appConfig config.AppConfig, logger *logger.UPPLogger) *Handler {
func NewMapperHandler(reader concept.Reader, baseAPIURL string, appConfig config.AppConfig, logger *logger.UPPLogger) *Handler {
return &Handler{
ConceptReader: reader,
BaseApiURL: baseApiURL,
BaseAPIURL: baseAPIURL,
Config: appConfig,
log: logger,
}
Expand All @@ -52,14 +53,14 @@ func NewMapperHandler(reader concept.Reader, baseApiURL string, appConfig config
func (h *Handler) ToIndexModel(enrichedContent schema.EnrichedContent, contentType string, tid string) schema.IndexModel {
model := schema.IndexModel{}

if strings.HasPrefix(h.BaseApiURL, "http://") {
h.BaseApiURL = strings.Replace(h.BaseApiURL, "http", "https", 1)
if strings.HasPrefix(h.BaseAPIURL, "http://") {
h.BaseAPIURL = strings.Replace(h.BaseAPIURL, "http", "https", 1)
}
h.populateContentRelatedFields(&model, enrichedContent, contentType, tid)

annotations, concepts, err := h.prepareAnnotationsWithConcepts(&enrichedContent, tid)
if err != nil {
if err == noAnnotationErr {
if err == errNoAnnotation {
h.log.WithTransactionID(tid).Warn(err.Error())
} else {
h.log.WithError(err).WithTransactionID(tid).Error(err)
Expand Down Expand Up @@ -157,7 +158,7 @@ func (h *Handler) prepareAnnotationsWithConcepts(enrichedContent *schema.Enriche
}

if len(ids) == 0 {
return nil, nil, noAnnotationErr
return nil, nil, errNoAnnotation
}

concepts, err := h.ConceptReader.GetConcepts(tid, ids)
Expand Down Expand Up @@ -251,7 +252,7 @@ func (h *Handler) populateContentRelatedFields(model *schema.IndexModel, enriche
model.URL = new(string)
*model.URL = webURLPrefix + enrichedContent.Content.UUID
model.ModelAPIURL = new(string)
*model.ModelAPIURL = fmt.Sprintf("%v%v%v", h.BaseApiURL, apiURLPrefix, enrichedContent.Content.UUID)
*model.ModelAPIURL = fmt.Sprintf("%v%v%v", h.BaseAPIURL, apiURLPrefix, enrichedContent.Content.UUID)
model.PublishReference = tid
}

Expand Down
17 changes: 9 additions & 8 deletions pkg/mapper/mapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,25 @@ package mapper

import (
"encoding/json"
"testing"
"time"

"github.com/Financial-Times/content-rw-elasticsearch/pkg/config"
"github.com/Financial-Times/content-rw-elasticsearch/pkg/schema"
tst "github.com/Financial-Times/content-rw-elasticsearch/test"
"github.com/Financial-Times/go-logger/v2"
"testing"
"time"

"github.com/Financial-Times/content-rw-elasticsearch/pkg/concept"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)

type concordanceApiMock struct {
type concordanceAPIMock struct {
mock.Mock
}

func (m *concordanceApiMock) GetConcepts(tid string, ids []string) (map[string]concept.Model, error) {
func (m *concordanceAPIMock) GetConcepts(tid string, ids []string) (map[string]concept.Model, error) {
args := m.Called(tid, ids)
return args.Get(0).(map[string]concept.Model), args.Error(1)
}
Expand All @@ -45,8 +46,8 @@ func TestConvertToESContentModel(t *testing.T) {
if err != nil {
log.Fatal(err)
}
concordanceApiMock := new(concordanceApiMock)
mapperHandler := NewMapperHandler(concordanceApiMock, "http://api.ft.com", appConfig, log)
concordanceAPIMock := new(concordanceAPIMock)
mapperHandler := NewMapperHandler(concordanceAPIMock, "http://api.ft.com", appConfig, log)

for _, test := range tests {
if test.inputFileConcordanceModel != "" {
Expand All @@ -56,7 +57,7 @@ func TestConvertToESContentModel(t *testing.T) {
err = json.Unmarshal(inputConcordanceJSON, &concResp)
require.NoError(t, err, "Unexpected error")

concordanceApiMock.On("GetConcepts", test.tid, mock.AnythingOfType("[]string")).Return(concept.TransformToConceptModel(concResp), nil)
concordanceAPIMock.On("GetConcepts", test.tid, mock.AnythingOfType("[]string")).Return(concept.TransformToConceptModel(concResp), nil)
}
ecModel := schema.EnrichedContent{}
inputJSON := tst.ReadTestResource(test.inputFileEnrichedModel)
Expand Down Expand Up @@ -88,7 +89,7 @@ func TestConvertToESContentModel(t *testing.T) {

expect.Equal(expectedESModel, esModel, "ES model not matching with the one from %v", test.outputFile)

mock.AssertExpectationsForObjects(t, concordanceApiMock)
mock.AssertExpectationsForObjects(t, concordanceAPIMock)
}
}

Expand Down
15 changes: 8 additions & 7 deletions pkg/message/message_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package message

import (
"encoding/json"
"github.com/Financial-Times/content-rw-elasticsearch/pkg/config"
"github.com/Financial-Times/content-rw-elasticsearch/pkg/mapper"
"github.com/Financial-Times/content-rw-elasticsearch/pkg/schema"
"net/http"
"strings"
"sync"
"time"

"github.com/Financial-Times/content-rw-elasticsearch/pkg/config"
"github.com/Financial-Times/content-rw-elasticsearch/pkg/mapper"
"github.com/Financial-Times/content-rw-elasticsearch/pkg/schema"

"github.com/Financial-Times/content-rw-elasticsearch/pkg/es"
"github.com/Financial-Times/go-logger/v2"
"github.com/Financial-Times/message-queue-gonsumer/consumer"
Expand Down Expand Up @@ -37,19 +38,19 @@ type Handler struct {
messageConsumer consumer.MessageConsumer
Mapper *mapper.Handler
esClient ESClient
wg sync.WaitGroup
wg *sync.WaitGroup
mu sync.Mutex
log *logger.UPPLogger
}

func NewMessageHandler(service es.Service, mapper *mapper.Handler, client *http.Client, queueConfig consumer.QueueConfig, wg *sync.WaitGroup, esClient ESClient, logger *logger.UPPLogger) *Handler {
indexer := &Handler{esService: service, Mapper: mapper, esClient: esClient, wg: *wg, log: logger}
indexer := &Handler{esService: service, Mapper: mapper, esClient: esClient, wg: wg, log: logger}
indexer.messageConsumer = consumer.NewConsumer(queueConfig, indexer.handleMessage, client)
return indexer
}

func (h *Handler) Start(baseApiURL string, accessConfig es.AccessConfig, httpClient *http.Client) {
h.Mapper.BaseApiURL = baseApiURL
func (h *Handler) Start(baseAPIURL string, accessConfig es.AccessConfig, httpClient *http.Client) {
h.Mapper.BaseAPIURL = baseAPIURL
channel := make(chan es.Client)
go func() {
defer close(channel)
Expand Down
Loading

0 comments on commit 5dd7ea8

Please sign in to comment.