Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions github/repos_contents.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
)

// RepositoryContent represents a file or directory in a github repository.
// It's content may be encoded and, if so, call Decode to decode it.
type RepositoryContent struct {
Type *string `json:"type,omitempty"`
Encoding *string `json:"encoding,omitempty"`
Expand Down Expand Up @@ -56,6 +57,7 @@ type RepositoryContentGetOptions struct {
Ref string `url:"ref,omitempty"`
}

// String converts RepositoryContent to a string. It's primarily for testing.
func (r RepositoryContent) String() string {
return Stringify(r)
}
Expand All @@ -72,7 +74,7 @@ func (r *RepositoryContent) Decode() ([]byte, error) {
return o, nil
}

// GetReadme gets the Readme file for the repository.
// GetReadme gets the encoded readme file for the repository.
//
// GitHub API docs: http://developer.github.com/v3/repos/contents/#get-the-readme
func (s *RepositoriesService) GetReadme(owner, repo string, opt *RepositoryContentGetOptions) (*RepositoryContent, *Response, error) {
Expand Down Expand Up @@ -119,7 +121,7 @@ func (s *RepositoriesService) DownloadContents(owner, repo, filepath string, opt
return nil, fmt.Errorf("No file named %s found in %s", filename, dir)
}

// GetContents can return either the metadata and content of a single file
// GetContents can return either the metadata and encoded 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
Expand Down
21 changes: 21 additions & 0 deletions github/repos_contents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,27 @@ func TestRepositoriesService_GetReadme(t *testing.T) {
}
}

func ExampleRepositoriesService_GetReadme() {
setup()
defer teardown()

fmt.Println("Retrieving the go-github README.md file.")
encodedText, _, err := client.Repositories.GetReadme("google", "go-github", &RepositoryContentGetOptions{})

if err != nil {
fmt.Printf("Error: %v\n\n", err)
}
if encodedText == nil {
fmt.Println("The returned text is nil. Are you sure it exists?")
}
text, err := encodedText.Decode()
if err != nil {
fmt.Printf("Decoding failed: %v", err)
}
readme := string(text)
fmt.Printf("Converted readme:\n%v\n", readme)
}

func TestRepositoriesService_DownloadContents_Success(t *testing.T) {
setup()
defer teardown()
Expand Down