|
6 | 6 | "io" |
7 | 7 | "net/http" |
8 | 8 | "os" |
| 9 | + "strings" |
9 | 10 |
|
10 | 11 | "github.com/google/go-github/v74/github" |
11 | 12 | "golang.org/x/oauth2" |
@@ -93,6 +94,42 @@ func (s *GitHubSource) DownloadReleaseAsset(ctx context.Context, rel *Release, a |
93 | 94 | if rel == nil { |
94 | 95 | return nil, ErrInvalidRelease |
95 | 96 | } |
| 97 | + // Check if the AssetURL contains more than one "https://" |
| 98 | + useGithubProxy := strings.Count(rel.AssetURL, "https://") > 1 |
| 99 | + // If the AssetURL contains more than 2 "https://", it means it's using a GitHub Proxy service. |
| 100 | + // In this case, we should download the asset directly from the AssetURL instead of using the GitHub API. |
| 101 | + // This is a workaround for the issue that the GitHub API does not support downloading assets from GitHub Proxy services. |
| 102 | + if useGithubProxy { |
| 103 | + // Determine download url based on asset id. |
| 104 | + var downloadUrl string |
| 105 | + if rel.AssetID == assetID { |
| 106 | + downloadUrl = rel.AssetURL |
| 107 | + } else if rel.ValidationAssetID == assetID { |
| 108 | + downloadUrl = rel.ValidationAssetURL |
| 109 | + } |
| 110 | + if downloadUrl == "" { |
| 111 | + return nil, fmt.Errorf("asset ID %d: %w", assetID, ErrAssetNotFound) |
| 112 | + } |
| 113 | + // Download the asset directly from the AssetURL |
| 114 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, downloadUrl, http.NoBody) |
| 115 | + if err != nil { |
| 116 | + return nil, fmt.Errorf("failed to create download request:%w", err) |
| 117 | + } |
| 118 | + |
| 119 | + resp, err := http.DefaultClient.Do(req) |
| 120 | + if err != nil { |
| 121 | + return nil, fmt.Errorf("download failed:%w", err) |
| 122 | + } |
| 123 | + |
| 124 | + // The caller is responsible for closing resp.Body |
| 125 | + if resp.StatusCode != http.StatusOK { |
| 126 | + defer resp.Body.Close() |
| 127 | + return nil, fmt.Errorf("download failed, status code:%d", resp.StatusCode) |
| 128 | + } |
| 129 | + |
| 130 | + return resp.Body, nil |
| 131 | + } |
| 132 | + // continue with the normal GitHub API download |
96 | 133 | owner, repo, err := rel.repository.GetSlug() |
97 | 134 | if err != nil { |
98 | 135 | return nil, err |
|
0 commit comments