diff --git a/action.yml b/action.yml index 32e3b24..9e54d3c 100644 --- a/action.yml +++ b/action.yml @@ -1,10 +1,26 @@ name: 'danger-action-tnp' +author: 'The Neon Project' description: 'A danger action to add to your CI' inputs: - JiraTag: + jira-tag: description: 'Your repo tag in JIRA' required: false default: '' + + jira-url: + description: 'Your JIRA url' + required: false + default: 'https://www.example.atlassian.net/browse' + + coverage: + description: 'Enable coverage. That implies having tests' + required: false + default: false + + eslint: + description: 'Enable eslint errors' + required: false + default: false runs: using: 'node12' main: 'dist/index.js' diff --git a/dangerfile.js b/dangerfile.js index 786f2c9..691190d 100644 --- a/dangerfile.js +++ b/dangerfile.js @@ -1,3 +1,89 @@ -import { markdown } from "danger"; +import { danger, fail, markdown, message } from 'danger' +import { istanbulCoverage } from 'danger-plugin-istanbul-coverage' +import jiraIssue from 'danger-plugin-jira-issue' +import * as fs from 'fs' +import * as path from 'path' -markdown(process.env['JIRA_TAG']) \ No newline at end of file +const getEslintMarkdown = (eslintOutput, newFiles, modifiedFiles) => { + let eslintMarkdown = '## ESlint Issues:\n' + let warnings = false + + eslintOutput.map(record => { + const file = path.parse(record.filePath).base + + if (modifiedFiles.includes(file) || newFiles.includes(file)) { + if (record.messages.length > 0) { + warnings = true + eslintMarkdown = eslintMarkdown.concat(file).concat('\n') + record.messages.map(error => { + eslintMarkdown = eslintMarkdown.concat( + `* ${error.ruleId} - ${error.message} Line ${error.line} \n` + ) + }) + } + eslintMarkdown = eslintMarkdown.concat('\n') + } + }) + + if (warnings) { + return eslintMarkdown + } else { + return '' + } +} + +const existsChangelog = fs.existsSync('CHANGELOG.md') + +if (!existsChangelog) { + fail('Create a changelog file following the instructions of [KeepaChangelog](https://keepachangelog.com/en/1.0.0/)') +} else { + const hasChangelogModified = danger.git.modified_files.includes('CHANGELOG.md') + + if (!hasChangelogModified) { + fail('Please add a changelog entry for your changes and follow the instructions of [KeepaChangelog](https://keepachangelog.com/en/1.0.0/)') + } +} + + +if (process.env['COVERAGE']) { + istanbulCoverage({ + customSuccessMessage: 'Congrats, coverage is good', + customFailureMessage: 'Coverage is a little low, take a look', + coveragePath: 'coverage/coverage-summary.json', + reportFileSet: 'createdOrModified', + reportMode: 'warn' + }) +} + + +if (process.env['JIRA_TAG'] !== '') { + jiraIssue({ + key: process.env['JIRA_TAG'], + url: process.env['JIRA_URL'], + emoji: ':paperclip:', + location: 'title' + }) +} + + +if (process.env['ESLINT']) { + const eslintOutput = JSON.parse(fs.readFileSync('eslint.json', 'utf8')) + + if (danger.git.modified_files.length > 0 || danger.git.created_files.length > 0) { + const modifiedFiles = danger.git.modified_files.map(pathname => { + return path.parse(pathname).base + }) + + const newFiles = danger.git.created_files.map(pathname => { + return path.parse(pathname).base + }) + + const messageToSend = getEslintMarkdown(eslintOutput, newFiles, modifiedFiles) + + if (messageToSend !== '') { + markdown(messageToSend) + } else { + message('There aren\'t eslint errors in your code :rocket:') + } + } +} diff --git a/dist/index.js b/dist/index.js index 91a5c0c..ad3677b 100644 --- a/dist/index.js +++ b/dist/index.js @@ -66,8 +66,20 @@ __webpack_require__.r(__webpack_exports__); async function run() { try { - process.env['JIRA_TAG'] = _actions_core__WEBPACK_IMPORTED_MODULE_0__.getInput('JiraTag') - child_process__WEBPACK_IMPORTED_MODULE_1__.execSync("npm run danger") + process.env['JIRA_TAG'] = _actions_core__WEBPACK_IMPORTED_MODULE_0__.getInput('jira-tag') + process.env['JIRA_URL'] = _actions_core__WEBPACK_IMPORTED_MODULE_0__.getInput('jira-url') + process.env['COVERAGE'] = _actions_core__WEBPACK_IMPORTED_MODULE_0__.getInput('coverage') + process.env['ESLINT'] = _actions_core__WEBPACK_IMPORTED_MODULE_0__.getInput('eslint') + + if (process.env['COVERAGE']) { + child_process__WEBPACK_IMPORTED_MODULE_1__.execSync('npm run coverage') + } + + if (process.env['ESLINT']) { + child_process__WEBPACK_IMPORTED_MODULE_1__.execSync('npm run eslint') + } + + child_process__WEBPACK_IMPORTED_MODULE_1__.execSync('npm run danger') } catch (error) { _actions_core__WEBPACK_IMPORTED_MODULE_0__.setFailed(error.message); diff --git a/index.js b/index.js index 0942654..0a7bc55 100644 --- a/index.js +++ b/index.js @@ -3,8 +3,20 @@ import * as cp from 'child_process' async function run() { try { - process.env['JIRA_TAG'] = core.getInput('JiraTag') - cp.execSync("npm run danger") + process.env['JIRA_TAG'] = core.getInput('jira-tag') + process.env['JIRA_URL'] = core.getInput('jira-url') + process.env['COVERAGE'] = core.getInput('coverage') + process.env['ESLINT'] = core.getInput('eslint') + + if (process.env['COVERAGE']) { + cp.execSync('npm run coverage') + } + + if (process.env['ESLINT']) { + cp.execSync('npm run eslint') + } + + cp.execSync('npm run danger') } catch (error) { core.setFailed(error.message); diff --git a/package.json b/package.json index 23c7678..f55a800 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "danger-action", - "version": "1.0.0", + "version": "0.1.4", "description": "", "main": "index.js", "scripts": { @@ -9,7 +9,7 @@ "danger": "npx danger ci" }, "keywords": [], - "author": "", + "author": "The Neon Project", "license": "ISC", "dependencies": { "@actions/core": "^1.2.3",