|
| 1 | +package todos |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "regexp" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/databrickslabs/sandbox/go-libs/fileset" |
| 12 | + "github.com/databrickslabs/sandbox/go-libs/git" |
| 13 | + "github.com/databrickslabs/sandbox/go-libs/github" |
| 14 | +) |
| 15 | + |
| 16 | +func New(ctx context.Context, gh *github.GitHubClient, root string) (*Finder, error) { |
| 17 | + fs, err := fileset.RecursiveChildren(root) |
| 18 | + if err != nil { |
| 19 | + return nil, fmt.Errorf("fileset: %w", err) |
| 20 | + } |
| 21 | + raw, err := os.ReadFile(filepath.Join(root, ".gitignore")) |
| 22 | + if err != nil { |
| 23 | + return nil, fmt.Errorf("read .gitignore: %w", err) |
| 24 | + } |
| 25 | + ignorer := newIncluder(append(strings.Split(string(raw), "\n"), ".git/", "*.gif", "*.png")) |
| 26 | + var scope fileset.FileSet |
| 27 | + for _, file := range fs { |
| 28 | + include, _ := ignorer.IgnoreFile(file.Relative) |
| 29 | + if !include { |
| 30 | + continue |
| 31 | + } |
| 32 | + scope = append(scope, file) |
| 33 | + } |
| 34 | + checkout, err := git.NewCheckout(ctx, root) |
| 35 | + if err != nil { |
| 36 | + return nil, fmt.Errorf("git: %w", err) |
| 37 | + } |
| 38 | + return &Finder{ |
| 39 | + fs: scope, |
| 40 | + git: checkout, |
| 41 | + gh: gh, |
| 42 | + }, nil |
| 43 | +} |
| 44 | + |
| 45 | +type Finder struct { |
| 46 | + fs fileset.FileSet |
| 47 | + git *git.Checkout |
| 48 | + gh *github.GitHubClient |
| 49 | +} |
| 50 | + |
| 51 | +type Todo struct { |
| 52 | + message string |
| 53 | + link string |
| 54 | +} |
| 55 | + |
| 56 | +func (f *Finder) Create(ctx context.Context) error { |
| 57 | + todos, err := f.allTodos(ctx) |
| 58 | + if err != nil { |
| 59 | + return fmt.Errorf("all todos: %w", err) |
| 60 | + } |
| 61 | + seen, err := f.seenIssues(ctx) |
| 62 | + if err != nil { |
| 63 | + return fmt.Errorf("seen issues: %w", err) |
| 64 | + } |
| 65 | + for _, todo := range todos { |
| 66 | + if seen[todo.message] { |
| 67 | + continue |
| 68 | + } |
| 69 | + if err := f.createIssue(ctx, todo); err != nil { |
| 70 | + return fmt.Errorf("create issue: %w", err) |
| 71 | + } |
| 72 | + } |
| 73 | + return nil |
| 74 | +} |
| 75 | + |
| 76 | +func (f *Finder) createIssue(ctx context.Context, todo Todo) error { |
| 77 | + org, repo, ok := f.git.OrgAndRepo() |
| 78 | + if !ok { |
| 79 | + return fmt.Errorf("git org and repo") |
| 80 | + } |
| 81 | + _, err := f.gh.CreateIssue(ctx, org, repo, github.NewIssue{ |
| 82 | + Title: fmt.Sprintf("[TODO] %s", todo.message), |
| 83 | + Body: fmt.Sprintf("See: %s", todo.link), |
| 84 | + Labels: []string{"tech debt"}, |
| 85 | + }) |
| 86 | + if err != nil { |
| 87 | + return fmt.Errorf("create issue: %w", err) |
| 88 | + } |
| 89 | + return nil |
| 90 | +} |
| 91 | + |
| 92 | +func (f *Finder) seenIssues(ctx context.Context) (map[string]bool, error) { |
| 93 | + org, repo, ok := f.git.OrgAndRepo() |
| 94 | + if !ok { |
| 95 | + return nil, fmt.Errorf("git org and repo") |
| 96 | + } |
| 97 | + seen := map[string]bool{} |
| 98 | + it := f.gh.ListRepositoryIssues(ctx, org, repo, &github.ListIssues{ |
| 99 | + State: "all", |
| 100 | + Labels: []string{"tech debt"}, |
| 101 | + }) |
| 102 | + for it.HasNext(ctx) { |
| 103 | + issue, err := it.Next(ctx) |
| 104 | + if err != nil { |
| 105 | + return nil, fmt.Errorf("next: %w", err) |
| 106 | + } |
| 107 | + if !strings.HasPrefix(issue.Title, "[TODO] ") { |
| 108 | + continue |
| 109 | + } |
| 110 | + norm := strings.TrimPrefix(issue.Title, "[TODO] ") |
| 111 | + seen[norm] = true |
| 112 | + } |
| 113 | + return seen, nil |
| 114 | +} |
| 115 | + |
| 116 | +func (f *Finder) allTodos(ctx context.Context) ([]Todo, error) { |
| 117 | + prefix, err := f.prefix(ctx) |
| 118 | + if err != nil { |
| 119 | + return nil, fmt.Errorf("prefix: %w", err) |
| 120 | + } |
| 121 | + var todos []Todo |
| 122 | + needle := regexp.MustCompile(`TODO:(.*)`) |
| 123 | + for _, v := range f.fs { |
| 124 | + raw, err := v.Raw() |
| 125 | + if err != nil { |
| 126 | + return nil, fmt.Errorf("%s: %w", v.Relative, err) |
| 127 | + } |
| 128 | + lines := strings.Split(string(raw), "\n") |
| 129 | + for i, line := range lines { |
| 130 | + if !needle.MatchString(line) { |
| 131 | + continue |
| 132 | + } |
| 133 | + todos = append(todos, Todo{ |
| 134 | + message: strings.TrimSpace(needle.FindStringSubmatch(line)[1]), |
| 135 | + link: fmt.Sprintf("%s/%s#L%d-L%d", prefix, v.Relative, i, i+5), |
| 136 | + }) |
| 137 | + } |
| 138 | + } |
| 139 | + return todos, nil |
| 140 | +} |
| 141 | + |
| 142 | +func (f *Finder) prefix(ctx context.Context) (string, error) { |
| 143 | + org, repo, ok := f.git.OrgAndRepo() |
| 144 | + if !ok { |
| 145 | + return "", fmt.Errorf("git org and repo") |
| 146 | + } |
| 147 | + commits, err := f.git.History(ctx) |
| 148 | + if err != nil { |
| 149 | + return "", fmt.Errorf("git history: %w", err) |
| 150 | + } |
| 151 | + // example: https://github.com/databrickslabs/ucx/blob/69a0cf8ce3450680dc222150f500d84a1eb523fc/src/databricks/labs/ucx/azure/access.py#L25-L35 |
| 152 | + prefix := fmt.Sprintf("https://github.com/%s/%s/blob/%s", org, repo, commits[0].Sha) |
| 153 | + return prefix, nil |
| 154 | +} |
0 commit comments