feat(config,workspace,user): jobs/notify config + workspace fetch repo + user ListAdmins

This commit is contained in:
2026-05-06 09:10:43 +08:00
parent 895799a6c9
commit 7b3892c42e
13 changed files with 338 additions and 6 deletions
+9
View File
@@ -102,6 +102,15 @@ type Credential struct {
UpdatedAt time.Time
}
// FetchTarget 是 workspace 包暴露给 jobs 模块的"周期 fetch 目标"投影。
// owner_id 通过 JOIN projects 取,避免 workspaces 表加冗余列。
type FetchTarget struct {
ID uuid.UUID
OwnerID uuid.UUID
Name string
MainPath string
}
// Caller 表示发起请求的用户上下文。Service 据此判定鉴权与 holder 字符串。
type Caller struct {
UserID uuid.UUID
+20
View File
@@ -41,3 +41,23 @@ SET sync_status = 'error',
last_sync_error = 'process_restarted_during_' || sync_status,
updated_at = now()
WHERE sync_status IN ('cloning', 'syncing');
-- name: ListFetchTargets :many
SELECT w.id, p.owner_id, w.name, w.main_path
FROM workspaces w
JOIN projects p ON p.id = w.project_id
WHERE w.archived_at IS NULL
AND w.sync_status IN ('idle','error')
ORDER BY w.id;
-- name: MarkSyncingCAS :execrows
UPDATE workspaces SET sync_status='syncing', updated_at=now()
WHERE id=$1 AND sync_status IN ('idle','error');
-- name: MarkFetchResult :exec
UPDATE workspaces
SET sync_status = CASE WHEN $2::bool THEN 'idle' ELSE 'error' END,
last_synced_at = CASE WHEN $2::bool THEN now() ELSE last_synced_at END,
last_sync_error = $3,
updated_at = now()
WHERE id = $1;
+48
View File
@@ -57,6 +57,11 @@ type Repository interface {
FinishPruneSuccess(ctx context.Context, id uuid.UUID) error
FinishPruneRollback(ctx context.Context, id uuid.UUID) error
// Workspace fetch (jobs runner)
ListFetchTargets(ctx context.Context) ([]FetchTarget, error)
MarkSyncing(ctx context.Context, id uuid.UUID) (bool, error)
MarkFetchResult(ctx context.Context, id uuid.UUID, success bool, errMsg string) error
// Credential
UpsertCredential(ctx context.Context, c *Credential) (*Credential, error)
GetCredentialByWorkspace(ctx context.Context, wsID uuid.UUID) (*Credential, error)
@@ -242,6 +247,49 @@ func (r *pgRepo) ResetStuckSyncStatuses(ctx context.Context) error {
return nil
}
// ===== Workspace fetch (jobs runner) =====
func (r *pgRepo) ListFetchTargets(ctx context.Context) ([]FetchTarget, error) {
rows, err := r.q.ListFetchTargets(ctx)
if err != nil {
return nil, errs.Wrap(err, errs.CodeInternal, "list fetch targets")
}
out := make([]FetchTarget, 0, len(rows))
for _, row := range rows {
out = append(out, FetchTarget{
ID: fromPgUUID(row.ID),
OwnerID: fromPgUUID(row.OwnerID),
Name: row.Name,
MainPath: row.MainPath,
})
}
return out, nil
}
func (r *pgRepo) MarkSyncing(ctx context.Context, id uuid.UUID) (bool, error) {
n, err := r.q.MarkSyncingCAS(ctx, toPgUUID(id))
if err != nil {
return false, errs.Wrap(err, errs.CodeInternal, "mark syncing")
}
return n > 0, nil
}
func (r *pgRepo) MarkFetchResult(ctx context.Context, id uuid.UUID, success bool, errMsg string) error {
var lastErr *string
if errMsg != "" {
lastErr = &errMsg
}
err := r.q.MarkFetchResult(ctx, workspacesqlc.MarkFetchResultParams{
ID: toPgUUID(id),
Column2: success,
LastSyncError: lastErr,
})
if err != nil {
return errs.Wrap(err, errs.CodeInternal, "mark fetch result")
}
return nil
}
// ===== Worktree =====
func rowToWorktree(r workspacesqlc.WorkspaceWorktree) *Worktree {
+74
View File
@@ -128,6 +128,47 @@ func (q *Queries) GetWorkspaceBySlug(ctx context.Context, arg GetWorkspaceBySlug
return i, err
}
const listFetchTargets = `-- name: ListFetchTargets :many
SELECT w.id, p.owner_id, w.name, w.main_path
FROM workspaces w
JOIN projects p ON p.id = w.project_id
WHERE w.archived_at IS NULL
AND w.sync_status IN ('idle','error')
ORDER BY w.id
`
type ListFetchTargetsRow struct {
ID pgtype.UUID `json:"id"`
OwnerID pgtype.UUID `json:"owner_id"`
Name string `json:"name"`
MainPath string `json:"main_path"`
}
func (q *Queries) ListFetchTargets(ctx context.Context) ([]ListFetchTargetsRow, error) {
rows, err := q.db.Query(ctx, listFetchTargets)
if err != nil {
return nil, err
}
defer rows.Close()
var items []ListFetchTargetsRow
for rows.Next() {
var i ListFetchTargetsRow
if err := rows.Scan(
&i.ID,
&i.OwnerID,
&i.Name,
&i.MainPath,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const listWorkspacesByProject = `-- name: ListWorkspacesByProject :many
SELECT id, project_id, slug, name, description, git_remote_url, default_branch, main_path, sync_status, last_synced_at, last_sync_error, created_at, updated_at FROM workspaces WHERE project_id = $1
ORDER BY created_at DESC
@@ -167,6 +208,39 @@ func (q *Queries) ListWorkspacesByProject(ctx context.Context, projectID pgtype.
return items, nil
}
const markFetchResult = `-- name: MarkFetchResult :exec
UPDATE workspaces
SET sync_status = CASE WHEN $2::bool THEN 'idle' ELSE 'error' END,
last_synced_at = CASE WHEN $2::bool THEN now() ELSE last_synced_at END,
last_sync_error = $3,
updated_at = now()
WHERE id = $1
`
type MarkFetchResultParams struct {
ID pgtype.UUID `json:"id"`
Column2 bool `json:"column_2"`
LastSyncError *string `json:"last_sync_error"`
}
func (q *Queries) MarkFetchResult(ctx context.Context, arg MarkFetchResultParams) error {
_, err := q.db.Exec(ctx, markFetchResult, arg.ID, arg.Column2, arg.LastSyncError)
return err
}
const markSyncingCAS = `-- name: MarkSyncingCAS :execrows
UPDATE workspaces SET sync_status='syncing', updated_at=now()
WHERE id=$1 AND sync_status IN ('idle','error')
`
func (q *Queries) MarkSyncingCAS(ctx context.Context, id pgtype.UUID) (int64, error) {
result, err := q.db.Exec(ctx, markSyncingCAS, id)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const resetStuckSyncStatuses = `-- name: ResetStuckSyncStatuses :exec
UPDATE workspaces
SET sync_status = 'error',
@@ -76,6 +76,11 @@ func (f *fakeRepo) DeleteWorkspace(_ context.Context, id uuid.UUID) error {
return nil
}
func (f *fakeRepo) ResetStuckSyncStatuses(_ context.Context) error { return nil }
func (f *fakeRepo) ListFetchTargets(_ context.Context) ([]FetchTarget, error) { return nil, nil }
func (f *fakeRepo) MarkSyncing(_ context.Context, _ uuid.UUID) (bool, error) { return false, nil }
func (f *fakeRepo) MarkFetchResult(_ context.Context, _ uuid.UUID, _ bool, _ string) error {
return nil
}
func (f *fakeRepo) GetWorktreeByID(_ context.Context, id uuid.UUID) (*Worktree, error) {
if w, ok := f.wts[id]; ok {
return w, nil