diff --git a/.gitignore b/.gitignore index 6d3a831..4dd7aff 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules coverage -.DS_Store \ No newline at end of file +.DS_Store +.idea \ No newline at end of file diff --git a/lib/snapci/examples/errored.json b/lib/snapci/examples/errored.json new file mode 100644 index 0000000..9e5156a --- /dev/null +++ b/lib/snapci/examples/errored.json @@ -0,0 +1,9 @@ +{ + "body": { + "project_name": "ExampleProject/example-client (branchname)", + "stage_name": "Test", + "counter": 1, + "build_result": "errored" + }, + "headers": {} +} \ No newline at end of file diff --git a/lib/snapci/examples/failed.json b/lib/snapci/examples/failed.json new file mode 100644 index 0000000..545d5fd --- /dev/null +++ b/lib/snapci/examples/failed.json @@ -0,0 +1,9 @@ +{ + "body": { + "project_name": "ExampleProject/example-client (branchname)", + "stage_name": "Test", + "counter": 1, + "build_result": "Failed" + }, + "headers": {} +} \ No newline at end of file diff --git a/lib/snapci/examples/passed.json b/lib/snapci/examples/passed.json new file mode 100644 index 0000000..98167f1 --- /dev/null +++ b/lib/snapci/examples/passed.json @@ -0,0 +1,9 @@ +{ + "body": { + "project_name": "ExampleProject/example-client (branchname)", + "stage_name": "Test", + "counter": 1, + "build_result": "Passed" + }, + "headers": {} +} \ No newline at end of file diff --git a/lib/snapci/icons/frown.png b/lib/snapci/icons/frown.png new file mode 100644 index 0000000..cd07390 Binary files /dev/null and b/lib/snapci/icons/frown.png differ diff --git a/lib/snapci/icons/frown@2x.png b/lib/snapci/icons/frown@2x.png new file mode 100644 index 0000000..adcd115 Binary files /dev/null and b/lib/snapci/icons/frown@2x.png differ diff --git a/lib/snapci/icons/logo.png b/lib/snapci/icons/logo.png new file mode 100644 index 0000000..ed2edb9 Binary files /dev/null and b/lib/snapci/icons/logo.png differ diff --git a/lib/snapci/icons/logo@2x.png b/lib/snapci/icons/logo@2x.png new file mode 100644 index 0000000..943ee54 Binary files /dev/null and b/lib/snapci/icons/logo@2x.png differ diff --git a/lib/snapci/icons/smile.png b/lib/snapci/icons/smile.png new file mode 100644 index 0000000..40f6508 Binary files /dev/null and b/lib/snapci/icons/smile.png differ diff --git a/lib/snapci/icons/smile@2x.png b/lib/snapci/icons/smile@2x.png new file mode 100644 index 0000000..cea852e Binary files /dev/null and b/lib/snapci/icons/smile@2x.png differ diff --git a/lib/snapci/index.js b/lib/snapci/index.js new file mode 100644 index 0000000..aaf254f --- /dev/null +++ b/lib/snapci/index.js @@ -0,0 +1,38 @@ +/*jshint globalstrict:true, trailing:false, unused:true, node:true */ +"use strict"; + +var parse = function (headers, body) { + var projectName = body.project_name.split(" ")[0]; + var branch = body.project_name.split(" ")[1].replace(/\(([\-a-zA-Z0-9]+)\)/g, "\$1"); + var buildURL = "https://snap-ci.com/{0}/branch/{1}" + .replace("{0}", projectName) + .replace("{1}", branch); + var message = "{0} Stage for project {1} [{3}]({2})" + .replace("{0}", body.stage_name) + .replace("{1}", projectName) + .replace("{2}", buildURL); + return { + "passed": { + icon: "smile", + errorLevel: "normal", + message: message.replace("{3}", "Passed") + }, + "failed": { + icon: "frown", + errorLevel: "error", + message: message.replace("{3}", "Failed") + }, + "errored": { + icon: "frown", + errorLevel: "error", + message: message.replace("{3}", "Errored") + } + }[body.build_result.toLocaleLowerCase()]; + +}; + +module.exports = { + apiVersion: 1, + name: 'SnapCI', + parse: parse +}; diff --git a/lib/snapci/instructions.md b/lib/snapci/instructions.md new file mode 100644 index 0000000..b8ccd02 --- /dev/null +++ b/lib/snapci/instructions.md @@ -0,0 +1,3 @@ +You need to configure your Snap CI job to send [Webhook notifications](https://docs.snap-ci.com/notifications/webhook-notifications/). + +Add your Gitter webhook URL to Snap-CI's Notification Settings for your project. \ No newline at end of file diff --git a/lib/snapci/settings.json b/lib/snapci/settings.json new file mode 100644 index 0000000..6cc4cc5 --- /dev/null +++ b/lib/snapci/settings.json @@ -0,0 +1,3 @@ +{ + "events": [] +} \ No newline at end of file diff --git a/lib/snapci/test/index.js b/lib/snapci/test/index.js new file mode 100644 index 0000000..21bb573 --- /dev/null +++ b/lib/snapci/test/index.js @@ -0,0 +1,35 @@ +var assert = require('assert'); +var snapciParse = require('../index').parse; +var failedJson = require('../examples/failed.json'); +var passedJson = require('../examples/passed.json'); +var erroredJson = require('../examples/errored.json'); + +describe('Snap CI', function () { + + it('should generate a build passed message', function () { + var parsedResponse = snapciParse(passedJson.headers, passedJson.body); + assert.equal(parsedResponse.icon, "smile"); + assert.equal(parsedResponse.message, "Test Stage for project ExampleProject/example-client [Passed](https://snap-ci.com/ExampleProject/example-client/branch/branchname)"); + assert.equal(parsedResponse.errorLevel, "normal"); + }); + + it('should generate a build failed message', function () { + var parsedResponse = snapciParse(failedJson.headers, failedJson.body); + assert.equal(parsedResponse.icon, "frown"); + assert.equal(parsedResponse.message, "Test Stage for project ExampleProject/example-client [Failed](https://snap-ci.com/ExampleProject/example-client/branch/branchname)"); + assert.equal(parsedResponse.errorLevel, "error"); + }); + + it('should generate a build errored message', function () { + var parsedResponse = snapciParse(erroredJson.headers, erroredJson.body); + assert.equal(parsedResponse.icon, "frown"); + assert.equal(parsedResponse.message, "Test Stage for project ExampleProject/example-client [Errored](https://snap-ci.com/ExampleProject/example-client/branch/branchname)"); + assert.equal(parsedResponse.errorLevel, "error"); + }); + + it('should be undefined for an build unknown hook call', function () { + erroredJson.body.build_result = "anfasnknd"; + var parsedResponse = snapciParse(erroredJson.headers, erroredJson.body); + assert.equal(parsedResponse, undefined); + }); +});