diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..a8359c0 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,24 @@ +# +# EditorConfig +# +# This file helps maintain consistent coding styles for multiple developers +# working on the same project across various editors and IDEs +# +# https://EditorConfig.org +# + +root = true + +[*] +charset = utf-8 +indent_style = space +indent_size = 2 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +[*.md] +trim_trailing_whitespace = false + +[*.{json, html}] +indent_size = 4 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..379eb3a --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +# IDE +/.idea + +# Node +/node_modules +*.tgz diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..de87c50 --- /dev/null +++ b/.npmignore @@ -0,0 +1,3 @@ +/.idea +/.editorconfig +*.tgz diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..17dc3c5 --- /dev/null +++ b/LICENSE @@ -0,0 +1,15 @@ +ISC License + +Copyright (c) 2019, Marek Sierociński + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..9172358 --- /dev/null +++ b/README.md @@ -0,0 +1,45 @@ +# dashbi-data-provider-jenkins-build + +Dashbi data provider that fetches last build for given Jenkins job. + +## Usage + +### Install + +```sh +npm install --save dashbi-data-provider-jenkins-build +``` + +### Register + +Dashbi should detect and auto-register data provider. + +### Config Source + +You need to provide to params: + +* `jenkinsUrl` +* `jobPath` + +### Example + +Let's say that your Jenkins Job URL is `https://myjenkins.example.org/job/Hello/`. +Your widget configuration could look like this: + +```js +dashbiLayout.addWidget({ + name: 'jenkins-build-status', + title: 'My Jenkins Job', + source: { + name: 'jenkins-build', + params: { + jenkinsUrl: 'https://myjenkins.example.org', + jobPath: 'job/Hello' + } + } +}); +``` + +### Widget + +Widget created to display build status is [jenkins-build-status](https://github.com/marverix/dashbi-widget-jenkins-build-status) diff --git a/index.js b/index.js new file mode 100644 index 0000000..6ba71aa --- /dev/null +++ b/index.js @@ -0,0 +1,40 @@ +'use strict'; + +const axios = require('axios'); +const Worker = require('dashbi-worker'); + +/** + * Strip wrapping slashes + * @param {string} str + */ +function stripWrappingSlashes (str) { + return str.replace(/^\/|\/$/g, ''); +} + +/** + * Get last build + */ +function getLastBuild () { + let that = this; + axios.get(`${stripWrappingSlashes(that.config.jenkinsUrl)}/${stripWrappingSlashes(that.config.jobPath)}/lastBuild/api/json`, { + timeout: 2 * Date.SECOND + }) + .then(function (res) { + that.storeState('id', res.data.id); + that.storeState('result', res.data.result); + that.storeState('duration', res.data.duration); + }) + .catch(function (err) { + // Support error? + }); +} + +// Create worker +const worker = new Worker({ + jenkinsUrl: '', + jobPath: '' +}); + +// Add checks +worker.addCheck(getLastBuild, Math.floor(5 * Date.SECOND + Math.rand() * Date.SECOND) ); +worker.sendOnChange(); diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..25bb30b --- /dev/null +++ b/package-lock.json @@ -0,0 +1,56 @@ +{ + "name": "dashbi-data-provider-jenkins-build", + "version": "0.1.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "axios": { + "version": "0.19.0", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.19.0.tgz", + "integrity": "sha512-1uvKqKQta3KBxIz14F2v06AEHZ/dIoeKfbTRkK1E5oqjDnuEerLmYTgJB5AiQZHJcljpg1TuRzdjDR06qNk0DQ==", + "requires": { + "follow-redirects": "1.5.10", + "is-buffer": "^2.0.2" + } + }, + "dashbi-worker": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/dashbi-worker/-/dashbi-worker-0.1.0.tgz", + "integrity": "sha512-PUGIsypHcmmx3ZR3HH1UYWdE40dLVshkZchxO81uiLDLfwN/hQhDxPwVn7ljngSK4ppE4ia6b2Zcj34zHp7gMw==", + "requires": { + "finka": "^1.4.0" + } + }, + "debug": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", + "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", + "requires": { + "ms": "2.0.0" + } + }, + "finka": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/finka/-/finka-1.4.0.tgz", + "integrity": "sha512-QWETHd/UaSrOdCyvhVXlBmFRLZlrMgNEFAa2ZXBq0DYogzB2F/ttKa3CyuIQ7Z539TNnQY0jrzeiR9pxHQqmFw==" + }, + "follow-redirects": { + "version": "1.5.10", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.5.10.tgz", + "integrity": "sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==", + "requires": { + "debug": "=3.1.0" + } + }, + "is-buffer": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-2.0.3.tgz", + "integrity": "sha512-U15Q7MXTuZlrbymiz95PJpZxu8IlipAp4dtS3wOdgPXx3mqBnslrWU14kxfHB+Py/+2PVKSr37dMAgM2A4uArw==" + }, + "ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..91b2c72 --- /dev/null +++ b/package.json @@ -0,0 +1,22 @@ +{ + "name": "dashbi-data-provider-jenkins-build", + "version": "0.1.0", + "description": "Dashbi data provider that fetches last build for given Jenkins job", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [ + "dashbi", + "dashbi-data-provider", + "jenkins", + "jenkins-build", + "jenkins-job" + ], + "author": "Marek Sierociński ", + "license": "ISC", + "dependencies": { + "axios": "^0.19.0", + "dashbi-worker": "^0.1.0" + } +}