From 65d939196cd27c131588cfd808705322b8066100 Mon Sep 17 00:00:00 2001 From: Luke Tainton Date: Sat, 18 Apr 2026 20:07:15 +0100 Subject: [PATCH] Add CI and SonarQube workflows for automated testing and code quality analysis Co-authored-by: Copilot --- .gitea/workflows/ci.yml | 81 ++++++++++++++++++++++++++++++++++ .gitea/workflows/sonarqube.yml | 31 +++++++++++++ SONARQUBE.md | 68 ++++++++++++++++++++++++++++ sonar-project.properties | 26 +++++++++++ 4 files changed, 206 insertions(+) create mode 100644 .gitea/workflows/ci.yml create mode 100644 .gitea/workflows/sonarqube.yml create mode 100644 SONARQUBE.md create mode 100644 sonar-project.properties diff --git a/.gitea/workflows/ci.yml b/.gitea/workflows/ci.yml new file mode 100644 index 0000000..74ca48f --- /dev/null +++ b/.gitea/workflows/ci.yml @@ -0,0 +1,81 @@ +name: CI + +on: + pull_request: + branches: + - main + - develop + +jobs: + test: + name: Test + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: '1.23' + cache: true + + - name: Run tests + run: go test -v -race -coverprofile=coverage.out ./src + + - name: Upload coverage + uses: actions/upload-artifact@v3 + with: + name: coverage + path: coverage.out + + lint: + name: Lint + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: '1.23' + cache: true + + - name: Install golangci-lint + run: go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest + + - name: Run linter + run: golangci-lint run ./src + + build: + name: Build + runs-on: ubuntu-latest + needs: [test, lint] + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: '1.23' + cache: true + + - name: Build binary (Linux) + run: CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o epage-linux ./src + + - name: Build binary (macOS) + run: CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -o epage-macos ./src + + - name: Build binary (Windows) + run: CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o epage-windows.exe ./src + + - name: Upload artifacts + uses: actions/upload-artifact@v3 + with: + name: binaries + path: | + epage-linux + epage-macos + epage-windows.exe diff --git a/.gitea/workflows/sonarqube.yml b/.gitea/workflows/sonarqube.yml new file mode 100644 index 0000000..4a7c7d2 --- /dev/null +++ b/.gitea/workflows/sonarqube.yml @@ -0,0 +1,31 @@ +name: SonarQube Scan + +on: + push: + branches: + - main + +jobs: + sonarqube: + name: SonarQube Analysis + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: '1.23' + cache: true + + - name: Run tests with coverage + run: go test -v -coverprofile=coverage.out ./src + + - name: Run SonarQube scanner + uses: SonarSource/sonarqube-scan-action@v7.1.0 + env: + SONAR_HOST_URL: ${{ secrets.SONAR_URL }} + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} diff --git a/SONARQUBE.md b/SONARQUBE.md new file mode 100644 index 0000000..9e2bd55 --- /dev/null +++ b/SONARQUBE.md @@ -0,0 +1,68 @@ +# SonarQube Configuration + +This project uses SonarQube for code quality analysis. + +## Setup + +To enable SonarQube scanning, you need to configure the following GitHub/Gitea secrets: + +### Secrets Required + +- `SONAR_HOST_URL`: Your SonarQube server URL (e.g., `https://sonarqube.example.com`) +- `SONAR_LOGIN`: Your SonarQube authentication token + +### Adding Secrets in Gitea + +1. Go to your repository settings +2. Navigate to "Secrets and variables" → "Actions" +3. Add the following secrets: + - `SONAR_HOST_URL` + - `SONAR_LOGIN` + +## SonarQube Properties + +The `sonar-project.properties` file configures: + +- **Project Key**: `epage-go` +- **Sources**: `src/` directory +- **Tests**: All `*_test.go` files in `src/` +- **Coverage**: Reports from `coverage.out` +- **Exclusions**: Test files excluded from main analysis + +## Workflow Triggers + +- **CI Workflow** (`.gitea/workflows/ci.yml`): + - Runs on: Pull requests to `main` or `develop` + - Tests, lints, and builds the application + +- **SonarQube Workflow** (`.gitea/workflows/sonarqube.yml`): + - Runs on: Pushes to `main` branch + - Generates coverage reports and uploads to SonarQube + +## Local SonarQube Scanning + +To scan locally (requires SonarQube CLI): + +```bash +# Generate coverage report +go test -coverprofile=coverage.out ./src + +# Run SonarQube scanner +sonar-scanner \ + -Dsonar.projectKey=epage-go \ + -Dsonar.sources=src \ + -Dsonar.host.url=https://sonarqube.example.com \ + -Dsonar.login=your_token +``` + +## Coverage Reports + +Test coverage is automatically collected and can be viewed: + +```bash +# Run tests with coverage +go test -v -coverprofile=coverage.out ./src + +# View coverage report +go tool cover -html=coverage.out +``` diff --git a/sonar-project.properties b/sonar-project.properties new file mode 100644 index 0000000..2210649 --- /dev/null +++ b/sonar-project.properties @@ -0,0 +1,26 @@ +sonar.projectKey=epage-go +sonar.projectName=epage-go +sonar.projectVersion=1.0.0 + +# Source code location +sonar.sources=src +sonar.sourceEncoding=UTF-8 + +# Exclude test files from analysis +sonar.exclusions=**/*_test.go + +# Test configuration +sonar.tests=src +sonar.test.inclusions=**/*_test.go + +# Coverage reports +sonar.go.coverage.reportPaths=coverage.out + +# Code coverage exclusions +sonar.coverage.exclusions=**/*_test.go + +# Quality gates +sonar.qualitygate.wait=true + +# Language +sonar.language=go