ExecuteRemoteRepoSync will take several steps to sync the source and destination repositories. First it will try to use the merge-upstream API endpoint. If this fails due to merge conflicts or unknown merge issues then it will fallback to using the low level git references API endpoint. The reason t
(client *api.Client, destRepo, srcRepo ghrepo.Interface, opts *SyncOptions)
| 279 | // the git references API endpoint gives more detailed error responses as to why the sync failed. |
| 280 | // Unless the --force flag is specified we will not perform non-fast-forward merges. |
| 281 | func executeRemoteRepoSync(client *api.Client, destRepo, srcRepo ghrepo.Interface, opts *SyncOptions) (string, error) { |
| 282 | branchName := opts.Branch |
| 283 | if branchName == "" { |
| 284 | var err error |
| 285 | branchName, err = api.RepoDefaultBranch(client, destRepo) |
| 286 | if err != nil { |
| 287 | return "", err |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | var apiErr upstreamMergeErr |
| 292 | if baseBranch, err := triggerUpstreamMerge(client, destRepo, branchName); err == nil { |
| 293 | return baseBranch, nil |
| 294 | } else if !errors.As(err, &apiErr) { |
| 295 | return "", err |
| 296 | } |
| 297 | |
| 298 | if srcRepo == nil { |
| 299 | var err error |
| 300 | srcRepo, err = api.RepoParent(client, destRepo) |
| 301 | if err != nil { |
| 302 | return "", err |
| 303 | } |
| 304 | if srcRepo == nil { |
| 305 | return "", fmt.Errorf("can't determine source repository for %s because repository is not fork", ghrepo.FullName(destRepo)) |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | commit, err := latestCommit(client, srcRepo, branchName) |
| 310 | if err != nil { |
| 311 | return "", err |
| 312 | } |
| 313 | |
| 314 | // Using string comparison is a brittle way to determine the error returned by the API |
| 315 | // endpoint but unfortunately the API returns 422 for many reasons so we must |
| 316 | // interpret the message provide better error messaging for our users. |
| 317 | err = syncFork(client, destRepo, branchName, commit.Object.SHA, opts.Force) |
| 318 | var httpErr api.HTTPError |
| 319 | if err != nil { |
| 320 | if errors.As(err, &httpErr) { |
| 321 | switch httpErr.Message { |
| 322 | case notFastForwardErrorMessage: |
| 323 | return "", divergingError |
| 324 | case branchDoesNotExistErrorMessage: |
| 325 | return "", fmt.Errorf("%s branch does not exist on %s repository", branchName, ghrepo.FullName(destRepo)) |
| 326 | } |
| 327 | } |
| 328 | return "", err |
| 329 | } |
| 330 | |
| 331 | return fmt.Sprintf("%s:%s", srcRepo.RepoOwner(), branchName), nil |
| 332 | } |
no test coverage detected