-
-
Notifications
You must be signed in to change notification settings - Fork 605
feat: add Forgejo module #3556
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: add Forgejo module #3556
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6eca958
feat: add Forgejo module
s04 d33ba4f
fix: address review feedback on Forgejo module
s04 e3cb125
fix: add input validation to WithAdminCredentials and WithConfig
s04 1ed8388
fix: make Forgejo container credential fields private
s04 091484b
fix: validate non-empty section and key in WithConfig
s04 dd8f26a
fix: use defaultHTTPPort constant in wait strategy
s04 32fc734
chore: bump Go version
mdelapenya 86d4668
fix: lint
mdelapenya 5dc7000
Merge branch 'main' into feat/add-forgejo-module
mdelapenya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| # Forgejo | ||
|
|
||
| Not available until the next release <a href="https://github.com/testcontainers/testcontainers-go"><span class="tc-version">:material-tag: main</span></a> | ||
|
|
||
| ## Introduction | ||
|
|
||
| The Testcontainers module for [Forgejo](https://forgejo.org/), a self-hosted Git forge. Forgejo is a community-driven fork of Gitea, providing a lightweight code hosting solution. | ||
|
|
||
| ## Adding this module to your project dependencies | ||
|
|
||
| Please run the following command to add the Forgejo module to your Go dependencies: | ||
|
|
||
| ```sh | ||
| go get github.com/testcontainers/testcontainers-go/modules/forgejo | ||
| ``` | ||
|
|
||
| ## Usage example | ||
|
|
||
| <!--codeinclude--> | ||
| [Creating a Forgejo container](../../modules/forgejo/examples_test.go) inside_block:runForgejoContainer | ||
| <!--/codeinclude--> | ||
|
|
||
| ## Module Reference | ||
|
|
||
| ### Run function | ||
|
|
||
| - Not available until the next release <a href="https://github.com/testcontainers/testcontainers-go"><span class="tc-version">:material-tag: main</span></a> | ||
|
|
||
| The Forgejo module exposes one entrypoint function to create the Forgejo container, and this function receives three parameters: | ||
|
|
||
| ```golang | ||
| func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*Container, error) | ||
| ``` | ||
|
|
||
| - `context.Context`, the Go context. | ||
| - `string`, the Docker image to use. | ||
| - `testcontainers.ContainerCustomizer`, a variadic argument for passing options. | ||
|
|
||
| #### Image | ||
|
|
||
| Use the second argument in the `Run` function to set a valid Docker image. | ||
| In example: `Run(context.Background(), "codeberg.org/forgejo/forgejo:11")`. | ||
|
|
||
| ### Container Options | ||
|
|
||
| When starting the Forgejo container, you can pass options in a variadic way to configure it. | ||
|
|
||
| #### Admin Credentials | ||
|
|
||
| - Not available until the next release <a href="https://github.com/testcontainers/testcontainers-go"><span class="tc-version">:material-tag: main</span></a> | ||
|
|
||
| Use `WithAdminCredentials(username, password, email)` to set the admin user credentials. An admin user is automatically created when the container starts. Default credentials are `forgejo-admin` / `forgejo-admin`. | ||
|
|
||
| #### Configuration via Environment | ||
|
|
||
| - Not available until the next release <a href="https://github.com/testcontainers/testcontainers-go"><span class="tc-version">:material-tag: main</span></a> | ||
|
|
||
| Use `WithConfig(section, key, value)` to set Forgejo configuration values using the `FORGEJO__section__key` environment variable format. See the [Forgejo Configuration Cheat Sheet](https://forgejo.org/docs/latest/admin/config-cheat-sheet/) for available options. | ||
|
|
||
| {% include "../features/common_functional_options_list.md" %} | ||
|
|
||
| ### Container Methods | ||
|
|
||
| The Forgejo container exposes the following methods: | ||
|
|
||
| #### ConnectionString | ||
|
|
||
| - Not available until the next release <a href="https://github.com/testcontainers/testcontainers-go"><span class="tc-version">:material-tag: main</span></a> | ||
|
|
||
| The `ConnectionString` method returns the HTTP URL for the Forgejo instance (e.g. `http://localhost:12345`). | ||
|
|
||
| #### SSHConnectionString | ||
|
|
||
| - Not available until the next release <a href="https://github.com/testcontainers/testcontainers-go"><span class="tc-version">:material-tag: main</span></a> | ||
|
|
||
| The `SSHConnectionString` method returns the SSH endpoint for Git operations. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| include ../../commons-test.mk | ||
|
|
||
| .PHONY: test | ||
| test: | ||
| $(MAKE) test-forgejo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package forgejo_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "log" | ||
|
|
||
| "github.com/testcontainers/testcontainers-go" | ||
| "github.com/testcontainers/testcontainers-go/modules/forgejo" | ||
| ) | ||
|
|
||
| func ExampleRun() { | ||
| // runForgejoContainer { | ||
| ctx := context.Background() | ||
|
|
||
| forgejoContainer, err := forgejo.Run(ctx, "codeberg.org/forgejo/forgejo:11") | ||
| defer func() { | ||
| if err := testcontainers.TerminateContainer(forgejoContainer); err != nil { | ||
| log.Printf("failed to terminate container: %s", err) | ||
| } | ||
| }() | ||
| if err != nil { | ||
| log.Printf("failed to start container: %s", err) | ||
| return | ||
| } | ||
| // } | ||
|
|
||
| state, err := forgejoContainer.State(ctx) | ||
| if err != nil { | ||
| log.Printf("failed to get container state: %s", err) | ||
| return | ||
| } | ||
|
|
||
| fmt.Println(state.Running) | ||
|
|
||
| // Output: | ||
| // true | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| package forgejo | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "strings" | ||
|
|
||
| "github.com/testcontainers/testcontainers-go" | ||
| "github.com/testcontainers/testcontainers-go/exec" | ||
| "github.com/testcontainers/testcontainers-go/wait" | ||
| ) | ||
|
|
||
| const ( | ||
| defaultHTTPPort = "3000/tcp" | ||
| defaultSSHPort = "22/tcp" | ||
| defaultUser = "forgejo-admin" | ||
| defaultPassword = "forgejo-admin" | ||
| defaultEmail = "admin@forgejo.local" | ||
| ) | ||
|
|
||
| // Container represents the Forgejo container type used in the module | ||
| type Container struct { | ||
| testcontainers.Container | ||
| adminUsername string | ||
| adminPassword string | ||
| } | ||
|
|
||
| // AdminUsername returns the admin username for the Forgejo instance. | ||
| func (c *Container) AdminUsername() string { | ||
| return c.adminUsername | ||
| } | ||
|
|
||
| // AdminPassword returns the admin password for the Forgejo instance. | ||
| func (c *Container) AdminPassword() string { | ||
| return c.adminPassword | ||
| } | ||
|
|
||
| // extractAdminCredentials parses FORGEJO_ADMIN_* env vars from the container | ||
| // environment, falling back to the default values for any that are not set. | ||
| func extractAdminCredentials(env []string) (username, password, email string) { | ||
| username, password, email = defaultUser, defaultPassword, defaultEmail | ||
| for _, e := range env { | ||
| if v, ok := strings.CutPrefix(e, "FORGEJO_ADMIN_USERNAME="); ok { | ||
| username = v | ||
| } | ||
| if v, ok := strings.CutPrefix(e, "FORGEJO_ADMIN_PASSWORD="); ok { | ||
| password = v | ||
| } | ||
| if v, ok := strings.CutPrefix(e, "FORGEJO_ADMIN_EMAIL="); ok { | ||
| email = v | ||
| } | ||
| } | ||
| return | ||
| } | ||
|
|
||
| // Run creates an instance of the Forgejo container type | ||
| func Run(ctx context.Context, img string, opts ...testcontainers.ContainerCustomizer) (*Container, error) { | ||
| // Closure variables populated by the PostReadies hook so we can avoid | ||
| // a second container.Inspect call after Run returns. | ||
| var adminUser, adminPass string | ||
|
|
||
| moduleOpts := make([]testcontainers.ContainerCustomizer, 0, 4+len(opts)) | ||
| moduleOpts = append(moduleOpts, | ||
| testcontainers.WithExposedPorts(defaultHTTPPort, defaultSSHPort), | ||
| testcontainers.WithWaitStrategy( | ||
| wait.ForHTTP("/api/healthz").WithPort(defaultHTTPPort), | ||
| ), | ||
| // Use SQLite for simplicity in tests (no external DB needed). | ||
| // INSTALL_LOCK skips the install wizard so the instance is ready to use. | ||
| testcontainers.WithEnv(map[string]string{ | ||
| "FORGEJO__database__DB_TYPE": "sqlite3", | ||
| "FORGEJO__security__INSTALL_LOCK": "true", | ||
| "FORGEJO_ADMIN_USERNAME": defaultUser, | ||
| "FORGEJO_ADMIN_PASSWORD": defaultPassword, | ||
| "FORGEJO_ADMIN_EMAIL": defaultEmail, | ||
| }), | ||
| ) | ||
|
|
||
| moduleOpts = append(moduleOpts, opts...) | ||
|
|
||
| // Add lifecycle hook to create admin user after container is ready. | ||
| // The hook reads credentials from container env vars so that user-provided | ||
| // options (which override the defaults above) are respected. | ||
| // The command runs as the "git" user because Forgejo refuses to run CLI | ||
| // commands as root. | ||
| adminHook := testcontainers.ContainerLifecycleHooks{ | ||
| PostReadies: []testcontainers.ContainerHook{ | ||
| func(ctx context.Context, container testcontainers.Container) error { | ||
| inspect, err := container.Inspect(ctx) | ||
| if err != nil { | ||
| return fmt.Errorf("inspect forgejo: %w", err) | ||
| } | ||
|
|
||
| username, password, email := extractAdminCredentials(inspect.Config.Env) | ||
|
|
||
| // Store credentials in closure for Run to use later. | ||
| adminUser = username | ||
| adminPass = password | ||
|
|
||
| code, output, err := container.Exec(ctx, []string{ | ||
| "forgejo", "admin", "user", "create", | ||
| "--username", username, | ||
| "--password", password, | ||
| "--email", email, | ||
| "--admin", | ||
| "--must-change-password=false", | ||
| }, exec.WithUser("git")) | ||
| if err != nil { | ||
| return fmt.Errorf("create admin user: %w", err) | ||
| } | ||
| if code != 0 { | ||
| data, _ := io.ReadAll(output) | ||
| return fmt.Errorf("create admin user: exit code %d: %s", code, string(data)) | ||
| } | ||
| return nil | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| moduleOpts = append(moduleOpts, testcontainers.WithAdditionalLifecycleHooks(adminHook)) | ||
|
|
||
| ctr, err := testcontainers.Run(ctx, img, moduleOpts...) | ||
| var c *Container | ||
| if ctr != nil { | ||
| c = &Container{Container: ctr} | ||
| } | ||
|
|
||
| if err != nil { | ||
| return c, fmt.Errorf("run forgejo: %w", err) | ||
| } | ||
|
|
||
| // Credentials were populated by the PostReadies hook above. | ||
| c.adminUsername = adminUser | ||
| c.adminPassword = adminPass | ||
|
|
||
| return c, nil | ||
| } | ||
|
|
||
| // ConnectionString returns the HTTP URL for the Forgejo instance | ||
| func (c *Container) ConnectionString(ctx context.Context) (string, error) { | ||
| return c.PortEndpoint(ctx, defaultHTTPPort, "http") | ||
| } | ||
|
|
||
| // SSHConnectionString returns the SSH endpoint for Git operations | ||
| func (c *Container) SSHConnectionString(ctx context.Context) (string, error) { | ||
| return c.PortEndpoint(ctx, defaultSSHPort, "") | ||
| } | ||
|
|
||
| // WithAdminCredentials sets the admin username, password, and email for the Forgejo instance. | ||
| // These credentials are used to create an admin user after the container is ready. | ||
| func WithAdminCredentials(username, password, email string) testcontainers.CustomizeRequestOption { | ||
| return func(req *testcontainers.GenericContainerRequest) error { | ||
| if username == "" || password == "" || email == "" { | ||
| return errors.New("WithAdminCredentials: username, password, and email must not be empty") | ||
| } | ||
| if req.Env == nil { | ||
| req.Env = make(map[string]string) | ||
| } | ||
| req.Env["FORGEJO_ADMIN_USERNAME"] = username | ||
| req.Env["FORGEJO_ADMIN_PASSWORD"] = password | ||
| req.Env["FORGEJO_ADMIN_EMAIL"] = email | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| // WithConfig sets a Forgejo configuration value using the FORGEJO__section__key | ||
| // environment variable format. | ||
| // See https://forgejo.org/docs/latest/admin/config-cheat-sheet/ for available options. | ||
| func WithConfig(section, key, value string) testcontainers.CustomizeRequestOption { | ||
| return func(req *testcontainers.GenericContainerRequest) error { | ||
| if section == "" || key == "" { | ||
| return fmt.Errorf("WithConfig: section and key must not be empty (got section=%q, key=%q)", section, key) | ||
| } | ||
| if strings.Contains(section, "__") || strings.Contains(key, "__") { | ||
| return fmt.Errorf("WithConfig: section and key must not contain \"__\" (got section=%q, key=%q)", section, key) | ||
| } | ||
| if req.Env == nil { | ||
| req.Env = make(map[string]string) | ||
| } | ||
| envKey := fmt.Sprintf("FORGEJO__%s__%s", section, key) | ||
| req.Env[envKey] = value | ||
| return nil | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
coderabbitai[bot] marked this conversation as resolved.
|
||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.