feat: move npm workaround for missing gitHead to the npm plugin
This commit is contained in:
		
							parent
							
								
									754b420fd6
								
							
						
					
					
						commit
						996305d69c
					
				| @ -1,8 +1,8 @@ | ||||
| const gitLogParser = require('git-log-parser'); | ||||
| const getStream = require('get-stream'); | ||||
| const debug = require('debug')('semantic-release:get-commits'); | ||||
| const {unshallow} = require('./git'); | ||||
| const getVersionHead = require('./get-version-head'); | ||||
| const SemanticReleaseError = require('@semantic-release/error'); | ||||
| const {unshallow, gitCommitTag, gitTagHead, isCommitInHistory} = require('./git'); | ||||
| 
 | ||||
| /** | ||||
|  * Commit message. | ||||
| @ -44,22 +44,28 @@ const getVersionHead = require('./get-version-head'); | ||||
|  * @return {Promise<Result>} The list of commits on the branch `branch` since the last release and the updated lastRelease with the gitHead used to retrieve the commits. | ||||
|  * | ||||
|  * @throws {SemanticReleaseError} with code `ENOTINHISTORY` if `lastRelease.gitHead` or the commit sha derived from `config.lastRelease.version` is not in the direct history of `branch`. | ||||
|  * @throws {SemanticReleaseError} with code `ENOGITHEAD` if `lastRelease.gitHead` is undefined and no commit sha can be found for the `config.lastRelease.version`. | ||||
|  */ | ||||
| module.exports = async ({version, gitHead} = {}, branch, logger) => { | ||||
|   let gitTag; | ||||
|   if (gitHead || version) { | ||||
|     try { | ||||
|       ({gitHead, gitTag} = await getVersionHead(gitHead, version, branch)); | ||||
|     } catch (err) { | ||||
|       if (err.code === 'ENOTINHISTORY') { | ||||
|         logger.error(notInHistoryMessage(err.gitHead, branch, version)); | ||||
|       } else { | ||||
|         logger.error(noGitHeadMessage(branch, version)); | ||||
|   if (gitHead) { | ||||
|     // If gitHead doesn't exists in release branch
 | ||||
|     if (!await isCommitInHistory(gitHead)) { | ||||
|       // Unshallow the repository
 | ||||
|       await unshallow(); | ||||
|     } | ||||
|       throw err; | ||||
|     // If gitHead still doesn't exists in release branch
 | ||||
|     if (!await isCommitInHistory(gitHead)) { | ||||
|       // Try to find the commit corresponding to the version, using got tags
 | ||||
|       const tagHead = (await gitTagHead(`v${version}`)) || (await gitTagHead(version)); | ||||
| 
 | ||||
|       // If tagHead doesn't exists in release branch
 | ||||
|       if (!tagHead || !await isCommitInHistory(tagHead)) { | ||||
|         // Then the commit corresponding to the version cannot be found in the bracnh hsitory
 | ||||
|         logger.error(notInHistoryMessage(gitHead, branch, version)); | ||||
|         throw new SemanticReleaseError('Commit not in history', 'ENOTINHISTORY'); | ||||
|       } | ||||
|     logger.log('Retrieving commits since %s, corresponding to version %s', gitHead, version); | ||||
|       gitHead = tagHead; | ||||
|     } | ||||
|     debug('Use gitHead: %s', gitHead); | ||||
|   } else { | ||||
|     logger.log('No previous release found, retrieving all commits'); | ||||
|     // If there is no gitHead nor a version, there is no previous release. Unshallow the repo in order to retrieve all commits
 | ||||
| @ -76,20 +82,9 @@ module.exports = async ({version, gitHead} = {}, branch, logger) => { | ||||
|   ); | ||||
|   logger.log('Found %s commits since last release', commits.length); | ||||
|   debug('Parsed commits: %o', commits); | ||||
|   return {commits, lastRelease: {version, gitHead, gitTag}}; | ||||
|   return {commits, lastRelease: {version, gitHead, gitTag: await gitCommitTag(gitHead)}}; | ||||
| }; | ||||
| 
 | ||||
| function noGitHeadMessage(branch, version) { | ||||
|   return `The commit the last release of this package was derived from cannot be determined from the release metadata nor from the repository tags.
 | ||||
| This means semantic-release can not extract the commits between now and then. | ||||
| This is usually caused by releasing from outside the repository directory or with innaccessible git metadata. | ||||
| 
 | ||||
| You can recover from this error by creating a tag for the version "${version}" on the commit corresponding to this release: | ||||
| $ git tag -f v${version} <commit sha1 corresponding to last release> | ||||
| $ git push -f --tags origin ${branch} | ||||
| `;
 | ||||
| } | ||||
| 
 | ||||
| function notInHistoryMessage(gitHead, branch, version) { | ||||
|   return `The commit the last release of this package was derived from is not in the direct history of the "${branch}" branch.
 | ||||
| This means semantic-release can not extract the commits between now and then. | ||||
|  | ||||
| @ -1,52 +0,0 @@ | ||||
| const debug = require('debug')('semantic-release:get-version-head'); | ||||
| const SemanticReleaseError = require('@semantic-release/error'); | ||||
| const {gitTagHead, gitCommitTag, isCommitInHistory, unshallow} = require('./git'); | ||||
| 
 | ||||
| /** | ||||
|  * Get the commit sha for a given version, if it's contained in the given branch. | ||||
|  * | ||||
|  * @param {string} gitHead The commit sha to look for. | ||||
|  * @param {string} version The version corresponding to the commit sha to look for. Used to search in git tags. | ||||
|  * | ||||
|  * @return {Promise<Object>} A Promise that resolves to an object with the `gitHead` and `gitTag` for the the `version`. | ||||
|  * | ||||
|  * @throws {SemanticReleaseError} with code `ENOTINHISTORY` if `gitHead` or the commit sha dereived from `version` is not in the direct history of `branch`. | ||||
|  * @throws {SemanticReleaseError} with code `ENOGITHEAD` if `gitHead` is undefined and no commit sha can be found for the `version`. | ||||
|  */ | ||||
| module.exports = async (gitHead, version) => { | ||||
|   // Check if gitHead is defined and exists in release branch
 | ||||
|   if (gitHead && (await isCommitInHistory(gitHead))) { | ||||
|     debug('Use gitHead: %s', gitHead); | ||||
|     return {gitHead, gitTag: await gitCommitTag(gitHead)}; | ||||
|   } | ||||
| 
 | ||||
|   await unshallow(); | ||||
| 
 | ||||
|   // Check if gitHead is defined and exists in release branch again
 | ||||
|   if (gitHead && (await isCommitInHistory(gitHead))) { | ||||
|     debug('Use gitHead: %s', gitHead); | ||||
|     return {gitHead, gitTag: await gitCommitTag(gitHead)}; | ||||
|   } | ||||
| 
 | ||||
|   let tagHead; | ||||
|   if (version) { | ||||
|     // If a version is defined search a corresponding tag
 | ||||
|     tagHead = (await gitTagHead(`v${version}`)) || (await gitTagHead(version)); | ||||
| 
 | ||||
|     // Check if tagHead is found and exists in release branch again
 | ||||
|     if (tagHead && (await isCommitInHistory(tagHead))) { | ||||
|       debug('Use tagHead: %s', tagHead); | ||||
|       return {gitHead: tagHead, gitTag: await gitCommitTag(tagHead)}; | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   // Either gitHead is defined or a tagHead has been found but none is in the branch history
 | ||||
|   if (gitHead || tagHead) { | ||||
|     const error = new SemanticReleaseError('Commit not in history', 'ENOTINHISTORY'); | ||||
|     error.gitHead = gitHead || tagHead; | ||||
|     throw error; | ||||
|   } | ||||
| 
 | ||||
|   // There is no gitHead in the last release and there is no tags correponsing to the last release version
 | ||||
|   throw new SemanticReleaseError('There is no commit associated with last release', 'ENOGITHEAD'); | ||||
| }; | ||||
| @ -25,7 +25,7 @@ async function gitTagHead(tagName) { | ||||
|  * | ||||
|  * @param {string} gitHead The commit sha for which to retrieve the associated tag. | ||||
|  * | ||||
|  * @return {string} The tag associatedwith the sha in parameter or `null`. | ||||
|  * @return {string} The tag associatedwith the sha in parameter or `undefined`. | ||||
|  */ | ||||
| async function gitCommitTag(gitHead) { | ||||
|   try { | ||||
| @ -34,7 +34,7 @@ async function gitCommitTag(gitHead) { | ||||
|     return shell.stdout; | ||||
|   } catch (err) { | ||||
|     debug(err); | ||||
|     return null; | ||||
|     return undefined; | ||||
|   } | ||||
| } | ||||
| 
 | ||||
|  | ||||
| @ -24,9 +24,9 @@ module.exports = { | ||||
|       validator: output => | ||||
|         !output || | ||||
|         (isObject(output) && !output.version) || | ||||
|         (isString(output.version) && Boolean(semver.valid(semver.clean(output.version)))), | ||||
|         (isString(output.version) && Boolean(semver.valid(semver.clean(output.version))) && Boolean(output.gitHead)), | ||||
|       message: | ||||
|         'The "getLastRelease" plugin output if defined, must be an object with an optionnal valid semver version in the "version" property.', | ||||
|         'The "getLastRelease" plugin output if defined, must be an object with a valid semver version in the "version" property and the corresponding git reference in "gitHead" property.', | ||||
|     }, | ||||
|   }, | ||||
|   analyzeCommits: { | ||||
|  | ||||
| @ -180,7 +180,7 @@ test.serial('Get all commits since gitHead (from tag) ', async t => { | ||||
|   commits = (await gitCommits(['Second', 'Third'])).concat(commits); | ||||
| 
 | ||||
|   // Retrieve the commits with the commits module, since commit 'First' (associated with tag v1.0.0)
 | ||||
|   const result = await getCommits({version: '1.0.0'}, 'master', t.context.logger); | ||||
|   const result = await getCommits({version: '1.0.0', gitHead: 'missing_ref'}, 'master', t.context.logger); | ||||
| 
 | ||||
|   // Verify the commits created and retrieved by the module are identical
 | ||||
|   t.is(result.commits.length, 2); | ||||
| @ -214,7 +214,7 @@ test.serial('Get all commits since gitHead (from tag) on a detached head repo', | ||||
|   await gitDetachedHead(repo, commits[1].hash); | ||||
| 
 | ||||
|   // Retrieve the commits with the commits module, since commit 'First' (associated with tag 1.0.0)
 | ||||
|   const result = await getCommits({version: '1.0.0'}, 'master', t.context.logger); | ||||
|   const result = await getCommits({version: '1.0.0', gitHead: 'missing_ref'}, 'master', t.context.logger); | ||||
| 
 | ||||
|   // Verify the module retrieved only the commit 'feat: Second' (included in the detached and after 'fix: First')
 | ||||
|   t.is(result.commits.length, 1); | ||||
| @ -241,7 +241,7 @@ test.serial('Get all commits since gitHead (from tag formatted like v<version>) | ||||
|   commits = (await gitCommits(['Second', 'Third'])).concat(commits); | ||||
| 
 | ||||
|   // Retrieve the commits with the commits module, since commit 'First' (associated with tag v1.0.0)
 | ||||
|   const result = await getCommits({version: '1.0.0'}, 'master', t.context.logger); | ||||
|   const result = await getCommits({version: '1.0.0', gitHead: 'missing_ref'}, 'master', t.context.logger); | ||||
| 
 | ||||
|   // Verify the commits created and retrieved by the module are identical
 | ||||
|   t.is(result.commits.length, 2); | ||||
| @ -261,40 +261,7 @@ test.serial('Get all commits since gitHead (from tag formatted like v<version>) | ||||
|   t.is(result.lastRelease.gitTag, 'v1.0.0'); | ||||
|   t.is(result.lastRelease.version, '1.0.0'); | ||||
| }); | ||||
| 
 | ||||
| test.serial('Get commits when last release gitHead is missing but a tag match the version', async t => { | ||||
|   // Create a git repository, set the current working directory at the root of the repo
 | ||||
|   await gitRepo(); | ||||
|   // Add commits to the master branch
 | ||||
|   let commits = await gitCommits(['First']); | ||||
|   // Create the tag corresponding to version 1.0.0
 | ||||
|   await gitTagVersion('v1.0.0'); | ||||
|   // Add new commits to the master branch
 | ||||
|   commits = (await gitCommits(['Second', 'Third'])).concat(commits); | ||||
| 
 | ||||
|   // Retrieve the commits with the commits module, since commit 'First' (associated with tag v1.0.0)
 | ||||
|   const result = await getCommits({version: '1.0.0', gitHead: 'missing'}, 'master', t.context.logger); | ||||
| 
 | ||||
|   // Verify the commits created and retrieved by the module are identical
 | ||||
|   t.is(result.commits.length, 2); | ||||
|   t.is(result.commits[0].hash.substring(0, 7), commits[0].hash); | ||||
|   t.is(result.commits[0].message, commits[0].message); | ||||
|   t.truthy(result.commits[0].committerDate); | ||||
|   t.truthy(result.commits[0].author.name); | ||||
|   t.truthy(result.commits[0].committer.name); | ||||
|   t.is(result.commits[1].hash.substring(0, 7), commits[1].hash); | ||||
|   t.is(result.commits[1].message, commits[1].message); | ||||
|   t.truthy(result.commits[1].committerDate); | ||||
|   t.truthy(result.commits[1].author.name); | ||||
|   t.truthy(result.commits[1].committer.name); | ||||
|   // Verify the last release is returned and updated
 | ||||
|   t.truthy(result.lastRelease); | ||||
|   t.is(result.lastRelease.gitHead.substring(0, 7), commits[commits.length - 1].hash); | ||||
|   t.is(result.lastRelease.gitTag, 'v1.0.0'); | ||||
|   t.is(result.lastRelease.version, '1.0.0'); | ||||
| }); | ||||
| 
 | ||||
| test.serial('Get all commits since gitHead, when gitHead are mising from the shallow clone', async t => { | ||||
| test.serial('Get all commits since gitHead, when gitHead is missing from the shallow clone', async t => { | ||||
|   // Create a git repository, set the current working directory at the root of the repo
 | ||||
|   const repo = await gitRepo(); | ||||
|   // Add commits to the master branch
 | ||||
| @ -328,7 +295,7 @@ test.serial('Get all commits since gitHead, when gitHead are mising from the sha | ||||
|   t.falsy(result.lastRelease.gitTag); | ||||
| }); | ||||
| 
 | ||||
| test.serial('Get all commits since gitHead from tag, when tags are mising from the shallow clone', async t => { | ||||
| test.serial('Get all commits since gitHead from tag, when tags is missing from the shallow clone', async t => { | ||||
|   // Create a git repository, set the current working directory at the root of the repo
 | ||||
|   const repo = await gitRepo(); | ||||
|   // Add commits to the master branch
 | ||||
| @ -344,7 +311,7 @@ test.serial('Get all commits since gitHead from tag, when tags are mising from t | ||||
|   t.is((await gitTags()).length, 0); | ||||
| 
 | ||||
|   // Retrieve the commits with the commits module, since commit 'First' (associated with tag v1.0.0)
 | ||||
|   const result = await getCommits({version: '1.0.0'}, 'master', t.context.logger); | ||||
|   const result = await getCommits({version: '1.0.0', gitHead: 'missing_ref'}, 'master', t.context.logger); | ||||
| 
 | ||||
|   // Verify the commits created and retrieved by the module are identical
 | ||||
|   t.is(result.commits.length, 2); | ||||
| @ -398,25 +365,6 @@ test.serial('Return empty array if there is no commits', async t => { | ||||
|   t.falsy(result.lastRelease.version); | ||||
| }); | ||||
| 
 | ||||
| test.serial('Throws ENOGITHEAD error if the gitHead of the last release cannot be found', async t => { | ||||
|   // Create a git repository, set the current working directory at the root of the repo
 | ||||
|   await gitRepo(); | ||||
|   // Add commits to the master branch
 | ||||
|   await gitCommits(['First', 'Second']); | ||||
| 
 | ||||
|   // Retrieve the commits with the commits module
 | ||||
|   const error = await t.throws(getCommits({version: '1.0.0'}, 'master', t.context.logger)); | ||||
| 
 | ||||
|   // Verify error code and type
 | ||||
|   t.is(error.code, 'ENOGITHEAD'); | ||||
|   t.is(error.name, 'SemanticReleaseError'); | ||||
|   // Verify the log function has been called with a message explaining the error
 | ||||
|   t.regex( | ||||
|     t.context.error.args[0][0], | ||||
|     /The commit the last release of this package was derived from cannot be determined from the release metadata nor from the repository tags/ | ||||
|   ); | ||||
| }); | ||||
| 
 | ||||
| test.serial('Throws ENOTINHISTORY error if gitHead is not in history', async t => { | ||||
|   // Create a git repository, set the current working directory at the root of the repo
 | ||||
|   await gitRepo(); | ||||
| @ -505,7 +453,7 @@ test.serial('Throws ENOTINHISTORY error when a tag is not in branch history but | ||||
|   await gitCommits(['Forth']); | ||||
| 
 | ||||
|   // Retrieve the commits with the commits module
 | ||||
|   const error = await t.throws(getCommits({version: '1.0.0'}, 'master', t.context.logger)); | ||||
|   const error = await t.throws(getCommits({version: '1.0.0', gitHead: shaTag}, 'master', t.context.logger)); | ||||
|   // Verify error code and type
 | ||||
|   t.is(error.code, 'ENOTINHISTORY'); | ||||
|   t.is(error.name, 'SemanticReleaseError'); | ||||
|  | ||||
| @ -288,7 +288,7 @@ test.serial('Release patch, minor and major versions', async t => { | ||||
| }); | ||||
| 
 | ||||
| test.serial('Release versions from a packed git repository, using tags to determine last release gitHead', async t => { | ||||
|   const packageName = 'test-git-packaed'; | ||||
|   const packageName = 'test-git-packed'; | ||||
|   const owner = 'test-repo'; | ||||
|   // Create a git repository, set the current working directory at the root of the repo
 | ||||
|   t.log('Create git repository'); | ||||
|  | ||||
| @ -67,15 +67,16 @@ test('The "publish" plugin is mandatory, and must be a single or an array of plu | ||||
|   t.true(definitions.publish.config.validator([{path: 'plugin-path.js'}, 'plugin-path.js', () => {}])); | ||||
| }); | ||||
| 
 | ||||
| test('The "getLastRelease" plugin output if defined, must be an object with an optionnal valid semver version in the "version" property', t => { | ||||
| test('The "getLastRelease" plugin output if defined, must be an object with a valid semver version in the "version" property and the corresponding git reference in "gitHead" property', t => { | ||||
|   t.false(definitions.getLastRelease.output.validator('string')); | ||||
|   t.false(definitions.getLastRelease.output.validator(1)); | ||||
|   t.false(definitions.getLastRelease.output.validator({version: 'v1.0.0'})); | ||||
|   t.false(definitions.getLastRelease.output.validator({version: 'invalid'})); | ||||
| 
 | ||||
|   t.true(definitions.getLastRelease.output.validator()); | ||||
|   t.true(definitions.getLastRelease.output.validator({})); | ||||
|   t.true(definitions.getLastRelease.output.validator({version: 'v1.0.0'})); | ||||
|   t.true(definitions.getLastRelease.output.validator({version: '1.0.0'})); | ||||
|   t.true(definitions.getLastRelease.output.validator({version: 'v1.0.0', gitHead: '123'})); | ||||
|   t.true(definitions.getLastRelease.output.validator({version: '1.0.0', gitHead: '123'})); | ||||
|   t.true(definitions.getLastRelease.output.validator({version: null})); | ||||
| }); | ||||
| 
 | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user