Skip to content

Commit

Permalink
Add event ID from X-GitHub-Delivery if present
Browse files Browse the repository at this point in the history
If we see a `X-GitHub-Delivery` header use its value as the Event ID,
which allows us to use the GitHub Webhook page listing of recent events
to check against what the gosmee client has logged.

Further, we change the code to consolidate header handling so that each
header is handled once.
  • Loading branch information
portante authored and chmouel committed Apr 25, 2023
1 parent 2edca88 commit d6e70b0
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions gosmee/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type payloadMsg struct {
timestamp string
contentType string
eventType string
eventID string
}

type messageBody struct {
Expand All @@ -68,6 +69,7 @@ func (c goSmee) parse(data []byte) (payloadMsg, error) {
pm := payloadMsg{
headers: make(map[string]string),
}
pm.eventID = ""
var message interface{}
_ = json.Unmarshal(data, &message)
var payload map[string]interface{}
Expand All @@ -76,10 +78,30 @@ func (c goSmee) parse(data []byte) (payloadMsg, error) {
return pm, err
}
for payloadKey, payloadValue := range payload {
if payloadKey == "x-github-event" || payloadKey == "x-gitlab-event" || payloadKey == "x-event-key" {
if pv, ok := payloadValue.(string); ok {
pm.headers[title(payloadKey)] = pv
// github action don't like it
replace := strings.NewReplacer(":", "-", " ", "_", "/", "_")
pv = replace.Replace(strings.ToLower(pv))
// remove all non-alphanumeric characters and don't let directory straversal
pv = pmEventRe.FindString(pv)
pm.eventType = pv
}
continue
}
if payloadKey == "x-github-delivery" {
if pv, ok := payloadValue.(string); ok {
pm.headers[title(payloadKey)] = pv
pm.eventID = pv
}
continue
}
if strings.HasPrefix(payloadKey, "x-") || payloadKey == "user-agent" {
if pv, ok := payloadValue.(string); ok {
pm.headers[title(payloadKey)] = pv
}
continue
}
switch payloadKey {
case "bodyB":
Expand Down Expand Up @@ -118,17 +140,6 @@ func (c goSmee) parse(data []byte) (payloadMsg, error) {

pm.timestamp = dt.Format("20060102T15h04")
}

if payloadKey == "x-github-event" || payloadKey == "x-gitlab-event" || payloadKey == "x-event-key" {
if pv, ok := payloadValue.(string); ok {
// github action don't like it
replace := strings.NewReplacer(":", "-", " ", "_", "/", "_")
pv = replace.Replace(strings.ToLower(pv))
// remove all non-alphanumeric characters and don't let directory straversal
pv = pmEventRe.FindString(pv)
pm.eventType = pv
}
}
}

if len(c.ignoreEvents) > 0 && pm.eventType != "" {
Expand Down Expand Up @@ -256,6 +267,9 @@ func (c goSmee) replayData(b []byte) error {
} else {
msg = "request"
}
if pm.eventID != "" {
msg = fmt.Sprintf("%s %s", pm.eventID, msg)
}

msg = fmt.Sprintf("%s replayed to %s, status: %s", msg, ansi.Color(c.targetURL, "green+ub"), ansi.Color(fmt.Sprintf("%d", resp.StatusCode), "blue+b"))
if resp.StatusCode > 299 {
Expand Down

0 comments on commit d6e70b0

Please sign in to comment.