package workflow import ( "context" "sort" "strings" "github.com/barerepo/server/internal/gitx" ) // Load reads every workflow at a commit, in two processes whatever the file count. Chapter 25. func Load(ctx context.Context, dir, commit string, c Context) ([]Job, error) { if commit == "" { return nil, nil } spec := commit + ":" + Dir tree, err := gitx.Batch(ctx, dir, []string{spec}) if err != nil { return nil, err } // No directory is the ordinary case, and it is not an error. dirObj := tree[spec] if dirObj == nil || dirObj.Type != "tree" { return nil, nil } entries := gitx.TreeEntries(dirObj.Body) names := make([]string, 0, len(entries)) for name := range entries { if isWorkflowFile(name) { names = append(names, name) } } // Sorted, so two reads of one commit queue the same jobs in the same order. sort.Strings(names) if len(names) == 0 { return nil, nil } specs := make([]string, 0, len(names)) for _, name := range names { specs = append(specs, entries[name]) } blobs, err := gitx.Batch(ctx, dir, specs) if err != nil { return nil, err } var out []Job for _, name := range names { blob := blobs[entries[name]] if blob == nil || blob.Type != "blob" { continue } path := Dir + "/" + name jobs, err := Parse(path, blob.Body, c) if err != nil { // One unreadable file does not hide the others, and the reader is told which. out = append(out, Job{Workflow: name, Path: path, ID: name, Skipped: []Skip{{Step: path, Reason: err.Error()}}}) continue } out = append(out, jobs...) } return out, nil } // isWorkflowFile takes what GitHub takes, which is yml and yaml at the top of that one directory. func isWorkflowFile(name string) bool { return strings.HasSuffix(name, ".yml") || strings.HasSuffix(name, ".yaml") } // Files lists the workflow files at a commit, in one process, for a page that only needs to say so. func Files(ctx context.Context, dir, commit string) []string { if commit == "" { return nil } spec := commit + ":" + Dir tree, err := gitx.Batch(ctx, dir, []string{spec}) if err != nil { return nil } obj := tree[spec] if obj == nil || obj.Type != "tree" { return nil } var out []string for name := range gitx.TreeEntries(obj.Body) { if isWorkflowFile(name) { out = append(out, Dir+"/"+name) } } sort.Strings(out) return out }