From a2109c414c8f9c1be68ad859e8e35efcd74f44d2 Mon Sep 17 00:00:00 2001 From: wingkwong Date: Tue, 25 Feb 2020 01:26:24 +0800 Subject: [PATCH] feat: add gist queries #397 --- api/queries_gist.go | 81 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 api/queries_gist.go diff --git a/api/queries_gist.go b/api/queries_gist.go new file mode 100644 index 000000000..f75c1207f --- /dev/null +++ b/api/queries_gist.go @@ -0,0 +1,81 @@ +package api + +import ( + "bytes" + "encoding/json" + "time" + + "io/ioutil" + "os" +) + +// Gist represents a GitHub's gist. +type Gist struct { + ID string `json:"id,omitempty"` + Description string `json:"description,omitempty"` + Public bool `json:"public,omitempty"` + Files map[GistFilename]GistFile `json:"files,omitempty"` + Comments int `json:"comments,omitempty"` + HTMLURL string `json:"html_url,omitempty"` + GitPullURL string `json:"git_pull_url,omitempty"` + GitPushURL string `json:"git_push_url,omitempty"` + CreatedAt time.Time `json:"created_at,omitempty"` + UpdatedAt time.Time `json:"updated_at,omitempty"` + NodeID string `json:"node_id,omitempty"` +} + +// GistFilename represents filename on a gist. +type GistFilename string + +// GistFile represents a file on a gist. +type GistFile struct { + Size int `json:"size,omitempty"` + Filename string `json:"filename,omitempty"` + Language string `json:"language,omitempty"` + Type string `json:"type,omitempty"` + RawURL string `json:"raw_url,omitempty"` + Content string `json:"content,omitempty"` +} + +// Create a gist for authenticated user. +// +// GitHub API docs: https://developer.github.com/v3/gists/#create-a-gist +func GistCreate(client *Client, description string, public bool, file string) (*Gist, error) { + + b, err := ioutil.ReadFile(file) + if err != nil { + return nil, err + } + + content := string(b) + + fileStat, err := os.Stat(file) + if err != nil { + return nil, err + } + fileName := GistFilename(fileStat.Name()) + + gistFile := map[GistFilename]GistFile{} + gistFile[fileName] = GistFile{Content: content} + + path := "gists" + body := &Gist{ + Description: description, + Public: public, + Files: gistFile, + } + result := Gist{} + + requestByte, err := json.Marshal(body) + if err != nil { + return nil, err + } + requestBody := bytes.NewReader(requestByte) + + err = client.REST("POST", path, requestBody, &result) + if err != nil { + return nil, err + } + + return &result, nil +}