diff --git a/index.js b/index.js index 5fa91bc..f453474 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,6 @@ module.exports = { prepare: require('./lib/prepare'), publish: require('./lib/publish'), + verifyConditions: require('./lib/verify-conditions'), verifyRelease: require('./lib/verify-release') } diff --git a/lib/definitions/errors.js b/lib/definitions/errors.js index d9cbb9b..4bb10ac 100644 --- a/lib/definitions/errors.js +++ b/lib/definitions/errors.js @@ -1,17 +1,17 @@ /* eslint-disable sort-keys */ // for better readability module.exports = { - EGITREFMISMATCH: ({ branchRef, headRef }) => ({ - message: 'Git ref mismatch', - details: `HEAD ref (${headRef}) does not match ${process.env.TRAVIS_BRANCH} ref (${branchRef}). - -Someone may have pushed new commits before this build cloned the repo.` - }), EMAVENDEPLOY: () => ({ message: 'Deployment to maven failed.', details: `The deployment to maven failed for an unknown reason. Please check the logs on the CI server to see what happened.` + }), + ENOMAVENSETTINGS: () => ({ + message: 'Missing the `maven-settings.xml` file.', + details: `The \`maven-settings.xml\` file could not be found in this repository. + +This file is required to publish this java package to OSSRH. Please create a \`maven-settings.xml\` file according to [this guide](https://github.com/conveyal/maven-semantic-release/#step-3-create-a-maven-settingsxml-file).` }), ENOPOMPROJECT: () => ({ message: 'Missing `project` entry in `pom.xml` file', diff --git a/lib/prepare.js b/lib/prepare.js index 0df44a9..3bfa9b5 100644 --- a/lib/prepare.js +++ b/lib/prepare.js @@ -1,8 +1,10 @@ const {saveChangesToPomXml} = require('./git') const {updateVersionInPomXml} = require('./maven') +const {printVersion} = require('./util') module.exports = async function publish (pluginConfig, context) { const {logger, nextRelease, options} = context + printVersion(logger) // set and commit version number in pom.xml await updateVersionInPomXml(logger, nextRelease.version) diff --git a/lib/publish.js b/lib/publish.js index c1991b7..7d953ba 100644 --- a/lib/publish.js +++ b/lib/publish.js @@ -1,5 +1,6 @@ const {configureGit, mergeMasterIntoDev, saveChangesToPomXml} = require('./git') const {updateVersionInPomXml, deploy} = require('./maven') +const {printVersion} = require('./util') /** * Publish repo to maven @@ -8,6 +9,7 @@ const {updateVersionInPomXml, deploy} = require('./maven') */ module.exports = async function publish (pluginConfig, context) { const {logger, nextRelease, options} = context + printVersion(logger) await deploy(logger, nextRelease) diff --git a/lib/util.js b/lib/util.js index 2ee8c37..9b6bee8 100644 --- a/lib/util.js +++ b/lib/util.js @@ -1,6 +1,9 @@ const SemanticReleaseError = require('@semantic-release/error') const execa = require('execa') +const fs = require('fs-extra') +const xml2js = require('xml2js-es6-promise') +const pkg = require('../package.json') const ERROR_DEFINITIONS = require('./definitions/errors') /** @@ -21,7 +24,36 @@ function getError (code, ctx = {}) { return new SemanticReleaseError(message, code, details) } +/** + * get package info from pom.xml + */ +async function getPomInfo (logger) { + const pomXmlFilePath = './pom.xml' + const stats = await fs.stat(pomXmlFilePath) + + if (!stats) { + throw getError('ENOPOMXML') + } + + let pomXml + try { + const pomContents = await fs.readFile(pomXmlFilePath, 'utf8') + pomXml = await xml2js(pomContents) + } catch (e) { + logger.log(e) + throw getError('EREADPOMXML') + } + + return pomXml +} + +function printVersion (logger) { + logger.log(`Running ${pkg.name} version ${pkg.version}`) +} + module.exports = { exec, - getError + getError, + getPomInfo, + printVersion } diff --git a/lib/verify-conditions.js b/lib/verify-conditions.js new file mode 100644 index 0000000..1f2133c --- /dev/null +++ b/lib/verify-conditions.js @@ -0,0 +1,62 @@ +const AggregateError = require('aggregate-error') +const fs = require('fs-extra') + +const {getError, getPomInfo, printVersion} = require('./util') + +/** + * Verify that the maven project is properly setup to allow deployment to maven central + */ +module.exports = async function verifyConditions (pluginConfig, context) { + const {logger} = context + printVersion(logger) + + // make sure pom.xml file is good to go + logger.log('validating pom.xml') + const pomXml = await getPomInfo(logger) + validatePomXml(pomXml) + logger.log('pom.xml validation successful') + + // make sure maven-settings file exists + logger.log('validating maven-settings.xml') + const stats = await fs.stat('./maven-settings.xml') + + if (!stats) { + throw getError('ENOMAVENSETTINGS') + } + + logger.log('validating maven-settings.xml') + + // HELP WANTED: do more validation of maven-settings.xml file and OSSRH login +} + +/** + * Validate that the contents of pom.xml appear to be setup properly + */ +function validatePomXml (pomXml) { + if (!pomXml) { + throw getError('EREADPOMXML') + } + + const pomValidationErrors = [] + + if (!pomXml.project) { + pomValidationErrors.push(getError('ENOPOMPROJECT')) + } else { + if (!pomXml.project.groupId || pomXml.project.groupId.length === 0) { + pomValidationErrors.push(getError('ENOPOMPROJECTGROUPID')) + } + if (!pomXml.project.artifactId || pomXml.project.artifactId.length === 0) { + pomValidationErrors.push(getError('ENOPOMPROJECTARTIFACTID')) + } + // does the version need to be set if using semantic-release? + if (!pomXml.project.version || !pomXml.project.version.length === 0) { + pomValidationErrors.push(getError('ENOPOMPROJECTVERSION')) + } + } + + // HELP WANTED: validate more things in pom.xml file + + if (pomValidationErrors.length > 0) { + throw new AggregateError(pomValidationErrors) + } +} diff --git a/lib/verify-release.js b/lib/verify-release.js index 2b393ae..4e2c3ae 100644 --- a/lib/verify-release.js +++ b/lib/verify-release.js @@ -1,17 +1,14 @@ -const AggregateError = require('aggregate-error') -const fs = require('fs-extra') const got = require('got') const semver = require('semver') -const xml2js = require('xml2js-es6-promise') -const {getError} = require('./util') +const {getError, getPomInfo, printVersion} = require('./util') /** * Get the last release of the maven repository */ module.exports = async function verifyRelease (pluginConfig, context) { - const {logger} = context - logger.log('begin maven verify') + const {logger, options} = context + printVersion(logger) const pomXml = await getPomInfo(logger) const pomVersion = pomXml.project.version[0] @@ -55,62 +52,15 @@ module.exports = async function verifyRelease (pluginConfig, context) { } if (semver.inc(lastReleaseVersion, 'patch') !== semver.inc(pomVersion, 'patch')) { - throw getError('ETOOLARGELASTRELEASEPOMDIFF', { lastReleaseVersion, pomVersion }) - } -} - -/** - * get package info from pom.xml - */ -async function getPomInfo (logger) { - const pomXmlFilePath = './pom.xml' - const stats = await fs.stat('./pom.xml') - - if (!stats) { - throw getError('ENOPOMXML') - } - - let pomXml - try { - const pomContents = await fs.readFile(pomXmlFilePath, 'utf8') - pomXml = await xml2js(pomContents) - } catch (e) { - logger.log(e) - throw getError('EREADPOMXML') - } - - validatePomXml(pomXml) - - return pomXml -} - -/** - * Validate that the contents of pom.xml appear to be setup properly - */ -function validatePomXml (pomXml) { - if (!pomXml) { - throw getError('EREADPOMXML') - } - - const pomValidationErrors = [] - - if (!pomXml.project) { - pomValidationErrors.push(getError('ENOPOMPROJECT')) - } else { - if (!pomXml.project.groupId || pomXml.project.groupId.length === 0) { - pomValidationErrors.push(getError('ENOPOMPROJECTGROUPID')) - } - if (!pomXml.project.artifactId || pomXml.project.artifactId.length === 0) { - pomValidationErrors.push(getError('ENOPOMPROJECTARTIFACTID')) - } - if (!pomXml.project.version || !pomXml.project.version.length === 0) { - pomValidationErrors.push(getError('ENOPOMPROJECTVERSION')) + // only throw an error if using the Conveyal workflow + if (options.useConveyalWorkflow) { + throw getError('ETOOLARGELASTRELEASEPOMDIFF', { lastReleaseVersion, pomVersion }) + } else { + logger.log( + `The pom.xml version of \`${pomVersion}\` differs too much from last git tag version of \`${lastReleaseVersion}\`.` + ) } } - - if (pomValidationErrors.length > 0) { - throw new AggregateError(pomValidationErrors) - } } /**