Skip to content

Commit

Permalink
feat: add golangci-lint version manager (#3457)
Browse files Browse the repository at this point in the history
The changes in this pull request involve updates to the Go workflow configuration, the introduction of a new Golang CI Lint Version Manager, and enhancements to documentation files. The workflow modifications include dynamic version handling for golangci-lint and improved job execution conditions. Additionally, the .golangci-version file has been updated to specify a new version. Documentation in CONTRIBUTING.md, README.md, and a new README.md for the Golang CI Lint Version Manager has been added to clarify contribution processes and setup instructions.



Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: [email protected] <[email protected]>
Co-authored-by: Trajan0x <[email protected]>
  • Loading branch information
3 people authored Dec 14, 2024
1 parent fb55d90 commit 3511c73
Show file tree
Hide file tree
Showing 39 changed files with 2,206 additions and 572 deletions.
12 changes: 8 additions & 4 deletions .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,18 @@ flags:
paths:
- contrib/screener-api/
carryforward: true
opbot:
paths:
- contrib/opbot/
carryforward: true
git-changes-action:
paths:
- contrib/git-changes-action/
carryforward: true
golang-ci-lint:
paths:
- contrib/golang-ci-lint/
carryforward: true
opbot:
paths:
- contrib/opbot/
carryforward: true
core:
paths:
- core/
Expand Down
11 changes: 7 additions & 4 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ jobs:
# note: without building a yaml tree of our workflow, we won't be able to tell if golangci version changed so any ci change triggers full lint.
files: |
.golangci.yml
.golangci-version
.github/workflows/go.yml
Expand Down Expand Up @@ -336,19 +337,21 @@ jobs:
- name: Setup cache key
run: cp ${{matrix.package}}/go.mod go.mod -v

- name: Read golangci-lint version
id: golangci_version
run: echo "version=v$(cat .golangci-version)" >> $GITHUB_OUTPUT

- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
working-directory: ${{matrix.package}}/
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: v1.60.3
# see: https://github.com/golangci/golangci-lint/issues/2654
version: ${{ steps.golangci_version.outputs.version }}
args: --timeout=60m
env:
# GitHub token for annotations (optional)
GITHUB_TOKEN: ${{ secrets.WORKFLOW_PAT || secrets.GITHUB_TOKEN }}
GOMEMLIMIT: 6GiB
GOGC: -1

pr_metadata:
# this is needed to prevent us from hitting the github api rate limit
name: Get PR Metadata
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,6 @@ main
.devnet/

**/__debug_bin*

# golang-ci-lint binary
contrib/golang-ci-lint/golang-ci-lint
1 change: 1 addition & 0 deletions .golangci-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1.61.0
4 changes: 3 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ linters:
- typecheck
# magic numbers
- mnd
# constants
- goconst
fast: false

issues:
Expand Down Expand Up @@ -138,7 +140,7 @@ issues:
- cyclop
- path: signoz/*
linters:
- mnd
- gomnd
- stylecheck
- path: example/*
linters:
Expand Down
14 changes: 13 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,19 @@ If you need to make a new JS/TS package, here are the steps to follow:

## Linting

Linting for go is used using [golangci-lint](https://golangci-lint.run/) at the latest released version. Please upgrade or install using your package manager. and run `make lint` from your desired module.
Linting for Go code uses [golangci-lint](https://golangci-lint.run/). The version is pinned in `.golangci-version` file and managed automatically through our tooling. Simply run `make lint` from your desired module, and the correct version will be downloaded (if not already cached) and used automatically.

### Golang-CI-Lint Module
The `contrib/golang-ci-lint` module provides version-pinned golangci-lint execution across repositories. The module automatically downloads and manages the correct version of golangci-lint based on the version specified in `.golangci-version` file, ensuring consistent linting across all Go modules.

#### Building and Testing
```bash
cd contrib/golang-ci-lint
make lint
make test
```

For more details about the module's implementation and usage, see the [module's README](contrib/golang-ci-lint/README.md).

## Adding a new Go Module

Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,23 @@ Clone the repository, open it, and install nodejs packages with `yarn`:
git clone https://github.com/synapsecns/sanguine --recurse-submodules -j10
cd sanguine
yarn install

### Go Development Setup

For Go development:
1. The project uses a specific version of golangci-lint that is managed automatically through our version management system
2. No manual installation of golangci-lint is required
3. The version is pinned in `.golangci-version` file and managed by the `contrib/golang-ci-lint` module
4. Run `make lint` in any Go module directory to automatically download and use the correct version

### Golang-CI-Lint Version Management
The repository uses a pinned version of golangci-lint specified in `.golangci-version`. The linter is automatically downloaded and executed through `make lint`. This ensures consistent linting across all Go modules and development environments.

Key features:
- Automatic version management based on `.golangci-version`
- Cross-platform compatibility
- Secure download and verification
- Caching for improved performance
```


Expand Down
16 changes: 16 additions & 0 deletions contrib/golang-ci-lint/.codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
coverage:
status:
project:
default:
target: '80%'
threshold: '1%'
patch:
default:
target: '80%'

ignore:
- '**/testdata/**'
- '**/mocks/**'
- '**/*_test.go'
- '**/cmd/**'
- '**/docs/**'
68 changes: 68 additions & 0 deletions contrib/golang-ci-lint/.goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
project_name: golang-ci-lint

monorepo:
tag_prefix: contrib/golang-ci-lint/
dir: contrib/golang-ci-lint/

builds:
- id: golang-ci-lint
binary: golang-ci-lint
gcflags:
- all=-dwarflocationlists=true
ldflags:
- -s -w -extldflags '-static'
tags:
- netgo
env:
- CGO_ENABLED=0
main: main.go
flags:
- -trimpath
goos:
- linux
goarch:
- amd64

dockers:
- goos: linux
goarch: amd64
image_templates:
- 'ghcr.io/synapsecns/sanguine/golang-ci-lint:latest'
- 'ghcr.io/synapsecns/sanguine/golang-ci-lint:{{ .FullCommit }}'
- 'ghcr.io/synapsecns/sanguine/golang-ci-lint:{{ .Tag }}'
build_flag_templates:
- '--label=org.opencontainers.image.created={{.Date}}'
- '--label=org.opencontainers.image.name={{.ProjectName}}'
- '--label=org.opencontainers.image.revision={{.FullCommit}}'
- '--label=org.opencontainers.image.version={{.Version}}'
- '--label=org.opencontainers.image.source={{.GitURL}}'
dockerfile: ../../docker/golang-ci-lint.Dockerfile
ids:
- golang-ci-lint

source:
enabled: true

archives:
- format: tar.gz
wrap_in_directory: true
format_overrides:
- goos: windows
format: zip
name_template: '{{.ProjectName}}-{{.Version}}_{{.Os}}_{{.Arch}}'
files:
- README.md

checksum:
name_template: checksums.txt

changelog:
sort: asc

report_sizes: true

metadata:
mod_timestamp: '{{ .CommitTimestamp }}'

sboms:
- artifacts: archive
1 change: 1 addition & 0 deletions contrib/golang-ci-lint/Makefile
116 changes: 116 additions & 0 deletions contrib/golang-ci-lint/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# Golang CI Lint Version Manager

A standalone tool for managing and running specific versions of golangci-lint across different repositories. This package can be used in any Go project to ensure consistent linting with version pinning, regardless of where it's installed.

## Features
- **Cross-Repository Usage**: Can be installed and used in any Go project
- **Version Pinning**: Automatically uses the version specified in `.golangci-version`
- **Architecture Support**: Downloads correct binary for your system (AMD64/ARM64)
- **Binary Caching**: Avoids redundant downloads of the same version
- **Secure Downloads**: Verifies binary checksums for security
- **CI/CD Integration**: Easy to integrate with GitHub Actions and other CI systems

## Installation

As a standalone tool:
```bash
go install github.com/synapsecns/sanguine/contrib/golang-ci-lint@latest
```

Or in your project's go.mod:
```go
require github.com/synapsecns/sanguine/contrib/golang-ci-lint v1.0.0
```

## Usage

### 1. Version Configuration
Create a `.golangci-version` file in your repository root:
```
1.61.0
```

### 2. Running the Linter

As a standalone binary:
```bash
golang-ci-lint run --fix --config=.golangci.yml
```

Or using go run:
```bash
go run github.com/synapsecns/sanguine/contrib/golang-ci-lint run --fix --config=.golangci.yml
```

### 3. Makefile Integration

Add to your project's Makefile:
```makefile
.PHONY: lint
lint:
go run github.com/synapsecns/sanguine/contrib/golang-ci-lint run --fix --config=.golangci.yml
```

### 4. CI/CD Integration

GitHub Actions example:
```yaml
name: Lint
on: [push, pull_request]

jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version: '1.21'
- name: Install golang-ci-lint manager
run: go install github.com/synapsecns/sanguine/contrib/golang-ci-lint@latest
- name: Run linter
run: golang-ci-lint run --config=.golangci.yml
```

## Common Use Cases

1. **Multiple Projects**: Use the same linter version across all your repositories
2. **CI/CD Pipelines**: Ensure consistent linting in automated workflows
3. **Team Collaboration**: Maintain consistent code style across development teams
4. **Version Control**: Lock linter version to avoid unexpected behavior changes

## Troubleshooting

1. **Binary Not Found**
```bash
# Clear the cache and redownload
rm -rf ~/.cache/golangci-lint
golang-ci-lint run
```

2. **Version Mismatch**
- Ensure `.golangci-version` exists in repository root
- Check file permissions and format

3. **Architecture Issues**
- The tool automatically detects and downloads the correct binary
- Supported architectures: linux-amd64, linux-arm64, darwin-amd64, darwin-arm64, windows-amd64
- MacOS (/private/var) symlink handling:
- Properly resolves /private/var to /var for temp directories
- Maintains secure path validation across symlinked paths
- Handles platform-specific temp directory structures

## Contributing

Contributions are welcome! Please ensure:
- Cross-platform compatibility:
- Linux: Standard path resolution
- MacOS: Handles /private/var symlinks and temp directories
- Windows: Supports standard Windows paths
- Backward compatibility with existing `.golangci-version` files
- Proper error handling and user feedback

## License

MIT License - See LICENSE file for details
5 changes: 5 additions & 0 deletions contrib/golang-ci-lint/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/synapsecns/sanguine/contrib/golang-ci-lint

go 1.22.4

require github.com/integralist/go-findroot v0.0.0-20160518114804-ac90681525dc
2 changes: 2 additions & 0 deletions contrib/golang-ci-lint/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/integralist/go-findroot v0.0.0-20160518114804-ac90681525dc h1:4IZpk3M4m6ypx0IlRoEyEyY1gAdicWLMQ0NcG/gBnnA=
github.com/integralist/go-findroot v0.0.0-20160518114804-ac90681525dc/go.mod h1:UlaC6ndby46IJz9m/03cZPKKkR9ykeIVBBDE3UDBdJk=
Loading

0 comments on commit 3511c73

Please sign in to comment.