misc.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. }
  136. // APIMeta returns information about GitHub.com, the service. Or, if you access
  137. // this endpoint on your organization’s GitHub Enterprise installation, this
  138. // endpoint provides information about that installation.
  139. //
  140. // GitHub API docs: https://developer.github.com/v3/meta/
  141. func (c *Client) APIMeta(ctx context.Context) (*APIMeta, *Response, error) {
  142. req, err := c.NewRequest("GET", "meta", nil)
  143. if err != nil {
  144. return nil, nil, err
  145. }
  146. meta := new(APIMeta)
  147. resp, err := c.Do(ctx, req, meta)
  148. if err != nil {
  149. return nil, resp, err
  150. }
  151. return meta, resp, nil
  152. }
  153. // Octocat returns an ASCII art octocat with the specified message in a speech
  154. // bubble. If message is empty, a random zen phrase is used.
  155. func (c *Client) Octocat(ctx context.Context, message string) (string, *Response, error) {
  156. u := "octocat"
  157. if message != "" {
  158. u = fmt.Sprintf("%s?s=%s", u, url.QueryEscape(message))
  159. }
  160. req, err := c.NewRequest("GET", u, nil)
  161. if err != nil {
  162. return "", nil, err
  163. }
  164. buf := new(bytes.Buffer)
  165. resp, err := c.Do(ctx, req, buf)
  166. if err != nil {
  167. return "", resp, err
  168. }
  169. return buf.String(), resp, nil
  170. }
  171. // Zen returns a random line from The Zen of GitHub.
  172. //
  173. // see also: http://warpspire.com/posts/taste/
  174. func (c *Client) Zen(ctx context.Context) (string, *Response, error) {
  175. req, err := c.NewRequest("GET", "zen", nil)
  176. if err != nil {
  177. return "", nil, err
  178. }
  179. buf := new(bytes.Buffer)
  180. resp, err := c.Do(ctx, req, buf)
  181. if err != nil {
  182. return "", resp, err
  183. }
  184. return buf.String(), resp, nil
  185. }
  186. // ServiceHook represents a hook that has configuration settings, a list of
  187. // available events, and default events.
  188. type ServiceHook struct {
  189. Name *string `json:"name,omitempty"`
  190. Events []string `json:"events,omitempty"`
  191. SupportedEvents []string `json:"supported_events,omitempty"`
  192. Schema [][]string `json:"schema,omitempty"`
  193. }
  194. func (s *ServiceHook) String() string {
  195. return Stringify(s)
  196. }
  197. // ListServiceHooks lists all of the available service hooks.
  198. //
  199. // GitHub API docs: https://developer.github.com/webhooks/#services
  200. func (c *Client) ListServiceHooks(ctx context.Context) ([]*ServiceHook, *Response, error) {
  201. u := "hooks"
  202. req, err := c.NewRequest("GET", u, nil)
  203. if err != nil {
  204. return nil, nil, err
  205. }
  206. var hooks []*ServiceHook
  207. resp, err := c.Do(ctx, req, &hooks)
  208. if err != nil {
  209. return nil, resp, err
  210. }
  211. return hooks, resp, nil
  212. }