|
4 | 4 | "fmt" |
5 | 5 | "os" |
6 | 6 | "path/filepath" |
| 7 | + "strings" |
7 | 8 |
|
8 | 9 | "github.com/github/gh-aw/pkg/console" |
9 | 10 | "github.com/github/gh-aw/pkg/logger" |
@@ -83,11 +84,37 @@ func getCLICmdPrefix(actionMode ActionMode) string { |
83 | 84 | return "gh aw" |
84 | 85 | } |
85 | 86 |
|
| 87 | +// FetchDefaultBranch queries the GitHub API to determine the default branch of the |
| 88 | +// given repository slug (owner/repo). Returns "main" as a fallback when the slug is |
| 89 | +// empty, not in owner/repo format, or when the API call fails. |
| 90 | +func FetchDefaultBranch(slug string) string { |
| 91 | + const fallback = "main" |
| 92 | + if slug == "" || strings.Count(slug, "/") != 1 { |
| 93 | + maintenanceLog.Printf("No valid repository slug, using default branch fallback: %s", fallback) |
| 94 | + return fallback |
| 95 | + } |
| 96 | + maintenanceLog.Printf("Fetching default branch for repository: %s", slug) |
| 97 | + output, err := RunGH("Fetching default branch...", "api", "/repos/"+slug, "--jq", ".default_branch") |
| 98 | + if err != nil { |
| 99 | + maintenanceLog.Printf("Failed to fetch default branch for %s: %v, falling back to %s", slug, err, fallback) |
| 100 | + return fallback |
| 101 | + } |
| 102 | + branch := strings.TrimSpace(string(output)) |
| 103 | + if branch == "" { |
| 104 | + maintenanceLog.Printf("Empty default branch response for %s, falling back to %s", slug, fallback) |
| 105 | + return fallback |
| 106 | + } |
| 107 | + maintenanceLog.Printf("Default branch for %s: %s", slug, branch) |
| 108 | + return branch |
| 109 | +} |
| 110 | + |
86 | 111 | // GenerateMaintenanceWorkflow generates the agentics-maintenance.yml workflow |
87 | 112 | // if any workflows use the expires field for discussions or issues. |
88 | 113 | // When repoConfig is non-nil and repoConfig.MaintenanceDisabled is true the |
89 | 114 | // maintenance workflow is deleted and the function returns immediately. |
90 | | -func GenerateMaintenanceWorkflow(workflowDataList []*WorkflowData, workflowDir string, version string, actionMode ActionMode, actionTag string, verbose bool, repoConfig *RepoConfig) error { |
| 115 | +// repoSlug is the owner/repo slug used to determine the default branch for the push |
| 116 | +// trigger; pass an empty string to fall back to "main". |
| 117 | +func GenerateMaintenanceWorkflow(workflowDataList []*WorkflowData, workflowDir string, version string, actionMode ActionMode, actionTag string, verbose bool, repoConfig *RepoConfig, repoSlug string) error { |
91 | 118 | maintenanceLog.Print("Checking if maintenance workflow is needed") |
92 | 119 |
|
93 | 120 | // Respect explicit opt-out from aw.json: maintenance: false |
@@ -144,8 +171,12 @@ func GenerateMaintenanceWorkflow(workflowDataList []*WorkflowData, workflowDir s |
144 | 171 | cronSchedule, scheduleDesc := generateMaintenanceCron(minExpiresDays) |
145 | 172 | maintenanceLog.Printf("Maintenance schedule: %s (%s)", cronSchedule, scheduleDesc) |
146 | 173 |
|
| 174 | + // Fetch the default branch for the push trigger (dev mode only) |
| 175 | + // Resolved here to avoid passing it through multiple layers; empty slug falls back to "main" |
| 176 | + defaultBranch := FetchDefaultBranch(repoSlug) |
| 177 | + |
147 | 178 | // Generate the YAML content for the maintenance workflow |
148 | | - content := buildMaintenanceWorkflowYAML(cronSchedule, scheduleDesc, minExpiresDays, runsOnValue, actionMode, version, actionTag, resolver, configuredRunsOn) |
| 179 | + content := buildMaintenanceWorkflowYAML(cronSchedule, scheduleDesc, minExpiresDays, runsOnValue, actionMode, version, actionTag, resolver, configuredRunsOn, defaultBranch) |
149 | 180 |
|
150 | 181 | // Write the maintenance workflow file |
151 | 182 | maintenanceFile := filepath.Join(workflowDir, "agentics-maintenance.yml") |
|
0 commit comments