[gh workflow run] Improve error handling for --ref flag

This commit is contained in:
Azeem Sajid 2025-01-29 16:39:56 +05:00
parent b0cd1fb655
commit c7dd9edd57
2 changed files with 13 additions and 6 deletions

View file

@ -262,11 +262,15 @@ func runRun(opts *RunOptions) error {
ref := opts.Ref
if ref == "" {
ref, err = api.RepoDefaultBranch(client, repo)
if err != nil {
return fmt.Errorf("unable to determine default branch for %s: %w", ghrepo.FullName(repo), err)
}
defaultBranch, err := api.RepoDefaultBranch(client, repo)
if err != nil {
return fmt.Errorf("unable to determine default branch for %s: %w", ghrepo.FullName(repo), err)
}
if ref != "" && ref != defaultBranch {
return fmt.Errorf("ref %s is not the default branch, and workflows must be on the default branch to be triggered by workflow_dispatch", ref)
} else {
ref = defaultBranch
}
states := []shared.WorkflowState{shared.Active}

View file

@ -131,7 +131,10 @@ func FindWorkflow(client *api.Client, repo ghrepo.Interface, workflowSelector st
if _, err := strconv.Atoi(workflowSelector); err == nil || isWorkflowFile(workflowSelector) {
workflow, err := getWorkflowByID(client, repo, workflowSelector)
if err != nil {
return nil, err
var httpErr api.HTTPError
if errors.As(err, &httpErr) && httpErr.StatusCode == 404 {
return nil, fmt.Errorf("workflow %s not found on the default branch", workflowSelector)
}
}
return []Workflow{*workflow}, nil
}