From e9fd36b22da810e3f8d5dea528f0d5ab85952e8d Mon Sep 17 00:00:00 2001 From: HugeFrog24 <62775760+HugeFrog24@users.noreply.github.com> Date: Thu, 24 Oct 2024 00:18:32 +0200 Subject: [PATCH] docs rm unused params fix test test workflow security add ci badge --- .github/workflows/go-ci.yaml | 60 +++++++++++++++++++----------------- README.md | 11 +++++++ bot.go | 6 ++-- config.go | 12 +++++--- config_test.go | 8 +++-- handlers.go | 2 +- 6 files changed, 61 insertions(+), 38 deletions(-) diff --git a/.github/workflows/go-ci.yaml b/.github/workflows/go-ci.yaml index 64413b1..774c1de 100755 --- a/.github/workflows/go-ci.yaml +++ b/.github/workflows/go-ci.yaml @@ -7,23 +7,15 @@ on: branches: [ main ] jobs: - build: + # Common setup job that other jobs can depend on + setup: runs-on: ubuntu-latest - steps: - # Checkout the repository - - name: Checkout code - uses: actions/checkout@v4 - - # Set up Go environment - - name: Set up Go - uses: actions/setup-go@v5 + - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 with: - go-version: '1.23' # Specify the Go version you are using - - # Cache Go modules - - name: Cache Go modules - uses: actions/cache@v4 + go-version: '1.23' + - uses: actions/cache@v4 with: path: | ~/.cache/go-build @@ -31,24 +23,36 @@ jobs: key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} restore-keys: | ${{ runner.os }}-go- + - run: go mod tidy - # Install Dependencies - - name: Install Dependencies - run: go mod tidy - - # Run Linters using golangci-lint - - name: Lint Code - uses: golangci/golangci-lint-action@v6 + # Lint job + lint: + needs: setup + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: golangci/golangci-lint-action@v6 with: - version: v1.60 # Specify the version of golangci-lint + version: v1.60 args: --timeout 5m - # Run Tests - - name: Run Tests - run: go test ./... -v + # Test job + test: + needs: setup + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 + with: + go-version: '1.23' + - run: go test ./... -v - # Security Analysis using gosec - - name: Security Scan - uses: securego/gosec@master + # Security scan job + security: + needs: setup + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: securego/gosec@master with: args: ./... diff --git a/README.md b/README.md index 765230a..ed6af73 100755 --- a/README.md +++ b/README.md @@ -108,3 +108,14 @@ View errors: ```bash journalctl -u telegram-bot -p err ``` + +## Testing + +The GitHub actions workflow already runs tests on every commit: + +> [![CI](https://github.com/HugeFrog24/go-telegram-bot/actions/workflows/go-ci.yaml/badge.svg?branch=main)](https://github.com/HugeFrog24/go-telegram-bot/actions/workflows/go-ci.yaml) + +However, you can run the tests locally using: +```bash +go test -race -v ./... +``` diff --git a/bot.go b/bot.go index 7f15876..ec45c30 100755 --- a/bot.go +++ b/bot.go @@ -279,7 +279,7 @@ func initTelegramBot(token string, handleUpdate func(ctx context.Context, tgBot func (b *Bot) sendResponse(ctx context.Context, chatID int64, text string, businessConnectionID string) error { // Pass the outgoing message through the centralized screen for storage - _, err := b.screenOutgoingMessage(chatID, text, businessConnectionID) + _, err := b.screenOutgoingMessage(chatID, text) if err != nil { ErrorLogger.Printf("Error storing assistant message: %v", err) return err @@ -306,7 +306,7 @@ func (b *Bot) sendResponse(ctx context.Context, chatID int64, text string, busin } // sendStats sends the bot statistics to the specified chat. -func (b *Bot) sendStats(ctx context.Context, chatID int64, userID int64, username string, businessConnectionID string) { +func (b *Bot) sendStats(ctx context.Context, chatID int64, businessConnectionID string) { totalUsers, totalMessages, err := b.getStats() if err != nil { ErrorLogger.Printf("Error fetching stats: %v\n", err) @@ -425,7 +425,7 @@ func (b *Bot) screenIncomingMessage(message *models.Message) (Message, error) { } // screenOutgoingMessage handles storing of outgoing messages. -func (b *Bot) screenOutgoingMessage(chatID int64, response string, businessConnectionID string) (Message, error) { +func (b *Bot) screenOutgoingMessage(chatID int64, response string) (Message, error) { assistantMessage := b.createMessage(chatID, 0, "", string(anthropic.RoleAssistant), response, false) // Store the message. diff --git a/config.go b/config.go index 370fcd4..904b784 100755 --- a/config.go +++ b/config.go @@ -143,9 +143,11 @@ func validateConfig(config *BotConfig, ids, tokens map[string]bool) error { func loadConfig(filename string) (BotConfig, error) { var config BotConfig - file, err := os.OpenFile(filename, os.O_RDONLY, 0) + // Use filepath.Clean before opening the file + cleanPath := filepath.Clean(filename) + file, err := os.OpenFile(cleanPath, os.O_RDONLY, 0) if err != nil { - return config, fmt.Errorf("failed to open config file %s: %w", filename, err) + return config, fmt.Errorf("failed to open config file %s: %w", cleanPath, err) } defer file.Close() @@ -165,9 +167,11 @@ func (c *BotConfig) Reload(configDir, filename string) error { return fmt.Errorf("invalid config path: %w", err) } - file, err := os.OpenFile(validPath, os.O_RDONLY, 0) + // Use filepath.Clean before opening the file + cleanPath := filepath.Clean(validPath) + file, err := os.OpenFile(cleanPath, os.O_RDONLY, 0) if err != nil { - return fmt.Errorf("failed to open config file %s: %w", validPath, err) + return fmt.Errorf("failed to open config file %s: %w", cleanPath, err) } defer file.Close() diff --git a/config_test.go b/config_test.go index b4aba5d..0fcd945 100755 --- a/config_test.go +++ b/config_test.go @@ -477,8 +477,12 @@ func TestLoadAllConfigs(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { // Clear the tempDir before each test - os.RemoveAll(tempDir) - os.MkdirAll(tempDir, 0755) + if err := os.RemoveAll(tempDir); err != nil { + t.Fatalf("Failed to remove temp dir: %v", err) + } + if err := os.MkdirAll(tempDir, 0755); err != nil { + t.Fatalf("Failed to create temp dir: %v", err) + } // Write the test files directly for filename, content := range tt.setupFiles { diff --git a/handlers.go b/handlers.go index a3e370a..9fbe698 100755 --- a/handlers.go +++ b/handlers.go @@ -48,7 +48,7 @@ func (b *Bot) handleUpdate(ctx context.Context, tgBot *bot.Bot, update *models.U command := strings.TrimSpace(message.Text[entity.Offset : entity.Offset+entity.Length]) switch command { case "/stats": - b.sendStats(ctx, chatID, userID, username, businessConnectionID) + b.sendStats(ctx, chatID, businessConnectionID) return case "/whoami": b.sendWhoAmI(ctx, chatID, userID, username, businessConnectionID)