Skip to content

Conversation

pandey-adarsh147
Copy link
Contributor

@pandey-adarsh147 pandey-adarsh147 commented Jan 18, 2022

  • Identified the issue which this PR solves.
  • Read the CONTRIBUTING document.
  • Code builds clean without any errors or warnings.
  • Added appropriate tests for any new functionality.
  • All new and existing tests passed.
  • Added comments in the code, where necessary.
  • Ran make check to catch common errors. Fixed any that came up.

Description:
Incoming webhook will now accept application/json content type
{"summary":"", "details":"", "action":""}

Which issue(s) this PR fixes:
#2092

Usage: Fixes #2092

Copy link
Member

@mastercactapus mastercactapus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few changes requested, in summary:

  • check for errors and return appropriate 4xx response code
  • Ensure we fallback to query parameters for missing fields
  • use mime helper for parsing/normalizing Content-Type header

details = validate.SanitizeText(details, alert.MaxDetailsLength)
contentType := r.Header.Get("Content-type")
if contentType == "application/json" {
requestBoday, _ := ioutil.ReadAll(r.Body)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check for error and return bad request if not nil, and a bit of a nit but let's use data as the var to match conventions elsewhere

requestBoday, _ := ioutil.ReadAll(r.Body)

var alertMessage AlertMessage
json.Unmarshal(requestBoday, &alertMessage)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, check error and return bad request if not nil

Comment on lines +97 to +98
contentType := r.Header.Get("Content-type")
if contentType == "application/json" {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use the ParseMediaType helper to handle any variations/encoding info:

Suggested change
contentType := r.Header.Get("Content-type")
if contentType == "application/json" {
ct, _, err = mime.ParseMediaType(r.Header.Get("Content-Type"))
if err != nil {
http.Error(w, err.Error(), http.StatusUnsupportedMediaType)
return
}
if ct == "application/json" {

if contentType == "application/json" {
requestBoday, _ := ioutil.ReadAll(r.Body)

var alertMessage AlertMessage
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this, let's do an inline struct since we don't need it elsewhere. Also we can ignore the json tags since Go parses JSON ignoring case already. Lastly, let's make the fields pointers so we can fallback to query parameters for any missing fields (this follows the current behavior of FormValue)

Suggested change
var alertMessage AlertMessage
var b struct {
Summary, Details, Action *string
}

Comment on lines +104 to +106
summary = alertMessage.Summary
details = alertMessage.Details
action = alertMessage.Action
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now for these we can do a nil-check and ignore any missing values

Suggested change
summary = alertMessage.Summary
details = alertMessage.Details
action = alertMessage.Action
if b.Summary != nil {
summary = *b.Summary
}
if b.Details != nil {
details = *b.Details
}
if b.Action != nil {
action = *b.Action
}

Comment on lines +45 to +46
},
"version": "0.0.0"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated editor change committed by mistake?

Suggested change
},
"version": "0.0.0"
}

@mastercactapus
Copy link
Member

@pandey-adarsh147 is this something you still wanted to get in? I may be able to wrap it up for you too, but you'll need to allow edits from maintainers on your end for me to push changes.

@mastercactapus
Copy link
Member

Closing, merged with #2161

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants