Skip to content

Commit

Permalink
* [feat] standardize naming and enhance pull request events
Browse files Browse the repository at this point in the history
Signed-off-by: ysicing <[email protected]>
  • Loading branch information
ysicing committed Nov 19, 2024
1 parent 03f7306 commit eaea3ae
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Gitfox Library Webhooks
# GitFox Library Webhooks

Library webhooks allows for easy receiving and parsing of Gitfox Webhook Events
Library webhooks allows for easy receiving and parsing of GitFox Webhook Events

## Installation

Expand Down Expand Up @@ -28,7 +28,7 @@ import (
)

func main() {
hook, _ := gitfox.New(gitfox.Options.Secret("MyGitfoxSecret...?"))
hook, _ := gitfox.New(gitfox.Options.Secret("MyGitFoxSecret...?"))

http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
payload, err := hook.Parse(r, gitfox.BranchUpdatedEvent)
Expand Down
17 changes: 11 additions & 6 deletions gitfox/gitfox.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import (
var (
ErrEventNotSpecifiedToParse = errors.New("no Event specified to parse")
ErrInvalidHTTPMethod = errors.New("invalid HTTP Method")
ErrMissingGitfoxEventHeader = errors.New("missing X-Gitfox-Event Header")
ErrMissingGitfoxTriggerHeader = errors.New("missing X-Gitfox-Trigger Header")
ErrMissingGitfoxWebhookParentTypeHeader = errors.New("missing Webhook-Parent-Type Header")
ErrMissingGitfoxSignatureHeader = errors.New("missing X-Gitfox-Signature Header")
ErrMissingGitFoxEventHeader = errors.New("missing X-Gitfox-Event Header")
ErrMissingGitFoxTriggerHeader = errors.New("missing X-Gitfox-Trigger Header")
ErrMissingGitFoxWebhookParentTypeHeader = errors.New("missing Webhook-Parent-Type Header")
ErrMissingGitFoxSignatureHeader = errors.New("missing X-Gitfox-Signature Header")
ErrEventNotFound = errors.New("event not defined to be parsed")
ErrParsingPayload = errors.New("error parsing payload")
ErrHMACVerificationFailed = errors.New("HMAC verification failed")
Expand All @@ -50,6 +50,7 @@ const (
PullReqReviewerCreatedEvent HookEventType = "pullreq_reviewer_created"
PullReqReviewerDeletedEvent HookEventType = "pullreq_reviewer_deleted"
PullReqReviewSubmittedEvent HookEventType = "pullreq_review_submitted"
PullReqUpdatedEvent HookEventType = "pullreq_updated"
)

// Option is a configuration option for the webhook
Expand Down Expand Up @@ -100,7 +101,7 @@ func (hook Webhook) Parse(r *http.Request, events ...HookEventType) (interface{}
}
event := r.Header.Get("X-Gitfox-Trigger")
if len(event) == 0 {
return nil, ErrMissingGitfoxTriggerHeader
return nil, ErrMissingGitFoxTriggerHeader
}
gitfoxEvent := HookEventType(event)
var found bool
Expand All @@ -122,7 +123,7 @@ func (hook Webhook) Parse(r *http.Request, events ...HookEventType) (interface{}
if len(hook.secret) > 0 {
signature := r.Header.Get("X-Gitfox-Signature")
if len(signature) == 0 {
return nil, ErrMissingGitfoxSignatureHeader
return nil, ErrMissingGitFoxSignatureHeader
}
sig256 := hmac.New(sha256.New, []byte(hook.secret))
_, _ = io.Writer(sig256).Write([]byte(payload))
Expand Down Expand Up @@ -181,6 +182,10 @@ func (hook Webhook) Parse(r *http.Request, events ...HookEventType) (interface{}
var pl PullReqMergedPayload
err = json.Unmarshal([]byte(payload), &pl)
return pl, err
case PullReqUpdatedEvent:
var pl PullReqUpdatedPayload
err = json.Unmarshal([]byte(payload), &pl)
return pl, err
case PullReqReviewerCreatedEvent:
var pl PullReqReviewerCreatedPayload
err = json.Unmarshal([]byte(payload), &pl)
Expand Down
35 changes: 32 additions & 3 deletions gitfox/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,19 @@ type ReferencePayload struct {
ReferenceUpdateSegment
}

// BaseSegment is the common segment for all payloads.
type BaseSegment struct {
Trigger string `json:"trigger"`
Repo Repo `json:"repo"`
Principal PrincipalInfo `json:"principal"`
}

// ReferenceSegment contains the reference info for webhooks.
type ReferenceSegment struct {
Ref ReferenceInfo `json:"ref"`
}

// ReferenceDetailsSegment contains extra details for reference related payloads for webhooks.
type ReferenceDetailsSegment struct {
SHA string `json:"sha"`

Expand All @@ -89,6 +92,7 @@ type ReferenceDetailsSegment struct {
Commit *CommitInfo `json:"commit,omitempty"`
}

// ReferenceUpdateSegment contains extra details for reference update related payloads for webhooks.
type ReferenceUpdateSegment struct {
OldSHA string `json:"old_sha"`
Forced bool `json:"forced"`
Expand Down Expand Up @@ -174,14 +178,39 @@ type PullReqReviewerChangedPayload struct {
ReviewerSegment
}

// ReviewerSegment contains details for all reviewer related payloads for webhooks.
type ReviewerSegment struct {
Reviewer PrincipalInfo `json:"reviewer"`
}

type PullReqReviewSegment struct {
ReviewDecision string `json:"review_decision"`
ReviewerInfo PrincipalInfo `json:"reviewer"`
}

type PullReqReviewSubmittedPayload struct {
BaseSegment
PullReqSegment
Author *PrincipalInfo
Reviewer *PrincipalInfo
Decision string
PullReqTargetReferenceSegment
ReferenceSegment
PullReqReviewSegment
}

// PullReqUpdateSegment contains details what has been updated in the pull request.
type PullReqUpdateSegment struct {
TitleChanged bool `json:"title_changed"`
TitleOld string `json:"title_old"`
TitleNew string `json:"title_new"`
DescriptionChanged bool `json:"description_changed"`
DescriptionOld string `json:"description_old"`
DescriptionNew string `json:"description_new"`
}

// PullReqUpdatedPayload describes the body of the pullreq updated trigger.
type PullReqUpdatedPayload struct {
BaseSegment
PullReqSegment
PullReqTargetReferenceSegment
ReferenceSegment
PullReqUpdateSegment
}

0 comments on commit eaea3ae

Please sign in to comment.