diff --git a/handler.js b/handler.js index 001206b..f600c29 100644 --- a/handler.js +++ b/handler.js @@ -3,6 +3,7 @@ var request = require('request'); var services = require('./services/index') + // POST: /events or // POST: /events?url=your_team_connector_url module.exports.hello = (event, context, callback) => { @@ -15,7 +16,7 @@ module.exports.hello = (event, context, callback) => { let teamConnectorURL = preTeamUrl || process.env.TEAMS_CONNECTOR_URL - if (body.object_kind === 'merge_request' || body.object_kind === 'issue'){ + if (payload){ request.post(teamConnectorURL, { json: payload }, function (error, response, body) { if (!error && response.statusCode == 200) { diff --git a/services/index.js b/services/index.js index 57628a0..ac0db3c 100644 --- a/services/index.js +++ b/services/index.js @@ -2,33 +2,54 @@ var _ = require('lodash') var humanize = require('string-humanize') +var cardActivityTitle = function (kind, user, action) { + let pron = "d a " + if (action === 'open') + pron = "ed a " + + return user + " " + humanize(action) + pron + humanize(kind) +} + +var cardTitle = function (kind, name, action) { + if (action == 'open') { + return "[New] " + humanize(kind) + ": " + name + } else if (action == 'merge') { + return "[Merged] " + humanize(kind) + ": " + name + } else if (action == 'close') { + return "[Closed] " + humanize(kind) + ": " + name + } +} + const services = (body) => { - let actionUrl, assignees, card_title = '' + let assignees = '' + + // Allow Only MR and Issues + if (!(body.object_kind === 'merge_request' || body.object_kind === 'issue')) + return false + + // Allow only open, merge, close actions + if (!["open", "merge", "close"].includes(body.object_attributes.action)) + return false if (body.object_kind === 'merge_request') { - actionUrl = body.project.web_url + '/merge_requests/' + body.object_attributes.iid assignees = body.assignee.name - card_title = body.user.name + " opened a merge request" } else if (body.object_kind === 'issue') { - actionUrl = body.project.web_url + '/issues/' + body.object_attributes.iid - card_title = body.user.name + " opened an issue" if (body.assignees){ let assignees_users = _.map(body.assignees, 'name') assignees = _.join(assignees_users, ',') } - } let payload = { "@context": "http://schema.org/extensions", "@type": "MessageCard", "themeColor": "F4F4F4", - "title": "New " + humanize(body.object_kind) + ": " + body.object_attributes.title, + "title": cardTitle(body.object_kind, body.object_attributes.title, body.object_attributes.action), "summary": "Click to action button to see datails.", "sections": [ { - "activityTitle": card_title, + "activityTitle": cardActivityTitle(body.object_kind, body.user.name, body.object_attributes.action), "activitySubtitle": body.object_attributes.created_at, "activityImage": body.user.avatar_url, "facts": [ @@ -48,7 +69,7 @@ const services = (body) => { "@type": "OpenUri", "name": "Go To " + humanize(body.object_kind), "targets": [ - { "os": "default", "uri": actionUrl } + { "os": "default", "uri": body.object_attributes.url } ] } ]