Skip to content

Commit

Permalink
allow only open, merge and close events
Browse files Browse the repository at this point in the history
  • Loading branch information
sadikay committed Oct 19, 2017
1 parent 7a796b5 commit 7b3ea67
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
3 changes: 2 additions & 1 deletion handler.js
Expand Up @@ -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) => {
Expand All @@ -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) {
Expand Down
39 changes: 30 additions & 9 deletions services/index.js
Expand Up @@ -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": [
Expand All @@ -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 }
]
}
]
Expand Down

0 comments on commit 7b3ea67

Please sign in to comment.