Files
gastown/internal/mq/id.go
Steve Yegge 4048cdc373 fix(lint): resolve all errcheck warnings
Fix ~50 errcheck warnings across the codebase:

- Add explicit `_ =` for intentionally ignored error returns (cleanup,
  best-effort operations, etc.)
- Use `defer func() { _ = ... }()` pattern for defer statements
- Handle tmux SetEnvironment, KillSession, SendKeysRaw returns
- Handle mail router.Send returns
- Handle os.RemoveAll, os.Rename in cleanup paths
- Handle rand.Read returns for ID generation
- Handle fmt.Fprint* returns when writing to io.Writer
- Fix for-select with single case to use for-range
- Handle cobra MarkFlagRequired returns

All tests pass. Code compiles without errors.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-19 12:44:42 -08:00

50 lines
1.7 KiB
Go

// Package mq provides merge queue functionality.
package mq
import (
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"fmt"
"time"
)
// GenerateMRID generates a merge request ID following the convention: <prefix>-mr-<hash>
//
// The hash is derived from the branch name + current timestamp + random bytes to ensure uniqueness.
// Example: gt-mr-abc123 for a gastown merge request.
//
// Parameters:
// - prefix: The project prefix (e.g., "gt" for gastown)
// - branch: The source branch name (e.g., "polecat/Nux/gt-xyz")
//
// Returns a string in the format "<prefix>-mr-<6-char-hash>"
func GenerateMRID(prefix, branch string) string {
// Generate 8 random bytes for additional uniqueness
randomBytes := make([]byte, 8)
_, _ = rand.Read(randomBytes)
return generateMRIDInternal(prefix, branch, time.Now(), randomBytes)
}
// GenerateMRIDWithTime generates a merge request ID using a specific timestamp.
// This is primarily useful for testing to ensure deterministic output.
// Note: Without randomness, two calls with identical inputs will produce the same ID.
func GenerateMRIDWithTime(prefix, branch string, timestamp time.Time) string {
return generateMRIDInternal(prefix, branch, timestamp, nil)
}
// generateMRIDInternal is the internal implementation that combines all inputs.
func generateMRIDInternal(prefix, branch string, timestamp time.Time, randomBytes []byte) string {
// Combine branch, timestamp, and optional random bytes for uniqueness
input := fmt.Sprintf("%s:%d:%x", branch, timestamp.UnixNano(), randomBytes)
// Generate SHA256 hash
hash := sha256.Sum256([]byte(input))
// Take first 6 characters of hex-encoded hash
hashStr := hex.EncodeToString(hash[:])[:6]
return fmt.Sprintf("%s-mr-%s", prefix, hashStr)
}