MCPcopy Create free account
hub / github.com/cli/cli / RepositoryReadme

Function RepositoryReadme

pkg/cmd/repo/view/http.go:26–63  ·  view source on GitHub ↗
(client *http.Client, repo ghrepo.Interface, branch string)

Source from the content-addressed store, hash-verified

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

Callers 2

GetMethod · 0.92
viewRunFunction · 0.85

Calls 6

NewClientFromHTTPFunction · 0.92
getReadmePathFunction · 0.85
RESTMethod · 0.65
RepoHostMethod · 0.65
StringMethod · 0.65
ErrorfMethod · 0.65

Tested by

no test coverage detected