GetAddonTarballURL retrieves the tarball URL for an addon from the add-on registry based on the provided owner/repo and flags. Parameters: - ownerRepo: The owner/repo string (e.g., "ddev/ddev-redis") - gitRef: Specific git reference to install - can be a tag (v1.2.3), branch name (main), or commit
(ownerRepo, gitRef string, defaultBranch bool, prNumber int)
| 807 | // - version: The version string (tag, branch, or PR reference) |
| 808 | // - error: Any error encountered |
| 809 | func GetAddonTarballURL(ownerRepo, gitRef string, defaultBranch bool, prNumber int) (tarballURL, version string, err error) { |
| 810 | // Parse owner/repo |
| 811 | parts := strings.Split(ownerRepo, "/") |
| 812 | if len(parts) != 2 { |
| 813 | return "", "", fmt.Errorf("invalid add-on format '%s', expected owner/repo", ownerRepo) |
| 814 | } |
| 815 | owner, repo := parts[0], parts[1] |
| 816 | baseURL := fmt.Sprintf("https://github.com/%s/%s/tarball", owner, repo) |
| 817 | |
| 818 | // If specific git ref is requested, use it directly |
| 819 | if gitRef != "" { |
| 820 | tarballURL = fmt.Sprintf("%s/%s", baseURL, gitRef) |
| 821 | return tarballURL, gitRef, nil |
| 822 | } |
| 823 | |
| 824 | // If PR number is specified, use it |
| 825 | if prNumber > 0 { |
| 826 | tarballURL = fmt.Sprintf("%s/refs/pull/%d/head", baseURL, prNumber) |
| 827 | version = fmt.Sprintf("pr-%d", prNumber) |
| 828 | return tarballURL, version, nil |
| 829 | } |
| 830 | |
| 831 | // Use cached registry to get latest commit from default_branch |
| 832 | if defaultBranch { |
| 833 | addonData, err := getAddonRegistryWithFallback() |
| 834 | if err != nil { |
| 835 | return "", "", fmt.Errorf("failed to get add-on registry: %w", err) |
| 836 | } |
| 837 | |
| 838 | addon := addonData.FindAddon(ownerRepo) |
| 839 | if addon == nil { |
| 840 | return "", "", fmt.Errorf("add-on '%s' not found in registry", ownerRepo) |
| 841 | } |
| 842 | |
| 843 | if addon.DefaultBranch == "" { |
| 844 | return "", "", fmt.Errorf("add-on '%s' has no default_branch specified in registry", ownerRepo) |
| 845 | } |
| 846 | tarballURL = fmt.Sprintf("%s/%s", baseURL, addon.DefaultBranch) |
| 847 | version = addon.DefaultBranch |
| 848 | return tarballURL, version, nil |
| 849 | } |
| 850 | |
| 851 | // For default behavior (no flags), try GitHub API first for fresh release info |
| 852 | // This ensures users get the latest release, falling back to registry if API fails |
| 853 | tarballURL, version, err = github.GetGitHubRelease(owner, repo, "") |
| 854 | if err == nil { |
| 855 | // Successfully got fresh release from GitHub |
| 856 | return tarballURL, version, nil |
| 857 | } |
| 858 | |
| 859 | // GitHub API failed (rate limit, network, etc.), fall back to cached registry |
| 860 | util.Warning("Warning: %v\nFalling back to cached add-on registry...", err) |
| 861 | addonData, cacheErr := getAddonRegistryWithFallback() |
| 862 | if cacheErr != nil { |
| 863 | // Both GitHub and cache failed, return the GitHub error |
| 864 | return "", "", err |
| 865 | } |
| 866 |