Follow up of https://github.com/cli/cli/pull/7612 The `missingWorkflowScopeRE` is defined to capture the error message when the `GH_TOKEN` does not have `workflow` scope in `gh repo sync <remote>`, but this is only intended for error messages for OAuth Apps and does not work with GitHub Apps. In GitHub App, you will get the following error: ``` { "message": "refusing to allow a GitHub App to create or update workflow `.github/workflows/teamcity-pr-checks.yml` without `workflows` permission", "documentation_url": "https://docs.github.com/rest/branches/branches#sync-a-fork-branch-with-the-upstream-repository", "status": "422" } ``` As you can see above, the existing regexp does not match the "`workflows` permission". This change modifies the regexp to return the user-friendly error message when the `workflow` permission is missing, even in the case of a GitHub App.
80 lines
2.4 KiB
Go
80 lines
2.4 KiB
Go
package sync
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"regexp"
|
|
|
|
"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 missingWorkflowScopeRE = regexp.MustCompile("refusing to allow.*without `workflow(s)?` (scope|permission)")
|
|
var missingWorkflowScopeErr = errors.New("Upstream commits contain workflow changes, which require the `workflow` scope or permission to merge. To request it, run: gh auth refresh -s workflow")
|
|
|
|
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:
|
|
if missingWorkflowScopeRE.MatchString(httpErr.Message) {
|
|
return "", missingWorkflowScopeErr
|
|
}
|
|
return "", upstreamMergeErr{errors.New(httpErr.Message)}
|
|
}
|
|
}
|
|
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)
|
|
}
|