GitLabgitlab-org
cli
main
1// Add (or update) a curated remote skill entry in
2// internal/commands/skills/remote/registry.yaml.
3//
4// Usage:
5//
6// go run ./scripts/skills/add-remote <gitlab-url> [--replace] [--name <name>]
7//
8// By default the entry name is the SKILL.md frontmatter `name`, which must
9// match the skill directory. Pass --name to publish the skill under a
10// different public name; glab rewrites the frontmatter `name` to it at
11// install time.
12//
13// The URL can be either form:
14//
15// https://gitlab.com/<group>/<project>/-/tree/<ref>/<path>
16// https://gitlab.com/<group>/<project>/-/blob/<ref>/<path>/SKILL.md
17//
18// The script fetches SKILL.md from gitlab.com (anonymously), reads its
19// frontmatter, and inserts/updates the registry entry sorted by name.
20// Branch refs are recorded as "latest"; tags and SHAs are preserved as-is.
21package main
23import (
24 "bytes"
25 "fmt"
26 "io"
27 "net/http"
28 "net/url"
29 "os"
30 "path/filepath"
31 "regexp"
32 "sort"
33 "strings"
34 "time"
36 "github.com/spf13/pflag"
37 "go.yaml.in/yaml/v3"
39 "gitlab.com/gitlab-org/cli/internal/commands/skills/skill"
40)
42const registryPath = "internal/commands/skills/remote/registry.yaml"
44type entry struct {
45 Name string `yaml:"name"`
46 Description string `yaml:"description"`
47 Project string `yaml:"project"`
48 Ref string `yaml:"ref"`
49 Path string `yaml:"path"`
50}
52type registryFile struct {
53 Version int `yaml:"version"`
54 Skills []entry `yaml:"skills"`
55}
57func main() {
58 // pflag (not stdlib `flag`) so that flags may appear after the
59 // positional URL argument — `<url> --replace` and `--replace <url>`
60 // both work.
61 replace := pflag.Bool("replace", false, "Overwrite an existing entry with the same name.")
62 name := pflag.String("name", "", "Public skill name, when it differs from the upstream frontmatter name.")
63 pflag.Usage = func() {
64 fmt.Fprintf(os.Stderr, "Usage: go run ./scripts/skills/add-remote <gitlab-url> [--replace] [--name <name>]\n")
65 pflag.PrintDefaults()
66 }
67 pflag.Parse()
68 if pflag.NArg() != 1 {
69 pflag.Usage()
70 os.Exit(2)
71 }
73 if err := run(pflag.Arg(0), *replace, *name); err != nil {
74 fmt.Fprintf(os.Stderr, "error: %v\n", err)
75 os.Exit(1)
76 }
77}
79func run(rawURL string, replace bool, nameOverride string) error {
80 project, ref, path, err := parseURL(rawURL)
81 if err != nil {
82 return err
83 }
85 skillMD, err := fetchSkillMD(project, ref, path)
86 if err != nil {
87 return fmt.Errorf("fetching SKILL.md: %w", err)
88 }
90 name, description, err := parseFrontmatter(skillMD)
91 if err != nil {
92 return fmt.Errorf("parsing SKILL.md frontmatter: %w", err)
93 }
95 if nameOverride != "" {
96 name = nameOverride
97 } else if base := filepath.Base(path); base != name {
98 return fmt.Errorf("frontmatter name %q does not match skill directory %q; pass --name to publish it under a different name", name, base)
99 }
101 rf, err := readRegistry()
102 if err != nil {
103 return err
104 }
106 e := entry{
107 Name: name,
108 Description: description,
109 Project: project,
110 Ref: normalizeRef(ref),
111 Path: path,
112 }
114 if err := upsert(&rf, e, replace); err != nil {
115 return err
116 }
118 if err := writeRegistry(rf); err != nil {
119 return err
120 }
122 fmt.Printf("Added/updated %q in %s:\n", e.Name, registryPath)
123 out, _ := yaml.Marshal([]entry{e})
124 fmt.Printf("\n%s\n", indent(string(out), " "))
125 return nil
126}
128// parseURL extracts project, ref, and path from a gitlab.com tree/blob URL.
129func parseURL(raw string) (string, string, string, error) {
130 u, err := url.Parse(raw)
131 if err != nil {
132 return "", "", "", err
133 }
134 if u.Host != "gitlab.com" {
135 return "", "", "", fmt.Errorf("URL host must be gitlab.com, got %q", u.Host)
136 }
138 idx := strings.Index(u.Path, "/-/")
139 if idx == -1 {
140 return "", "", "", fmt.Errorf("URL does not contain '/-/'; expected /-/tree/ or /-/blob/")
141 }
142 project := strings.Trim(u.Path[:idx], "/")
143 rest := u.Path[idx+len("/-/"):]
145 rest = strings.TrimPrefix(rest, "tree/")
146 if after, ok := strings.CutPrefix(rest, "blob/"); ok {
147 rest = after
148 } else if !strings.Contains(rest[:min(len(rest), 5)], "/") {
149 return "", "", "", fmt.Errorf("URL must contain /-/tree/ or /-/blob/")
150 }
152 ref, after, ok := strings.Cut(rest, "/")
153 if !ok {
154 return "", "", "", fmt.Errorf("URL is missing the path after the ref")
155 }
156 path := strings.TrimSuffix(after, "/SKILL.md")
157 path = strings.TrimSuffix(path, "/")
159 if project == "" || ref == "" || path == "" {
160 return "", "", "", fmt.Errorf("could not parse project/ref/path from URL")
161 }
162 return project, ref, path, nil
163}
165// normalizeRef maps branch-looking refs to "latest". Tags (vN.N.N
166// style) and 40-char SHAs are preserved verbatim. Anything else is
167// also passed through — the curator can override post-hoc.
168var (
169 tagRe = regexp.MustCompile(`^v\d+\.\d+\.\d+(?:[-+].*)?$`)
170 shaRe = regexp.MustCompile(`^[0-9a-f]{40}$`)
171)
173func normalizeRef(ref string) string {
174 if tagRe.MatchString(ref) || shaRe.MatchString(ref) {
175 return ref
176 }
177 return "latest"
178}
180func fetchSkillMD(project, ref, path string) ([]byte, error) {
181 u := fmt.Sprintf("https://gitlab.com/%s/-/raw/%s/%s/SKILL.md", project, ref, path)
182 client := &http.Client{Timeout: 30 * time.Second}
183 resp, err := client.Get(u)
184 if err != nil {
185 return nil, err
186 }
187 defer resp.Body.Close()
188 if resp.StatusCode != http.StatusOK {
189 return nil, fmt.Errorf("GET %s: %s", u, resp.Status)
190 }
191 return io.ReadAll(resp.Body)
192}
194func parseFrontmatter(content []byte) (string, string, error) {
195 fm, err := skill.ParseFrontmatter(content)
196 if err != nil {
197 return "", "", err
198 }
199 if fm.Name == "" {
200 return "", "", fmt.Errorf("frontmatter is missing 'name'")
201 }
202 if fm.Description == "" {
203 return "", "", fmt.Errorf("frontmatter is missing 'description'")
204 }
205 return fm.Name, fm.Description, nil
206}
208func readRegistry() (registryFile, error) {
209 body, err := os.ReadFile(registryPath)
210 if err != nil {
211 return registryFile{}, fmt.Errorf("reading %s: %w", registryPath, err)
212 }
213 var rf registryFile
214 if err := yaml.Unmarshal(body, &rf); err != nil {
215 return registryFile{}, fmt.Errorf("parsing %s: %w", registryPath, err)
216 }
217 if rf.Version == 0 {
218 rf.Version = 1
219 }
220 return rf, nil
221}
223func writeRegistry(rf registryFile) error {
224 sort.Slice(rf.Skills, func(i, j int) bool { return rf.Skills[i].Name < rf.Skills[j].Name })
226 header, err := readHeader()
227 if err != nil {
228 return err
229 }
231 var buf bytes.Buffer
232 buf.Write(header)
233 enc := yaml.NewEncoder(&buf)
234 enc.SetIndent(2)
235 if err := enc.Encode(rf); err != nil {
236 return err
237 }
238 if err := enc.Close(); err != nil {
239 return err
240 }
241 return os.WriteFile(registryPath, buf.Bytes(), 0o644)
242}
244// readHeader preserves the leading comment block in registry.yaml so
245// re-runs of the generator don't strip the human-written header.
246func readHeader() ([]byte, error) {
247 body, err := os.ReadFile(registryPath)
248 if err != nil {
249 return nil, err
250 }
251 var header bytes.Buffer
252 for _, line := range bytes.SplitAfter(body, []byte("\n")) {
253 trimmed := bytes.TrimLeft(line, " \t")
254 if len(trimmed) == 0 || trimmed[0] == '#' {
255 header.Write(line)
256 continue
257 }
258 break
259 }
260 return header.Bytes(), nil
261}
263func upsert(rf *registryFile, e entry, replace bool) error {
264 for i := range rf.Skills {
265 if rf.Skills[i].Name == e.Name {
266 if !replace {
267 return fmt.Errorf("entry %q already exists; pass --replace to overwrite", e.Name)
268 }
269 rf.Skills[i] = e
270 return nil
271 }
272 }
273 rf.Skills = append(rf.Skills, e)
274 return nil
275}
277func indent(s, prefix string) string {
278 lines := strings.Split(s, "\n")
279 for i, line := range lines {
280 if line == "" {
281 continue
282 }
283 lines[i] = prefix + line
284 }
285 return strings.Join(lines, "\n")
286}
Diagnostics7 git calls · 56 ms
CallStatusSizeTime056 ms
discover200185 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=185 git.server.agent=git/2.56.g1891de4-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=185 git.pkt.payload.max=35
ls-refs20058 KiB3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=59849 git.response.cache=UPDATING git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=731 git.pkt.data=730 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=59849 git.pkt.payload.max=143 git.ls_refs.count=730 git.ls_refs.head=refs/heads/main
fetchtree:0 · deepen 1020032 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=3 git.response.bytes=32926 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=tree:0 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=22 git.pkt.data=20 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=32926 git.pkt.payload.max=32046 git.sideband.band1.bytes=32046 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=80 git.pack.bytes=32046 git.pack.ofs_delta=36 git.pack.ref_delta=0 git.pack.chain.max=2 git.pack.trailer=cb0bad5d7ffc514895d8b21c4dd470327f90bc91 git.pack.full=44Open pack map
fetchcombine:blob:none+tree:2 · deepen 12004.2 KiB · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=4273 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=combine:blob:none+tree:2 git.fetch.wants=0dd7574dce9e385c5c30540e6c15657adc6bdce9 git.fetch.haves.count=0 git.fetch.deepen=1 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=4273 git.pkt.payload.max=4173 git.sideband.band1.bytes=4173 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=11 git.pack.bytes=4173 git.fetch.wide.objects=11 git.fetch.wide.extra_bytes=3930 git.fetch.wide.blobs=0
fetchtree:0200106 B · hit4 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=4 git.response.bytes=106 git.response.cache=HIT git.response.duration_ms=4 git.fetch.filter=tree:0 git.fetch.wants=1ee282f77a507d27b9dd8b075e79eb909b6b2a01 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=106 git.pkt.payload.max=79 git.sideband.band1.bytes=79 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=79 git.pack.trailer=c0c02eee9d40ad700e5c8e356bb39c09f2611d68Open pack map
fetchtree:0200104 B · hit6 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=6 git.response.bytes=104 git.response.cache=HIT git.response.duration_ms=6 git.fetch.filter=tree:0 git.fetch.wants=90ce464f1b5de3614655e23eae468f5f42492424 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=104 git.pkt.payload.max=77 git.sideband.band1.bytes=77 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=1 git.pack.bytes=77 git.pack.trailer=168b8a31703a6450317ac6b74b5c0746515cafc1Open pack map
fetch2002.8 KiB · hit39 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=39 git.response.bytes=2887 git.response.cache=HIT git.response.duration_ms=39 git.fetch.wants=3a53c902e104727714347635a77195652d687249 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=2887 git.pkt.payload.max=2860 git.sideband.band1.bytes=2860 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://gitlab.com/gitlab-org/cli
- Ref
- refs/heads/main
- Commit
- 0dd7574dce9e385c5c30540e6c15657adc6bdce9
- cf-ray
- a5e247448a79ee28
- Request
- 183d9dd3…
- Protocol
- v2 · sha1
- Agent
- git/2.56.g1891de4-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format