package main import ( "fmt" "go/parser" "go/token" "io/fs" "net/url" "os" "path/filepath" "regexp" "strings" ) var ( capabilityRow = regexp.MustCompile("^\\|\\s*`([^`]+)`\\s*\\|") markdownLink = regexp.MustCompile(`\]\(([^)]+)\)`) ) // checkDocs 核对接入指南公开包清单与仓库内 Markdown 相对链接。 // 入参:root 为项目根目录,不按当前平台过滤生产源码。 // 返回:缺少指南、能力清单漂移或失效相对链接时返回具体位置。 func checkDocs(root string) error { guide := filepath.Join(root, "docs", "getting-started.md") content, err := os.ReadFile(guide) if err != nil { return fmt.Errorf("read integration guide: %w", err) } text := string(content) const start = "" const end = "" first, last := strings.Index(text, start), strings.Index(text, end) if first < 0 || last < first+len(start) || strings.Count(text, start) != 1 || strings.Count(text, end) != 1 { return fmt.Errorf("%s: capability table markers are missing or ambiguous", guide) } declared := make(map[string]bool) for _, line := range strings.Split(text[first+len(start):last], "\n") { match := capabilityRow.FindStringSubmatch(strings.TrimSpace(line)) if match == nil { continue } if declared[match[1]] { return fmt.Errorf("%s: duplicate capability %s", guide, match[1]) } declared[match[1]] = true } actual := make(map[string]bool) err = filepath.WalkDir(root, func(path string, entry fs.DirEntry, walkErr error) error { if walkErr != nil { return walkErr } if entry.IsDir() { if entry.Name() == ".git" || entry.Name() == "vendor" { return filepath.SkipDir } return nil } if strings.HasSuffix(path, ".md") { if err := checkMarkdownLinks(root, path); err != nil { return err } } if !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") { return nil } relative, err := filepath.Rel(root, filepath.Dir(path)) if err != nil { return err } directory := filepath.ToSlash(relative) if directory == "internal" || strings.HasPrefix(directory, "internal/") || directory == "examples" || strings.HasPrefix(directory, "examples/") || directory == "script" || strings.HasPrefix(directory, "script/") { return nil } file, err := parser.ParseFile(token.NewFileSet(), path, nil, parser.PackageClauseOnly) if err != nil { return err } if file.Name.Name != "main" { actual[directory] = true } return nil }) if err != nil { return err } for path := range actual { if !declared[path] { return fmt.Errorf("%s: public package %s is undocumented; update integration guide in the same change", guide, path) } } for path := range declared { if !actual[path] { return fmt.Errorf("%s: removed or renamed public package %s is still documented", guide, path) } } return nil } // checkMarkdownLinks 检查仓库相对目标,不访问外部 URL 或推断页面锚点。 // 入参:root 为项目根目录,path 为 Markdown 文件。 // 返回:链接路径越出仓库、转义无效或文件缺失时返回具体位置。 func checkMarkdownLinks(root string, path string) error { content, err := os.ReadFile(path) if err != nil { return err } for _, match := range markdownLink.FindAllStringSubmatch(string(content), -1) { target := strings.Trim(match[1], "<>") parsed, err := url.Parse(target) if err != nil { return fmt.Errorf("%s: invalid Markdown target %q: %w", path, target, err) } if parsed.IsAbs() || parsed.Host != "" || parsed.Path == "" { continue } destination := filepath.Join(filepath.Dir(path), filepath.FromSlash(parsed.Path)) relative, err := filepath.Rel(root, destination) if err != nil { return err } if relative == ".." || strings.HasPrefix(relative, ".."+string(filepath.Separator)) { return fmt.Errorf("%s: link escapes repository: %s", path, target) } if _, err = os.Stat(destination); err != nil { return fmt.Errorf("%s: broken repository link %s: %w", path, target, err) } } return nil }