Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ci/golangci): fix golangcl-lint git push and apply format code on Push #4077

Merged
merged 24 commits into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions .github/workflows/before_script.sh
Original file line number Diff line number Diff line change
@@ -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."
# 安装 gci
houseme marked this conversation as resolved.
Show resolved Hide resolved
echo "Installing gci..."
go install github.com/daixiang0/gci@latest

# 检查 gci 是否安装成功
if ! command -v gci &> /dev/null
then
echo "gci could not be installed. Please check your Go setup."
exit 1
fi

# 使用 gci 格式化代码
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)" \
./

# 检查代码是否有变化
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."

# 添加本地域名到 /etc/hosts
echo "Adding local domain to /etc/hosts..."
sudo echo "127.0.0.1 local" | sudo tee -a /etc/hosts
61 changes: 61 additions & 0 deletions .github/workflows/format-code-on-push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: Format code on Push
houseme marked this conversation as resolved.
Show resolved Hide resolved

on:
push

jobs:
apply-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 }}
40 changes: 4 additions & 36 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ on:
- feat/**

jobs:
golangci:
golang-ci:
strategy:
matrix:
go-version: [ 'stable' ]
Expand All @@ -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)
args: --timeout 3m0s --config=.golangci.yml -v
50 changes: 34 additions & 16 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading