(opts CreateOptions, ctx CreateContext)
| 1159 | } |
| 1160 | |
| 1161 | func handlePush(opts CreateOptions, ctx CreateContext) error { |
| 1162 | refs := ctx.PRRefs |
| 1163 | forkableRefs, requiresFork := refs.(forkableRefs) |
| 1164 | if requiresFork { |
| 1165 | opts.IO.StartProgressIndicator() |
| 1166 | forkedRepo, err := api.ForkRepo(ctx.Client, forkableRefs.BaseRepo(), "", "", true) |
| 1167 | opts.IO.StopProgressIndicator() |
| 1168 | if err != nil { |
| 1169 | return fmt.Errorf("error forking repo: %w", err) |
| 1170 | } |
| 1171 | |
| 1172 | refs = pushableRefs{ |
| 1173 | headRepo: forkedRepo, |
| 1174 | headBranchName: forkableRefs.qualifiedHeadRef.BranchName(), |
| 1175 | baseRefs: baseRefs{ |
| 1176 | baseRepo: forkableRefs.baseRepo, |
| 1177 | baseBranchName: forkableRefs.baseBranchName, |
| 1178 | }, |
| 1179 | } |
| 1180 | } |
| 1181 | |
| 1182 | // We may have upcast to pushableRefs on fork, or we may have been passed an instance |
| 1183 | // already. But if we haven't, then there's nothing more to do. |
| 1184 | pushableRefs, ok := refs.(pushableRefs) |
| 1185 | if !ok { |
| 1186 | return nil |
| 1187 | } |
| 1188 | |
| 1189 | // There are two cases when an existing remote for the head repo will be |
| 1190 | // missing (and an error will be returned): |
| 1191 | // 1. the head repo was just created by auto-forking; |
| 1192 | // 2. an existing fork was discovered by querying the API. |
| 1193 | // In either case, we want to add the head repo as a new git remote so we |
| 1194 | // can push to it. We will try to add the head repo as the "origin" remote |
| 1195 | // and fallback to the "fork" remote if it is unavailable. Also, if the |
| 1196 | // base repo is the "origin" remote we will rename it "upstream". |
| 1197 | headRemote, _ := ctx.ResolvedRemotes.RemoteForRepo(pushableRefs.HeadRepo()) |
| 1198 | if headRemote == nil { |
| 1199 | cfg, err := opts.Config() |
| 1200 | if err != nil { |
| 1201 | return err |
| 1202 | } |
| 1203 | |
| 1204 | remotes, err := opts.Remotes() |
| 1205 | if err != nil { |
| 1206 | return err |
| 1207 | } |
| 1208 | |
| 1209 | cloneProtocol := cfg.GitProtocol(pushableRefs.HeadRepo().RepoHost()).Value |
| 1210 | headRepoURL := ghrepo.FormatRemoteURL(pushableRefs.HeadRepo(), cloneProtocol) |
| 1211 | gitClient := ctx.GitClient |
| 1212 | origin, _ := remotes.FindByName("origin") |
| 1213 | upstreamName := "upstream" |
| 1214 | upstream, _ := remotes.FindByName(upstreamName) |
| 1215 | remoteName := "origin" |
| 1216 | |
| 1217 | if origin != nil { |
| 1218 | remoteName = "fork" |
no test coverage detected