-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(publish): fix version conflict recognition for github packages (#738
) The version conflict recognition logic was not working for github because it returns a different `code` for the same error compared to what npmjs.org returns. I've added two utility functions to check for the error code and message to make the code more readable in the error handler of the publish operation. This way there is a robust detection supporting both npmjs.com and npm.pkg.github.com registries, not just npmjs.org. Signed-off-by: Peter Somogyvari <peter.metz@unarin.com>
- Loading branch information
Showing
4 changed files
with
200 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
packages/publish/src/lib/is-npm-js-publish-version-conflict.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* The purpose of this function is to determine whether the error object passed | ||
* in represents a version conflict when publishing a package to the npm registry. | ||
* | ||
* An example of a version conflict error is: | ||
* ```json | ||
* { | ||
* "name": "HttpErrorGeneral", | ||
* "headers": { | ||
* }, | ||
* "statusCode": 403, | ||
* "code": "E403", | ||
* "method": "PUT", | ||
* "uri": "https://registry.npmjs.org/@hyperledger%2fcactus-cmd-api-server", | ||
* "body": { | ||
* "success": false, | ||
* "error": "You cannot publish over the previously published versions: 2.0.0-alpha.2." | ||
* }, | ||
* "pkgid": "@hyperledger/cactus-cmd-api-server@2.0.0-alpha.2", | ||
* "message": "403 Forbidden - PUT https://registry.npmjs.org/@hyperledger%2fcactus-cmd-api-server - You cannot publish over the previously published versions: 2.0.0-alpha.2." | ||
* } | ||
* ``` | ||
* | ||
* @param ex The exception object to check for a version conflict error. | ||
* @returns {boolean} true if the error represents a version conflict, false otherwise | ||
*/ | ||
export function isNpmJsPublishVersionConflict(ex: unknown): boolean { | ||
if (!ex || typeof ex !== 'object' || !(ex instanceof Error)) { | ||
return false; | ||
} else if ('code' in ex && ex.code === 'EPUBLISHCONFLICT') { | ||
return true; | ||
} else if ( | ||
'code' in ex && | ||
ex.code === 'E403' && | ||
ex.message?.includes('You cannot publish over the previously published versions') | ||
) { | ||
return true; | ||
} | ||
return false; | ||
} |
41 changes: 41 additions & 0 deletions
41
packages/publish/src/lib/is-npm-pkg-github-publish-version-conflict.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* The purpose of this function is to determine whether the exception object | ||
* passed in represents a version conflict when publishing a package to the | ||
* GitHub npm registry. | ||
* | ||
* An example of a version conflict error is pasted below: | ||
* | ||
* ```json | ||
* { | ||
* "name": "HttpErrorGeneral", | ||
* "headers": { | ||
* }, | ||
* "statusCode": 409, | ||
* "code": "E409", | ||
* "method": "PUT", | ||
* "uri": "https://npm.pkg.github.com/hyperledger/@hyperledger%2fcacti-cactus-cmd-api-server", | ||
* "body": { | ||
* "error": "Cannot publish over existing version" | ||
* }, | ||
* "pkgid": "@hyperledger/cacti-cactus-cmd-api-server@2.0.0-alpha.2", | ||
* "message": "409 Conflict - PUT https://npm.pkg.github.com/hyperledger/@hyperledger%2fcacti-cactus-cmd-api-server - Cannot publish over existing version" | ||
* } | ||
* ``` | ||
* @param ex The exception object to check for a version conflict error. | ||
* @returns {boolean} true if the error represents a version conflict, false otherwise. | ||
*/ | ||
export function isNpmPkgGitHubPublishVersionConflict(ex: unknown): boolean { | ||
if (!ex || typeof ex !== 'object' || !(ex instanceof Error)) { | ||
return false; | ||
} else if ('code' in ex && ex.code === 'E409') { | ||
return true; | ||
} else if ( | ||
'body' in ex && | ||
typeof ex.body === 'object' && | ||
(ex.body as Record<string, unknown>).error === 'Cannot publish over existing version' | ||
) { | ||
return true; | ||
} else { | ||
return ex.message.startsWith('409 Conflict - PUT https://npm.pkg.github.com'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters