(client *http.Client, repo ghrepo.Interface, branch string)
| 23 | } |
| 24 | |
| 25 | func RepositoryReadme(client *http.Client, repo ghrepo.Interface, branch string) (*RepoReadme, error) { |
| 26 | apiClient := api.NewClientFromHTTP(client) |
| 27 | var response struct { |
| 28 | Name string |
| 29 | Content string |
| 30 | HTMLURL string `json:"html_url"` |
| 31 | } |
| 32 | |
| 33 | err := apiClient.REST(repo.RepoHost(), "GET", getReadmePath(repo, branch), nil, &response) |
| 34 | if err != nil { |
| 35 | var httpError api.HTTPError |
| 36 | if errors.As(err, &httpError) && httpError.StatusCode == 404 { |
| 37 | return nil, NotFoundError |
| 38 | } |
| 39 | return nil, err |
| 40 | } |
| 41 | |
| 42 | decoded, err := base64.StdEncoding.DecodeString(response.Content) |
| 43 | if err != nil { |
| 44 | return nil, fmt.Errorf("failed to decode readme: %w", err) |
| 45 | } |
| 46 | |
| 47 | sanitized, err := io.ReadAll(transform.NewReader(bytes.NewReader(decoded), &asciisanitizer.Sanitizer{})) |
| 48 | if err != nil { |
| 49 | return nil, err |
| 50 | } |
| 51 | |
| 52 | return &RepoReadme{ |
| 53 | Filename: response.Name, |
| 54 | Content: string(sanitized), |
| 55 | BaseURL: response.HTMLURL, |
| 56 | }, nil |
| 57 | } |
| 58 | |
| 59 | func getReadmePath(repo ghrepo.Interface, branch string) string { |
| 60 | path := fmt.Sprintf("repos/%s/readme", ghrepo.FullName(repo)) |
no test coverage detected