Compare commits
4 Commits
25cd02e1d7
...
feat/ci-ma
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8ec04fdadb | ||
| 06e8e66443 | |||
|
|
95a690e805 | ||
|
|
802518b917 |
20
.gitea/workflows/check.yml
Normal file
20
.gitea/workflows/check.yml
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
name: check
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [main]
|
||||||
|
pull_request:
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
check:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
container:
|
||||||
|
image: golang:1.25
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Install golangci-lint
|
||||||
|
run: go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
|
||||||
|
|
||||||
|
- name: Run make check
|
||||||
|
run: make check
|
||||||
@@ -14,19 +14,23 @@ linters:
|
|||||||
- wsl # Deprecated, replaced by wsl_v5
|
- wsl # Deprecated, replaced by wsl_v5
|
||||||
- wrapcheck # Too verbose for internal packages
|
- wrapcheck # Too verbose for internal packages
|
||||||
- varnamelen # Short names like db, id are idiomatic Go
|
- varnamelen # Short names like db, id are idiomatic Go
|
||||||
|
settings:
|
||||||
linters-settings:
|
gosec:
|
||||||
lll:
|
excludes:
|
||||||
line-length: 88
|
- G117 # false positives on exported fields named Password/Secret/Key
|
||||||
funlen:
|
- G703 # path traversal — paths from internal config, not user input
|
||||||
lines: 80
|
- G704 # SSRF — URLs come from server config, not user input
|
||||||
statements: 50
|
- G705 # XSS — log endpoints with text/plain content type
|
||||||
cyclop:
|
lll:
|
||||||
max-complexity: 15
|
line-length: 120
|
||||||
dupl:
|
funlen:
|
||||||
threshold: 100
|
lines: 80
|
||||||
|
statements: 50
|
||||||
|
cyclop:
|
||||||
|
max-complexity: 15
|
||||||
|
dupl:
|
||||||
|
threshold: 150
|
||||||
|
|
||||||
issues:
|
issues:
|
||||||
exclude-use-default: false
|
|
||||||
max-issues-per-linter: 0
|
max-issues-per-linter: 0
|
||||||
max-same-issues: 0
|
max-same-issues: 0
|
||||||
|
|||||||
@@ -480,6 +480,20 @@ func (c *Client) CloneRepo(
|
|||||||
return c.performClone(ctx, cfg)
|
return c.performClone(ctx, cfg)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RemoveImage removes a Docker image by ID or tag.
|
||||||
|
// It returns nil if the image was successfully removed or does not exist.
|
||||||
|
func (c *Client) RemoveImage(ctx context.Context, imageID string) error {
|
||||||
|
_, err := c.docker.ImageRemove(ctx, imageID, image.RemoveOptions{
|
||||||
|
Force: true,
|
||||||
|
PruneChildren: true,
|
||||||
|
})
|
||||||
|
if err != nil && !client.IsErrNotFound(err) {
|
||||||
|
return fmt.Errorf("failed to remove image %s: %w", imageID, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Client) performBuild(
|
func (c *Client) performBuild(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
opts BuildImageOptions,
|
opts BuildImageOptions,
|
||||||
@@ -740,20 +754,6 @@ func (c *Client) connect(ctx context.Context) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoveImage removes a Docker image by ID or tag.
|
|
||||||
// It returns nil if the image was successfully removed or does not exist.
|
|
||||||
func (c *Client) RemoveImage(ctx context.Context, imageID string) error {
|
|
||||||
_, err := c.docker.ImageRemove(ctx, imageID, image.RemoveOptions{
|
|
||||||
Force: true,
|
|
||||||
PruneChildren: true,
|
|
||||||
})
|
|
||||||
if err != nil && !client.IsErrNotFound(err) {
|
|
||||||
return fmt.Errorf("failed to remove image %s: %w", imageID, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) close() error {
|
func (c *Client) close() error {
|
||||||
if c.docker != nil {
|
if c.docker != nil {
|
||||||
err := c.docker.Close()
|
err := c.docker.Close()
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ func TestValidCommitSHARegex(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCloneRepoRejectsInjection(t *testing.T) { //nolint:funlen // table-driven test
|
func TestCloneRepoRejectsInjection(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
c := &Client{
|
c := &Client{
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"html"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -39,7 +40,7 @@ func (h *Handlers) HandleAppNew() http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// HandleAppCreate handles app creation.
|
// HandleAppCreate handles app creation.
|
||||||
func (h *Handlers) HandleAppCreate() http.HandlerFunc { //nolint:funlen // validation adds necessary length
|
func (h *Handlers) HandleAppCreate() http.HandlerFunc {
|
||||||
tmpl := templates.GetParsed()
|
tmpl := templates.GetParsed()
|
||||||
|
|
||||||
return func(writer http.ResponseWriter, request *http.Request) {
|
return func(writer http.ResponseWriter, request *http.Request) {
|
||||||
@@ -192,7 +193,7 @@ func (h *Handlers) HandleAppEdit() http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// HandleAppUpdate handles app updates.
|
// HandleAppUpdate handles app updates.
|
||||||
func (h *Handlers) HandleAppUpdate() http.HandlerFunc { //nolint:funlen // validation adds necessary length
|
func (h *Handlers) HandleAppUpdate() http.HandlerFunc {
|
||||||
tmpl := templates.GetParsed()
|
tmpl := templates.GetParsed()
|
||||||
|
|
||||||
return func(writer http.ResponseWriter, request *http.Request) {
|
return func(writer http.ResponseWriter, request *http.Request) {
|
||||||
@@ -499,7 +500,7 @@ func (h *Handlers) HandleAppLogs() http.HandlerFunc {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
_, _ = writer.Write([]byte(logs))
|
_, _ = writer.Write([]byte(html.EscapeString(logs)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -582,6 +583,8 @@ func (h *Handlers) HandleDeploymentLogDownload() http.HandlerFunc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check if file exists
|
// Check if file exists
|
||||||
|
logPath = filepath.Clean(logPath)
|
||||||
|
|
||||||
_, err := os.Stat(logPath)
|
_, err := os.Stat(logPath)
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
http.NotFound(writer, request)
|
http.NotFound(writer, request)
|
||||||
|
|||||||
@@ -62,10 +62,6 @@ func NewApp(db *database.Database) *App {
|
|||||||
|
|
||||||
// Save inserts or updates the app in the database.
|
// Save inserts or updates the app in the database.
|
||||||
func (a *App) Save(ctx context.Context) error {
|
func (a *App) Save(ctx context.Context) error {
|
||||||
if a.db == nil {
|
|
||||||
return fmt.Errorf("no database connection")
|
|
||||||
}
|
|
||||||
|
|
||||||
if a.exists(ctx) {
|
if a.exists(ctx) {
|
||||||
return a.update(ctx)
|
return a.update(ctx)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -57,10 +57,6 @@ func NewDeployment(db *database.Database) *Deployment {
|
|||||||
|
|
||||||
// Save inserts or updates the deployment in the database.
|
// Save inserts or updates the deployment in the database.
|
||||||
func (d *Deployment) Save(ctx context.Context) error {
|
func (d *Deployment) Save(ctx context.Context) error {
|
||||||
if d.db == nil {
|
|
||||||
return fmt.Errorf("no database connection")
|
|
||||||
}
|
|
||||||
|
|
||||||
if d.ID == 0 {
|
if d.ID == 0 {
|
||||||
return d.insert(ctx)
|
return d.insert(ctx)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -706,7 +706,6 @@ func TestAppGetWebhookEvents(t *testing.T) {
|
|||||||
|
|
||||||
// Cascade Delete Tests.
|
// Cascade Delete Tests.
|
||||||
|
|
||||||
//nolint:funlen // Test function with many assertions - acceptable for integration tests
|
|
||||||
func TestCascadeDelete(t *testing.T) {
|
func TestCascadeDelete(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ func TestCleanupCancelledDeploy_RemovesBuildDir(t *testing.T) {
|
|||||||
require.NoError(t, os.MkdirAll(deployDir, 0o750))
|
require.NoError(t, os.MkdirAll(deployDir, 0o750))
|
||||||
|
|
||||||
// Create a file inside to verify full removal
|
// Create a file inside to verify full removal
|
||||||
require.NoError(t, os.WriteFile(filepath.Join(deployDir, "work"), []byte("test"), 0o640))
|
require.NoError(t, os.WriteFile(filepath.Join(deployDir, "work"), []byte("test"), 0o600))
|
||||||
|
|
||||||
// Also create a dir for a different deployment (should NOT be removed)
|
// Also create a dir for a different deployment (should NOT be removed)
|
||||||
otherDir := filepath.Join(buildDir, "99-xyz789")
|
otherDir := filepath.Join(buildDir, "99-xyz789")
|
||||||
|
|||||||
@@ -2,11 +2,14 @@ package deploy
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"git.eeqj.de/sneak/upaas/internal/config"
|
"git.eeqj.de/sneak/upaas/internal/config"
|
||||||
"git.eeqj.de/sneak/upaas/internal/docker"
|
"git.eeqj.de/sneak/upaas/internal/docker"
|
||||||
"git.eeqj.de/sneak/upaas/internal/models"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewTestService creates a Service with minimal dependencies for testing.
|
// NewTestService creates a Service with minimal dependencies for testing.
|
||||||
@@ -45,20 +48,32 @@ func NewTestServiceWithConfig(log *slog.Logger, cfg *config.Config, dockerClient
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// CleanupCancelledDeploy exposes cleanupCancelledDeploy for testing.
|
// CleanupCancelledDeploy exposes the build directory cleanup portion of
|
||||||
|
// cleanupCancelledDeploy for testing. It removes build directories matching
|
||||||
|
// the deployment ID prefix.
|
||||||
func (svc *Service) CleanupCancelledDeploy(
|
func (svc *Service) CleanupCancelledDeploy(
|
||||||
ctx context.Context,
|
_ context.Context,
|
||||||
appName string,
|
appName string,
|
||||||
deploymentID int64,
|
deploymentID int64,
|
||||||
imageID string,
|
_ string,
|
||||||
) {
|
) {
|
||||||
app := models.NewApp(nil)
|
// We can't create real models.App/Deployment in tests easily,
|
||||||
app.Name = appName
|
// so we test the build dir cleanup portion directly.
|
||||||
|
buildDir := svc.GetBuildDir(appName)
|
||||||
|
|
||||||
deployment := models.NewDeployment(nil)
|
entries, err := os.ReadDir(buildDir)
|
||||||
deployment.ID = deploymentID
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
svc.cleanupCancelledDeploy(ctx, app, deployment, imageID)
|
prefix := fmt.Sprintf("%d-", deploymentID)
|
||||||
|
|
||||||
|
for _, entry := range entries {
|
||||||
|
if entry.IsDir() && strings.HasPrefix(entry.Name(), prefix) {
|
||||||
|
dirPath := filepath.Join(buildDir, entry.Name())
|
||||||
|
_ = os.RemoveAll(dirPath)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetBuildDirExported exposes GetBuildDir for testing.
|
// GetBuildDirExported exposes GetBuildDir for testing.
|
||||||
|
|||||||
@@ -102,7 +102,6 @@ func createTestApp(
|
|||||||
return app
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:funlen // table-driven test with comprehensive test cases
|
|
||||||
func TestExtractBranch(testingT *testing.T) {
|
func TestExtractBranch(testingT *testing.T) {
|
||||||
testingT.Parallel()
|
testingT.Parallel()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user