-
Notifications
You must be signed in to change notification settings - Fork 61
feat(api): add local-dev seed command and document it #308
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
Changes from 1 commit
Commits
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
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,170 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "log/slog" | ||
| "os" | ||
|
|
||
| "github.com/Devlaner/devlane/api/internal/auth" | ||
| "github.com/Devlaner/devlane/api/internal/config" | ||
| "github.com/Devlaner/devlane/api/internal/database" | ||
| "github.com/Devlaner/devlane/api/internal/model" | ||
| "github.com/Devlaner/devlane/api/internal/service" | ||
| "github.com/Devlaner/devlane/api/internal/store" | ||
| "github.com/google/uuid" | ||
| "gorm.io/gorm" | ||
| ) | ||
|
|
||
| // Demo credentials for the local-development seed. Not secret; documented in the | ||
| // local dev guide. Never use in a real deployment. | ||
| const ( | ||
| seedEmail = "demo@devlane.test" | ||
| seedPassword = "Demo1234!" | ||
| seedFirstName = "Demo" | ||
| seedLastName = "User" | ||
| seedWorkspaceName = "Demo Workspace" | ||
| seedWorkspaceSlug = "demo" | ||
| seedProjectName = "Getting Started" | ||
| seedProjectIdent = "DEMO" | ||
| ) | ||
|
|
||
| // runSeed handles `api seed`: it populates a local database with a demo user, | ||
| // workspace, project, workflow states, and sample work items so a fresh clone | ||
| // has something to explore. Idempotent — a second run is a no-op. | ||
| func runSeed(args []string) int { | ||
| log := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelWarn})) | ||
| if len(args) != 0 { | ||
| fmt.Fprintln(os.Stderr, "usage: api seed") | ||
| return 2 | ||
| } | ||
| cfg, err := config.Load() | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "config: %v\n", err) | ||
| return 1 | ||
| } | ||
| db, err := database.NewDB(cfg, log) | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "database: %v\n", err) | ||
| return 1 | ||
| } | ||
| if sqlDB, err := db.DB(); err == nil { | ||
| defer sqlDB.Close() | ||
| } | ||
| if err := seedDevData(context.Background(), db); err != nil { | ||
| fmt.Fprintf(os.Stderr, "seed failed: %v\n", err) | ||
| return 1 | ||
| } | ||
| return 0 | ||
| } | ||
|
|
||
| func seedDevData(ctx context.Context, db *gorm.DB) error { | ||
| userStore := store.NewUserStore(db) | ||
|
|
||
| // Idempotency: if the demo user already exists, assume the DB is seeded. | ||
| if u, _ := userStore.GetByEmail(ctx, seedEmail); u != nil { | ||
| fmt.Printf("seed: %s already exists — nothing to do\n", seedEmail) | ||
| return nil | ||
| } | ||
|
|
||
| authSvc := auth.NewService(userStore, store.NewSessionStore(db), store.NewPasswordResetTokenStore(db)) | ||
| _, user, err := authSvc.SignUp(ctx, auth.SignUpRequest{ | ||
| Email: seedEmail, | ||
| Password: seedPassword, | ||
| FirstName: seedFirstName, | ||
| LastName: seedLastName, | ||
| }) | ||
| if err != nil { | ||
| return fmt.Errorf("create demo user: %w", err) | ||
| } | ||
|
|
||
| // Make the demo user an instance admin and mark the instance as set up, so | ||
| // the app is immediately usable without the first-run setup wizard. | ||
| admins := store.NewInstanceAdminStore(db) | ||
| if n, _ := admins.CountActive(ctx); n == 0 { | ||
| _ = admins.Create(ctx, &model.InstanceAdmin{UserID: user.ID, Role: model.RoleOwner, IsVerified: true}) | ||
| } | ||
| settings := store.NewInstanceSettingStore(db) | ||
| if row, _ := settings.Get(ctx, "general"); row == nil { | ||
| _ = settings.Upsert(ctx, "general", model.JSONMap{ | ||
| "instance_id": "devlocalseed00000000000", | ||
| "admin_email": seedEmail, | ||
| "instance_name": "Devlane (local)", | ||
| "only_admin_can_create_workspace": false, | ||
| }) | ||
| } | ||
|
|
||
| wsSvc := service.NewWorkspaceService(store.NewWorkspaceStore(db), store.NewWorkspaceInviteStore(db), userStore) | ||
| wrk, err := wsSvc.Create(ctx, seedWorkspaceName, seedWorkspaceSlug, "", user.ID) | ||
| if err != nil { | ||
| return fmt.Errorf("create workspace: %w", err) | ||
| } | ||
|
|
||
| projSvc := service.NewProjectService(store.NewProjectStore(db), store.NewProjectInviteStore(db), store.NewWorkspaceStore(db), userStore) | ||
| proj, err := projSvc.Create(ctx, wrk.Slug, seedProjectName, seedProjectIdent, user.ID) | ||
| if err != nil { | ||
| return fmt.Errorf("create project: %w", err) | ||
| } | ||
|
|
||
| // Seed a standard set of workflow states, one marked default. | ||
| stateStore := store.NewStateStore(db) | ||
| seedStates := []struct { | ||
| name, group, color string | ||
| def bool | ||
| seq float64 | ||
| }{ | ||
| {"Backlog", "backlog", "#94a3b8", false, 1000}, | ||
| {"Todo", "unstarted", "#6366f1", true, 2000}, | ||
| {"In Progress", "started", "#f59e0b", false, 3000}, | ||
| {"Done", "completed", "#22c55e", false, 4000}, | ||
| {"Cancelled", "cancelled", "#ef4444", false, 5000}, | ||
| } | ||
| for _, st := range seedStates { | ||
| m := &model.State{ | ||
| Name: st.name, Group: st.group, Color: st.color, Default: st.def, | ||
| Sequence: st.seq, ProjectID: proj.ID, WorkspaceID: wrk.ID, | ||
| } | ||
| if err := stateStore.RestoreOrCreateByNameAndProject(ctx, m); err != nil { | ||
| return fmt.Errorf("create state %q: %w", st.name, err) | ||
| } | ||
| } | ||
| stateByName := map[string]uuid.UUID{} | ||
| if all, err := stateStore.ListByProjectID(ctx, proj.ID); err == nil { | ||
| for i := range all { | ||
| stateByName[all[i].Name] = all[i].ID | ||
| } | ||
| } | ||
|
|
||
| issueSvc := service.NewIssueService(store.NewIssueStore(db), store.NewProjectStore(db), store.NewWorkspaceStore(db)) | ||
| issueSvc.SetActivityStore(store.NewIssueActivityStore(db)) | ||
| issueSvc.SetStateStore(stateStore) | ||
| issueSvc.SetLabelStore(store.NewLabelStore(db)) | ||
|
|
||
| seedIssues := []struct { | ||
| name, desc, priority, state string | ||
| }{ | ||
| {"Welcome to Devlane 👋", "This is a sample work item. Open it to see the detail view, then try editing the state, priority, and assignees.", "high", "Todo"}, | ||
| {"Set up your first project", "Projects group work items. Create your own from the sidebar when you're ready.", "medium", "In Progress"}, | ||
| {"Explore the board and list layouts", "Switch layouts from the work-item view to see the same issues grouped differently.", "low", "Backlog"}, | ||
| {"Try importing issues from CSV", "The project work-item list has an Import CSV action for bulk-adding items.", "none", "Backlog"}, | ||
| {"Mark something Done", "Move a work item to the Done state to see it settle.", "medium", "Done"}, | ||
| } | ||
| created := 0 | ||
| for _, di := range seedIssues { | ||
| var stateID *uuid.UUID | ||
| if id, ok := stateByName[di.state]; ok { | ||
| id := id | ||
| stateID = &id | ||
| } | ||
| if _, err := issueSvc.Create(ctx, wrk.Slug, proj.ID, user.ID, | ||
| di.name, di.desc, di.priority, stateID, nil, nil, nil, nil, nil, false); err != nil { | ||
| return fmt.Errorf("create issue %q: %w", di.name, err) | ||
| } | ||
| created++ | ||
| } | ||
|
|
||
| fmt.Printf("seed: created user %s (password %s), workspace %q, project %q with %d work items\n", | ||
| seedEmail, seedPassword, seedWorkspaceSlug, seedProjectIdent, created) | ||
| fmt.Printf("seed: sign in at the web app with %s / %s\n", seedEmail, seedPassword) | ||
| return nil | ||
| } | ||
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,55 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/Devlaner/devlane/api/internal/store" | ||
| "github.com/Devlaner/devlane/api/internal/testutil" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // The local-dev seed creates a demo user, workspace, project, states, and | ||
| // issues, and is idempotent on a second run. Covers #24. | ||
| func TestSeedDevData_CreatesDemoAndIsIdempotent(t *testing.T) { | ||
| ts := testutil.NewTestServer(t) | ||
| ctx := context.Background() | ||
|
|
||
| require.NoError(t, seedDevData(ctx, ts.DB)) | ||
|
|
||
| users := store.NewUserStore(ts.DB) | ||
| u, err := users.GetByEmail(ctx, seedEmail) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, u, "demo user should exist") | ||
|
|
||
| ws := store.NewWorkspaceStore(ts.DB) | ||
| wrk, err := ws.GetBySlug(ctx, seedWorkspaceSlug) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, wrk) | ||
|
|
||
| projects, err := store.NewProjectStore(ts.DB).ListByWorkspaceID(ctx, wrk.ID) | ||
| require.NoError(t, err) | ||
| require.Len(t, projects, 1) | ||
|
|
||
| states, err := store.NewStateStore(ts.DB).ListByProjectID(ctx, projects[0].ID) | ||
| require.NoError(t, err) | ||
| require.Len(t, states, 5) | ||
| defaults := 0 | ||
| for _, s := range states { | ||
| if s.Default { | ||
| defaults++ | ||
| } | ||
| } | ||
| assert.Equal(t, 1, defaults, "exactly one default state") | ||
|
|
||
| issues, err := store.NewIssueStore(ts.DB).ListByProjectID(ctx, projects[0].ID, 100, 0) | ||
| require.NoError(t, err) | ||
| assert.Len(t, issues, 5) | ||
|
|
||
| // Second run is a no-op: no error and no duplicate workspace/issues. | ||
| require.NoError(t, seedDevData(ctx, ts.DB)) | ||
| issues2, err := store.NewIssueStore(ts.DB).ListByProjectID(ctx, projects[0].ID, 100, 0) | ||
| require.NoError(t, err) | ||
| assert.Len(t, issues2, 5, "second seed should not add issues") | ||
| } |
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.