fix(server): stop dropping git and metadata errors in check, uninstall and batch targets

The dashboard check reimplemented the CLI's per-repo check but fetched
without the HTTPS token and ignored git status errors. Private tracked
repos could fail in the dashboard while `skillshare check` worked, and a
repo whose status could not be read showed as up to date. Both check
endpoints now call check.ParallelCheckRepos, the same code the CLI uses.
The JSON and SSE response shape does not change.

Batch uninstall treated a failed dirty check as clean and moved the
repo to trash. Without force, that item now fails with the git error
instead. This is stricter than the CLI, which only warns and continues.

Batch target updates ignored metadata save failures and still reported
success, so an override could silently disappear on the next reload.
They now return 500.
This commit is contained in:
Willie
2026-09-30 01:34:58 +08:00
parent d2b0a01d02
commit 20ae89a838
7 changed files with 116 additions and 40 deletions
+8 -17
View File
@@ -32,6 +32,13 @@ type skillCheckResult struct {
Kind string `json:"kind,omitempty"`
}
// checkTrackedRepo checks one tracked repo with the same logic as the CLI:
// auth-aware fetch and a reported dirty-check error.
func checkTrackedRepo(name, repoPath string) repoCheckResult {
out := check.ParallelCheckRepos([]check.RepoCheckInput{{Name: name, RepoPath: repoPath}}, nil)[0]
return repoCheckResult{Name: out.Name, Status: out.Status, Behind: out.Behind, Message: out.Message}
}
func (s *Server) handleCheck(w http.ResponseWriter, r *http.Request) {
// Snapshot config under RLock, then release before I/O.
s.mu.RLock()
@@ -43,23 +50,7 @@ func (s *Server) handleCheck(w http.ResponseWriter, r *http.Request) {
var repoResults []repoCheckResult
for _, repo := range repos {
repoPath := filepath.Join(sourceDir, repo)
result := repoCheckResult{Name: repo}
if isDirty, _ := git.IsDirty(repoPath); isDirty {
result.Status = "dirty"
result.Message = "has uncommitted changes"
} else if behind, err := git.GetBehindCount(repoPath); err != nil {
result.Status = "error"
result.Message = err.Error()
} else if behind == 0 {
result.Status = "up_to_date"
} else {
result.Status = "behind"
result.Behind = behind
}
repoResults = append(repoResults, result)
repoResults = append(repoResults, checkTrackedRepo(repo, filepath.Join(sourceDir, repo)))
}
// Group skills by repo URL+branch for efficient checking
+1 -18
View File
@@ -9,7 +9,6 @@ import (
"time"
"skillshare/internal/check"
"skillshare/internal/git"
"skillshare/internal/install"
)
@@ -102,23 +101,7 @@ func (s *Server) handleCheckStream(w http.ResponseWriter, r *http.Request) {
default:
}
repoPath := filepath.Join(sourceDir, repo)
result := repoCheckResult{Name: repo}
if isDirty, _ := git.IsDirty(repoPath); isDirty {
result.Status = "dirty"
result.Message = "has uncommitted changes"
} else if behind, err := git.GetBehindCount(repoPath); err != nil {
result.Status = "error"
result.Message = err.Error()
} else if behind == 0 {
result.Status = "up_to_date"
} else {
result.Status = "behind"
result.Behind = behind
}
repoResults = append(repoResults, result)
repoResults = append(repoResults, checkTrackedRepo(repo, filepath.Join(sourceDir, repo)))
checked.Add(1)
}
+42
View File
@@ -4,6 +4,8 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
)
@@ -29,3 +31,43 @@ func TestHandleCheck_EmptySource(t *testing.T) {
t.Errorf("expected 0 skills, got %d", len(resp.Skills))
}
}
// addTrackedRepoWithBrokenIndex clones a tracked repo from a local origin and
// corrupts its index, so `git status` fails while `git fetch` still succeeds.
func addTrackedRepoWithBrokenIndex(t *testing.T, sourceDir, name string) string {
t.Helper()
seed := t.TempDir()
initGitRepo(t, seed)
origin := filepath.Join(t.TempDir(), "origin.git")
runGit(t, seed, "clone", "--bare", seed, origin)
runGit(t, sourceDir, "clone", origin, name)
repoDir := filepath.Join(sourceDir, name)
// fetch reads the index for submodule recursion unless it is disabled.
runGit(t, repoDir, "config", "fetch.recurseSubmodules", "false")
if err := os.WriteFile(filepath.Join(repoDir, ".git", "index"), []byte("corrupt"), 0644); err != nil {
t.Fatalf("corrupt index: %v", err)
}
return repoDir
}
func TestHandleCheck_DirtyCheckErrorReported(t *testing.T) {
s, src := newTestServer(t)
addTrackedRepoWithBrokenIndex(t, src, "_team")
req := httptest.NewRequest(http.MethodGet, "/api/check", nil)
rr := httptest.NewRecorder()
s.handler.ServeHTTP(rr, req)
if rr.Code != http.StatusOK {
t.Fatalf("expected 200, got %d: %s", rr.Code, rr.Body.String())
}
var resp struct {
TrackedRepos []repoCheckResult `json:"tracked_repos"`
}
if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil {
t.Fatalf("failed to decode response: %v", err)
}
if len(resp.TrackedRepos) != 1 || resp.TrackedRepos[0].Status != "error" {
t.Fatalf("expected one repo with status error, got %+v", resp.TrackedRepos)
}
}
+16 -3
View File
@@ -124,7 +124,11 @@ func (s *Server) handleBatchSetTargets(w http.ResponseWriter, r *http.Request) {
// Save the overrides before unlocking: every API request reloads skillsStore
// from disk, so an override left unsaved here can be dropped by the next one.
if overridden {
s.skillsStore.Save(s.cfg.EffectiveSkillsSource()) //nolint:errcheck
if err := s.skillsStore.Save(s.cfg.EffectiveSkillsSource()); err != nil {
s.mu.Unlock()
writeError(w, http.StatusInternalServerError, "failed to save target overrides: "+err.Error())
return
}
}
s.mu.Unlock()
@@ -141,8 +145,12 @@ func (s *Server) handleBatchSetTargets(w http.ResponseWriter, r *http.Request) {
for name, h := range hashes {
s.skillsStore.SetFileHashes(name, h)
}
s.skillsStore.Save(s.cfg.EffectiveSkillsSource()) //nolint:errcheck
err := s.skillsStore.Save(s.cfg.EffectiveSkillsSource())
s.mu.Unlock()
if err != nil {
writeError(w, http.StatusInternalServerError, "failed to save file hashes: "+err.Error())
return
}
}
s.writeOpsLog("batch-set-targets", "ok", start, map[string]any{
@@ -232,8 +240,13 @@ func (s *Server) handleSetSkillTargets(w http.ResponseWriter, r *http.Request) {
if hashes, err := install.ComputeFileHashes(d.SourcePath); err == nil {
s.mu.Lock()
s.skillsStore.SetFileHashes(d.RelPath, hashes)
s.skillsStore.Save(s.cfg.EffectiveSkillsSource()) //nolint:errcheck
err := s.skillsStore.Save(s.cfg.EffectiveSkillsSource())
s.mu.Unlock()
if err != nil {
writeError(w, http.StatusInternalServerError, "failed to update skill: "+err.Error())
return
}
}
}
}
@@ -412,3 +412,20 @@ func TestHandleBatchUninstall_RemovesTargetOverrides(t *testing.T) {
t.Errorf("expected no target overrides after uninstall, got %v", overrides)
}
}
func TestHandleBatchSetTargets_SaveFailureReported(t *testing.T) {
s, src := newTestServer(t)
addTrackedRepoSkill(t, src)
// A directory at the metadata path makes the atomic rename in Save fail.
if err := os.MkdirAll(filepath.Join(src, install.MetadataFileName), 0755); err != nil {
t.Fatalf("block metadata file: %v", err)
}
req := httptest.NewRequest(http.MethodPost, "/api/resources/batch/targets", bytes.NewBufferString(`{"folder":"_caveman","target":"claude"}`))
rr := httptest.NewRecorder()
s.handler.ServeHTTP(rr, req)
if rr.Code != http.StatusInternalServerError {
t.Fatalf("expected 500, got %d: %s", rr.Code, rr.Body.String())
}
}
+9 -2
View File
@@ -182,9 +182,16 @@ func (s *Server) handleBatchUninstallSkills(w http.ResponseWriter, body batchUni
continue
}
if dirty, _ := git.IsDirty(repoPath); !body.Force && dirty {
if !body.Force {
dirty, err := git.IsDirty(repoPath)
if err != nil {
res.Error = fmt.Sprintf("failed to check git status: %v", err)
} else if dirty {
res.Error = "uncommitted changes (use force to override)"
}
}
if res.Error != "" {
res.Success = false
res.Error = "uncommitted changes (use force to override)"
results = append(results, res)
failed++
if firstErr == "" {
+23
View File
@@ -128,3 +128,26 @@ func TestHandleBatchUninstall_GlobalMode_GitignorePath(t *testing.T) {
t.Fatal("expected _team-skills/ to be removed from global source .gitignore")
}
}
func TestHandleBatchUninstall_DirtyCheckErrorBlocksRepo(t *testing.T) {
s, src := newTestServer(t)
repoDir := addTrackedRepoWithBrokenIndex(t, src, "_team")
b, _ := json.Marshal(batchUninstallRequest{Names: []string{"_team"}})
req := httptest.NewRequest(http.MethodPost, "/api/uninstall", bytes.NewReader(b))
rr := httptest.NewRecorder()
s.handleBatchUninstall(rr, req)
var resp struct {
Summary batchUninstallSummary `json:"summary"`
}
if err := json.Unmarshal(rr.Body.Bytes(), &resp); err != nil {
t.Fatalf("failed to decode response: %v (%s)", err, rr.Body.String())
}
if resp.Summary.Failed != 1 || resp.Summary.Succeeded != 0 {
t.Fatalf("expected 1 failed / 0 succeeded, got %+v", resp.Summary)
}
if _, err := os.Stat(repoDir); err != nil {
t.Fatalf("expected repo to stay in place: %v", err)
}
}