|
| 1 | +package checker |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "regexp" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "opencsg.com/csghub-server/builder/git" |
| 11 | + "opencsg.com/csghub-server/builder/git/gitserver" |
| 12 | + "opencsg.com/csghub-server/builder/store/database" |
| 13 | + "opencsg.com/csghub-server/common/config" |
| 14 | + "opencsg.com/csghub-server/common/types" |
| 15 | +) |
| 16 | + |
| 17 | +type SkillFileChecker struct { |
| 18 | + repoStore database.RepoStore |
| 19 | + gitServer gitserver.GitServer |
| 20 | + config *config.Config |
| 21 | +} |
| 22 | + |
| 23 | +func NewSkillFileChecker(config *config.Config) (GitCallbackChecker, error) { |
| 24 | + git, err := git.NewGitServer(config) |
| 25 | + if err != nil { |
| 26 | + return nil, fmt.Errorf("failed to create git server: %w", err) |
| 27 | + } |
| 28 | + return &SkillFileChecker{ |
| 29 | + repoStore: database.NewRepoStore(), |
| 30 | + gitServer: git, |
| 31 | + config: config, |
| 32 | + }, nil |
| 33 | +} |
| 34 | + |
| 35 | +func (c *SkillFileChecker) Check(ctx context.Context, req types.GitalyAllowedReq) (bool, error) { |
| 36 | + var ref string |
| 37 | + repoType, namespace, name := req.GetRepoTypeNamespaceAndName() |
| 38 | + |
| 39 | + // Only check skill repositories |
| 40 | + if repoType != types.SkillRepo { |
| 41 | + return true, nil |
| 42 | + } |
| 43 | + |
| 44 | + repo, err := c.repoStore.FindByPath(ctx, repoType, namespace, name) |
| 45 | + if err != nil { |
| 46 | + return false, fmt.Errorf("failed to find repo, err: %v", err) |
| 47 | + } |
| 48 | + if repo == nil { |
| 49 | + return false, errors.New("repo not found") |
| 50 | + } |
| 51 | + changes := strings.Split(req.Changes, " ") |
| 52 | + if len(changes) > 1 { |
| 53 | + ref = changes[1] |
| 54 | + } |
| 55 | + |
| 56 | + // Check if SKILL.md exists and has required metadata in the new content |
| 57 | + skillsContent, err := c.gitServer.GetRepoFileRaw(ctx, gitserver.GetRepoInfoByPathReq{ |
| 58 | + Namespace: namespace, |
| 59 | + Name: name, |
| 60 | + RepoType: repoType, |
| 61 | + GitObjectDirectoryRelative: req.GitEnv.GitObjectDirectoryRelative, |
| 62 | + GitAlternateObjectDirectoriesRelative: req.GitEnv.GitAlternateObjectDirectoriesRelative, |
| 63 | + Path: "SKILL.md", |
| 64 | + Ref: ref, |
| 65 | + }) |
| 66 | + if err != nil { |
| 67 | + // SKILL.md not found in new content, reject push |
| 68 | + return false, fmt.Errorf("skill repository must have a SKILL.md file with name and description metadata") |
| 69 | + } |
| 70 | + |
| 71 | + // Check if SKILL.md is in the correct YAML format with name and description |
| 72 | + pattern := `^---\s*\nname:\s*.+\s*\ndescription:\s*.+\s*---*.` |
| 73 | + matched, err := regexp.MatchString(pattern, skillsContent) |
| 74 | + if err != nil { |
| 75 | + return false, fmt.Errorf("failed to check SKILL.md format: %w", err) |
| 76 | + } |
| 77 | + if !matched { |
| 78 | + return false, fmt.Errorf("SKILL.md must be in YAML format with name and description fields") |
| 79 | + } |
| 80 | + |
| 81 | + return true, nil |
| 82 | +} |
0 commit comments