MCPcopy Index your code
hub / github.com/nikivdev/go / runClonePR

Function runClonePR

cli/flow/main.go:1110–1171  ·  view source on GitHub ↗
(ctx *snap.Context)

Source from the content-addressed store, hash-verified

1108}
1109
1110func runClonePR(ctx *snap.Context) error {
1111 if ctx.NArgs() != 1 {
1112 fmt.Fprintf(ctx.Stderr(), "Usage: %s clonePR <github-pr-url-or-owner/repo#num>\n", commandName)
1113 return fmt.Errorf("expected 1 argument, got %d", ctx.NArgs())
1114 }
1115
1116 ref := strings.TrimSpace(ctx.Arg(0))
1117 if ref == "" {
1118 fmt.Fprintf(ctx.Stderr(), "Usage: %s clonePR <github-pr-url-or-owner/repo#num>\n", commandName)
1119 return fmt.Errorf("pull request reference cannot be empty")
1120 }
1121
1122 owner, repo, prNumber, err := parsePullRequestRef(ref)
1123 if err != nil {
1124 return err
1125 }
1126
1127 if _, err := exec.LookPath("gh"); err != nil {
1128 return fmt.Errorf("gh CLI not found in PATH: %w", err)
1129 }
1130
1131 repoFull := fmt.Sprintf("%s/%s", owner, repo)
1132 dest, err := pullRequestCloneDestination(repo, prNumber)
1133 if err != nil {
1134 return err
1135 }
1136
1137 if err := os.MkdirAll(filepath.Dir(dest), 0o755); err != nil {
1138 return fmt.Errorf("create destination parent: %w", err)
1139 }
1140
1141 if info, err := os.Stat(dest); err == nil {
1142 if info.IsDir() {
1143 return fmt.Errorf("destination %s already exists", dest)
1144 }
1145 return fmt.Errorf("destination %s exists and is not a directory", dest)
1146 } else if !errors.Is(err, os.ErrNotExist) {
1147 return fmt.Errorf("check destination %s: %w", dest, err)
1148 }
1149
1150 fmt.Fprintf(ctx.Stdout(), "Cloning %s PR #%d into %s\n", repoFull, prNumber, dest)
1151
1152 cloneCmd := exec.Command("gh", "repo", "clone", repoFull, dest)
1153 cloneCmd.Stdout = ctx.Stdout()
1154 cloneCmd.Stderr = ctx.Stderr()
1155 cloneCmd.Stdin = ctx.Stdin()
1156 if err := cloneCmd.Run(); err != nil {
1157 return fmt.Errorf("gh repo clone %s: %w", repoFull, err)
1158 }
1159
1160 checkoutCmd := exec.Command("gh", "pr", "checkout", strconv.Itoa(prNumber))
1161 checkoutCmd.Dir = dest
1162 checkoutCmd.Stdout = ctx.Stdout()
1163 checkoutCmd.Stderr = ctx.Stderr()
1164 checkoutCmd.Stdin = ctx.Stdin()
1165 if err := checkoutCmd.Run(); err != nil {
1166 return fmt.Errorf("gh pr checkout %d: %w", prNumber, err)
1167 }

Callers 1

mainFunction · 0.85

Calls 2

parsePullRequestRefFunction · 0.85

Tested by

no test coverage detected