MCPcopy
hub / github.com/cli/cli / RepositoryReadme

Function RepositoryReadme

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

Source from the content-addressed store, hash-verified

23}
24
25func 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
59func getReadmePath(repo ghrepo.Interface, branch string) string {
60 path := fmt.Sprintf("repos/%s/readme", ghrepo.FullName(repo))

Callers 2

GetMethod · 0.92
viewRunFunction · 0.85

Calls 5

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

Tested by

no test coverage detected