GetContents can return either the metadata and content of a single file (when path references a file) or the metadata of all the files and/or subdirectories of a directory (when path references a directory). To make it easy to distinguish between both result types and to mimic the API as much as pos
(ctx context.Context, owner, repo, path string, opts *RepositoryContentGetOptions)
| 216 | // |
| 217 | //meta:operation GET /repos/{owner}/{repo}/contents/{path} |
| 218 | func (s *RepositoriesService) GetContents(ctx context.Context, owner, repo, path string, opts *RepositoryContentGetOptions) (fileContent *RepositoryContent, directoryContent []*RepositoryContent, resp *Response, err error) { |
| 219 | escapedPath := (&url.URL{Path: strings.TrimSuffix(path, "/")}).String() |
| 220 | u := fmt.Sprintf("repos/%v/%v/contents/%v", owner, repo, escapedPath) |
| 221 | u, err = addOptions(u, opts) |
| 222 | if err != nil { |
| 223 | return nil, nil, nil, err |
| 224 | } |
| 225 | |
| 226 | req, err := s.client.NewRequest(ctx, "GET", u, nil) |
| 227 | if err != nil { |
| 228 | return nil, nil, nil, err |
| 229 | } |
| 230 | |
| 231 | var rawJSON json.RawMessage |
| 232 | resp, err = s.client.Do(req, &rawJSON) |
| 233 | if err != nil { |
| 234 | return nil, nil, resp, err |
| 235 | } |
| 236 | |
| 237 | fileUnmarshalError := json.Unmarshal(rawJSON, &fileContent) |
| 238 | if fileUnmarshalError == nil { |
| 239 | return fileContent, nil, resp, nil |
| 240 | } |
| 241 | |
| 242 | directoryUnmarshalError := json.Unmarshal(rawJSON, &directoryContent) |
| 243 | if directoryUnmarshalError == nil { |
| 244 | return nil, directoryContent, resp, nil |
| 245 | } |
| 246 | |
| 247 | return nil, nil, resp, fmt.Errorf("unmarshaling failed for both file and directory content: %v and %v", fileUnmarshalError, directoryUnmarshalError) |
| 248 | } |
| 249 | |
| 250 | // CreateFile creates a new file in a repository at the given path and returns |
| 251 | // the commit and file metadata. |