misc.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // Copyright 2014 The go-github AUTHORS. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. package github
  6. import (
  7. "bytes"
  8. "context"
  9. "fmt"
  10. "net/url"
  11. )
  12. // MarkdownOptions specifies optional parameters to the Markdown method.
  13. type MarkdownOptions struct {
  14. // Mode identifies the rendering mode. Possible values are:
  15. // markdown - render a document as plain Markdown, just like
  16. // README files are rendered.
  17. //
  18. // gfm - to render a document as user-content, e.g. like user
  19. // comments or issues are rendered. In GFM mode, hard line breaks are
  20. // always taken into account, and issue and user mentions are linked
  21. // accordingly.
  22. //
  23. // Default is "markdown".
  24. Mode string
  25. // Context identifies the repository context. Only taken into account
  26. // when rendering as "gfm".
  27. Context string
  28. }
  29. type markdownRequest struct {
  30. Text *string `json:"text,omitempty"`
  31. Mode *string `json:"mode,omitempty"`
  32. Context *string `json:"context,omitempty"`
  33. }
  34. // Markdown renders an arbitrary Markdown document.
  35. //
  36. // GitHub API docs: https://developer.github.com/v3/markdown/
  37. func (c *Client) Markdown(ctx context.Context, text string, opt *MarkdownOptions) (string, *Response, error) {
  38. request := &markdownRequest{Text: String(text)}
  39. if opt != nil {
  40. if opt.Mode != "" {
  41. request.Mode = String(opt.Mode)
  42. }
  43. if opt.Context != "" {
  44. request.Context = String(opt.Context)
  45. }
  46. }
  47. req, err := c.NewRequest("POST", "markdown", request)
  48. if err != nil {
  49. return "", nil, err
  50. }
  51. buf := new(bytes.Buffer)
  52. resp, err := c.Do(ctx, req, buf)
  53. if err != nil {
  54. return "", resp, err
  55. }
  56. return buf.String(), resp, nil
  57. }
  58. // ListEmojis returns the emojis available to use on GitHub.
  59. //
  60. // GitHub API docs: https://developer.github.com/v3/emojis/
  61. func (c *Client) ListEmojis(ctx context.Context) (map[string]string, *Response, error) {
  62. req, err := c.NewRequest("GET", "emojis", nil)
  63. if err != nil {
  64. return nil, nil, err
  65. }
  66. var emoji map[string]string
  67. resp, err := c.Do(ctx, req, &emoji)
  68. if err != nil {
  69. return nil, resp, err
  70. }
  71. return emoji, resp, nil
  72. }
  73. // CodeOfConduct represents a code of conduct.
  74. type CodeOfConduct struct {
  75. Name *string `json:"name,omitempty"`
  76. Key *string `json:"key,omitempty"`
  77. URL *string `json:"url,omitempty"`
  78. Body *string `json:"body,omitempty"`
  79. }
  80. func (c *CodeOfConduct) String() string {
  81. return Stringify(c)
  82. }
  83. // ListCodesOfConduct returns all codes of conduct.
  84. //
  85. // GitHub API docs: https://developer.github.com/v3/codes_of_conduct/#list-all-codes-of-conduct
  86. func (c *Client) ListCodesOfConduct(ctx context.Context) ([]*CodeOfConduct, *Response, error) {
  87. req, err := c.NewRequest("GET", "codes_of_conduct", nil)
  88. if err != nil {
  89. return nil, nil, err
  90. }
  91. // TODO: remove custom Accept header when this API fully launches.
  92. req.Header.Set("Accept", mediaTypeCodesOfConductPreview)
  93. var cs []*CodeOfConduct
  94. resp, err := c.Do(ctx, req, &cs)
  95. if err != nil {
  96. return nil, resp, err
  97. }
  98. return cs, resp, nil
  99. }
  100. // GetCodeOfConduct returns an individual code of conduct.
  101. //
  102. // https://developer.github.com/v3/codes_of_conduct/#get-an-individual-code-of-conduct
  103. func (c *Client) GetCodeOfConduct(ctx context.Context, key string) (*CodeOfConduct, *Response, error) {
  104. u := fmt.Sprintf("codes_of_conduct/%s", key)
  105. req, err := c.NewRequest("GET", u, nil)
  106. if err != nil {
  107. return nil, nil, err
  108. }
  109. // TODO: remove custom Accept header when this API fully launches.
  110. req.Header.Set("Accept", mediaTypeCodesOfConductPreview)
  111. coc := new(CodeOfConduct)
  112. resp, err := c.Do(ctx, req, coc)
  113. if err != nil {
  114. return nil, resp, err
  115. }
  116. return coc, resp, nil
  117. }
  118. // APIMeta represents metadata about the GitHub API.
  119. type APIMeta struct {
  120. // An Array of IP addresses in CIDR format specifying the addresses
  121. // that incoming service hooks will originate from on GitHub.com.
  122. Hooks []string `json:"hooks,omitempty"`
  123. // An Array of IP addresses in CIDR format specifying the Git servers
  124. // for GitHub.com.
  125. Git []string `json:"git,omitempty"`
  126. // Whether authentication with username and password is supported.
  127. // (GitHub Enterprise instances using CAS or OAuth for authentication
  128. // will return false. Features like Basic Authentication with a
  129. // username and password, sudo mode, and two-factor authentication are
  130. // not supported on these servers.)
  131. VerifiablePasswordAuthentication *bool `json:"verifiable_password_authentication,omitempty"`
  132. // An array of IP addresses in CIDR format specifying the addresses
  133. // which serve GitHub Pages websites.
  134. Pages []string `json:"pages,omitempty"`
  135. // An Array of IP addresses specifying the addresses that source imports
  136. // will originate from on GitHub.com.
  137. Importer []string `json:"importer,omitempty"`
  138. }
  139. // APIMeta returns information about GitHub.com, the service. Or, if you access
  140. // this endpoint on your organization’s GitHub Enterprise installation, this
  141. // endpoint provides information about that installation.
  142. //
  143. // GitHub API docs: https://developer.github.com/v3/meta/
  144. func (c *Client) APIMeta(ctx context.Context) (*APIMeta, *Response, error) {
  145. req, err := c.NewRequest("GET", "meta", nil)
  146. if err != nil {
  147. return nil, nil, err
  148. }
  149. meta := new(APIMeta)
  150. resp, err := c.Do(ctx, req, meta)
  151. if err != nil {
  152. return nil, resp, err
  153. }
  154. return meta, resp, nil
  155. }
  156. // Octocat returns an ASCII art octocat with the specified message in a speech
  157. // bubble. If message is empty, a random zen phrase is used.
  158. func (c *Client) Octocat(ctx context.Context, message string) (string, *Response, error) {
  159. u := "octocat"
  160. if message != "" {
  161. u = fmt.Sprintf("%s?s=%s", u, url.QueryEscape(message))
  162. }
  163. req, err := c.NewRequest("GET", u, nil)
  164. if err != nil {
  165. return "", nil, err
  166. }
  167. buf := new(bytes.Buffer)
  168. resp, err := c.Do(ctx, req, buf)
  169. if err != nil {
  170. return "", resp, err
  171. }
  172. return buf.String(), resp, nil
  173. }
  174. // Zen returns a random line from The Zen of GitHub.
  175. //
  176. // see also: http://warpspire.com/posts/taste/
  177. func (c *Client) Zen(ctx context.Context) (string, *Response, error) {
  178. req, err := c.NewRequest("GET", "zen", nil)
  179. if err != nil {
  180. return "", nil, err
  181. }
  182. buf := new(bytes.Buffer)
  183. resp, err := c.Do(ctx, req, buf)
  184. if err != nil {
  185. return "", resp, err
  186. }
  187. return buf.String(), resp, nil
  188. }
  189. // ServiceHook represents a hook that has configuration settings, a list of
  190. // available events, and default events.
  191. type ServiceHook struct {
  192. Name *string `json:"name,omitempty"`
  193. Events []string `json:"events,omitempty"`
  194. SupportedEvents []string `json:"supported_events,omitempty"`
  195. Schema [][]string `json:"schema,omitempty"`
  196. }
  197. func (s *ServiceHook) String() string {
  198. return Stringify(s)
  199. }
  200. // ListServiceHooks lists all of the available service hooks.
  201. //
  202. // GitHub API docs: https://developer.github.com/webhooks/#services
  203. func (c *Client) ListServiceHooks(ctx context.Context) ([]*ServiceHook, *Response, error) {
  204. u := "hooks"
  205. req, err := c.NewRequest("GET", u, nil)
  206. if err != nil {
  207. return nil, nil, err
  208. }
  209. var hooks []*ServiceHook
  210. resp, err := c.Do(ctx, req, &hooks)
  211. if err != nil {
  212. return nil, resp, err
  213. }
  214. return hooks, resp, nil
  215. }