|
| 1 | +package app |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "os" |
| 6 | + "os/exec" |
| 7 | + "path/filepath" |
| 8 | + "slices" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/google/go-github/v89/github" |
| 13 | + "github.com/multimediallc/codeowners-plus/internal/git" |
| 14 | + gh "github.com/multimediallc/codeowners-plus/internal/github" |
| 15 | + "github.com/multimediallc/codeowners-plus/pkg/codeowners" |
| 16 | +) |
| 17 | + |
| 18 | +// The shared mock approves everything without reading the diff, which is the |
| 19 | +// decision under test, so the real staleness check is spliced back in. |
| 20 | +type realCheckApprovalsClient struct { |
| 21 | + *mockGitHubClient |
| 22 | + real gh.Client |
| 23 | + dismissed []*gh.CurrentApproval |
| 24 | +} |
| 25 | + |
| 26 | +func (c *realCheckApprovalsClient) CheckApprovals( |
| 27 | + fileReviewerMap map[string][]string, |
| 28 | + approvals []*gh.CurrentApproval, |
| 29 | + originalDiff git.Diff, |
| 30 | +) ([]codeowners.Slug, []*gh.CurrentApproval) { |
| 31 | + return c.real.CheckApprovals(fileReviewerMap, approvals, originalDiff) |
| 32 | +} |
| 33 | + |
| 34 | +func (c *realCheckApprovalsClient) DismissStaleReviews(approvals []*gh.CurrentApproval) error { |
| 35 | + c.dismissed = append(c.dismissed, approvals...) |
| 36 | + return c.mockGitHubClient.DismissStaleReviews(approvals) |
| 37 | +} |
| 38 | + |
| 39 | +func runGit(t *testing.T, dir string, args ...string) string { |
| 40 | + t.Helper() |
| 41 | + cmd := exec.Command("git", args...) |
| 42 | + cmd.Dir = dir |
| 43 | + cmd.Env = append(os.Environ(), "GIT_CONFIG_GLOBAL=/dev/null", "GIT_CONFIG_SYSTEM=/dev/null") |
| 44 | + out, err := cmd.CombinedOutput() |
| 45 | + if err != nil { |
| 46 | + t.Fatalf("git %s (in %s): %v\n%s", strings.Join(args, " "), dir, err, out) |
| 47 | + } |
| 48 | + return strings.TrimSpace(string(out)) |
| 49 | +} |
| 50 | + |
| 51 | +func initRepo(t *testing.T, dir string) { |
| 52 | + t.Helper() |
| 53 | + runGit(t, dir, "init", "-q", "-b", "main") |
| 54 | + runGit(t, dir, "config", "user.email", "test@example.invalid") |
| 55 | + runGit(t, dir, "config", "user.name", "Test User") |
| 56 | + runGit(t, dir, "config", "commit.gpgsign", "false") |
| 57 | +} |
| 58 | + |
| 59 | +func writeRepoFile(t *testing.T, dir, name, content string) { |
| 60 | + t.Helper() |
| 61 | + path := filepath.Join(dir, name) |
| 62 | + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { |
| 63 | + t.Fatalf("mkdir for %s: %v", name, err) |
| 64 | + } |
| 65 | + if err := os.WriteFile(path, []byte(content), 0o644); err != nil { |
| 66 | + t.Fatalf("write %s: %v", name, err) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func commitAll(t *testing.T, dir, message string) string { |
| 71 | + t.Helper() |
| 72 | + runGit(t, dir, "add", "-A") |
| 73 | + runGit(t, dir, "commit", "-q", "-m", message) |
| 74 | + return runGit(t, dir, "rev-parse", "HEAD") |
| 75 | +} |
| 76 | + |
| 77 | +func runApp(t *testing.T, repoDir, baseSHA, headSHA, approvalSHA string) (*OutputData, []*gh.CurrentApproval, string) { |
| 78 | + t.Helper() |
| 79 | + |
| 80 | + warnings := &bytes.Buffer{} |
| 81 | + info := &bytes.Buffer{} |
| 82 | + |
| 83 | + realClient, err := gh.NewClient("test-owner", "test-repo", "test-token") |
| 84 | + if err != nil { |
| 85 | + t.Fatalf("failed to build the real client: %v", err) |
| 86 | + } |
| 87 | + realClient.SetWarningBuffer(warnings) |
| 88 | + realClient.SetInfoBuffer(info) |
| 89 | + |
| 90 | + client := &realCheckApprovalsClient{ |
| 91 | + mockGitHubClient: &mockGitHubClient{ |
| 92 | + pr: &github.PullRequest{ |
| 93 | + Number: github.Ptr(1), |
| 94 | + Base: &github.PullRequestBranch{SHA: github.Ptr(baseSHA)}, |
| 95 | + Head: &github.PullRequestBranch{SHA: github.Ptr(headSHA)}, |
| 96 | + User: &github.User{Login: github.Ptr("author")}, |
| 97 | + }, |
| 98 | + currentApprovals: []*gh.CurrentApproval{{ |
| 99 | + GHLogin: codeowners.NewSlug("@reviewer"), |
| 100 | + ReviewID: 1, |
| 101 | + Reviewers: []codeowners.Slug{codeowners.NewSlug("@owner")}, |
| 102 | + CommitID: approvalSHA, |
| 103 | + }}, |
| 104 | + }, |
| 105 | + real: realClient, |
| 106 | + } |
| 107 | + |
| 108 | + app := &App{ |
| 109 | + config: &Config{ |
| 110 | + RepoDir: repoDir, |
| 111 | + PR: 1, |
| 112 | + Quiet: true, |
| 113 | + InfoBuffer: info, |
| 114 | + WarningBuffer: warnings, |
| 115 | + }, |
| 116 | + client: client, |
| 117 | + } |
| 118 | + |
| 119 | + output, err := app.Run() |
| 120 | + if err != nil { |
| 121 | + t.Fatalf("app.Run failed: %v\nwarnings: %s", err, warnings) |
| 122 | + } |
| 123 | + return output, client.dismissed, warnings.String() |
| 124 | +} |
| 125 | + |
| 126 | +const retentionBaseSource = `package service |
| 127 | +
|
| 128 | +func Alpha() int { |
| 129 | + return 1 |
| 130 | +} |
| 131 | +
|
| 132 | +func Beta() int { |
| 133 | + return 2 |
| 134 | +} |
| 135 | +` |
| 136 | + |
| 137 | +// retentionApprovedSource is the change the reviewer approved. |
| 138 | +const retentionApprovedSource = `package service |
| 139 | +
|
| 140 | +func Alpha() int { |
| 141 | + return 1 |
| 142 | +} |
| 143 | +
|
| 144 | +func Beta() int { |
| 145 | + return 20 |
| 146 | +} |
| 147 | +` |
| 148 | + |
| 149 | +// Adds a comment and nothing else, so a comment is all the reviewer has not seen. |
| 150 | +const retentionHeadSource = `package service |
| 151 | +
|
| 152 | +// Alpha is the first step. |
| 153 | +func Alpha() int { |
| 154 | + return 1 |
| 155 | +} |
| 156 | +
|
| 157 | +func Beta() int { |
| 158 | + return 20 |
| 159 | +} |
| 160 | +` |
| 161 | + |
| 162 | +// configBody is committed as codeowners.toml on the base ref, which is where the |
| 163 | +// application reads its configuration from. |
| 164 | +func buildCommentOnlyRepo(t *testing.T, configBody string) (repoDir, baseSHA, headSHA, approvalSHA string) { |
| 165 | + t.Helper() |
| 166 | + repoDir = t.TempDir() |
| 167 | + initRepo(t, repoDir) |
| 168 | + |
| 169 | + writeRepoFile(t, repoDir, ".codeowners", "* @owner\n") |
| 170 | + writeRepoFile(t, repoDir, "codeowners.toml", configBody) |
| 171 | + writeRepoFile(t, repoDir, "service.go", retentionBaseSource) |
| 172 | + baseSHA = commitAll(t, repoDir, "base") |
| 173 | + |
| 174 | + writeRepoFile(t, repoDir, "service.go", retentionApprovedSource) |
| 175 | + approvalSHA = commitAll(t, repoDir, "approved change") |
| 176 | + |
| 177 | + writeRepoFile(t, repoDir, "service.go", retentionHeadSource) |
| 178 | + headSHA = commitAll(t, repoDir, "comment on top of the approved change") |
| 179 | + |
| 180 | + return repoDir, baseSHA, headSHA, approvalSHA |
| 181 | +} |
| 182 | + |
| 183 | +const retentionOffConfig = `disable_review_status_comments = true |
| 184 | +` |
| 185 | + |
| 186 | +// The feature is inert until asked for: no section and an all-off section have to |
| 187 | +// produce the same bytes. |
| 188 | +func TestRunWithoutRetentionSectionIsUnchanged(t *testing.T) { |
| 189 | + const explicitlyOff = `disable_review_status_comments = true |
| 190 | +
|
| 191 | +[approval_retention] |
| 192 | +enabled = false |
| 193 | +whitespace = false |
| 194 | +comments = false |
| 195 | +formatting = false |
| 196 | +string_literals = false |
| 197 | +renames = false |
| 198 | +fetch_orphaned_approval = false |
| 199 | +` |
| 200 | + |
| 201 | + repoDir, baseSHA, headSHA, approvalSHA := buildCommentOnlyRepo(t, retentionOffConfig) |
| 202 | + absentOutput, absentDismissed, absentWarnings := runApp(t, repoDir, baseSHA, headSHA, approvalSHA) |
| 203 | + |
| 204 | + repoDir, baseSHA, headSHA, approvalSHA = buildCommentOnlyRepo(t, explicitlyOff) |
| 205 | + offOutput, offDismissed, offWarnings := runApp(t, repoDir, baseSHA, headSHA, approvalSHA) |
| 206 | + |
| 207 | + if absentOutput.Message != offOutput.Message || absentOutput.Success != offOutput.Success { |
| 208 | + t.Errorf("expected identical results, got %+v and %+v", absentOutput, offOutput) |
| 209 | + } |
| 210 | + if !slices.Equal(absentOutput.StillRequired, offOutput.StillRequired) { |
| 211 | + t.Errorf("expected identical still required, got %v and %v", absentOutput.StillRequired, offOutput.StillRequired) |
| 212 | + } |
| 213 | + if len(absentDismissed) != len(offDismissed) { |
| 214 | + t.Errorf("expected identical dismissals, got %d and %d", len(absentDismissed), len(offDismissed)) |
| 215 | + } |
| 216 | + if absentWarnings != offWarnings { |
| 217 | + t.Errorf("expected identical warnings, got %q and %q", absentWarnings, offWarnings) |
| 218 | + } |
| 219 | + // Both are the pre-feature behavior, not merely equal to each other. |
| 220 | + if len(absentDismissed) != 1 || absentOutput.Success { |
| 221 | + t.Errorf("expected the approval to be dismissed as it always was, got %d dismissals, success %t", |
| 222 | + len(absentDismissed), absentOutput.Success) |
| 223 | + } |
| 224 | +} |
0 commit comments