Skip to content

Commit 6169cba

Browse files
committed
fix: optimize github scm polling guards
1 parent c6ae0e1 commit 6169cba

7 files changed

Lines changed: 528 additions & 74 deletions

File tree

‎backend/internal/adapters/scm/github/commands.go‎

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,38 @@ func (p *Provider) Checkout(ctx context.Context, req ports.SCMCommandRequest) (p
104104
return res, nil
105105
}
106106
branch := fmt.Sprintf("pr-%d", req.ChangeRequest.Number)
107-
cmd := exec.CommandContext(ctx, "git", "fetch", "origin", fmt.Sprintf("pull/%d/head:%s", req.ChangeRequest.Number, branch))
108-
cmd.Dir = req.WorkspacePath
107+
if err := checkoutWithGit(ctx, req.WorkspacePath, req.ChangeRequest.Number, branch); err == nil {
108+
res.Message = "checked out " + branch
109+
return res, nil
110+
} else if ghErr := checkoutWithGH(ctx, req.WorkspacePath, req.ChangeRequest); ghErr == nil {
111+
res.Message = "checked out with gh pr checkout"
112+
return res, nil
113+
} else {
114+
return res, &domain.SCMError{Kind: domain.SCMErrorCommand, Operation: "github.command.checkout", Message: fmt.Sprintf("git checkout failed: %v; gh fallback failed: %v", err, ghErr), Cause: err}
115+
}
116+
}
117+
118+
func checkoutWithGit(ctx context.Context, workspacePath string, number int, branch string) error {
119+
cmd := exec.CommandContext(ctx, "git", "fetch", "origin", fmt.Sprintf("pull/%d/head:%s", number, branch))
120+
cmd.Dir = workspacePath
109121
if out, err := cmd.CombinedOutput(); err != nil {
110-
return res, &domain.SCMError{Kind: domain.SCMErrorCommand, Operation: "github.command.checkout", Message: strings.TrimSpace(string(out)), Cause: err}
122+
return fmt.Errorf("git fetch: %s: %w", strings.TrimSpace(string(out)), err)
111123
}
112124
cmd = exec.CommandContext(ctx, "git", "checkout", branch)
113-
cmd.Dir = req.WorkspacePath
125+
cmd.Dir = workspacePath
114126
if out, err := cmd.CombinedOutput(); err != nil {
115-
return res, &domain.SCMError{Kind: domain.SCMErrorCommand, Operation: "github.command.checkout", Message: strings.TrimSpace(string(out)), Cause: err}
127+
return fmt.Errorf("git checkout: %s: %w", strings.TrimSpace(string(out)), err)
116128
}
117-
res.Message = "checked out " + branch
118-
return res, nil
129+
return nil
130+
}
131+
132+
func checkoutWithGH(ctx context.Context, workspacePath string, cr domain.SCMChangeRequestID) error {
133+
cmd := exec.CommandContext(ctx, "gh", "pr", "checkout", strconv.Itoa(cr.Number), "--repo", cr.Repo)
134+
cmd.Dir = workspacePath
135+
if out, err := cmd.CombinedOutput(); err != nil {
136+
return fmt.Errorf("gh pr checkout: %s: %w", strings.TrimSpace(string(out)), err)
137+
}
138+
return nil
119139
}
120140

121141
func (p *Provider) normalizeCommandRequest(ctx context.Context, req ports.SCMCommandRequest, command ports.SCMCommand) ports.SCMCommandRequest {

0 commit comments

Comments
 (0)