Remove old code paths and improve code comments for repo sync (#7610)

This commit is contained in:
Sam Coe 2023-06-23 20:32:48 +09:00 committed by GitHub
parent ddd27a5897
commit ddfe476ff9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 7 deletions

View file

@ -32,8 +32,6 @@ func latestCommit(client *api.Client, repo ghrepo.Interface, branch string) (com
type upstreamMergeErr struct{ error }
var upstreamMergeUnavailableErr = upstreamMergeErr{errors.New("upstream merge API is unavailable")}
var missingWorkflowScopeRE = regexp.MustCompile("refusing to allow.*without `workflow` scope")
var missingWorkflowScopeErr = errors.New("Upstream commits contain workflow changes, which require the `workflow` scope to merge. To request it, run: gh auth refresh -s workflow")
@ -60,8 +58,6 @@ func triggerUpstreamMerge(client *api.Client, repo ghrepo.Interface, branch stri
return "", missingWorkflowScopeErr
}
return "", upstreamMergeErr{errors.New(httpErr.Message)}
case http.StatusNotFound:
return "", upstreamMergeUnavailableErr
}
}
return "", err

View file

@ -284,6 +284,13 @@ func executeLocalRepoSync(srcRepo ghrepo.Interface, remote string, opts *SyncOpt
return nil
}
// 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 the fallback is necessary is to better support these error cases. The git references API
// endpoint allows us to sync repositories that are not fast-forward merge compatible. Additionally,
// the git references API endpoint gives more detailed error responses as to why the sync failed.
// Unless the --force flag is specified we will not perform non-fast-forward merges.
func executeRemoteRepoSync(client *api.Client, destRepo, srcRepo ghrepo.Interface, opts *SyncOptions) (string, error) {
branchName := opts.Branch
if branchName == "" {
@ -317,8 +324,9 @@ func executeRemoteRepoSync(client *api.Client, destRepo, srcRepo ghrepo.Interfac
return "", err
}
// This is not a great way to detect the error returned by the API
// Unfortunately API returns 422 for multiple reasons
// Using string comparison is a brittle way to determine the error returned by the API
// endpoint but unfortunately the API returns 422 for many reasons so we must
// interpret the message provide better error messaging for our users.
err = syncFork(client, destRepo, branchName, commit.Object.SHA, opts.Force)
var httpErr api.HTTPError
if err != nil {

View file

@ -292,7 +292,7 @@ func Test_SyncRun(t *testing.T) {
httpmock.StringResponse(`{"data":{"repository":{"defaultBranchRef":{"name": "trunk"}}}}`))
reg.Register(
httpmock.REST("POST", "repos/FORKOWNER/REPO-FORK/merge-upstream"),
httpmock.StatusStringResponse(404, `{}`))
httpmock.StatusStringResponse(422, `{}`))
reg.Register(
httpmock.REST("GET", "repos/OWNER/REPO/git/refs/heads/trunk"),
httpmock.StringResponse(`{"object":{"sha":"0xDEADBEEF"}}`))