Skip to content

Commit

Permalink
feat(verify-conditions): add verify-conditions step
Browse files Browse the repository at this point in the history
- also add some more logging
- move some checks about the pom.xml into verify-conditions
- check for existence of maven-settings.xml file
  • Loading branch information
evansiroky committed Nov 30, 2018
1 parent 262bc8a commit db2148c
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 67 deletions.
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
prepare: require('./lib/prepare'),
publish: require('./lib/publish'),
verifyConditions: require('./lib/verify-conditions'),
verifyRelease: require('./lib/verify-release')
}
12 changes: 6 additions & 6 deletions lib/definitions/errors.js
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
2 changes: 2 additions & 0 deletions lib/prepare.js
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 2 additions & 0 deletions lib/publish.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const {configureGit, mergeMasterIntoDev, saveChangesToPomXml} = require('./git')
const {updateVersionInPomXml, deploy} = require('./maven')
const {printVersion} = require('./util')

/**
* Publish repo to maven
Expand All @@ -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)

Expand Down
34 changes: 33 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
@@ -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')

/**
Expand All @@ -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
}
62 changes: 62 additions & 0 deletions lib/verify-conditions.js
Original file line number Diff line number Diff line change
@@ -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)
}
}
70 changes: 10 additions & 60 deletions lib/verify-release.js
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -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)
}
}

/**
Expand Down

0 comments on commit db2148c

Please sign in to comment.