123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- package github
- import (
- "context"
- "encoding/base64"
- "encoding/json"
- "fmt"
- "io"
- "net/http"
- "net/url"
- "path"
- )
- 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"`
-
-
-
- Content *string `json:"content,omitempty"`
- SHA *string `json:"sha,omitempty"`
- URL *string `json:"url,omitempty"`
- GitURL *string `json:"git_url,omitempty"`
- HTMLURL *string `json:"html_url,omitempty"`
- DownloadURL *string `json:"download_url,omitempty"`
- }
- type RepositoryContentResponse struct {
- Content *RepositoryContent `json:"content,omitempty"`
- Commit `json:"commit,omitempty"`
- }
- type RepositoryContentFileOptions struct {
- Message *string `json:"message,omitempty"`
- Content []byte `json:"content,omitempty"`
- SHA *string `json:"sha,omitempty"`
- Branch *string `json:"branch,omitempty"`
- Author *CommitAuthor `json:"author,omitempty"`
- Committer *CommitAuthor `json:"committer,omitempty"`
- }
- type RepositoryContentGetOptions struct {
- Ref string `url:"ref,omitempty"`
- }
- func (r RepositoryContent) String() string {
- return Stringify(r)
- }
- func (r *RepositoryContent) GetContent() (string, error) {
- var encoding string
- if r.Encoding != nil {
- encoding = *r.Encoding
- }
- switch encoding {
- case "base64":
- c, err := base64.StdEncoding.DecodeString(*r.Content)
- return string(c), err
- case "":
- if r.Content == nil {
- return "", nil
- }
- return *r.Content, nil
- default:
- return "", fmt.Errorf("unsupported content encoding: %v", encoding)
- }
- }
- func (s *RepositoriesService) GetReadme(ctx context.Context, owner, repo string, opt *RepositoryContentGetOptions) (*RepositoryContent, *Response, error) {
- u := fmt.Sprintf("repos/%v/%v/readme", owner, repo)
- u, err := addOptions(u, opt)
- if err != nil {
- return nil, nil, err
- }
- req, err := s.client.NewRequest("GET", u, nil)
- if err != nil {
- return nil, nil, err
- }
- readme := new(RepositoryContent)
- resp, err := s.client.Do(ctx, req, readme)
- if err != nil {
- return nil, resp, err
- }
- return readme, resp, nil
- }
- func (s *RepositoriesService) DownloadContents(ctx context.Context, owner, repo, filepath string, opt *RepositoryContentGetOptions) (io.ReadCloser, error) {
- dir := path.Dir(filepath)
- filename := path.Base(filepath)
- _, dirContents, _, err := s.GetContents(ctx, owner, repo, dir, opt)
- if err != nil {
- return nil, err
- }
- for _, contents := range dirContents {
- if *contents.Name == filename {
- if contents.DownloadURL == nil || *contents.DownloadURL == "" {
- return nil, fmt.Errorf("No download link found for %s", filepath)
- }
- resp, err := s.client.client.Get(*contents.DownloadURL)
- if err != nil {
- return nil, err
- }
- return resp.Body, nil
- }
- }
- return nil, fmt.Errorf("No file named %s found in %s", filename, dir)
- }
- func (s *RepositoriesService) GetContents(ctx context.Context, owner, repo, path string, opt *RepositoryContentGetOptions) (fileContent *RepositoryContent, directoryContent []*RepositoryContent, resp *Response, err error) {
- escapedPath := (&url.URL{Path: path}).String()
- u := fmt.Sprintf("repos/%s/%s/contents/%s", owner, repo, escapedPath)
- u, err = addOptions(u, opt)
- if err != nil {
- return nil, nil, nil, err
- }
- req, err := s.client.NewRequest("GET", u, nil)
- if err != nil {
- return nil, nil, nil, err
- }
- var rawJSON json.RawMessage
- resp, err = s.client.Do(ctx, req, &rawJSON)
- if err != nil {
- return nil, nil, resp, err
- }
- fileUnmarshalError := json.Unmarshal(rawJSON, &fileContent)
- if fileUnmarshalError == nil {
- return fileContent, nil, resp, nil
- }
- directoryUnmarshalError := json.Unmarshal(rawJSON, &directoryContent)
- if directoryUnmarshalError == nil {
- return nil, directoryContent, resp, nil
- }
- return nil, nil, resp, fmt.Errorf("unmarshalling failed for both file and directory content: %s and %s", fileUnmarshalError, directoryUnmarshalError)
- }
- func (s *RepositoriesService) CreateFile(ctx context.Context, owner, repo, path string, opt *RepositoryContentFileOptions) (*RepositoryContentResponse, *Response, error) {
- u := fmt.Sprintf("repos/%s/%s/contents/%s", owner, repo, path)
- req, err := s.client.NewRequest("PUT", u, opt)
- if err != nil {
- return nil, nil, err
- }
- createResponse := new(RepositoryContentResponse)
- resp, err := s.client.Do(ctx, req, createResponse)
- if err != nil {
- return nil, resp, err
- }
- return createResponse, resp, nil
- }
- func (s *RepositoriesService) UpdateFile(ctx context.Context, owner, repo, path string, opt *RepositoryContentFileOptions) (*RepositoryContentResponse, *Response, error) {
- u := fmt.Sprintf("repos/%s/%s/contents/%s", owner, repo, path)
- req, err := s.client.NewRequest("PUT", u, opt)
- if err != nil {
- return nil, nil, err
- }
- updateResponse := new(RepositoryContentResponse)
- resp, err := s.client.Do(ctx, req, updateResponse)
- if err != nil {
- return nil, resp, err
- }
- return updateResponse, resp, nil
- }
- func (s *RepositoriesService) DeleteFile(ctx context.Context, owner, repo, path string, opt *RepositoryContentFileOptions) (*RepositoryContentResponse, *Response, error) {
- u := fmt.Sprintf("repos/%s/%s/contents/%s", owner, repo, path)
- req, err := s.client.NewRequest("DELETE", u, opt)
- if err != nil {
- return nil, nil, err
- }
- deleteResponse := new(RepositoryContentResponse)
- resp, err := s.client.Do(ctx, req, deleteResponse)
- if err != nil {
- return nil, resp, err
- }
- return deleteResponse, resp, nil
- }
- type archiveFormat string
- const (
-
- Tarball archiveFormat = "tarball"
-
- Zipball archiveFormat = "zipball"
- )
- func (s *RepositoriesService) GetArchiveLink(ctx context.Context, owner, repo string, archiveformat archiveFormat, opt *RepositoryContentGetOptions) (*url.URL, *Response, error) {
- u := fmt.Sprintf("repos/%s/%s/%s", owner, repo, archiveformat)
- if opt != nil && opt.Ref != "" {
- u += fmt.Sprintf("/%s", opt.Ref)
- }
- req, err := s.client.NewRequest("GET", u, nil)
- if err != nil {
- return nil, nil, err
- }
- var resp *http.Response
-
- req = withContext(ctx, req)
- if s.client.client.Transport == nil {
- resp, err = http.DefaultTransport.RoundTrip(req)
- } else {
- resp, err = s.client.client.Transport.RoundTrip(req)
- }
- if err != nil {
- return nil, nil, err
- }
- resp.Body.Close()
- if resp.StatusCode != http.StatusFound {
- return nil, newResponse(resp), fmt.Errorf("unexpected status code: %s", resp.Status)
- }
- parsedURL, err := url.Parse(resp.Header.Get("Location"))
- return parsedURL, newResponse(resp), err
- }
|