DownloadGitHubZip fetches an owner/repo/branch zip and extracts it under destRoot. The returned path is the root of the extracted tree (which GitHub generates as " - /"). When the requested branch returns HTTP 404, it automatically retries with common fallback branches (e.g. "master" w
(owner, repo, branch, destRoot string)
| 42 | // common fallback branches (e.g. "master" when "main" was tried, or vice |
| 43 | // versa) before giving up. |
| 44 | func (c *Client) DownloadGitHubZip(owner, repo, branch, destRoot string) (string, error) { |
| 45 | if owner == "" || repo == "" { |
| 46 | return "", errors.New("fetching: owner and repo are required") |
| 47 | } |
| 48 | if branch == "" { |
| 49 | branch = "main" |
| 50 | } |
| 51 | |
| 52 | // Build the ordered list of branches to try. |
| 53 | branches := []string{branch} |
| 54 | switch branch { |
| 55 | case "main": |
| 56 | branches = append(branches, "master") |
| 57 | case "master": |
| 58 | branches = append(branches, "main") |
| 59 | } |
| 60 | |
| 61 | var lastErr error |
| 62 | for _, br := range branches { |
| 63 | path, err := c.downloadBranchZip(owner, repo, br, destRoot) |
| 64 | if err == nil { |
| 65 | return path, nil |
| 66 | } |
| 67 | lastErr = err |
| 68 | // Only retry on 404; any other error is returned immediately. |
| 69 | if !strings.Contains(err.Error(), "HTTP 404") { |
| 70 | return "", err |
| 71 | } |
| 72 | } |
| 73 | return "", lastErr |
| 74 | } |
| 75 | |
| 76 | // downloadBranchZip does the actual download for a single branch. |
| 77 | func (c *Client) downloadBranchZip(owner, repo, branch, destRoot string) (string, error) { |