Browse Source

ci: add lint workflow (#283)

* ci: add lint

Signed-off-by: ashing <axingfly@gmail.com>

* fix: add timeout

Signed-off-by: ashing <axingfly@gmail.com>

* fix: EOL

Signed-off-by: ashing <axingfly@gmail.com>

* fix: EOL

Signed-off-by: ashing <axingfly@gmail.com>

---------

Signed-off-by: ashing <axingfly@gmail.com>
Ashing Zheng 1 year ago
parent
commit
5300bfa297
4 changed files with 57 additions and 4 deletions
  1. 46 0
      .github/workflows/lint.yaml
  2. 1 1
      Makefile
  3. 1 1
      pkg/cache/user_requese.go
  4. 9 2
      pkg/dingbot/client.go

+ 46 - 0
.github/workflows/lint.yaml

@@ -0,0 +1,46 @@
+name: Lint Check
+
+on:
+  push:
+    branches:
+      - main
+      - release/**
+  pull_request:
+    branches:
+      - main
+      - release/**
+permissions: read-all
+jobs:
+  gofmt:
+    runs-on: ubuntu-latest
+    timeout-minutes: 5
+    steps:
+      - uses: actions/checkout@v3
+      - name: Setup Go Environment
+        uses: actions/setup-go@v3
+        with:
+          go-version: '1.20.3'
+      - name: Run gofmt Check
+        working-directory: ./
+        run: |
+          diffs=`gofmt -l .`
+          if [[ -n $diffs ]]; then
+              echo "Files are not formatted by gofmt:"
+              echo $diffs
+              exit 1
+          fi
+  golint:
+    runs-on: ubuntu-latest
+    timeout-minutes: 10
+    steps:
+      - uses: actions/checkout@v3
+      - name: Setup Go Environment
+        uses: actions/setup-go@v3
+        with:
+          go-version: '1.20.3'
+      - name: Download golangci-lint
+        run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.54.2
+      - name: Run Golang Linters
+        working-directory: ./
+        run: |
+          PATH=${PATH}:$(go env GOPATH)/bin make lint

+ 1 - 1
Makefile

@@ -13,7 +13,7 @@ build-linux-arm:
 	CGO_ENABLED=0 GOARCH=arm64 GOOS=linux go build -o chatgpt-dingtalk main.go
 
 lint:
-	env GOGC=25 golangci-lint run --fix -j 8 -v ./...
+	env GOGC=25 golangci-lint run --fix -j 8 --timeout 10m -v ./...
 
 goimports:
 	@bash ./scripts/goimports-reviser.sh

+ 1 - 1
pkg/cache/user_requese.go

@@ -7,7 +7,7 @@ import (
 // SetUseRequestCount 设置用户请求次数
 func (s *UserService) SetUseRequestCount(userId string, current int) {
 	expiration := time.Now().Add(time.Hour * 24).Truncate(time.Hour * 24)
-	duration := expiration.Sub(time.Now())
+	duration := time.Until(expiration)
 	// 设置缓存失效时间为第二天零点
 	s.cache.Set(userId+"_request", current, duration)
 }

+ 9 - 2
pkg/dingbot/client.go

@@ -139,7 +139,12 @@ func (c *DingTalkClient) UploadMedia(content []byte, filename, mediaType, mimeTy
 		return nil, err
 	}
 	_, err = part.Write(content)
-	writer.WriteField("type", mediaType)
+	if err != nil {
+		return nil, err
+	}
+	if err = writer.WriteField("type", mediaType); err != nil {
+		return nil, err
+	}
 	err = writer.Close()
 	if err != nil {
 		return nil, err
@@ -166,10 +171,12 @@ func (c *DingTalkClient) UploadMedia(content []byte, filename, mediaType, mimeTy
 	// Parse the response body as JSON and extract the media ID
 	media := &MediaUploadResult{}
 	bodyBytes, err := io.ReadAll(res.Body)
-	json.Unmarshal(bodyBytes, media)
 	if err != nil {
 		return nil, err
 	}
+	if err = json.Unmarshal(bodyBytes, media); err != nil {
+		return nil, err
+	}
 	if media.ErrorCode != 0 {
 		return nil, errors.New(media.ErrorMessage)
 	}