(owner, repo, path, ref string, symlinkDepth int, host string)
| 899 | } |
| 900 | |
| 901 | func downloadFileFromGitHubWithDepth(owner, repo, path, ref string, symlinkDepth int, host string) ([]byte, error) { |
| 902 | client, err := createRESTClientForHost(host) |
| 903 | if err != nil { |
| 904 | if gitutil.IsAuthError(err.Error()) { |
| 905 | remoteLog.Printf("REST client creation failed due to auth error, attempting git fallback for %s/%s/%s@%s: %v", owner, repo, path, ref, err) |
| 906 | content, gitErr := downloadFileViaGit(context.Background(), owner, repo, path, ref, host) |
| 907 | if gitErr != nil { |
| 908 | remoteLog.Printf("Git fallback also failed for %s/%s/%s@%s: %v", owner, repo, path, ref, gitErr) |
| 909 | return nil, fmt.Errorf("failed to fetch file content: %w", err) |
| 910 | } |
| 911 | return content, nil |
| 912 | } |
| 913 | return nil, fmt.Errorf("failed to create REST client: %w", err) |
| 914 | } |
| 915 | |
| 916 | var fileContent struct { |
| 917 | Content string `json:"content"` |
| 918 | Encoding string `json:"encoding"` |
| 919 | Name string `json:"name"` |
| 920 | } |
| 921 | |
| 922 | err = fetchRemoteFileContent(client, owner, repo, path, ref, &fileContent) |
| 923 | if err != nil { |
| 924 | if gitutil.IsAuthError(err.Error()) { |
| 925 | remoteLog.Printf("GitHub API authentication failed, attempting git fallback for %s/%s/%s@%s", owner, repo, path, ref) |
| 926 | content, gitErr := downloadFileViaGit(context.Background(), owner, repo, path, ref, host) |
| 927 | if gitErr != nil { |
| 928 | if host == "" || host == "github.com" { |
| 929 | remoteLog.Printf("Git fallback also failed, attempting unauthenticated API for %s/%s/%s@%s", owner, repo, path, ref) |
| 930 | return downloadFileViaPublicAPI(context.Background(), owner, repo, path, ref) |
| 931 | } |
| 932 | return nil, fmt.Errorf("failed to fetch file content via GitHub API (auth error) and git fallback: API error: %w, Git error: %w", err, gitErr) |
| 933 | } |
| 934 | return content, nil |
| 935 | } |
| 936 | |
| 937 | if errorutil.IsNotFoundError(err) && symlinkDepth < constants.MaxSymlinkDepth { |
| 938 | if content, handled, resolveErr := retryDownloadViaResolvedSymlink(client, owner, repo, path, ref, symlinkDepth, host); handled { |
| 939 | return content, resolveErr |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | return nil, fmt.Errorf("failed to fetch file content from %s/%s/%s@%s: %w", owner, repo, path, ref, err) |
| 944 | } |
| 945 | |
| 946 | if fileContent.Content == "" { |
| 947 | return nil, fmt.Errorf("empty content returned from GitHub API for %s/%s/%s@%s", owner, repo, path, ref) |
| 948 | } |
| 949 | |
| 950 | content, err := base64.StdEncoding.DecodeString(fileContent.Content) |
| 951 | if err != nil { |
| 952 | return nil, fmt.Errorf("failed to decode base64 content: %w", err) |
| 953 | } |
| 954 | |
| 955 | return content, nil |
| 956 | } |
| 957 | |
| 958 | func createRESTClientForHost(host string) (*api.RESTClient, error) { |
no test coverage detected