diff --git a/web/app.js b/web/app.js index 82952f3..2abf760 100644 --- a/web/app.js +++ b/web/app.js @@ -74,7 +74,7 @@ app.post('/hooks/:appid', function (req, res) { return res.status(500).end() } } - + if (app && verify(req, app, payload)) { executeHook(appid, app, payload, function () { res.end() @@ -82,6 +82,7 @@ app.post('/hooks/:appid', function (req, res) { } else { res.end() } + }) // listen when API is ready @@ -101,35 +102,45 @@ pod.once('ready', function () { }) // Helpers -function verify(req, app, payload) { +function verify (req, app, payload) { + var skipGitUrlCheck = false // skips the check for gitUrl : only happens if coming from TFS + var commit // not even a remote app if (!app.remote) return // check repo match var repo = payload.repository var repoURL - - if (repo.links && /bitbucket\.org/.test(repo.links.html.href)) { - console.log('\nreceived webhook request from: ' + repo.links.html.href) - + + // check comming from tfs + // NOTE: you need to add the following to your headers when creating the webhook in TFS + // TFS-Web-Hook:true + if(req.headers['tfs-web-hook']){ + repo = payload.resource.repository + repoURL = repo.url + skipGitUrlCheck = true + }else if (repo.links && /bitbucket\.org/.test(repo.links.html.href)) { repoURL = repo.links.html.href } else { - console.log('\nreceived webhook request from: ' + repo.url) - repoURL = repo.url } - + + console.log('\nreceived webhook request from: ' + repoURL) + if (!repoURL) return - if (ghURL(repoURL).repopath !== ghURL(app.remote).repopath) { + if (!skipGitUrlCheck && ghURL(repoURL).repopath !== ghURL(app.remote).repopath) { console.log('aborted.') return } - var commit // support bitbucket webhooks payload structure - if (/bitbucket\.org/.test(repoURL)) { + if(req.headers['tfs-web-hook']){ + // From TFS + commit = payload.resource.refUpdates[payload.resource.refUpdates.length -1] + commit.message = payload.resource.commits[payload.resource.commits.length - 1].comment + }else if (/bitbucket\.org/.test(repoURL)) { commit = payload.push.changes[0].new commit.message = commit.target.message @@ -147,17 +158,26 @@ function verify(req, app, payload) { console.log('aborted.') return } + + // check branch match + return checkBranch(commit, app, payload) +} + +// handle checking the branch +function checkBranch(commit, app, payload){ // check branch match var ref = commit.name ? commit.name : payload.ref - if (!ref) return + if (!ref) return false var branch = ref.replace('refs/heads/', ''), expected = app.branch || 'master' + console.log('expected branch: ' + expected + ', got branch: ' + branch) + if (branch !== expected) { console.log('aborted.') - return + return false } return true } diff --git a/web/tfs-webhook-sample.json b/web/tfs-webhook-sample.json new file mode 100644 index 0000000..c78bd9e --- /dev/null +++ b/web/tfs-webhook-sample.json @@ -0,0 +1,100 @@ +{ + "subscriptionId":"d2eddcd8-d1f9-43c7-ba8f-629bdb97b29b", + "notificationId":17, + "id":"d891cdac-47f4-4882-abf9-cb3e71291b1b", + "eventType":"git.push", + "publisherId":"tfs", + "scope":"all", + "message":{ + "text":"cHerendeen pushed updates to branch stable of WebPortal\r\n(https://envative.visualstudio.com/OBG%20Safety%20Moments%20App/_git/WebPortal/#version=GBstable)", + "html":"cHerendeen pushed updates to branch stable of WebPortal", + "markdown":"cHerendeen pushed updates to branch [stable](https://envative.visualstudio.com/OBG%20Safety%20Moments%20App/_git/WebPortal/#version=GBstable) of [WebPortal](https://envative.visualstudio.com/OBG%20Safety%20Moments%20App/_git/WebPortal/)" + }, + "detailedMessage":{ + "text":"cHerendeen pushed 1 commit to branch stable of WebPortal\r\n - auto push test again… 5f6bcc4b (https://envative.visualstudio.com/OBG%20Safety%20Moments%20App/_git/WebPortal/commit/5f6bcc4b0023e986102cd99002ccb89348c84a76)", + "html":"cHerendeen pushed 1 commit to branch stable of WebPortal\r\n", + "markdown":"cHerendeen pushed 1 commit to branch [stable](https://envative.visualstudio.com/OBG%20Safety%20Moments%20App/_git/WebPortal/#version=GBstable) of [WebPortal](https://envative.visualstudio.com/OBG%20Safety%20Moments%20App/_git/WebPortal/)\r\n* auto push test again… [5f6bcc4b](https://envative.visualstudio.com/OBG%20Safety%20Moments%20App/_git/WebPortal/commit/5f6bcc4b0023e986102cd99002ccb89348c84a76)" + }, + "resource":{ + "commits":[ + { + "commitId":"5f6bcc4b0023e986102cd99002ccb89348c84a76", + "author":{ + "name":"Clay Herendeen", + "email":"cherendeen@envative.com", + "date":"2017-01-20T18:59:56Z" + }, + "committer":{ + "name":"Clay Herendeen", + "email":"cherendeen@envative.com", + "date":"2017-01-20T18:59:56Z" + }, + "comment":"auto push test again…", + "url":"https://envative.visualstudio.com/_apis/git/repositories/fe3be7e8-f759-4a1d-be68-a30f23aad3e9/commits/5f6bcc4b0023e986102cd99002ccb89348c84a76" + } + ], + "refUpdates":[ + { + "name":"refs/heads/stable", + "oldObjectId":"19396a7d5b0967fb58ff7bd1b0b0827126333bce", + "newObjectId":"5f6bcc4b0023e986102cd99002ccb89348c84a76" + } + ], + "repository":{ + "id":"fe3be7e8-f759-4a1d-be68-a30f23aad3e9", + "name":"WebPortal", + "url":"https://envative.visualstudio.com/_apis/git/repositories/fe3be7e8-f759-4a1d-be68-a30f23aad3e9", + "project":{ + "id":"7f367f39-6695-4c77-8be7-aebf58d7e414", + "name":"OBG Safety Moments App", + "url":"https://envative.visualstudio.com/_apis/projects/7f367f39-6695-4c77-8be7-aebf58d7e414", + "state":"wellFormed" + }, + "defaultBranch":"refs/heads/master", + "remoteUrl":"https://envative.visualstudio.com/OBG%20Safety%20Moments%20App/_git/WebPortal" + }, + "pushedBy":{ + "id":"a13c2079-d832-4b81-8a40-683fb51e9b61", + "displayName":"cHerendeen", + "uniqueName":"cherendeen@envative.com", + "url":"https://app.vssps.visualstudio.com/Afbb8d8ef-76f1-415d-abd8-87c55ea8fb64/_apis/Identities/a13c2079-d832-4b81-8a40-683fb51e9b61", + "imageUrl":"https://envative.visualstudio.com/_api/_common/identityImage?id=a13c2079-d832-4b81-8a40-683fb51e9b61" + }, + "pushId":10569, + "date":"2017-01-20T18:59:58.5916971Z", + "url":"https://envative.visualstudio.com/_apis/git/repositories/fe3be7e8-f759-4a1d-be68-a30f23aad3e9/pushes/10569", + "_links":{ + "self":{ + "href":"https://envative.visualstudio.com/_apis/git/repositories/fe3be7e8-f759-4a1d-be68-a30f23aad3e9/pushes/10569" + }, + "repository":{ + "href":"https://envative.visualstudio.com/_apis/git/repositories/fe3be7e8-f759-4a1d-be68-a30f23aad3e9" + }, + "commits":{ + "href":"https://envative.visualstudio.com/_apis/git/repositories/fe3be7e8-f759-4a1d-be68-a30f23aad3e9/pushes/10569/commits" + }, + "pusher":{ + "href":"https://app.vssps.visualstudio.com/Afbb8d8ef-76f1-415d-abd8-87c55ea8fb64/_apis/Identities/a13c2079-d832-4b81-8a40-683fb51e9b61" + }, + "refs":{ + "href":"https://envative.visualstudio.com/_apis/git/repositories/fe3be7e8-f759-4a1d-be68-a30f23aad3e9/refs" + } + } + }, + "resourceVersion":"1.0", + "resourceContainers":{ + "collection":{ + "id":"81938269-1970-4a66-8848-6da1f799e6cf", + "baseUrl":"https://envative.visualstudio.com/" + }, + "account":{ + "id":"fbb8d8ef-76f1-415d-abd8-87c55ea8fb64", + "baseUrl":"https://envative.visualstudio.com/" + }, + "project":{ + "id":"7f367f39-6695-4c77-8be7-aebf58d7e414", + "baseUrl":"https://envative.visualstudio.com/" + } + }, + "createdDate":"2017-01-20T19:00:02.410893Z" +}