mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-20 12:23:22 +00:00
Updating a tracked repo is a fail-closed sequence: check for uncommitted changes, pull, audit what was pulled, reset to the pre-pull commit when the audit blocks. The CLI had two copies of the pull flow, the dashboard a third, and the audit gate existed four times (CLI, dashboard, and two in install). The copies had drifted: the dashboard ignored a failed git status and never refreshed root-skill metadata, and each copy worded a failed rollback on its own. update.TrackedRepo now owns the sequence and returns a structured result; install.AuditGate is the only gate. internal/update is a new package because internal/git already imports internal/install. The CLI keeps its spinner, rows and the "Apply anyway?" prompt, passed in as one Confirm callback; the dashboard turns the result into its JSON item. Behaviour changes, all towards the stricter copy: - the dashboard reports a failed git status as an error instead of pulling; - the dashboard refreshes root-skill metadata after an update, like the CLI; - a prompt that fails rolls the update back instead of leaving it applied; - the rollback-failed and rollback-unavailable texts come from one place. Accepted findings stay as they were (CLI honours and records them, the dashboard does neither), now as the explicit AcceptedFindings input. The "blocked and rolled back" texts are unchanged per caller because the dashboard matches on them. Refs #443
200 lines
5.2 KiB
Go
200 lines
5.2 KiB
Go
package server
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
"skillshare/internal/install"
|
|
)
|
|
|
|
// handleUpdateStream serves an SSE endpoint that streams update progress in real time.
|
|
// Events:
|
|
// - "start" → {"total": N} after collecting updatable items
|
|
// - "result" → updateResultItem after each item is updated
|
|
// - "done" → {"results":…,"summary":…} final payload
|
|
//
|
|
// Query params: names (comma-separated), force, skipAudit
|
|
func (s *Server) handleUpdateStream(w http.ResponseWriter, r *http.Request) {
|
|
safeSend, ok := initSSE(w)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
start := time.Now()
|
|
ctx := r.Context()
|
|
|
|
force := r.URL.Query().Get("force") == "true"
|
|
skipAudit := r.URL.Query().Get("skipAudit") == "true"
|
|
|
|
// Snapshot source under RLock, then release before slow I/O.
|
|
s.mu.RLock()
|
|
source := s.cfg.EffectiveSkillsSource()
|
|
walk := s.skillsWalk()
|
|
s.mu.RUnlock()
|
|
|
|
// Collect items to update based on "names" query param.
|
|
// If names is empty, update all updatable items.
|
|
namesParam := r.URL.Query().Get("names")
|
|
|
|
type updateItem struct {
|
|
name string
|
|
isRepo bool
|
|
path string
|
|
invalid bool // the name would resolve outside the source; reported, never touched
|
|
}
|
|
|
|
var items []updateItem
|
|
|
|
if namesParam != "" {
|
|
// Update specific items by name
|
|
for name := range strings.SplitSeq(namesParam, ",") {
|
|
name = strings.TrimSpace(name)
|
|
if name == "" {
|
|
continue
|
|
}
|
|
if !validSourceName(name) {
|
|
items = append(items, updateItem{name: name, invalid: true})
|
|
continue
|
|
}
|
|
// Check if it's a tracked repo
|
|
repoName := name
|
|
if !strings.HasPrefix(repoName, "_") {
|
|
repoName = "_" + name
|
|
}
|
|
repoPath := filepath.Join(source, repoName)
|
|
if install.IsGitRepo(repoPath) {
|
|
items = append(items, updateItem{name: repoName, isRepo: true, path: repoPath})
|
|
continue
|
|
}
|
|
// Check original name as repo
|
|
origPath := filepath.Join(source, name)
|
|
if install.IsGitRepo(origPath) {
|
|
items = append(items, updateItem{name: name, isRepo: true, path: origPath})
|
|
continue
|
|
}
|
|
// Regular skill
|
|
skillPath := filepath.Join(source, name)
|
|
items = append(items, updateItem{name: name, isRepo: false, path: skillPath})
|
|
}
|
|
} else {
|
|
// Update all: tracked repos + regular skills
|
|
repos, err := install.GetTrackedRepos(source, walk)
|
|
if err == nil {
|
|
for _, repo := range repos {
|
|
items = append(items, updateItem{
|
|
name: repo,
|
|
isRepo: true,
|
|
path: filepath.Join(source, repo),
|
|
})
|
|
}
|
|
}
|
|
s.mu.RLock()
|
|
skills, err := getServerUpdatableSkills(source, s.skillsStore, walk)
|
|
s.mu.RUnlock()
|
|
if err == nil {
|
|
for _, skill := range skills {
|
|
items = append(items, updateItem{
|
|
name: skill,
|
|
isRepo: false,
|
|
path: filepath.Join(source, skill),
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
safeSend("start", map[string]int{"total": len(items)})
|
|
|
|
var results []updateResultItem
|
|
summary := struct {
|
|
Updated int `json:"updated"`
|
|
UpToDate int `json:"upToDate"`
|
|
Blocked int `json:"blocked"`
|
|
Errors int `json:"errors"`
|
|
Skipped int `json:"skipped"`
|
|
MissingTrackedRepos []missingTrackedRepoInfo `json:"missingTrackedRepos,omitempty"`
|
|
}{}
|
|
|
|
for _, item := range items {
|
|
// Check if client disconnected
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
default:
|
|
}
|
|
|
|
var result updateResultItem
|
|
|
|
// Lock per-item for write operations
|
|
s.mu.Lock()
|
|
switch {
|
|
case item.invalid:
|
|
result = updateResultItem{Name: item.name, Action: "error", Message: "invalid skill name: " + item.name}
|
|
case item.isRepo:
|
|
result = s.updateTrackedRepo(item.name, item.path, s.cfg.EffectiveSkillsSource(), force, skipAudit, walk.Follow)
|
|
default:
|
|
result = s.updateRegularSkill(item.name, item.path, force, skipAudit, walk.Follow)
|
|
}
|
|
s.mu.Unlock()
|
|
|
|
results = append(results, result)
|
|
|
|
switch result.Action {
|
|
case "updated":
|
|
summary.Updated++
|
|
case "up-to-date":
|
|
summary.UpToDate++
|
|
case "blocked":
|
|
summary.Blocked++
|
|
case "error":
|
|
summary.Errors++
|
|
case "skipped":
|
|
summary.Skipped++
|
|
}
|
|
|
|
safeSend("result", result)
|
|
}
|
|
|
|
// Surface tracked repos declared in metadata but absent on disk (issue #212).
|
|
// Only relevant for the update-all flow, not targeted updates.
|
|
if namesParam == "" {
|
|
s.mu.RLock()
|
|
summary.MissingTrackedRepos = s.missingTrackedRepos()
|
|
s.mu.RUnlock()
|
|
}
|
|
|
|
// Write ops log
|
|
s.mu.Lock()
|
|
status := "ok"
|
|
msg := ""
|
|
if summary.Blocked > 0 {
|
|
status = "partial"
|
|
msg = fmt.Sprintf("%d update(s) blocked by security audit", summary.Blocked)
|
|
}
|
|
if summary.Errors > 0 {
|
|
status = "partial"
|
|
if msg != "" {
|
|
msg += fmt.Sprintf(", %d update(s) failed", summary.Errors)
|
|
} else {
|
|
msg = fmt.Sprintf("%d update(s) failed", summary.Errors)
|
|
}
|
|
}
|
|
s.writeOpsLog("update", status, start, map[string]any{
|
|
"name": "--all-stream",
|
|
"force": force,
|
|
"skip_audit": skipAudit,
|
|
"results_total": len(results),
|
|
"results_failed": summary.Errors,
|
|
"results_blocked": summary.Blocked,
|
|
"scope": "ui",
|
|
}, msg)
|
|
s.mu.Unlock()
|
|
|
|
safeSend("done", map[string]any{
|
|
"results": results,
|
|
"summary": summary,
|
|
})
|
|
}
|