mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-18 18:04:17 +00:00
fix(install): record a per-skill source every host can install again (#863)
* fix(install): record a per-skill source every host can install again `install <repo> -s <skill>` joined the repository and the skill path with a single "/". GitLab and Bitbucket URLs have no fixed depth, so the parser read the result as a longer repository path, and file:// URLs only split on "//". The recorded string is what a project's config.yaml shares, so `install -p` on a fresh clone tried to clone a repository that does not exist. Whole-repo file:// sources now join with "//", and GitLab or Bitbucket sources typed without ".git" or a web URL marker join with ".git/". The other hosts keep their form. `install --update` still recognises a record in the old form, but only when its recorded repository and path agree, because on these hosts the same string can also name a repository of its own. The dashboard's install dialog matched installed skills by rebuilding the old form, so it now accepts each form the server writes. Closes #859 * fix(ui): mark hub results installed under the new recorded source A hub entry that names a repository and a skill was matched against the installed skills by an exact source or a "<source>/" prefix. A GitLab or Bitbucket repository typed without ".git" is now recorded as "<source>.git/<skill>", so the search row and the preview kept offering Install, and a second install hit the already-exists warning. The matcher now accepts the ".git/" join as well. This is the same class as the discovery list fixed in the previous commit: both rebuild the recorded source in the dashboard. No other dashboard or Go code matches a recorded source by prefix.
This commit is contained in:
@@ -134,7 +134,7 @@ Expected:
|
||||
- jq: (.keys | index("local-skill")) == null
|
||||
- jq: .group == "grp"
|
||||
- jq: .local_group == "grp"
|
||||
- jq: .source == "file:///tmp/ss-move-git.git/git-skill"
|
||||
- jq: .source == "file:///tmp/ss-move-git.git//git-skill"
|
||||
- jq: .commit == 40
|
||||
- jq: (.dirs | contains("git-skill"))
|
||||
|
||||
@@ -176,7 +176,7 @@ ss check git-skill --json
|
||||
Expected:
|
||||
- exit_code: 0
|
||||
- jq: .skills[0].name == "grp/git-skill"
|
||||
- jq: .skills[0].source == "file:///tmp/ss-move-git.git/git-skill"
|
||||
- jq: .skills[0].source == "file:///tmp/ss-move-git.git//git-skill"
|
||||
|
||||
### 11. Move the whole folder
|
||||
|
||||
|
||||
@@ -12,7 +12,9 @@ import (
|
||||
)
|
||||
|
||||
// buildDiscoverySkillSource constructs metadata Source string for a skill
|
||||
// selected from a discovery result.
|
||||
// selected from a discovery result. The string is shared through the project
|
||||
// config, so ParseSource must split it back into the same repository and
|
||||
// subdirectory (#859).
|
||||
func buildDiscoverySkillSource(source *Source, skillPath string) string {
|
||||
if skillPath == "." {
|
||||
return source.Raw
|
||||
@@ -20,11 +22,39 @@ func buildDiscoverySkillSource(source *Source, skillPath string) string {
|
||||
if source.HasSubdir() {
|
||||
return source.Raw + "/" + skillPath
|
||||
}
|
||||
// Whole-repo SSH sources encode subdir using //path.
|
||||
if source.Type == SourceTypeGitSSH {
|
||||
return source.Raw + "//" + skillPath
|
||||
repo := strings.TrimRight(source.Raw, "/")
|
||||
switch {
|
||||
// Whole-repo SSH and file:// sources encode subdir using //path.
|
||||
case source.Type == SourceTypeGitSSH, strings.HasPrefix(source.CloneURL, "file://"):
|
||||
return repo + "//" + skillPath
|
||||
// GitLab and Bitbucket paths have no fixed depth: without ".git" or a web
|
||||
// URL marker the parser takes every segment as the repository.
|
||||
case source.webHost != "" && !strings.HasSuffix(repo, ".git") && !hasWebURLMarker(source.webHost, repo):
|
||||
return repo + ".git/" + skillPath
|
||||
}
|
||||
return source.Raw + "/" + skillPath
|
||||
return repo + "/" + skillPath
|
||||
}
|
||||
|
||||
// hasWebURLMarker reports whether raw is a GitLab (/-/tree/ref) or Bitbucket
|
||||
// (/src/ref) web URL, which parseGitHTTPS splits at the marker in the path.
|
||||
func hasWebURLMarker(webHost, raw string) bool {
|
||||
m := gitHTTPSPattern.FindStringSubmatch(raw)
|
||||
if m == nil {
|
||||
return false
|
||||
}
|
||||
return strings.Contains(m[3], "/-/") || webHost == "bitbucket" && strings.Contains(m[3], "/src/")
|
||||
}
|
||||
|
||||
// isDiscoverySkillSource reports whether entry records skillPath of source.
|
||||
// Installs from before #859 recorded a single-slash source, which GitLab,
|
||||
// Bitbucket and file:// hosts also use for a repository of its own, so that
|
||||
// form counts only when the recorded repository and path agree.
|
||||
func isDiscoverySkillSource(entry *MetadataEntry, source *Source, skillPath string) bool {
|
||||
if entry.Source == buildDiscoverySkillSource(source, skillPath) {
|
||||
return true
|
||||
}
|
||||
return entry.Source == source.Raw+"/"+skillPath &&
|
||||
repoURLsMatch(entry.RepoURL, source.CloneURL) && entry.Subdir == filepath.ToSlash(skillPath)
|
||||
}
|
||||
|
||||
func discoverySourceRoot(discovery *DiscoveryResult) string {
|
||||
@@ -127,7 +157,7 @@ func AgentRepoSource(entry *MetadataEntry, opts ParseOptions) (*Source, error) {
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
// Whole-repo SSH sources join the path with "//".
|
||||
// Whole-repo SSH and file:// sources join the path with "//".
|
||||
raw = strings.TrimSuffix(root, "/")
|
||||
}
|
||||
source, err := ParseSourceWithOptions(raw, opts)
|
||||
@@ -403,7 +433,7 @@ func installFromDiscoveryInternal(discovery *DiscoveryResult, skill SkillInfo, d
|
||||
// fullSource URL (which isn't a valid install target).
|
||||
hint := buildForceHint(discovery.Source.Raw, opts.Into)
|
||||
meta := LoadMetadataOrNew(opts.SourceDir).GetByPath(filepath.Join(opts.Into, filepath.Base(destPath)))
|
||||
knownUpdate := opts.Update && meta != nil && meta.Source == fullSource && meta.Group == opts.Into && !meta.Tracked && meta.EffectiveKind() == "skill"
|
||||
knownUpdate := opts.Update && meta != nil && isDiscoverySkillSource(meta, discovery.Source, skill.Path) && meta.Group == opts.Into && !meta.Tracked && meta.EffectiveKind() == "skill"
|
||||
if !knownUpdate {
|
||||
if err := checkExistingConflict(destPath, discovery.Source.CloneURL, hint); err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -182,7 +182,7 @@ func ExistingSkillInstallName(sourceDir string, source *Source, skill SkillInfo,
|
||||
var existing string
|
||||
for _, key := range store.List() {
|
||||
entry := store.Get(key)
|
||||
if entry.Tracked || entry.EffectiveKind() != "skill" || entry.Source != fullSource {
|
||||
if entry.Tracked || entry.EffectiveKind() != "skill" || !isDiscoverySkillSource(entry, source, skill.Path) {
|
||||
continue
|
||||
}
|
||||
rel := KeyToRelPath(key, entry)
|
||||
|
||||
@@ -66,6 +66,63 @@ func TestInstallFromDiscovery_FullSubdirUsesForwardSlashes(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// Regression for #859: the source recorded for a skill picked out of a whole
|
||||
// repository must parse back into that repository and the skill's path.
|
||||
func TestBuildDiscoverySkillSource_RoundTrips(t *testing.T) {
|
||||
opts := ParseOptions{GitLabHosts: []string{"code.example.com"}}
|
||||
for _, typed := range []string{
|
||||
"org/skills",
|
||||
"https://github.com/org/skills",
|
||||
"https://git.example.com/team/skills",
|
||||
"https://git.example.com/team/skills.git",
|
||||
"gitlab.com/team/skills",
|
||||
"https://gitlab.com/group/sub/skills/",
|
||||
"https://gitlab.com/team/skills.git",
|
||||
"https://gitlab.com/team/skills/-/tree/main",
|
||||
"https://code.example.com/group/sub/skills",
|
||||
"https://bitbucket.org/team/skills",
|
||||
"https://bitbucket.org/team/skills/src/main",
|
||||
"https://bitbucket.org/src/skills",
|
||||
"https://dev.azure.com/org/project/_git/skills",
|
||||
"git@gitlab.com:team/skills.git",
|
||||
"ssh://git@gitlab.com/team/skills.git",
|
||||
"file:///srv/skills",
|
||||
} {
|
||||
t.Run(typed, func(t *testing.T) {
|
||||
source, err := ParseSourceWithOptions(typed, opts)
|
||||
if err != nil {
|
||||
t.Fatalf("parse typed source: %v", err)
|
||||
}
|
||||
recorded := buildDiscoverySkillSource(source, "pack/x")
|
||||
got, err := ParseSourceWithOptions(recorded, opts)
|
||||
if err != nil {
|
||||
t.Fatalf("parse recorded source %q: %v", recorded, err)
|
||||
}
|
||||
if got.CloneURL != source.CloneURL || got.Subdir != "pack/x" || got.Branch != source.Branch {
|
||||
t.Errorf("recorded %q parsed as repo %q subdir %q branch %q, want repo %q subdir %q branch %q",
|
||||
recorded, got.CloneURL, got.Subdir, got.Branch, source.CloneURL, "pack/x", source.Branch)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// An install recorded in the single-slash form from before #859 is still
|
||||
// recognized as the same skill, so `install --update` keeps updating it. A
|
||||
// repository of its own at that URL is a different install.
|
||||
func TestIsDiscoverySkillSource_FormBefore859(t *testing.T) {
|
||||
source, err := ParseSource("file:///srv/skills")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
const recorded = "file:///srv/skills/x"
|
||||
if !isDiscoverySkillSource(&MetadataEntry{Source: recorded, RepoURL: "file:///srv/skills", Subdir: "x"}, source, "x") {
|
||||
t.Error("single-slash source of the same repository not recognized")
|
||||
}
|
||||
if isDiscoverySkillSource(&MetadataEntry{Source: recorded, RepoURL: recorded}, source, "x") {
|
||||
t.Error("a repository installed from the same URL was taken for the skill")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildDiscoverySkillSource(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
@@ -263,7 +320,7 @@ func TestAgentRepoSource_RoundTripsAgentSource(t *testing.T) {
|
||||
{"github repo subdir", "github.com/you/repo/pack/agents/demo.md", "agents/demo.md", "pack"},
|
||||
{"ssh whole repo", "git@github.com:you/repo.git//agents/demo.md", "agents/demo.md", ""},
|
||||
{"ssh repo subdir", "git@github.com:you/repo.git//pack/agents/demo.md", "agents/demo.md", "pack"},
|
||||
{"file whole repo", "file:///tmp/repo/agents/demo.md", "agents/demo.md", ""},
|
||||
{"file whole repo", "file:///tmp/repo//agents/demo.md", "agents/demo.md", ""},
|
||||
{"file repo subdir", "file:///tmp/repo//pack/agents/demo.md", "agents/demo.md", "pack"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
|
||||
@@ -63,6 +63,21 @@ describe('InstallDialog', () => {
|
||||
));
|
||||
});
|
||||
|
||||
it('leaves a skill unselected when it is installed under the source the server records for its host', async () => {
|
||||
vi.mocked(api.listSkills).mockResolvedValue({
|
||||
resources: [{ name: 'pdf', kind: 'skill', flatName: 'pdf', relPath: 'pdf', sourcePath: '/s/pdf', isInRepo: false, source: 'https://gitlab.com/team/skills.git/pdf' }],
|
||||
});
|
||||
vi.mocked(api.discover).mockResolvedValue({ needsSelection: true, skills: [{ name: 'pdf', path: 'pdf' }, { name: 'docx', path: 'docx' }], agents: [] });
|
||||
const user = userEvent.setup();
|
||||
renderDialog('url');
|
||||
|
||||
await user.type(screen.getByLabelText(/git url/i), 'https://gitlab.com/team/skills');
|
||||
await user.click(screen.getByRole('button', { name: /find skills/i }));
|
||||
|
||||
expect(await screen.findByRole('checkbox', { name: 'pdf' })).not.toBeChecked();
|
||||
expect(screen.getByRole('checkbox', { name: 'docx' })).toBeChecked();
|
||||
});
|
||||
|
||||
it('force installs only the skill the audit blocked', async () => {
|
||||
vi.mocked(api.discover).mockResolvedValue({ needsSelection: true, skills: [{ name: 'deploy', path: 'deploy' }], agents: [] });
|
||||
vi.mocked(api.installBatch)
|
||||
@@ -126,6 +141,24 @@ describe('InstallDialog', () => {
|
||||
expect(screen.getByText(/skipped git submodule "vendor\/up"/)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows a hub entry as installed when its skill is recorded under the source the server writes for its host', async () => {
|
||||
vi.mocked(api.listSkills).mockResolvedValue({
|
||||
resources: [{ name: 'pdf', kind: 'skill', flatName: 'pdf', relPath: 'pdf', sourcePath: '/s/pdf', isInRepo: false, source: 'https://gitlab.com/team/skills.git/pdf' }],
|
||||
});
|
||||
vi.mocked(api.getHubConfig).mockResolvedValue({ hubs: [{ label: 'Acme', url: 'https://acme.dev/hub.json' }], default: 'Acme' });
|
||||
vi.mocked(api.searchHub).mockResolvedValue({
|
||||
results: [{ name: 'pdf', description: 'Read PDFs', source: 'https://gitlab.com/team/skills', skill: 'pdf', stars: 0, owner: 'team', repo: 'skills' }],
|
||||
});
|
||||
const user = userEvent.setup();
|
||||
renderDialog('search');
|
||||
|
||||
await screen.findByText('Acme');
|
||||
await user.type(screen.getByLabelText(/search skills/i), 'pdf{Enter}');
|
||||
|
||||
expect(await screen.findByText('Installed')).toBeInTheDocument();
|
||||
expect(screen.queryByRole('button', { name: /^install$/i })).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('sends the folder picked from the existing ones as `into`', async () => {
|
||||
vi.mocked(api.listSkills).mockResolvedValue({
|
||||
resources: [{ name: 'demo', kind: 'skill', flatName: 'frontend__demo', relPath: 'frontend/demo', sourcePath: '/s/frontend/demo', isInRepo: false }],
|
||||
|
||||
@@ -92,9 +92,13 @@ function sourceKey(source: string): string {
|
||||
return github ? `github:${github.replace(/\.git(?=\/|$)/i, '').toLowerCase()}` : value;
|
||||
}
|
||||
|
||||
function discoveredSource(source: string, path: string): string {
|
||||
if (path === '.') return source;
|
||||
return `${source}${/^(?:git@|ssh:\/\/).+\.git$/i.test(source) ? '//' : '/'}${path}`;
|
||||
// The sources a skill found in `source` may be recorded under: the server joins
|
||||
// with "/", "//" (SSH, file://) or ".git/" (GitLab, Bitbucket), and joined
|
||||
// every non-SSH source with "/" before #859.
|
||||
function discoveredSources(source: string, path: string): string[] {
|
||||
if (path === '.') return [source];
|
||||
const repo = source.trim().replace(/\/+$/, '');
|
||||
return [`${repo}/${path}`, `${repo}//${path}`, `${repo}.git/${path}`];
|
||||
}
|
||||
|
||||
function mergeHubs(hubs: HubSavedEntry[]): HubSavedEntry[] {
|
||||
@@ -155,11 +159,13 @@ export default function InstallDialog({ kind, initialTab, initialSource, onClose
|
||||
const folders = useMemo(() => existingFolders(ofKind), [ofKind]);
|
||||
const installedKeys = useMemo(() => new Set((skillsData?.resources ?? []).filter((r) => r.source).map((r) => `${r.kind}:${sourceKey(r.source!)}`)), [skillsData]);
|
||||
const isInstalled = (k: Kind, source: string) => installedKeys.has(`${k}:${sourceKey(source)}`);
|
||||
const isDiscoveredInstalled = (from: string, item: DiscoveredSkill) => discoveredSources(from, item.path).some((s) => isInstalled(item.kind ?? 'skill', s));
|
||||
const isInstalledResult = (result: SearchResult) => {
|
||||
if (!result.skill) return isInstalled(kind, result.source);
|
||||
const source = sourceKey(result.source);
|
||||
// "/" also covers the "//" join; see discoveredSources.
|
||||
return (skillsData?.resources ?? []).some((item) => item.kind === kind && item.name === result.skill && item.source
|
||||
&& (sourceKey(item.source) === source || sourceKey(item.source).startsWith(`${source}/`)));
|
||||
&& (sourceKey(item.source) === source || ['/', '.git/'].some((join) => sourceKey(item.source!).startsWith(source + join))));
|
||||
};
|
||||
|
||||
const { data: hubConfig } = useQuery({
|
||||
@@ -332,7 +338,7 @@ export default function InstallDialog({ kind, initialTab, initialSource, onClose
|
||||
const show = (items: DiscoveredSkill[]) => {
|
||||
setSource(from);
|
||||
setFound({ source: from, items });
|
||||
const preselected = requestedSkills(wanted, from, items) ?? items.filter((i) => !isInstalled(i.kind ?? 'skill', discoveredSource(from, i.path)));
|
||||
const preselected = requestedSkills(wanted, from, items) ?? items.filter((i) => !isDiscoveredInstalled(from, i));
|
||||
setSelected(new Set(preselected.map((i) => i.path)));
|
||||
setFilter('');
|
||||
setTab('url');
|
||||
@@ -817,7 +823,7 @@ export default function InstallDialog({ kind, initialTab, initialSource, onClose
|
||||
<Checkbox label={item.name} hideLabel checked={selected.has(item.path)} onChange={() => toggle(item.path)} />
|
||||
<span className="nm m w-[170px] shrink-0 truncate">{item.name}</span>
|
||||
<span className="min-w-0 flex-1 truncate text-[13px] text-ink-2">{item.description}</span>
|
||||
{isInstalled(item.kind ?? 'skill', discoveredSource(found.source, item.path)) && <span className="ss-st ok shrink-0 text-ok">Installed</span>}
|
||||
{isDiscoveredInstalled(found.source, item) && <span className="ss-st ok shrink-0 text-ok">Installed</span>}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user