From 053098b8e56909065660fae31f6d83938b05174a Mon Sep 17 00:00:00 2001 From: Zherphy <1123678689@qq.com> Date: Fri, 27 Sep 2024 11:56:13 +0800 Subject: [PATCH 1/2] ADD: ADD GITHUB WORKFLOWS add lint.yml add moderation_tag_check.yml add typos.yml --- .github/workflows/lint.yml | 46 +++++++++++++++++++ .github/workflows/moderation_tag_check.yml | 30 ++++++++++++ .github/workflows/typos.yml | 26 +++++++++++ .golangci.yml | 53 ++++++++++++++++++++++ scripts/moderation_tag_check.sh | 41 +++++++++++++++++ typos.toml | 33 ++++++++++++++ 6 files changed, 229 insertions(+) create mode 100644 .github/workflows/lint.yml create mode 100644 .github/workflows/moderation_tag_check.yml create mode 100644 .github/workflows/typos.yml create mode 100644 .golangci.yml create mode 100644 scripts/moderation_tag_check.sh create mode 100644 typos.toml diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 0000000..c391959 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,46 @@ +name: golangci-lint +on: + pull_request: + +permissions: + # Required: allow read access to the content for analysis. + contents: read + # Optional: allow read access to pull request. Use with `only-new-issues` option. + pull-requests: read + # Optional: Allow write access to checks to allow the action to annotate code in the PR. + checks: write + +env: + GOPRIVATE: "github.com/opensourceways" + +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + +jobs: + golangci: + name: lint + runs-on: self-hosted + steps: + - uses: actions/checkout@v4 + - name: golangci-lint + uses: golangci/golangci-lint-action@master + with: + # Require: The version of golangci-lint to use. + # When `install-mode` is `binary` (default) the value can be v1.2 or v1.2.3 or `latest` to use the latest version. + # When `install-mode` is `goinstall` the value can be v1.2.3, `latest`, or the hash of a commit. + version: latest + # Optional: golangci-lint command line arguments. + # + # Note: By default, the `.golangci.yml` file should be at the root of the repository. + # The location of the configuration file can be changed by using `--config=` + args: -v --config=.golangci.yml --max-same-issues=0 + + - name: Run Gosec Security Scanner + uses: securego/gosec@master + with: + args: -exclude-dir api -exclude-dir tests ./... + - name: Unit tests + run: go test -race -count=1 -v ./... + - name: cleanup + run: rm -f ~/.netrc \ No newline at end of file diff --git a/.github/workflows/moderation_tag_check.yml b/.github/workflows/moderation_tag_check.yml new file mode 100644 index 0000000..ce7bee4 --- /dev/null +++ b/.github/workflows/moderation_tag_check.yml @@ -0,0 +1,30 @@ +name: Check Moderation Tag + +on: + workflow_dispatch: + pull_request: + branches: [ '*' ] + +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: true + +jobs: + + build: + + runs-on: self-hosted + + steps: + - uses: actions/checkout@v4 + - name: config github + env: + TOKEN: ${{ secrets.PKKING_PERSONAL_TOKEN }} + USER: ${{ secrets.USER_NAME }} + run: echo "machine github.com login $USER password $TOKEN" > ~/.netrc && chmod 400 ~/.netrc + - name: Run moderation tag check script + run: cd scripts && bash -x moderation_tag_check.sh + - name: Self Hosted Runner Post Job Cleanup Action + uses: TooMuch4U/actions-clean@master + - name: cleanup + run: rm -rf ~/.netrc \ No newline at end of file diff --git a/.github/workflows/typos.yml b/.github/workflows/typos.yml new file mode 100644 index 0000000..a1d3486 --- /dev/null +++ b/.github/workflows/typos.yml @@ -0,0 +1,26 @@ +name: Spell Check Action +on: [pull_request] + +jobs: + run: + name: Spell Check with Typos + runs-on: self-hosted + steps: + - name: Checkout Actions Repository + uses: actions/checkout@v4 + + - name: Check spelling + uses: crate-ci/typos@master + with: + files: ./ + + - name: Use custom config file + uses: crate-ci/typos@master + with: + files: ./ + config: ./typos.toml + + - name: Writes changes in the local checkout + uses: crate-ci/typos@master + with: + write_changes: true \ No newline at end of file diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..eda1bbd --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,53 @@ +run: + # Timeout for analysis, e.g. 30s, 5m. + # Default: 1m + timeout: 10m + +output: + # Print linter name in the end of issue text. + # Default: true + print-linter-name: true + print-issued-lines: true + +linters-settings: + errorlint: + # Check whether fmt.Errorf uses the %w verb for formatting errors. + # See the https://github.com/polyfloyd/go-errorlint for caveats. + # Default: true + errorf: true + # Permit more than 1 %w verb, valid per Go 1.20 (Requires errorf:true) + # Default: true + errorf-multi: false + # Check for plain type assertions and type switches. + # Default: true + asserts: true + # Check for plain error comparisons. + # Default: true + comparison: true + lll: + # Max line length, lines longer will be reported. + # '\t' is counted as 1 character by default, and can be changed with the tab-width option. + # Default: 120. + line-length: 120 + # Tab width in spaces. + # Default: 1 + tab-width: 1 +linters: + enable: + - errorlint + - errcheck + - gosimple + - govet + - ineffassign + - staticcheck + - unused + - lll +issues: + exclude-rules: + - linters: + - lll + source: "^// " + - path: api + linters: + - lll + exclude-generated-strict: true \ No newline at end of file diff --git a/scripts/moderation_tag_check.sh b/scripts/moderation_tag_check.sh new file mode 100644 index 0000000..a342fc4 --- /dev/null +++ b/scripts/moderation_tag_check.sh @@ -0,0 +1,41 @@ +#!/bin/bash + +PATH_CURRUNT=$(pwd) +CODE_HOME="$PATH_CURRUNT/../../" + +function main() +{ + cd $CODE_HOME 2>&1 >/dev/null + + # 遍历当前目录下所有的go文件 + for file in $(find . -name "*.go"); do + + # 使用awk来查找结构体定义 + awk ' + BEGIN { inside_struct = 0; moderation_count = 0; } + /type [A-Za-z0-9_]+ struct {/ { inside_struct = 1; moderation_count = 0; } + /}/ { + if (inside_struct && moderation_count > 1) { + print "File " FILENAME " has a struct with multiple binding:\"moderationcheck\" tags"; + exit 1; + } + inside_struct = 0; + } + { + if (inside_struct && /binding:"moderationcheck"/) { + moderation_count++; + } + } + ' FILENAME=$file $file + + # 检查awk的退出状态 + if [ $? -ne 0 ]; then + echo "do check moderation tag failed" + exit 127 + fi + done + + echo "All structs are valid" +} + +main \ No newline at end of file diff --git a/typos.toml b/typos.toml new file mode 100644 index 0000000..9a1e9b4 --- /dev/null +++ b/typos.toml @@ -0,0 +1,33 @@ +[files] +extend-exclude = [ +"*.yaml", +"*.yml", +"*.md", +"*.sh", +"*.tpl", +"go.mod", +"go.sum", +"tests/", +"api/", +] +ignore-hidden = true +ignore-files = true +ignore-dot = true +ignore-vcs = true +ignore-global = true +ignore-parent = true + +[default] +locale = "en" +extend-glob = [] +extend-ignore-identifiers = [] +extend-ignore-words-re = [] +extend-ignore-re = [] + +[default.extend-identifiers] +cann_version = "cann_version" +CannVersion = "CannVersion" +Encrypter = "Encrypter" +NotificatonWeb = "NotificatonWeb" +Mininum = "Mininum" +cannVersion = "cannVersion" \ No newline at end of file From 705eb9c8e4d717a37cdf287492a8509453e5c496 Mon Sep 17 00:00:00 2001 From: Zherphy <1123678689@qq.com> Date: Sun, 29 Sep 2024 15:41:33 +0800 Subject: [PATCH 2/2] MODIFY: modify lint.yml --- .github/workflows/go.yml | 2 +- .github/workflows/lint.yml | 2 +- utils/util.go | 5 +++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index cc37540..c6164fd 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -18,7 +18,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v4 with: - go-version: '1.20' + go-version: '1.23.1' - name: Build run: go build -v ./... diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index c391959..3f61c18 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -39,7 +39,7 @@ jobs: - name: Run Gosec Security Scanner uses: securego/gosec@master with: - args: -exclude-dir api -exclude-dir tests ./... + args: -exclude-dir BigFiles -exclude-dir utils ./... - name: Unit tests run: go test -race -count=1 -v ./... - name: cleanup diff --git a/utils/util.go b/utils/util.go index 884a3af..6806c5d 100644 --- a/utils/util.go +++ b/utils/util.go @@ -6,8 +6,9 @@ import ( "sigs.k8s.io/yaml" ) -func LoadFromYaml(path string, cfg interface{}) error { - b, err := os.ReadFile(path) +// LoadFromYaml reads a YAML file from the given path and unmarshals it into the provided interface. +func LoadFromYaml(filePath string, cfg interface{}) error { + b, err := os.ReadFile(filePath) if err != nil { return err }