github.com/crosbymichael/octokat@v0.0.0-20160826194511-076a32289ed5/hooks.go (about) 1 package octokat 2 3 import ( 4 "encoding/json" 5 "errors" 6 "strings" 7 ) 8 9 type IssueHook struct { 10 Action string `json:"action"` 11 Sender *User `json:"sender"` 12 Repo *Repository `json:"repository"` 13 Issue *Issue `json:"issue"` 14 Comment *Comment `json:"comment, omitempty"` 15 } 16 17 type PullRequestHook struct { 18 Action string `json:"action"` 19 Number int `json:"number"` 20 Sender *User `json:"sender"` 21 Repo *Repository `json:"repository"` 22 PullRequest *PullRequest `json:"pull_request"` 23 } 24 25 func ParseIssueHook(raw []byte) (*IssueHook, error) { 26 hook := IssueHook{} 27 if err := json.Unmarshal(raw, &hook); err != nil { 28 return nil, err 29 } 30 31 if hook.Issue == nil { 32 return nil, ErrInvalidPostReceiveHook 33 } 34 35 return &hook, nil 36 } 37 38 func (h *IssueHook) IsOpened() bool { 39 return h.Action == "opened" 40 } 41 42 // we will know if it is a comment if the Action is "created" 43 // since with NSQ we can't parse the headers 44 func (h *IssueHook) IsComment() bool { 45 return h.Action == "created" 46 } 47 48 func ParsePullRequestHook(raw []byte) (*PullRequestHook, error) { 49 hook := PullRequestHook{} 50 if err := json.Unmarshal(raw, &hook); err != nil { 51 return nil, err 52 } 53 54 // it is possible the JSON was parsed, however, 55 // was not from Github (maybe was from Bitbucket) 56 // So we'll check to be sure certain key fields 57 // were populated 58 if hook.PullRequest == nil { 59 return nil, ErrInvalidPostReceiveHook 60 } 61 62 return &hook, nil 63 } 64 65 func (h *PullRequestHook) IsOpened() bool { 66 return h.Action == "opened" 67 } 68 69 func (h *PullRequestHook) IsSynchronize() bool { 70 return h.Action == "synchronize" 71 } 72 73 var ErrInvalidPostReceiveHook = errors.New("Invalid Post Receive Hook") 74 75 type PostReceiveHook struct { 76 Before string `json:"before"` 77 After string `json:"after"` 78 Ref string `json:"ref"` 79 Repo *Repository `json:"repository"` 80 Commits []*Commit `json:"commits"` 81 Head *Commit `json:"head_commit"` 82 Deleted bool `json:"deleted"` 83 } 84 85 func ParseHook(raw []byte) (*PostReceiveHook, error) { 86 hook := PostReceiveHook{} 87 if err := json.Unmarshal(raw, &hook); err != nil { 88 return nil, err 89 } 90 91 // it is possible the JSON was parsed, however, 92 // was not from Github (maybe was from Bitbucket) 93 // So we'll check to be sure certain key fields 94 // were populated 95 switch { 96 case hook.Repo == nil: 97 return nil, ErrInvalidPostReceiveHook 98 case len(hook.Ref) == 0: 99 return nil, ErrInvalidPostReceiveHook 100 } 101 102 return &hook, nil 103 } 104 105 func (h *PostReceiveHook) IsGithubPages() bool { 106 return strings.HasSuffix(h.Ref, "/gh-pages") 107 } 108 109 func (h *PostReceiveHook) IsTag() bool { 110 return strings.HasPrefix(h.Ref, "refs/tags/") 111 } 112 113 func (h *PostReceiveHook) IsHead() bool { 114 return strings.HasPrefix(h.Ref, "refs/heads/") 115 } 116 117 func (h *PostReceiveHook) Branch() string { 118 return strings.Replace(h.Ref, "refs/heads/", "", -1) 119 } 120 121 func (h *PostReceiveHook) IsDeleted() bool { 122 return h.Deleted || h.After == "0000000000000000000000000000000000000000" 123 }