| 158 | } |
| 159 | |
| 160 | func resolveCommitSHAWithRetries(ctx context.Context, owner, repo, ref, workflowPath, host string, verbose bool) (string, error) { |
| 161 | attempts := len(shaResolutionRetryDelays) + 1 |
| 162 | var lastErr error |
| 163 | |
| 164 | for attempt := 1; attempt <= attempts; attempt++ { |
| 165 | commitSHA, err := resolveRefToSHAForHost(owner, repo, ref, host) |
| 166 | if err == nil { |
| 167 | remoteWorkflowLog.Printf("Resolved ref %s to SHA: %s", ref, commitSHA) |
| 168 | return commitSHA, nil |
| 169 | } |
| 170 | |
| 171 | lastErr = err |
| 172 | remoteWorkflowLog.Printf("Failed to resolve ref %s to SHA (attempt %d/%d): %v", ref, attempt, attempts, err) |
| 173 | |
| 174 | if !isTransientSHAResolutionError(err) { |
| 175 | retryCommand := fmt.Sprintf("gh aw add %s/%s/%s@<40-char-sha>", owner, repo, workflowPath) |
| 176 | if hostHint, ok := hostResolutionHintForNotFound(owner, repo, ref, workflowPath, host, err); ok { |
| 177 | return "", fmt.Errorf( |
| 178 | "failed to resolve '%s' to commit SHA for '%s/%s'. Expected the GitHub API to return a commit SHA for the ref. %s: %w", |
| 179 | ref, owner, repo, hostHint, err, |
| 180 | ) |
| 181 | } |
| 182 | return "", fmt.Errorf( |
| 183 | "failed to resolve '%s' to commit SHA for '%s/%s'. Expected the GitHub API to return a commit SHA for the ref. Try: %s: %w", |
| 184 | ref, owner, repo, retryCommand, err, |
| 185 | ) |
| 186 | } |
| 187 | |
| 188 | if attempt < attempts { |
| 189 | delay := shaResolutionRetryDelays[attempt-1] |
| 190 | if verbose { |
| 191 | fmt.Fprintln(os.Stderr, console.FormatWarningMessage( |
| 192 | fmt.Sprintf("Transient SHA resolution failure for '%s' (attempt %d/%d). Retrying in %s...", ref, attempt, attempts, delay), |
| 193 | )) |
| 194 | } |
| 195 | if waitErr := waitBeforeSHAResolutionRetry(ctx, delay); waitErr != nil { |
| 196 | retryCommand := fmt.Sprintf("gh aw add %s/%s/%s@<40-char-sha>", owner, repo, workflowPath) |
| 197 | return "", fmt.Errorf( |
| 198 | "failed to resolve '%s' to commit SHA because retry wait was cancelled. Expected the GitHub API to return a commit SHA for the ref. Try: %s: %w", |
| 199 | ref, retryCommand, waitErr, |
| 200 | ) |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | retryCommand := fmt.Sprintf("gh aw add %s/%s/%s@<40-char-sha>", owner, repo, workflowPath) |
| 206 | return "", fmt.Errorf( |
| 207 | "failed to resolve '%s' to commit SHA after %d retries for '%s/%s'. Expected the GitHub API to return a commit SHA for the ref. Check rate limits or try: %s: %w", |
| 208 | ref, len(shaResolutionRetryDelays), owner, repo, retryCommand, lastErr, |
| 209 | ) |
| 210 | } |
| 211 | |
| 212 | // hostResolutionHintForNotFound returns a user-facing hint and whether it is applicable. |
| 213 | // hasHint is true only for 404-style resolution failures on non-github.com hosts. |