resolveWorkflowDisplayName returns the human-readable display name for a workflow file. It first attempts to read the YAML file from the local filesystem (resolving the path relative to the git repository root so that it works from any working directory inside the repo); if that fails it falls back
(ctx context.Context, workflowPath, owner, repo, hostname string)
| 1199 | // the repo); if that fails it falls back to a GitHub API call. An empty string is |
| 1200 | // returned on any error so that callers can gracefully keep the original value. |
| 1201 | func resolveWorkflowDisplayName(ctx context.Context, workflowPath, owner, repo, hostname string) string { |
| 1202 | // Try local file first. workflowPath is a repo-relative path like |
| 1203 | // ".github/workflows/foo.lock.yml", so we resolve it against the git root to |
| 1204 | // produce a correct absolute path regardless of the current working directory. |
| 1205 | if gitRoot, err := gitutil.FindGitRoot(); err == nil { |
| 1206 | absPath := filepath.Join(gitRoot, workflowPath) |
| 1207 | if content, err := os.ReadFile(absPath); err == nil { |
| 1208 | if name := extractWorkflowNameFromYAML(content); name != "" { |
| 1209 | return name |
| 1210 | } |
| 1211 | } |
| 1212 | } |
| 1213 | |
| 1214 | // Fall back to the GitHub Actions workflows API. |
| 1215 | filename := filepath.Base(workflowPath) |
| 1216 | var endpoint string |
| 1217 | if owner != "" && repo != "" { |
| 1218 | endpoint = fmt.Sprintf("repos/%s/%s/actions/workflows/%s", owner, repo, filename) |
| 1219 | } else { |
| 1220 | endpoint = "repos/{owner}/{repo}/actions/workflows/" + filename |
| 1221 | } |
| 1222 | |
| 1223 | args := []string{"api"} |
| 1224 | if hostname != "" && hostname != "github.com" { |
| 1225 | args = append(args, "--hostname", hostname) |
| 1226 | } |
| 1227 | args = append(args, endpoint, "--jq", ".name") |
| 1228 | |
| 1229 | out, err := workflow.RunGHCombinedContext(ctx, "Fetching workflow name...", args...) |
| 1230 | if err != nil { |
| 1231 | auditLog.Printf("Failed to fetch workflow display name for %q: %v", workflowPath, err) |
| 1232 | return "" |
| 1233 | } |
| 1234 | |
| 1235 | return strings.TrimSpace(string(out)) |
| 1236 | } |
| 1237 | |
| 1238 | // extractWorkflowNameFromYAML parses a GitHub Actions workflow YAML document and |
| 1239 | // returns the value of its top-level "name:" field. An empty string is returned |
no test coverage detected