Skip to content

Commit

Permalink
feat(publish, verify): throw semantic-release compliant errors
Browse files Browse the repository at this point in the history
  • Loading branch information
evansiroky committed Jul 20, 2018
1 parent 3af1ca0 commit 5fdc0e2
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 32 deletions.
56 changes: 56 additions & 0 deletions lib/definitions/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* 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.`
}),
ENOPOMPROJECT: () => ({
message: 'Missing `project` entry in `pom.xml` file',
details: `The \`project\` entry must be included in the \`pom.xml\` file.
Please create a \`pom.xml\` file according to [this guide](https://github.com/conveyal/maven-semantic-release#step-2--make-sure-your-pomxml-file-is-ready-to-be-released).`
}),
ENOPOMPROJECTARTIFACTID: () => ({
message: 'Missing `artifactId` entry in `project` entry in `pom.xml` file',
details: `The \`artifactId\` entry must be included in the \`project\` entry in the \`pom.xml\` file.
Please create a \`pom.xml\` file according to [this guide](https://github.com/conveyal/maven-semantic-release#step-2--make-sure-your-pomxml-file-is-ready-to-be-released).`
}),
ENOPOMPROJECTGROUPID: () => ({
message: 'Missing `groupId` entry in `project` entry in `pom.xml` file',
details: `The \`groupId\` entry must be included in the \`project\` entry in the \`pom.xml\` file.
Please create a \`pom.xml\` file according to [this guide](https://github.com/conveyal/maven-semantic-release#step-2--make-sure-your-pomxml-file-is-ready-to-be-released).`
}),
ENOPOMPROJECTVERSION: () => ({
message: 'Missing `version` entry in `project` entry in `pom.xml` file',
details: `The \`version\` entry must be included in the \`project\` entry in the \`pom.xml\` file.
Please create a \`pom.xml\` file according to [this guide](https://github.com/conveyal/maven-semantic-release#step-2--make-sure-your-pomxml-file-is-ready-to-be-released).`
}),
ENOPOMXML: () => ({
message: 'Missing `pom.xml` file.',
details: `The \`pom.xml\` file could not be found in this repository.
This file is required to run and publish this java package. Please create a \`pom.xml\` file according to [this guide](https://github.com/conveyal/maven-semantic-release#step-2--make-sure-your-pomxml-file-is-ready-to-be-released).`
}),
EREADPOMXML: () => ({
message: 'Error parsing `pom.xml` file.',
details: 'An error was encountered while reading the `pom.xml` file. Please make sure the file contains valid xml.'
}),
ETOOLARGELASTRELEASEPOMDIFF: ({pomVersion, lastReleaseVersion}) => ({
message: 'Version mismatch',
details: `The pom.xml version of \`${pomVersion}\` differs too much from last git tag version of \`${lastReleaseVersion}\`.
Please edit the pom.xml file to have be no more than 1 patch version ahead of the git tag version of \`${lastReleaseVersion}\`.`
})
}
8 changes: 8 additions & 0 deletions lib/get-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const SemanticReleaseError = require('@semantic-release/error')

const ERROR_DEFINITIONS = require('./definitions/errors')

module.exports = (code, ctx = {}) => {
const {message, details} = ERROR_DEFINITIONS[code](ctx)
return new SemanticReleaseError(message, code, details)
}
9 changes: 4 additions & 5 deletions lib/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const execa = require('execa')
const semanticGithub = require('@semantic-release/github')
const {gitHead: getGitHead} = require('semantic-release/lib/git')

const getError = require('./get-error')

module.exports = publish

/**
Expand All @@ -26,7 +28,7 @@ async function publish (pluginConfig, context) {
} catch (e) {
logger.error('failed to deploy to maven')
logger.error(e)
throw new Error('failed to deploy to maven')
throw getError('EMAVENDEPLOY')
}

// tag and create a release on github
Expand Down Expand Up @@ -55,10 +57,7 @@ async function configureGit (repositoryUrl, logger) {
const branchRef = await execa.stdout('git', ['rev-parse', process.env.TRAVIS_BRANCH])

if (headRef !== branchRef) {
throw new Error(`
HEAD ref (${headRef}) does not match ${process.env.TRAVIS_BRANCH} ref (${branchRef}).
Someone may have pushed new commits before this build cloned the repo.
`)
throw getError('EGITREFMISMATCH', {branchRef, headRef})
}

// checkout branch so we can make commits and push
Expand Down
67 changes: 40 additions & 27 deletions lib/verify-release.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
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('./get-error')

/**
* Get the last release of the maven repository
*/
module.exports = async function verify (pluginConfig, context) {
const {logger} = context
logger.log('begin maven verify')

let pomXml
try {
pomXml = await getPomInfo()
} catch (e) {
logger.error(e)
throw e
}

const pomXml = await getPomInfo(logger)
const pomVersion = pomXml.project.version[0]
const mavenCentralVersion = await getLatestVersionFromMavenCentral(
pomXml,
Expand Down Expand Up @@ -59,45 +55,62 @@ module.exports = async function verify (pluginConfig, context) {
}

if (semver.inc(lastReleaseVersion, 'patch') !== semver.inc(pomVersion, 'patch')) {
throw new Error(
`pom.xml version of ${pomVersion} differs too much from last git tag version of ${lastReleaseVersion}`
)
throw getError('ETOOLARGELASTRELEASEPOMDIFF', { lastReleaseVersion, pomVersion })
}
}

// get package info from pom.xml
async function getPomInfo () {
/**
* get package info from pom.xml
*/
async function getPomInfo (logger) {
const pomXmlFilePath = './pom.xml'
const stats = await fs.stat('./pom.xml')

if (!stats) {
throw new Error('pom.xml file is missing!')
throw getError('ENOPOMXML')
}

let pomXml
try {
const pomContents = await fs.readFile(pomXmlFilePath, 'utf8')
pomXml = await xml2js(pomContents)
} catch (e) {
throw new Error('Error reading pom.xml')
logger.log(e)
throw getError('EREADPOMXML')
}

if (
!pomXml ||
!pomXml.project ||
!pomXml.project.groupId ||
pomXml.project.groupId.length === 0 ||
!pomXml.project.artifactId ||
pomXml.project.artifactId.length === 0
) {
throw new Error('pom.xml is missing groupId or artifactId')
validatePomXml(pomXml)

return pomXml
}

/**
* Validate that the contents of pom.xml appear to be setup properly
*/
function validatePomXml (pomXml) {
if (!pomXml) {
throw getError('EREADPOMXML')
}

if (!pomXml.project.version) {
throw new Error('Version is missing from pom.xml')
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'))
}
}

return pomXml
if (pomValidationErrors.length > 0) {
throw new AggregateError(pomValidationErrors)
}
}

/**
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"license": "MIT",
"private": false,
"dependencies": {
"@semantic-release/error": "^2.2.0",
"aggregate-error": "^1.0.0",
"execa": "^0.9.0",
"fs-extra": "^5.0.0",
"got": "^8.0.1",
Expand Down

0 comments on commit 5fdc0e2

Please sign in to comment.