Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@ReneWerner87
Copy link
Member

@ReneWerner87 ReneWerner87 commented Jul 27, 2025

Summary

  • add migrator to update OnShutdown hooks to OnPostShutdown
  • register the migrator in migration list
  • add tests for the new migration

Testing

  • go test ./...

https://chatgpt.com/codex/tasks/task_e_68865a5ab46c8326a0c37cf1f1d4736e

Summary by CodeRabbit

  • New Features

    • Added automated migration to update deprecated shutdown hook usage to the new format, ensuring compatibility with future versions.
  • Tests

    • Introduced tests to verify that shutdown hooks are correctly migrated and that migration feedback is provided to users.

@coderabbitai
Copy link

coderabbitai bot commented Jul 27, 2025

Walkthrough

A new migration function, MigrateShutdownHook, was introduced to update deprecated shutdown hook usage in Go code. This function is integrated into the migration sequence for versions between 2.0.0 and 4.0.0-0. Accompanying this, a test ensures the migration rewrites the relevant code as intended.

Changes

File(s) Change Summary
cmd/internal/migrations/lists.go Added MigrateShutdownHook to the migration sequence for versions >=2.0.0 and <4.0.0-0.
cmd/internal/migrations/v3/common.go Implemented MigrateShutdownHook to replace deprecated shutdown hook usages in Go source files.
cmd/internal/migrations/v3/common_test.go Added Test_MigrateShutdownHook to verify correct migration and output messaging.

Sequence Diagram(s)

sequenceDiagram
    participant Test as Test_MigrateShutdownHook
    participant Migrator as MigrateShutdownHook
    participant File as Go Source File

    Test->>File: Write file with old shutdown hook
    Test->>Migrator: Call MigrateShutdownHook on directory
    Migrator->>File: Read Go source file
    Migrator->>File: Replace OnShutdown with OnPostShutdown and update function signature
    Migrator-->>Test: Print migration confirmation
    Test->>File: Verify file content updated
    Test->>Test: Assert output contains migration message
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Suggested labels

✏️ Feature

Poem

In the warren of code, a hook did reside,
Now it’s PostShutdown, with error by its side.
Migrations march forward, with tests in the lead,
Ensuring all bunnies have just what they need.
With a hop and a print, the changes are done—
The code is refreshed, and bugs are none! 🐇✨

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch codex/2025-07-27-17-09-13

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @coderabbitai title anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions github-actions bot added the ✏️ Feature New feature or request label Jul 27, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Summary of Changes

Hello @ReneWerner87, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces an automated migration to facilitate the transition from the deprecated OnShutdown hook to the new OnPostShutdown hook. The migration handles both the renaming of the hook and the necessary adjustment of inline function signatures to include an error parameter, aiming to simplify the upgrade path for users.

Highlights

  • New Migration Added: A new migration function, MigrateShutdownHook, has been introduced to automatically update deprecated OnShutdown hooks to OnPostShutdown.
  • Function Signature Adaptation: The MigrateShutdownHook not only renames the hook but also adapts inline hook functions to accept an error parameter, aligning with the updated OnPostShutdown signature.
  • Migration Registration: The new MigrateShutdownHook has been registered in the v3 migration list, ensuring it is executed as part of the standard migration process.
  • Comprehensive Testing: A dedicated test case (Test_MigrateShutdownHook) has been added to verify the correct functionality of the new migrator, including its ability to transform file content and log migration messages.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments or fill out our survey to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request adds a new migrator to update OnShutdown hooks to OnPostShutdown. I've provided suggestions to improve the performance and robustness of the regular expression-based replacement, and to enhance the new test to cover more edge cases.

Comment on lines +831 to +852
func Test_MigrateShutdownHook(t *testing.T) {
t.Parallel()

dir, err := os.MkdirTemp("", "mhooks")
require.NoError(t, err)
defer func() { require.NoError(t, os.RemoveAll(dir)) }()

file := writeTempFile(t, dir, `package main
import "github.com/gofiber/fiber/v2"
func main() {
app := fiber.New()
app.Hooks().OnShutdown(func() error { return nil })
}`)

var buf bytes.Buffer
cmd := newCmd(&buf)
require.NoError(t, v3.MigrateShutdownHook(cmd, dir, nil, nil))

content := readFile(t, file)
assert.Contains(t, content, `.Hooks().OnPostShutdown(func(err error) error {`)
assert.Contains(t, buf.String(), "Migrating shutdown hooks")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

Consider converting this test to a table-driven test to improve robustness and ensure various formatting styles are handled correctly. This would make it easier to add more scenarios, such as different whitespace arrangements or function references instead of inline functions.

@ReneWerner87 ReneWerner87 merged commit 946f202 into master Jul 27, 2025
12 of 13 checks passed
@ReneWerner87 ReneWerner87 deleted the codex/2025-07-27-17-09-13 branch July 27, 2025 17:13
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
cmd/internal/migrations/v3/common.go (1)

405-406: Consider edge cases for the method name replacement.

The regex pattern \.Hooks\(\)\.OnShutdown\( should work for standard usage, but consider if there are edge cases with whitespace or different formatting that might be missed.

For more robust matching, consider:

-		reName := regexp.MustCompile(`\.Hooks\(\)\.OnShutdown\(`)
+		reName := regexp.MustCompile(`\.Hooks\(\)\s*\.OnShutdown\s*\(`)
cmd/internal/migrations/v3/common_test.go (1)

838-843: Consider testing additional shutdown hook patterns.

While the current test covers the basic inline function case, consider adding test cases for:

  • Named functions passed to OnShutdown
  • Multiple OnShutdown calls in the same file
  • Different formatting/whitespace variations

Example additional test case:

file := writeTempFile(t, dir, `package main
import "github.com/gofiber/fiber/v2"
func shutdownHandler() error { return nil }
func main() {
    app := fiber.New()
    app.Hooks().OnShutdown(shutdownHandler)
    app.Hooks().OnShutdown(func() error { 
        return nil 
    })
}`)
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1035f1a and 1af6282.

📒 Files selected for processing (3)
  • cmd/internal/migrations/lists.go (1 hunks)
  • cmd/internal/migrations/v3/common.go (1 hunks)
  • cmd/internal/migrations/v3/common_test.go (1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: norri
PR: gofiber/recipes#2701
File: clean-code/app/main.go:0-0
Timestamp: 2024-11-23T19:35:36.767Z
Learning: In the Go `clean-code` example (`clean-code/app/main.go`) in the `gofiber/recipes` repository, it's acceptable to omit graceful shutdown handling, as the example code prioritizes simplicity over production-level practices.
Learnt from: efectn
PR: gofiber/fiber#3162
File: app_test.go:893-895
Timestamp: 2024-11-29T12:37:27.581Z
Learning: In the `Test_App_ShutdownWithContext` function in `app_test.go`, the `clientDone` channel is used to synchronize the client's request completion before proceeding, eliminating the need for additional `time.Sleep` calls.
Learnt from: ReneWerner87
PR: gofiber/fiber#3161
File: app.go:923-932
Timestamp: 2024-11-15T07:56:21.623Z
Learning: In the Fiber framework, breaking changes are acceptable when moving from version 2 to version 3, including modifications to method signatures such as in the `Test` method in `app.go`.
cmd/internal/migrations/v3/common_test.go (10)

Learnt from: ReneWerner87
PR: gofiber/fiber#3161
File: app.go:923-932
Timestamp: 2024-11-15T07:56:21.623Z
Learning: In the Fiber framework, breaking changes are acceptable when moving from version 2 to version 3, including modifications to method signatures such as in the Test method in app.go.

Learnt from: efectn
PR: gofiber/fiber#3162
File: app_test.go:893-895
Timestamp: 2024-11-29T12:37:27.581Z
Learning: In the Test_App_ShutdownWithContext function in app_test.go, the clientDone channel is used to synchronize the client's request completion before proceeding, eliminating the need for additional time.Sleep calls.

Learnt from: sixcolors
PR: gofiber/fiber#3016
File: middleware/session/store.go:164-167
Timestamp: 2024-10-02T23:03:31.727Z
Learning: Unit tests in this project use testify require.

Learnt from: sixcolors
PR: gofiber/fiber#3016
File: middleware/session/store.go:164-167
Timestamp: 2024-10-08T19:06:06.583Z
Learning: Unit tests in this project use testify require.

Learnt from: norri
PR: gofiber/recipes#2701
File: clean-code/app/main.go:0-0
Timestamp: 2024-11-23T19:35:36.767Z
Learning: In the Go clean-code example (clean-code/app/main.go) in the gofiber/recipes repository, it's acceptable to omit graceful shutdown handling, as the example code prioritizes simplicity over production-level practices.

Learnt from: sixcolors
PR: gofiber/fiber#2922
File: middleware/cors/utils.go:63-71
Timestamp: 2024-07-26T21:00:12.902Z
Learning: The project uses the testify/assert package for assertions in unit tests.

Learnt from: sixcolors
PR: gofiber/fiber#2922
File: middleware/cors/utils.go:63-71
Timestamp: 2024-10-08T19:06:06.583Z
Learning: The project uses the testify/assert package for assertions in unit tests.

Learnt from: sixcolors
PR: gofiber/fiber#3051
File: middleware/session/session.go:215-216
Timestamp: 2024-06-30T00:38:06.580Z
Learning: Parallel tests for Session.Save already exist in the middleware/session/session_test.go file, specifically in the Test_Session_Save and Test_Session_Save_Expiration functions.

Learnt from: sixcolors
PR: gofiber/fiber#3051
File: middleware/session/session.go:215-216
Timestamp: 2024-10-08T19:06:06.583Z
Learning: Parallel tests for Session.Save already exist in the middleware/session/session_test.go file, specifically in the Test_Session_Save and Test_Session_Save_Expiration functions.

Learnt from: efectn
PR: gofiber/fiber#3162
File: hooks_test.go:228-228
Timestamp: 2024-12-13T08:14:22.851Z
Learning: In Go test files, prefer using the require methods from the testify package for assertions instead of manual comparisons and calls to t.Fatal or t.Fatalf.

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: Build (1.24.x, windows-latest)
🔇 Additional comments (4)
cmd/internal/migrations/v3/common.go (2)

401-419: LGTM! Clean implementation following established patterns.

The migration function correctly updates both the hook method name and inline function signatures. The regex patterns are appropriate for the typical usage patterns in Fiber applications.


408-409: Replacement logic covers all inline callback cases

I searched the entire repo for OnShutdown hooks and only found inline usages in common_test.go of the form:

  • app.Hooks().OnShutdown(func() error { … })
  • OnShutdownError: func(err error) { … }
  • OnShutdownSuccess: func() { … }

Your two‐step migration already handles all of these:

  • reName renames .Hooks().OnShutdown(.Hooks().OnPostShutdown(
  • reInline’s pattern \.OnPostShutdown\(func\(\s*\) matches both func() and func( ) and injects err, yielding func(err error)

No other formatting variations or variable‐passed hooks were detected. No further changes needed.

cmd/internal/migrations/lists.go (1)

44-44: Perfect placement in the migration sequence.

The MigrateShutdownHook function is logically positioned between MigrateListenerCallbacks and MigrateListenMethods, which makes sense as they're all related to application lifecycle and shutdown handling.

cmd/internal/migrations/v3/common_test.go (1)

831-852: Comprehensive test coverage following established patterns.

The test effectively validates both the method name migration and function signature transformation. It properly uses testify assertions and follows the same structure as other migration tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

codex ✏️ Feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants