Browse Source

Clarify use of RepositoryContent encoded content

It's not obvious from reading the GitHub API docs or the go-github docs
that RepositoryContent.Content may be base64 encoded.  This adds
clarifying docs for that as well as the String method.

Also add an example of using the GetReadme method.

Fixes #322
Fixes #323
Matt Brender 10 years ago
committed by Will Norris
parent
commit
61c6c14bbe
2 changed files with 27 additions and 5 deletions
  1. +9
    -5
      github/repos_contents.go
  2. +18
    -0
      github/repos_contents_test.go

+ 9
- 5
github/repos_contents.go View File

@ -21,11 +21,14 @@ import (
// RepositoryContent represents a file or directory in a github repository.
type RepositoryContent struct {
Type *string `json:"type,omitempty"`
Encoding *string `json:"encoding,omitempty"`
Size *int `json:"size,omitempty"`
Name *string `json:"name,omitempty"`
Path *string `json:"path,omitempty"`
Type *string `json:"type,omitempty"`
Encoding *string `json:"encoding,omitempty"`
Size *int `json:"size,omitempty"`
Name *string `json:"name,omitempty"`
Path *string `json:"path,omitempty"`
// Content contains the actual file content, which may be encoded.
// Callers should call GetContent which will decode the content if
// necessary.
Content *string `json:"content,omitempty"`
SHA *string `json:"sha,omitempty"`
URL *string `json:"url,omitempty"`
@ -56,6 +59,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)
}


+ 18
- 0
github/repos_contents_test.go View File

@ -119,6 +119,24 @@ func TestRepositoriesService_GetReadme(t *testing.T) {
}
}
func ExampleRepositoriesService_GetReadme() {
client := NewClient(nil)
readme, _, err := client.Repositories.GetReadme("google", "go-github", nil)
if err != nil {
fmt.Println(err)
return
}
content, err := readme.GetContent()
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("google/go-github README:\n%v\n", content)
}
func TestRepositoriesService_DownloadContents_Success(t *testing.T) {
setup()
defer teardown()


Loading…
Cancel
Save