mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-19 02:17:09 +00:00
* feat(check): detect changes at local install sources
Skills installed from a local path (for example a skill shipped inside an
app bundle) could already be refreshed with `update`, but `check` always
reported them as "local source", so users had no signal that the source
had changed after an app update.
`check` now re-hashes the files at the recorded source path and compares
them with the hashes stored at install/update time. It reports
update_available on a difference, up_to_date on a match, and an error
naming the path when the source is gone. The path is resolved the same
way `update` resolves it before re-copying, so a successful update always
brings the status back to up_to_date. The CLI and both Web API check
handlers share the comparison in internal/check.
Refs #206
* fix(check): avoid false local-source updates and show check errors
Review follow-ups for local-source change detection:
- The dashboard installs the root of a directory that also holds child
skills as its SKILL.md alone, so hashing the whole source reported
update_available right after a clean install. Compare only the files
such an install copies, recognized by recorded hashes that contain none
of the child skills present in the source.
- A relative source path resolves against the cwd of the install, which
is not recorded, so checking from another directory reported a missing
source. Relative sources are no longer compared.
- The dashboard dropped the new check `message`, so a missing source only
showed a generic "Check failed". Carry it into the status tooltip.
* revert: restore video files accidentally removed in 1fb13b75
The review-fix commit picked up unrelated staged deletions under video/. They belong to separate work and are not part of this change.
194 lines
5.2 KiB
Go
194 lines
5.2 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"skillshare/internal/check"
|
|
"skillshare/internal/git"
|
|
"skillshare/internal/install"
|
|
)
|
|
|
|
// skillWithMetaEntry holds a skill name paired with its centralized metadata entry.
|
|
type skillWithMetaEntry struct {
|
|
name string
|
|
entry *install.MetadataEntry
|
|
}
|
|
|
|
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"`
|
|
}
|
|
|
|
// localCheckResult checks a skill without a remote: local installs are
|
|
// compared against their recorded source path, the rest stay "local".
|
|
func localCheckResult(name string, entry *install.MetadataEntry) skillCheckResult {
|
|
status, message := check.LocalSourceStatus(entry)
|
|
return skillCheckResult{Name: name, Status: status, Message: message}
|
|
}
|
|
|
|
// 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()
|
|
s.mu.RUnlock()
|
|
|
|
repos, _ := install.GetTrackedRepos(sourceDir)
|
|
skills, _ := install.GetUpdatableSkills(sourceDir)
|
|
|
|
var repoResults []repoCheckResult
|
|
for _, repo := range repos {
|
|
repoResults = append(repoResults, checkTrackedRepo(repo, filepath.Join(sourceDir, repo)))
|
|
}
|
|
|
|
// Group skills by repo URL+branch for efficient checking
|
|
urlGroups := make(map[urlBranchGroup][]skillWithMetaEntry)
|
|
var localResults []skillCheckResult
|
|
|
|
for _, skill := range skills {
|
|
entry := s.skillEntry(skill)
|
|
if entry == nil || entry.RepoURL == "" {
|
|
localResults = append(localResults, localCheckResult(skill, entry))
|
|
continue
|
|
}
|
|
key := urlBranchGroup{url: entry.RepoURL, branch: entry.Branch}
|
|
urlGroups[key] = append(urlGroups[key], skillWithMetaEntry{
|
|
name: skill,
|
|
entry: entry,
|
|
})
|
|
}
|
|
|
|
skillResults := append([]skillCheckResult{}, localResults...)
|
|
|
|
for key, group := range urlGroups {
|
|
remoteHash, err := key.remoteHash()
|
|
|
|
if err != nil {
|
|
for _, sw := range group {
|
|
r := skillCheckResult{
|
|
Name: sw.name,
|
|
Source: sw.entry.Source,
|
|
Version: sw.entry.Version,
|
|
Status: "error",
|
|
}
|
|
if !sw.entry.InstalledAt.IsZero() {
|
|
r.InstalledAt = sw.entry.InstalledAt.Format("2006-01-02")
|
|
}
|
|
skillResults = append(skillResults, r)
|
|
}
|
|
continue
|
|
}
|
|
|
|
// Fast path: check if all skills match by commit hash
|
|
allMatch := true
|
|
for _, sw := range group {
|
|
if sw.entry.Version != remoteHash {
|
|
allMatch = false
|
|
break
|
|
}
|
|
}
|
|
if allMatch {
|
|
for _, sw := range group {
|
|
r := skillCheckResult{
|
|
Name: sw.name,
|
|
Source: sw.entry.Source,
|
|
Version: sw.entry.Version,
|
|
Status: "up_to_date",
|
|
}
|
|
if !sw.entry.InstalledAt.IsZero() {
|
|
r.InstalledAt = sw.entry.InstalledAt.Format("2006-01-02")
|
|
}
|
|
skillResults = append(skillResults, r)
|
|
}
|
|
continue
|
|
}
|
|
|
|
// Slow path: HEAD moved — try tree hash comparison
|
|
var hasTreeHash bool
|
|
for _, sw := range group {
|
|
if sw.entry.TreeHash != "" && sw.entry.Subdir != "" {
|
|
hasTreeHash = true
|
|
break
|
|
}
|
|
}
|
|
|
|
var remoteTreeHashes map[string]string
|
|
if hasTreeHash {
|
|
remoteTreeHashes = check.FetchRemoteTreeHashesForRef(key.url, key.branch)
|
|
}
|
|
|
|
for _, sw := range group {
|
|
r := skillCheckResult{
|
|
Name: sw.name,
|
|
Source: sw.entry.Source,
|
|
Version: sw.entry.Version,
|
|
}
|
|
if !sw.entry.InstalledAt.IsZero() {
|
|
r.InstalledAt = sw.entry.InstalledAt.Format("2006-01-02")
|
|
}
|
|
|
|
if sw.entry.Version == remoteHash {
|
|
r.Status = "up_to_date"
|
|
} else if sw.entry.TreeHash != "" && sw.entry.Subdir != "" && remoteTreeHashes != nil {
|
|
normalizedSubdir := strings.TrimPrefix(sw.entry.Subdir, "/")
|
|
if rh, ok := remoteTreeHashes[normalizedSubdir]; ok && sw.entry.TreeHash == rh {
|
|
r.Status = "up_to_date"
|
|
} else {
|
|
r.Status = "update_available"
|
|
}
|
|
} else {
|
|
r.Status = "update_available"
|
|
}
|
|
|
|
skillResults = append(skillResults, r)
|
|
}
|
|
}
|
|
|
|
if repoResults == nil {
|
|
repoResults = []repoCheckResult{}
|
|
}
|
|
if skillResults == nil {
|
|
skillResults = []skillCheckResult{}
|
|
}
|
|
|
|
writeJSON(w, map[string]any{
|
|
"tracked_repos": repoResults,
|
|
"skills": skillResults,
|
|
})
|
|
}
|
|
|
|
// urlBranchGroup keys skills that share a clone URL and installed branch, so a
|
|
// skill installed from a non-default branch is checked against that branch
|
|
// rather than the remote HEAD.
|
|
type urlBranchGroup struct {
|
|
url string
|
|
branch string
|
|
}
|
|
|
|
func (g urlBranchGroup) remoteHash() (string, error) {
|
|
if g.branch != "" {
|
|
return git.GetRemoteRefHashWithAuth(g.url, g.branch)
|
|
}
|
|
return git.GetRemoteHeadHashWithAuth(g.url)
|
|
}
|