Skip to content

[Repo Assist] refactor(auth): move generateRandomAPIKey to internal/auth package#3163

Merged
lpcox merged 1 commit intomainfrom
repo-assist/refactor-generateRandomAPIKey-2026-04-04-ec0a67b5375d7bb4
Apr 4, 2026
Merged

[Repo Assist] refactor(auth): move generateRandomAPIKey to internal/auth package#3163
lpcox merged 1 commit intomainfrom
repo-assist/refactor-generateRandomAPIKey-2026-04-04-ec0a67b5375d7bb4

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

@github-actions github-actions bot commented Apr 4, 2026

🤖 This pull request was created by Repo Assist, an automated AI assistant.

Summary

Moves generateRandomAPIKey from internal/cmd/root.go to internal/auth/ as the exported GenerateRandomAPIKey function. API key generation is an authentication concern; the internal/auth package is the natural home.

Changes

  • internal/auth/apikey.go — new file with GenerateRandomAPIKey() function
  • internal/auth/apikey_test.go — test migrated from cmd/root_test.go
  • internal/cmd/root.go — call site updated to auth.GenerateRandomAPIKey(); crypto/rand and encoding/hex imports removed
  • internal/cmd/root_test.goTestGenerateRandomAPIKey removed (now lives in auth package)

Motivation

  • Aligns the function's location with its semantic purpose (authentication)
  • Removes two low-level stdlib imports (crypto/rand, encoding/hex) from the CLI entry-point package
  • Makes GenerateRandomAPIKey reusable by any package that imports internal/auth

Test Status

⚠️ Infrastructure limitation: The CI environment has Go 1.24.13 locally but go.mod requires Go 1.25.0, and the toolchain download is blocked by the network firewall. make agent-finished could not complete. The changes are syntactically verified by code review:

  • Correct import paths, correct exported name, existing test logic preserved verbatim
  • No functional changes to the gateway's behaviour

Tests will run normally in the GitHub Actions CI environment where Go 1.25.0 is available.

Generated by Repo Assist ·

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@851905c06e905bf362a9f6cc54f912e3df747d55

The function is about API key generation for authentication purposes,
making internal/auth the natural home. Export it as GenerateRandomAPIKey
so cmd/root.go can call it via the auth package interface.

- Add internal/auth/apikey.go with GenerateRandomAPIKey()
- Add internal/auth/apikey_test.go (moved from cmd/root_test.go)
- Update cmd/root.go: add auth import, remove crypto/rand & encoding/hex
- Remove TestGenerateRandomAPIKey from cmd/root_test.go (moved to auth)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@lpcox lpcox marked this pull request as ready for review April 4, 2026 18:22
Copilot AI review requested due to automatic review settings April 4, 2026 18:22
@lpcox lpcox merged commit 5f2acfb into main Apr 4, 2026
3 checks passed
@lpcox lpcox deleted the repo-assist/refactor-generateRandomAPIKey-2026-04-04-ec0a67b5375d7bb4 branch April 4, 2026 18:22
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors API key generation by moving generateRandomAPIKey out of the CLI entrypoint (internal/cmd) into the authentication layer (internal/auth) as an exported helper, making the functionality reusable across the codebase.

Changes:

  • Introduces auth.GenerateRandomAPIKey() in a new internal/auth/apikey.go.
  • Updates the CLI startup path to call auth.GenerateRandomAPIKey() when no API key is configured.
  • Migrates the API key generation unit test from internal/cmd/root_test.go into internal/auth/apikey_test.go.
Show a summary per file
File Description
internal/cmd/root.go Switches API key generation call site to auth.GenerateRandomAPIKey() and removes low-level crypto/encoding imports.
internal/cmd/root_test.go Removes the old generateRandomAPIKey test after migration.
internal/auth/apikey.go Adds exported API key generator implementation to the auth package.
internal/auth/apikey_test.go Adds unit test for auth.GenerateRandomAPIKey() in the auth package.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 4/4 changed files
  • Comments generated: 2

Comment on lines +12 to +16
func GenerateRandomAPIKey() (string, error) {
bytes := make([]byte, 32)
if _, err := rand.Read(bytes); err != nil {
return "", fmt.Errorf("failed to generate random API key: %w", err)
}
Copy link

Copilot AI Apr 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GenerateRandomAPIKey wraps the rand.Read error with "failed to generate random API key", and the caller in run() wraps again with the same message. If this error ever surfaces, it will read redundantly ("failed...: failed..."). Prefer returning the raw rand.Read error from GenerateRandomAPIKey (or using a more specific/sentinel error) and let callers add context once.

Copilot uses AI. Check for mistakes.
Comment on lines +13 to +17
bytes := make([]byte, 32)
if _, err := rand.Read(bytes); err != nil {
return "", fmt.Errorf("failed to generate random API key: %w", err)
}
return hex.EncodeToString(bytes), nil
Copy link

Copilot AI Apr 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor naming: bytes is a very generic identifier and can be confused with the standard bytes package in Go codebases. Consider renaming it to something specific like keyBytes or buf to make the intent clearer.

Suggested change
bytes := make([]byte, 32)
if _, err := rand.Read(bytes); err != nil {
return "", fmt.Errorf("failed to generate random API key: %w", err)
}
return hex.EncodeToString(bytes), nil
keyBytes := make([]byte, 32)
if _, err := rand.Read(keyBytes); err != nil {
return "", fmt.Errorf("failed to generate random API key: %w", err)
}
return hex.EncodeToString(keyBytes), nil

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants