Skip to content

Commit

Permalink
Introduce short-and-sweet functions to send an HTTP response
Browse files Browse the repository at this point in the history
By calling `return ok(...)` or `return withStatus(...)`, it is now
really easy and concise to exit from the Azure Function by sending an
HTTP response.

Signed-off-by: Johannes Schindelin <[email protected]>
  • Loading branch information
dscho committed Dec 1, 2022
1 parent 5f575e4 commit d319ff1
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions LeaveMeAloneGitHubApp/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,23 @@ const validateGitHubWebHook = require('./validate-github-webhook')
const gitHubApiRequestAsApp = require('./github-api-request-as-app')

module.exports = async function (context, req) {
const withStatus = (status, headers, body) => {
context.res = {
status,
headers,
body
}
}

const ok = (body) => {
withStatus(undefined, undefined, body)
}

try {
validateGitHubWebHook(context)
} catch (e) {
context.log(e)
context.res = {
status: 403,
body: `Go away, you are not a valid GitHub webhook: ${e}`,
}
return
return withStatus(403, undefined, `Go away, you are not a valid GitHub webhook: ${e}`)
}

if (req.headers['x-github-event'] === 'installation' && req.body.action === 'created') {
Expand All @@ -26,10 +34,7 @@ module.exports = async function (context, req) {
}
} catch (e) {
context.log(e)
context.res = {
status: 500,
body: `Error:\n${e}`,
}
return withStatus(500, undefined, `Error:\n${e}`)
}
return
}
Expand All @@ -39,8 +44,5 @@ module.exports = async function (context, req) {
context.log("Got body")
context.log(req.body)

context.res = {
// status: 200, /* Defaults to 200 */
body: `Received event ${req.headers["x-github-event"]}`
}
ok(`Received event ${req.headers["x-github-event"]}`)
}

0 comments on commit d319ff1

Please sign in to comment.