Skip to content

Commit

Permalink
Add ESLint And Golang-ci (#276)
Browse files Browse the repository at this point in the history
  • Loading branch information
hengfeiyang committed Jul 10, 2022
1 parent 8af4cdf commit 14a6e3e
Show file tree
Hide file tree
Showing 38 changed files with 4,685 additions and 440 deletions.
6 changes: 0 additions & 6 deletions .github/workflows/base.yml → .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@ jobs:
Codecov-coverage:
runs-on: ubuntu-latest
steps:
- run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event."
- run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!"
- run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}."
- name: Check out repository code
uses: actions/checkout@v3
- run: echo "💡 The ${{ github.repository }} repository has been cloned to the runner."
- run: echo "🖥️ The workflow is now ready to test your code on the runner."
- name: List files in the repository
run: |
ls ${{ github.workspace }}
- run: echo "🍏 This job's status is ${{ job.status }}."
- run: cd web && npm i && npm run build && cd ..
- run: ZINC_FIRST_ADMIN_USER=admin ZINC_FIRST_ADMIN_PASSWORD=Complexpass#123 go test ./... -race -covermode=atomic -coverprofile=coverage.out
Expand Down
16 changes: 15 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ Make sure that you have [docker](https://docs.docker.com/get-docker/).
Simple build:
```shell
docker build --tag zinc:latest . -f Dockerfile.hub
docker build --tag zinc:latest . -f Dockerfile
````

Multi-arch build
Expand All @@ -129,6 +129,19 @@ In order to build multi-arch builds you will need [buildx](https://docs.docker.c
docker buildx build --platform linux/amd64 --tag zinc:latest-linux-amd64 . -f Dockerfile.hub
```

# Checks in CI pipeline

We check for following in CI pipeline for any pull requests.

1. Unit test code coverage for go code.
- If code coverage is less than 81% (according to go test) the CI tests will fail.
- You can test coverage yourself by running `./coverage.sh`
- We use codecov for visualizing code coverage of go code, codecov updates coverage for every PR through a comment. It allows you to see missing coverage for any lines.
1. Linting in Javascript for GUI
- We run eslint for javacript anf any linting failures will result in build failures.
- You can test for linting failures by running `./lint.sh` in web folder.
- You can also fix automatically fixable linting error by running `npm run lint-autofix`


## How to contribute code

Expand All @@ -138,3 +151,4 @@ docker buildx build --platform linux/amd64 --tag zinc:latest-linux-amd64 . -f Do
1. Make the changes to code.
1. Push the code to your repo.
1. Create a PR
1. Make sure that the automatic CI checks pass for your PR.
4 changes: 4 additions & 0 deletions buildspec-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ phases:
- echo "Building front end..."
- cd web
- npm install
- ./eslint.sh
- npm run build
- cd ..
- echo "golangci-lint ..."
- ./golangci-lint.sh
- echo "golangci-lint done ..."
- ./coverage.sh
# - go build -o main cmd/zinc/main.go
# Build binary and container images using goreleaser
Expand Down
7 changes: 6 additions & 1 deletion cmd/zinc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func profiling() {
ProfileID = strings.ToLower(core.Telemetry.GetInstanceID())
}

pyroscope.Start(pyroscope.Config{
_, err := pyroscope.Start(pyroscope.Config{
ApplicationName: "zincsearch-" + ProfileID,

// replace this with the address of pyroscope server
Expand All @@ -165,6 +165,11 @@ func profiling() {
pyroscope.ProfileInuseSpace,
},
})

if err != nil {
log.Print("pyroscope.Start: ", err.Error())
}

}

//shutdown support twice signal must exit
Expand Down
16 changes: 16 additions & 0 deletions golangci-lint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#! /bin/sh

if ! command -v golangci-lint &> /dev/null
then
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $PWD/ v1.46.2
./golangci-lint run
rm golangci-lint
else
golangci-lint run
fi

rc=$?
if [ $rc -ne 0 ]; then
echo "golangci-lint failed" >&2
exit $rc
fi
5 changes: 4 additions & 1 deletion pkg/core/index_shards.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ func (index *Index) NewShard() error {
index.Shards = append(index.Shards, &meta.IndexShard{ID: index.ShardNum - 1})
index.lock.Unlock()
// store update
metadata.Index.Set(index.Name, index.Index)
err := metadata.Index.Set(index.Name, index.Index)
if err != nil {
return err
}
return index.openWriter(index.ShardNum - 1)
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/core/indexlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package core

import (
"fmt"
"sync"

"github.com/rs/zerolog/log"
Expand All @@ -35,7 +36,10 @@ func init() {
// check version
version, _ := metadata.KV.Get("version")
if version == nil {
metadata.KV.Set("version", []byte(meta.Version))
err := metadata.KV.Set("version", []byte(meta.Version))
if err != nil {
fmt.Println("error:", err)
}
// } else {
// version := string(version)
// if version != meta.Version {
Expand Down
6 changes: 5 additions & 1 deletion pkg/handlers/index/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package index

import (
"fmt"
"net/http"
"sort"
"strings"
Expand All @@ -42,7 +43,10 @@ func List(c *gin.Context) {
index.Mappings = meta.NewMappings()
}
// update metadata while listing
index.UpdateMetadata()
err := index.UpdateMetadata()
if err != nil {
fmt.Println("Error updating metadata: ", err)
}
}

sort.Slice(items, func(i, j int) bool {
Expand Down
6 changes: 4 additions & 2 deletions pkg/metadata/storage/badger/badger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ func Test_badgerStorage_List(t *testing.T) {
store := New("/zinc/test")
defer store.Close()
t.Run("prepare", func(t *testing.T) {
store.Set("/test/foo", []byte("bar"))
err := store.Set("/test/foo", []byte("bar"))
assert.NoError(t, err)
})

for _, tt := range tests {
Expand Down Expand Up @@ -100,7 +101,8 @@ func Test_badgerStorage_Get(t *testing.T) {
store := New("/zinc/test")
defer store.Close()
t.Run("prepare", func(t *testing.T) {
store.Set("/test/foo", []byte("bar"))
err := store.Set("/test/foo", []byte("bar"))
assert.NoError(t, err)
})

for _, tt := range tests {
Expand Down
6 changes: 4 additions & 2 deletions pkg/metadata/storage/bolt/bolt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ func Test_boltStorage_List(t *testing.T) {
store := New("/zinc/test")
defer store.Close()
t.Run("prepare", func(t *testing.T) {
store.Set("/test/foo", []byte("bar"))
err := store.Set("/test/foo", []byte("bar"))
assert.NoError(t, err)
})

for _, tt := range tests {
Expand Down Expand Up @@ -100,7 +101,8 @@ func Test_boltStorage_Get(t *testing.T) {
store := New("/zinc/test")
defer store.Close()
t.Run("prepare", func(t *testing.T) {
store.Set("/test/foo", []byte("bar"))
err := store.Set("/test/foo", []byte("bar"))
assert.NoError(t, err)
})

for _, tt := range tests {
Expand Down
10 changes: 7 additions & 3 deletions pkg/metadata/storage/etcd/etcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package etcd

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -25,9 +26,10 @@ import (

func TestMain(m *testing.M) {
if config.Global.Etcd.Endpoints == nil {
os.Exit(0)
return
}
m.Run()
os.Exit(m.Run())
}

func Test_etcdStorage_List(t *testing.T) {
Expand Down Expand Up @@ -63,7 +65,8 @@ func Test_etcdStorage_List(t *testing.T) {
store := New("/zinc/test")
defer store.Close()
t.Run("prepare", func(t *testing.T) {
store.Set("/test/foo", []byte("bar"))
err := store.Set("/test/foo", []byte("bar"))
assert.NoError(t, err)
})

for _, tt := range tests {
Expand Down Expand Up @@ -109,7 +112,8 @@ func Test_etcdStorage_Get(t *testing.T) {
store := New("/zinc/test")
defer store.Close()
t.Run("prepare", func(t *testing.T) {
store.Set("/test/foo", []byte("bar"))
err := store.Set("/test/foo", []byte("bar"))
assert.NoError(t, err)
})

for _, tt := range tests {
Expand Down
4 changes: 4 additions & 0 deletions web/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/dist
/node_modules
.eslintrc.js
babel.config.js
17 changes: 17 additions & 0 deletions web/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
root: true,
env: { node: true },
// https://github.com/vuejs/vue-eslint-parser#parseroptionsparser
parser: "vue-eslint-parser",
parserOptions: {
parser: "@typescript-eslint/parser",
},
plugins: ["@typescript-eslint", "prettier"],
extends: [
"plugin:vue/vue3-recommended",
"prettier",
],
rules: {
"prettier/prettier": "error",
}
}
3 changes: 3 additions & 0 deletions web/.stylelintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: "stylelint-config-recommended-vue"
}
9 changes: 9 additions & 0 deletions web/eslint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#! /bin/sh

npm run lint

rc=$?
if [ $rc -ne 0 ]; then
echo "ES lint failed" >&2
exit $rc
fi
Loading

0 comments on commit 14a6e3e

Please sign in to comment.