cli/pkg/cmd/repo/sync/http.go
Mislav Marohnić 1260023547 repo sync: Use the new merge-upstream API if available
This helps avoid the errors when a new commit from upstream is not yet
available in the fork, resulting in a HTTP 404 when trying to update the
ref in the fork.

If the merge-upstream API is unavailable (404) or errors out (409/422),
the functionality falls back to the previous functionality of manually
updating refs. This ensures that `--force` still has effect.
2021-12-21 16:44:27 +01:00

77 lines
2.2 KiB
Go

package sync
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"github.com/cli/cli/v2/api"
"github.com/cli/cli/v2/internal/ghrepo"
)
type commit struct {
Ref string `json:"ref"`
NodeID string `json:"node_id"`
URL string `json:"url"`
Object struct {
Type string `json:"type"`
SHA string `json:"sha"`
URL string `json:"url"`
} `json:"object"`
}
func latestCommit(client *api.Client, repo ghrepo.Interface, branch string) (commit, error) {
var response commit
path := fmt.Sprintf("repos/%s/%s/git/refs/heads/%s", repo.RepoOwner(), repo.RepoName(), branch)
err := client.REST(repo.RepoHost(), "GET", path, nil, &response)
return response, err
}
type upstreamMergeErr struct{ error }
var upstreamMergeUnavailableErr = upstreamMergeErr{errors.New("upstream merge API is unavailable")}
func triggerUpstreamMerge(client *api.Client, repo ghrepo.Interface, branch string) (string, error) {
var payload bytes.Buffer
if err := json.NewEncoder(&payload).Encode(map[string]interface{}{
"branch": branch,
}); err != nil {
return "", err
}
var response struct {
Message string `json:"message"`
MergeType string `json:"merge_type"`
BaseBranch string `json:"base_branch"`
}
path := fmt.Sprintf("repos/%s/%s/merge-upstream", repo.RepoOwner(), repo.RepoName())
var httpErr api.HTTPError
if err := client.REST(repo.RepoHost(), "POST", path, &payload, &response); err != nil {
if errors.As(err, &httpErr) {
switch httpErr.StatusCode {
case http.StatusUnprocessableEntity, http.StatusConflict:
return "", upstreamMergeErr{errors.New(httpErr.Message)}
case http.StatusNotFound:
return "", upstreamMergeUnavailableErr
}
}
return "", err
}
return response.BaseBranch, nil
}
func syncFork(client *api.Client, repo ghrepo.Interface, branch, SHA string, force bool) error {
path := fmt.Sprintf("repos/%s/%s/git/refs/heads/%s", repo.RepoOwner(), repo.RepoName(), branch)
body := map[string]interface{}{
"sha": SHA,
"force": force,
}
requestByte, err := json.Marshal(body)
if err != nil {
return err
}
requestBody := bytes.NewReader(requestByte)
return client.REST(repo.RepoHost(), "PATCH", path, requestBody, nil)
}