From 6ea1526b751f7ec5b466eb98016402748eb793d5 Mon Sep 17 00:00:00 2001 From: houseme Date: Thu, 26 Dec 2024 10:19:01 +0800 Subject: [PATCH] fix(ci/golangci): fix golangcl-lint git push and apply format code on Push (#4077) --- .github/workflows/before_script.sh | 36 ++++++++++- .github/workflows/format-code-on-push.yml | 61 +++++++++++++++++++ .github/workflows/golangci-lint.yml | 44 ++----------- .github/workflows/issue-check-inactive.yml | 6 +- .github/workflows/issue-close-inactive.yml | 6 +- .github/workflows/issue-labeled.yml | 6 +- .github/workflows/issue-remove-inactive.yml | 6 +- .../issue-remove-need-more-details.yml | 6 +- .golangci.yml | 50 ++++++++++----- 9 files changed, 149 insertions(+), 72 deletions(-) create mode 100644 .github/workflows/format-code-on-push.yml diff --git a/.github/workflows/before_script.sh b/.github/workflows/before_script.sh index b0bbee27466..d9851ef497c 100644 --- a/.github/workflows/before_script.sh +++ b/.github/workflows/before_script.sh @@ -1,6 +1,36 @@ #!/usr/bin/env bash -find . -name "*.go" | xargs gofmt -w -git diff --name-only --exit-code || if [ $? != 0 ]; then echo "Notice: gofmt check failed,please gofmt before pr." && exit 1; fi -echo "gofmt check pass." +# Install gci +echo "Installing gci..." +go install github.com/daixiang0/gci@latest + +# Check if the GCI is installed successfully +if ! command -v gci &> /dev/null +then + echo "gci could not be installed. Please check your Go setup." + exit 1 +fi + +# Use GCI to format the code +echo "Running gci to format code..." +gci write \ + --custom-order \ + --skip-generated \ + --skip-vendor \ + -s standard \ + -s blank \ + -s default \ + -s dot \ + -s "prefix(github.com/gogf/gf/v2)" \ + -s "prefix(github.com/gogf/gf/cmd)" \ + -s "prefix(github.com/gogf/gf/contrib)" \ + -s "prefix(github.com/gogf/gf/example)" \ + ./ + +# Check the code for changes +git diff --name-only --exit-code || if [ $? != 0 ]; then echo "Notice: gci check failed, please gci before pr." && exit 1; fi +echo "gci check pass." + +# Add the local domain name to `/etc/hosts` +echo "Adding local domain to /etc/hosts..." sudo echo "127.0.0.1 local" | sudo tee -a /etc/hosts \ No newline at end of file diff --git a/.github/workflows/format-code-on-push.yml b/.github/workflows/format-code-on-push.yml new file mode 100644 index 00000000000..a9c4f63cc0f --- /dev/null +++ b/.github/workflows/format-code-on-push.yml @@ -0,0 +1,61 @@ +name: Format Code on Push + +on: + push + +jobs: + format-code: + strategy: + matrix: + go-version: [ 'stable' ] + name: format-code-by-gci + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + - name: Setup Golang ${{ matrix.go-version }} + uses: actions/setup-go@v5 + with: + go-version: ${{ matrix.go-version }} + - name: Install gci + run: go install github.com/daixiang0/gci@latest + - name: Run gci + run: | + gci write --custom-order \ + --skip-generated \ + --skip-vendor \ + -s standard \ + -s blank \ + -s default \ + -s dot \ + -s "prefix(github.com/gogf/gf/v2)" \ + -s "prefix(github.com/gogf/gf/cmd)" \ + -s "prefix(github.com/gogf/gf/contrib)" \ + -s "prefix(github.com/gogf/gf/example)" \ + ./ + - name: Check for changes + run: | + if [[ -n "$(git status --porcelain)" ]]; then + echo "HAS_CHANGES=true" >> $GITHUB_ENV + else + echo "HAS_CHANGES=false" >> $GITHUB_ENV + fi + - name: Configure Git + run: | + if [[ "$HAS_CHANGES" == 'true' ]]; then + git config --global user.name "github-actions[bot]" + git config --global user.email "github-actions[bot]@users.noreply.github.com" + else + echo "HAS_CHANGES= $HAS_CHANGES " + fi + - name: Commit and push changes + run: | + if [[ "$HAS_CHANGES" == 'true' ]]; then + git add . + git commit -m "Apply gci import order changes" + git push origin ${{ github.event.pull_request.head.ref }} + else + echo "No change to commit push" + fi + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index 9be6b9564d8..d15a8086986 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -4,7 +4,7 @@ # If a copy of the MIT was not distributed with this file, # You can obtain one at https://github.com/gogf/gf. -name: GolangCI-Lint +name: GolangCI Lint on: push: branches: @@ -26,11 +26,11 @@ on: - feat/** jobs: - golangci: + golang-ci: strategy: matrix: go-version: [ 'stable' ] - name: golangci-lint + name: golang-ci-lint runs-on: ubuntu-latest steps: - name: Checkout @@ -39,44 +39,12 @@ jobs: uses: actions/setup-go@v5 with: go-version: ${{ matrix.go-version }} - - name: golangci-lint + - name: golang-ci-lint uses: golangci/golangci-lint-action@v6 with: # Required: specify the golangci-lint version without the patch version to always use the latest patch. version: v1.62.2 only-new-issues: true + skip-cache: true github-token: ${{ secrets.GITHUB_TOKEN }} - args: --timeout 3m0s - - name: Install gci - run: go install github.com/daixiang0/gci@latest - - name: Run gci - run: | - gci write --custom-order \ - --skip-generated \ - --skip-vendor \ - -s standard \ - -s blank \ - -s default \ - -s dot \ - -s "prefix(github.com/gogf/gf/v2)" \ - -s "prefix(github.com/gogf/gf/cmd)" \ - -s "prefix(github.com/gogf/gf/contrib)" \ - -s "prefix(github.com/gogf/gf/example)" \ - ./ - - name: Check for changes - # Check if the event is a push or a pull request from a forked repository - if: github.event_name == 'push'|| (github.event_name == 'pull_request' && github.event.pull_request.head.repo.fork == true) - run: | - if [[ -n "$(git status --porcelain)" ]]; then - echo "HAS_CHANGES=true" >> $GITHUB_ENV - else - echo "HAS_CHANGES=false" >> $GITHUB_ENV - fi - - name: Commit and push changes - if: env.HAS_CHANGES == 'true' - run: | - git config --global user.name "github-actions[bot]" - git config --global user.email "github-actions[bot]@users.noreply.github.com" - git add . - git commit -m "Apply gci import order changes" - git push origin HEAD:$(git rev-parse --abbrev-ref HEAD) \ No newline at end of file + args: --timeout 3m0s --config=.golangci.yml -v diff --git a/.github/workflows/issue-check-inactive.yml b/.github/workflows/issue-check-inactive.yml index 8ca295d86ca..4b16b4f9a80 100644 --- a/.github/workflows/issue-check-inactive.yml +++ b/.github/workflows/issue-check-inactive.yml @@ -1,12 +1,12 @@ -# 规则描述:每天凌晨3点(GMT+8)执行一次,将最近7天没有活跃且非BUG的ISSUE设置标签:inactive +# Rule description: Execute the ISSUE once a day at 3 a.m. (GMT+8) and set the non-bug issue that has not been active in the last 7 days to inactive name: Issue Check Inactive on: schedule: - cron: "0 19 * * *" -env: # 设置环境变量 - TZ: Asia/Shanghai #时区(设置时区可使页面中的`最近更新时间`使用时区时间) +env: # Set environment variables + TZ: Asia/Shanghai #Time zone (setting the time zone allows the 'Last Updated' on the page to use the time zone) permissions: contents: read diff --git a/.github/workflows/issue-close-inactive.yml b/.github/workflows/issue-close-inactive.yml index 2044b4a7f4c..06272aa2132 100644 --- a/.github/workflows/issue-close-inactive.yml +++ b/.github/workflows/issue-close-inactive.yml @@ -1,12 +1,12 @@ -# 规则描述:每天凌晨 4 点 (GMT+8) 执行一次,将最近 30 天没有活跃且非 BUG 的 ISSUE 关闭 +# RULE DESCRIPTION: EXECUTED ONCE A DAY AT 4 A.M. (GMT+8) TO CLOSE NON-BUG ISSUES THAT HAVE NOT BEEN ACTIVE IN THE LAST 30 DAYS name: Issue Close Inactive on: schedule: - cron: "0 20 * * *" -env: # 设置环境变量 - TZ: Asia/Shanghai #时区(设置时区可使页面中的`最近更新时间`使用时区时间) +env: # Set environment variables + TZ: Asia/Shanghai #Time zone (setting the time zone allows the 'Last Updated' on the page to use the time zone) jobs: close-issues: diff --git a/.github/workflows/issue-labeled.yml b/.github/workflows/issue-labeled.yml index 5f5e2ecdd0f..08b615380a8 100644 --- a/.github/workflows/issue-labeled.yml +++ b/.github/workflows/issue-labeled.yml @@ -1,4 +1,4 @@ -## 规则描述:当 issue 被标记为 help wanted 时,增加评论 +## Rule description: Add comments when an issue is marked as help wanted name: Issue Labeled @@ -6,8 +6,8 @@ on: issues: types: [labeled] -env: # 设置环境变量 - TZ: Asia/Shanghai # 时区(设置时区可使页面中的`最近更新时间`使用时区时间) +env: # Set environment variables + TZ: Asia/Shanghai # Time zone (setting the time zone allows the 'Last Updated' on the page to use the time zone) jobs: reply-labeled: diff --git a/.github/workflows/issue-remove-inactive.yml b/.github/workflows/issue-remove-inactive.yml index 8cd3f1ad7c0..94e975dd288 100644 --- a/.github/workflows/issue-remove-inactive.yml +++ b/.github/workflows/issue-remove-inactive.yml @@ -1,4 +1,4 @@ -# 规则描述:在 issue 没有活跃且尚未被关闭期间,若 issue 作者更新或评论该 ISSUE,则移除其 inactive 标签 +# Rule description: If an issue author updates or comments on an issue while it is not active and has not been closed, the inactive tag will be removed name: Issue Remove Inactive on: @@ -7,8 +7,8 @@ on: issue_comment: types: [created, edited] -env: # 设置环境变量 - TZ: Asia/Shanghai #时区(设置时区可使页面中的`最近更新时间`使用时区时间) +env: # Set environment variables + TZ: Asia/Shanghai #Time zone (setting the time zone allows the 'Last Updated' on the page to use the time zone) permissions: contents: read diff --git a/.github/workflows/issue-remove-need-more-details.yml b/.github/workflows/issue-remove-need-more-details.yml index e9e2ab03b83..80379693e20 100644 --- a/.github/workflows/issue-remove-need-more-details.yml +++ b/.github/workflows/issue-remove-need-more-details.yml @@ -1,4 +1,4 @@ -# 规则描述:将需要提供更多细节且暂未关闭的 issue,在 issue 作者评论后,移除 need more details 标签 +# Rule Description: For issues that need more details and are not yet closed, remove the "need more details" tag after the issue author comments name: Issue Remove Need More Details on: @@ -7,8 +7,8 @@ on: issue_comment: types: [created, edited] -env: # 设置环境变量 - TZ: Asia/Shanghai #时区(设置时区可使页面中的`最近更新时间`使用时区时间) +env: # Set environment variables + TZ: Asia/Shanghai #Time zone (setting the time zone allows the 'Last Updated' on the page to use the time zone) permissions: contents: read diff --git a/.golangci.yml b/.golangci.yml index a2b04ea7735..f1512b2b310 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -6,28 +6,46 @@ # Options for analysis running. run: + # Timeout for analysis, e.g. 30s, 5m. + # Default: 1m + timeout: 5m # Exit code when at least one issue was found. # Default: 1 issues-exit-code: 2 - # Include test files or not. # Default: true tests: false - - # Which dirs to skip: issues from them won't be reported. - # Can use regexp here: `generated.*`, regexp is applied on full path. - # Default value is empty list, - # but default dirs are skipped independently of this option's value (see skip-dirs-use-default). - # "/" will be replaced by current OS file path separator to properly work on Windows. - skip-dirs: [] - - # Which files to skip: they will be analyzed, but issues from them won't be reported. - # Default value is empty list, - # but there is no need to include all autogenerated files, - # we confidently recognize autogenerated files. - # If it's not please let us know. - # "/" will be replaced by current OS file path separator to properly work on Windows. - skip-files: [] + # List of build tags, all linters use it. + # Default: [] + build-tags: [] + # If set, we pass it to "go list -mod={option}". From "go help modules": + # If invoked with -mod=readonly, the go command is disallowed from the implicit + # automatic updating of go.mod described above. Instead, it fails when any changes + # to go.mod are needed. This setting is most useful to check that go.mod does + # not need updates, such as in a continuous integration and testing system. + # If invoked with -mod=vendor, the go command assumes that the vendor + # directory holds the correct copies of dependencies and ignores + # the dependency descriptions in go.mod. + # + # Allowed values: readonly|vendor|mod + # Default: "" + modules-download-mode: readonly + # Allow multiple parallel golangci-lint instances running. + # If false, golangci-lint acquires file lock on start. + # Default: false + allow-parallel-runners: true + # Allow multiple golangci-lint instances running, but serialize them around a lock. + # If false, golangci-lint exits with an error if it fails to acquire file lock on start. + # Default: false + allow-serial-runners: true + # Define the Go version limit. + # Mainly related to generics support since go1.18. + # Default: use Go version from the go.mod file, fallback on the env var `GOVERSION`, fallback on 1.17 + go: '1.20' + # Number of operating system threads (`GOMAXPROCS`) that can execute golangci-lint simultaneously. + # If it is explicitly set to 0 (i.e. not the default) then golangci-lint will automatically set the value to match Linux container CPU quota. + # Default: the number of logical CPUs in the machine + concurrency: 4 # Main linters configurations.