PageRenderTime 26ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/github.com/gogits/go-gogs-client/repo_hooks.go

https://gitlab.com/rpi-romlinch/drone
Go | 203 lines | 153 code | 27 blank | 23 comment | 18 complexity | 210fd120611b65daf37613f271f3df7c MD5 | raw file
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package gogs
  5. import (
  6. "bytes"
  7. "encoding/json"
  8. "errors"
  9. "fmt"
  10. "net/http"
  11. "strings"
  12. "time"
  13. )
  14. var (
  15. ErrInvalidReceiveHook = errors.New("Invalid JSON payload received over webhook")
  16. )
  17. type Hook struct {
  18. ID int64 `json:"id"`
  19. Type string `json:"type"`
  20. URL string `json:"-"`
  21. Config map[string]string `json:"config"`
  22. Events []string `json:"events"`
  23. Active bool `json:"active"`
  24. Updated time.Time `json:"updated_at"`
  25. Created time.Time `json:"created_at"`
  26. }
  27. func (c *Client) ListRepoHooks(user, repo string) ([]*Hook, error) {
  28. hooks := make([]*Hook, 0, 10)
  29. return hooks, c.getParsedResponse("GET", fmt.Sprintf("/repos/%s/%s/hooks", user, repo), nil, nil, &hooks)
  30. }
  31. type CreateHookOption struct {
  32. Type string `json:"type" binding:"Required"`
  33. Config map[string]string `json:"config" binding:"Required"`
  34. Events []string `json:"events"`
  35. Active bool `json:"active"`
  36. }
  37. func (c *Client) CreateRepoHook(user, repo string, opt CreateHookOption) (*Hook, error) {
  38. body, err := json.Marshal(&opt)
  39. if err != nil {
  40. return nil, err
  41. }
  42. h := new(Hook)
  43. return h, c.getParsedResponse("POST", fmt.Sprintf("/repos/%s/%s/hooks", user, repo),
  44. http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body), h)
  45. }
  46. type EditHookOption struct {
  47. Config map[string]string `json:"config"`
  48. Events []string `json:"events"`
  49. Active *bool `json:"active"`
  50. }
  51. func (c *Client) EditRepoHook(user, repo string, id int64, opt EditHookOption) error {
  52. body, err := json.Marshal(&opt)
  53. if err != nil {
  54. return err
  55. }
  56. _, err = c.getResponse("PATCH", fmt.Sprintf("/repos/%s/%s/hooks/%d", user, repo, id),
  57. http.Header{"content-type": []string{"application/json"}}, bytes.NewReader(body))
  58. return err
  59. }
  60. type Payloader interface {
  61. SetSecret(string)
  62. JSONPayload() ([]byte, error)
  63. }
  64. type PayloadAuthor struct {
  65. Name string `json:"name"`
  66. Email string `json:"email"`
  67. UserName string `json:"username"`
  68. }
  69. type PayloadUser struct {
  70. UserName string `json:"login"`
  71. ID int64 `json:"id"`
  72. AvatarUrl string `json:"avatar_url"`
  73. }
  74. type PayloadCommit struct {
  75. ID string `json:"id"`
  76. Message string `json:"message"`
  77. URL string `json:"url"`
  78. Author *PayloadAuthor `json:"author"`
  79. }
  80. type PayloadRepo struct {
  81. ID int64 `json:"id"`
  82. Name string `json:"name"`
  83. URL string `json:"url"`
  84. Description string `json:"description"`
  85. Website string `json:"website"`
  86. Watchers int `json:"watchers"`
  87. Owner *PayloadAuthor `json:"owner"`
  88. Private bool `json:"private"`
  89. }
  90. // _________ __
  91. // \_ ___ \_______ ____ _____ _/ |_ ____
  92. // / \ \/\_ __ \_/ __ \\__ \\ __\/ __ \
  93. // \ \____| | \/\ ___/ / __ \| | \ ___/
  94. // \______ /|__| \___ >____ /__| \___ >
  95. // \/ \/ \/ \/
  96. type CreatePayload struct {
  97. Secret string `json:"secret"`
  98. Ref string `json:"ref"`
  99. RefType string `json:"ref_type"`
  100. Repo *PayloadRepo `json:"repository"`
  101. Sender *PayloadUser `json:"sender"`
  102. }
  103. func (p *CreatePayload) SetSecret(secret string) {
  104. p.Secret = secret
  105. }
  106. func (p *CreatePayload) JSONPayload() ([]byte, error) {
  107. data, err := json.MarshalIndent(p, "", " ")
  108. if err != nil {
  109. return []byte{}, err
  110. }
  111. return data, nil
  112. }
  113. // ParseCreateHook parses create event hook content.
  114. func ParseCreateHook(raw []byte) (*CreatePayload, error) {
  115. hook := new(CreatePayload)
  116. if err := json.Unmarshal(raw, hook); err != nil {
  117. return nil, err
  118. }
  119. // it is possible the JSON was parsed, however,
  120. // was not from Gogs (maybe was from Bitbucket)
  121. // So we'll check to be sure certain key fields
  122. // were populated
  123. switch {
  124. case hook.Repo == nil:
  125. return nil, ErrInvalidReceiveHook
  126. case len(hook.Ref) == 0:
  127. return nil, ErrInvalidReceiveHook
  128. }
  129. return hook, nil
  130. }
  131. // __________ .__
  132. // \______ \__ __ _____| |__
  133. // | ___/ | \/ ___/ | \
  134. // | | | | /\___ \| Y \
  135. // |____| |____//____ >___| /
  136. // \/ \/
  137. // PushPayload represents a payload information of push event.
  138. type PushPayload struct {
  139. Secret string `json:"secret"`
  140. Ref string `json:"ref"`
  141. Before string `json:"before"`
  142. After string `json:"after"`
  143. CompareUrl string `json:"compare_url"`
  144. Commits []*PayloadCommit `json:"commits"`
  145. Repo *PayloadRepo `json:"repository"`
  146. Pusher *PayloadAuthor `json:"pusher"`
  147. Sender *PayloadUser `json:"sender"`
  148. }
  149. func (p *PushPayload) SetSecret(secret string) {
  150. p.Secret = secret
  151. }
  152. func (p *PushPayload) JSONPayload() ([]byte, error) {
  153. data, err := json.MarshalIndent(p, "", " ")
  154. if err != nil {
  155. return []byte{}, err
  156. }
  157. return data, nil
  158. }
  159. // ParsePushHook parses push event hook content.
  160. func ParsePushHook(raw []byte) (*PushPayload, error) {
  161. hook := new(PushPayload)
  162. if err := json.Unmarshal(raw, hook); err != nil {
  163. return nil, err
  164. }
  165. switch {
  166. case hook.Repo == nil:
  167. return nil, ErrInvalidReceiveHook
  168. case len(hook.Ref) == 0:
  169. return nil, ErrInvalidReceiveHook
  170. }
  171. return hook, nil
  172. }
  173. // Branch returns branch name from a payload
  174. func (p *PushPayload) Branch() string {
  175. return strings.Replace(p.Ref, "refs/heads/", "", -1)
  176. }