Files
skillshare/internal/server/handler_check.go
T
Willie 7a7cceefab feat(update): flag and protect skills edited since install (#622)
* feat(update): flag and protect skills edited since install

A skill installed from a repository and then edited by hand looked
exactly like an untouched one: check said up to date, and update either
reported it current or silently reinstalled it over the edit (#621).

Install already records each file's hash, so check now compares a
remote skill's files with those hashes and reports "modified", and
update skips such a skill until --force (CLI single, --all/--group,
dashboard). --force, the dashboard's Force Retry on the skipped row, or
the confirmation on the skill page discards the edit and reinstalls.

Two hash paths had to be made reliable for this to be trusted: the
dashboard's folder-wide target edit refreshed hashes by basename, so a
nested skill looked edited after skillshare's own change, and a skill
whose files cannot be hashed now counts as modified instead of being
overwritten.

* fix(server): keep earlier local edits when a target change rewrites SKILL.md

Changing a skill's targets in the dashboard rewrites its SKILL.md, and
both the folder-wide and the single-skill handler then re-hashed the
whole directory as the new baseline. An edit the user had already made
to any file in the skill was absorbed into that baseline, so the skill
stopped counting as modified and the next update overwrote the edit
without --force.

Both handlers now move only SKILL.md's recorded hash, and only when the
file still matched it before the rewrite. skillshare's own target edit
no longer reads as a local change, and an earlier edit still does. The
whole-directory refresh helpers lost their last callers and are removed.
2026-10-24 16:33:37 +08:00

102 lines
3.2 KiB
Go

package server
import (
"context"
"net/http"
"path/filepath"
"skillshare/internal/check"
"skillshare/internal/install"
"skillshare/internal/sourcewalk"
)
type repoCheckResult struct {
Name string `json:"name"`
Status string `json:"status"`
Behind int `json:"behind"`
Message string `json:"message,omitempty"`
}
type skillCheckResult struct {
Name string `json:"name"`
Source string `json:"source"`
Version string `json:"version"`
Status string `json:"status"`
InstalledAt string `json:"installed_at,omitempty"`
Kind string `json:"kind,omitempty"`
Message string `json:"message,omitempty"`
}
// planSkillCheck plans the update check of every installed skill outside a
// followed checkout. It takes s.mu.RLock, so the caller must not hold s.mu.
func (s *Server) planSkillCheck(sourceDir, projectRoot string, walk sourcewalk.Options) *check.Resolution {
names, _ := install.GetUpdatableSkills(sourceDir)
var skills []check.Skill
for _, name := range names {
if followedCheckout(filepath.Join(sourceDir, name), walk.Follow) != "" {
continue
}
skills = append(skills, check.Skill{Name: name, Entry: s.skillEntry(name), Dir: filepath.Join(sourceDir, name), Follow: walk.Follow})
}
return check.Plan(skills, projectRoot)
}
// dashboardSkillResults converts resolved statuses to the dashboard payload,
// which is narrower than the CLI's: a skill without a remote carries only its
// name, status and message.
func dashboardSkillResults(results []check.SkillResult) []skillCheckResult {
out := make([]skillCheckResult, 0, len(results))
for _, r := range results {
if r.Local {
out = append(out, skillCheckResult{Name: r.Name, Status: r.Status, Message: r.Message})
continue
}
out = append(out, skillCheckResult{
Name: r.Name,
Source: r.Source,
Version: r.Version,
Status: r.Status,
InstalledAt: r.InstalledAt,
Message: r.Message,
})
}
return out
}
// 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()
sourceDir := s.skillsSource()
projectRoot := s.projectRoot
walk := s.skillsWalk()
s.mu.RUnlock()
repos, linked := dashboardRepos(sourceDir, walk)
plan := s.planSkillCheck(sourceDir, projectRoot, walk)
var repoResults []repoCheckResult
for _, repo := range repos {
repoResults = append(repoResults, checkTrackedRepo(repo, filepath.Join(sourceDir, repo)))
}
// Without a context the check runs to completion, so the error is always nil.
resolved, _ := plan.Run(context.Background(), check.Options{})
skillResults := dashboardSkillResults(resolved)
if repoResults == nil {
repoResults = []repoCheckResult{}
}
writeJSON(w, map[string]any{
"tracked_repos": repoResults,
"linked_repos": linked,
"skills": skillResults,
})
}