Skip to content

Commit

Permalink
Merge 86a28fc into bb39710
Browse files Browse the repository at this point in the history
  • Loading branch information
ryansonshine committed May 16, 2021
2 parents bb39710 + 86a28fc commit efc8b51
Show file tree
Hide file tree
Showing 32 changed files with 1,096 additions and 51 deletions.
113 changes: 105 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@
"license": "MIT",
"scripts": {
"build": "tsc",
"lint": "eslint ./src/ --fix",
"clean": "rm -rf ./lib/",
"test": "jest --coverage",
"start": "TS_NODE_FILES=true ts-node ./src/cli.ts",
"coverage": "codecov",
"cm": "cz",
"coverage": "codecov",
"lint": "eslint ./src/ --fix",
"semantic-release": "semantic-release",
"start": "TS_NODE_FILES=true ts-node ./src/cli.ts",
"test:watch": "jest --watch",
"semantic-release": "semantic-release"
"test": "jest --coverage",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@aws-sdk/client-codeartifact": "^3.13.1",
Expand All @@ -49,6 +50,7 @@
"@types/signale": "^1.4.1",
"@typescript-eslint/eslint-plugin": "^4.22.0",
"@typescript-eslint/parser": "^4.22.0",
"aws-sdk-client-mock": "^0.4.0",
"codecov": "^3.8.1",
"commitizen": "^4.2.4",
"cz-conventional-changelog": "^3.3.0",
Expand All @@ -62,6 +64,7 @@
"prettier": "^2.2.1",
"semantic-release": "^17.4.2",
"signale": "^1.4.0",
"tempy": "^1.0.1",
"ts-jest": "^26.5.5",
"ts-node": "^9.1.1",
"typescript": "^4.2.4"
Expand Down
28 changes: 25 additions & 3 deletions src/definitions/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import { ErrorDefinitions } from '../types';
import { SUPPORTED_TOOL_LIST } from './constants';

const [homepage]: string = require('../../../package.json').homepage.split('#');
const homepage =
'https://github.com/ryansonshine/semantic-release-codeartifact';

const linkify = (file: string): string => `${homepage}/blob/main/${file}`;

Expand All @@ -29,7 +30,7 @@ Your configuration for the \`tool\` option is \`${tool}\`.`,
'README.md#options'
)}) must be set in the plugin options.`,
}),
ENOAWSKEYID: () => ({
ENOAWSACCESSKEY: () => ({
message: 'No AWS access key specified.',
details: `An [AWS Access Key ID](https://docs.aws.amazon.com/general/latest/gr/glos-chap.html#accesskeyID) must be specified to get an auth token from CodeArtifact.
Expand All @@ -56,7 +57,7 @@ See [AWS Environment variables](${linkify(
'README.md#aws-environment-variables'
)}) for more details.`,
}),
EMISSINGPLUGIN: ({ plugin = '', tool = '', REQUIRED_PLUGINS = [] }) => ({
EMISSINGPLUGIN: ({ plugin, tool, REQUIRED_PLUGINS }) => ({
message: 'Missing plugin.',
details: `The plugin configuration is missing plugin '${plugin}' and is required for '${tool}'.
Expand All @@ -68,6 +69,20 @@ The required plugins for are: ['${REQUIRED_PLUGINS.join("','")}'].`,
The package.json \`publishConfig\` registry is '${publishConfig.registry}.'
The CodeArtifact endpoint is '${repositoryEndpoint}'.`,
}),
ENPMRCCONFIGMISMATCH: ({ repositoryEndpoint, registry }) => ({
message: 'Mismatch on CodeArtifact repository and npmrc registry',
details: `The registry set in the \`.npmrc\` of your project root does not match the CodeArtifact endpoint.
The \`.npmrc\` registry is '${registry}.'
The CodeArtifact endpoint is '${repositoryEndpoint}'.`,
}),
ENPMRCMULTIPLEREGISTRY: ({ registries }) => ({
message: 'Multiple registries found in npmrc',
details: `Your \`.npmrc\` contains multiple registries but should only contain one.
Please remove extraneous registries from your \`.npmrc\`.
Registries found: ['${registries.join("','")}'].`,
}),
ENOAUTHTOKEN: () => ({
message: 'No auth token returned from CodeArtifact client',
Expand All @@ -81,4 +96,11 @@ Please check your AWS configuration and try again.`,
Please check your AWS configuration and try again.`,
}),
EAWSSDK: ({ message, name }) => ({
message: 'AWS SDK Error',
details: `The AWS SDK threw an error while using the CodeArtifact client.
Name: '${name}'
Message: '${message}'`,
}),
};
21 changes: 17 additions & 4 deletions src/get-ca-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import {
} from '@aws-sdk/client-codeartifact';
import { getError } from './get-error';
import AggregateError from 'aggregate-error';
import { isAWSError } from './util/type-guards';

export const getCodeArtifactConfig = async (
pluginConfig: PluginConfig,
context: VerifyConditionsContext
): Promise<CodeArtifactConfig> => {
const errors = [];
const { env, logger } = context;
const { env } = context;
const { domain, domainOwner, tool, repository } = pluginConfig;
try {
const client = new CodeartifactClient({
Expand Down Expand Up @@ -54,8 +55,20 @@ export const getCodeArtifactConfig = async (

return { authorizationToken, repositoryEndpoint };
} catch (e) {
// TODO: handle aws sdk error
logger.error('Failed to get CodeArtifact credentials');
throw e;
if (e instanceof AggregateError) throw e;

if (isAWSError(e)) {
errors.push(getError('EAWSSDK', { message: e.message, name: e.name }));
} else {
errors.push(
getError('EAWSSDK', {
name: 'UnknownException',
message:
'An unknown error has occured, check the logs for more details.',
})
);
}

throw new AggregateError(errors);
}
};
7 changes: 5 additions & 2 deletions src/types/context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type SemanticReleaseError from '@semantic-release/error';
import type { WriteStream } from 'fs';
import type { Signale } from 'signale';
import type { KnownKeys } from './util';

/** Context keys shared across all lifecycle methods */
export interface CommonContext {
Expand Down Expand Up @@ -105,14 +106,16 @@ export interface PluginConfig extends OptionsBase {
domain: string;
/** The AWS Account ID that owns your CodeArtifact domain */
domainOwner?: string;
/** Associates a namespace with your repository tool */
namespace?: string;
/** The time, in seconds, that the login information is valid */
durationSeconds?: number;
/** Your CodeArtifact repository name */
repository: string;
}

export type PluginConfigKey = KnownKeys<
Exclude<PluginConfig, keyof OptionsBase>
>;

export type Channel = null | string;

export interface Release {
Expand Down
13 changes: 12 additions & 1 deletion src/types/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,21 @@ export interface ErrorDefinitions {
ENODOMAINSET: (...args: unknown[]) => ErrorDetails;
ENOREPOSET: () => ErrorDetails;
ENOAWSREGION: () => ErrorDetails;
ENOAWSKEYID: () => ErrorDetails;
ENOAWSACCESSKEY: () => ErrorDetails;
ENOAWSSECRETKEY: () => ErrorDetails;
EMISSINGPLUGIN: (pluginConfig: Partial<PluginConfig>) => ErrorDetails;
EPUBLISHCONFIGMISMATCH: (pluginConfig: Partial<PluginConfig>) => ErrorDetails;
ENPMRCCONFIGMISMATCH: (pluginConfig: Partial<PluginConfig>) => ErrorDetails;
ENPMRCMULTIPLEREGISTRY: (pluginConfig: Partial<PluginConfig>) => ErrorDetails;
ENOAUTHTOKEN: () => ErrorDetails;
ENOREPOENDPOINT: () => ErrorDetails;
EAWSSDK: (pluginConfig: Partial<PluginConfig>) => ErrorDetails;
}

export type AWSError = Error & {
$metadata: Record<string, unknown>;
code: string;
errno: string;
hostname: string;
syscall: string;
};
2 changes: 0 additions & 2 deletions src/types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ declare global {
SR_CA_DOMAIN?: string;
/** The AWS Account ID that owns your CodeArtifact domain */
SR_CA_DOMAIN_OWNER?: string;
/** Associates a namespace with your repository tool */
SR_CA_NAMESPACE?: string;
/** The time, in seconds, that the login information is valid */
SR_CA_DURATION_SEC?: string;
/** Your CodeArtifact repository name */
Expand Down
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './context';
export * from './errors';
export * from './code-artifact';
export * from './util';
6 changes: 6 additions & 0 deletions src/types/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// https://github.com/Microsoft/TypeScript/issues/25987#issuecomment-408339599
export type KnownKeys<T> = {
[K in keyof T]: string extends K ? never : number extends K ? never : K;
} extends { [_ in keyof T]: infer U }
? U
: never;
9 changes: 9 additions & 0 deletions src/util/npmrc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const getRegistryFromNpmrc = (npmrc = ''): string[] => {
return npmrc
.split('\n')
.filter(config => config.includes('registry='))
.map(registry => {
const [, url] = registry.split('registry=');
return url;
});
};
4 changes: 4 additions & 0 deletions src/util/type-guards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { AWSError } from '../types/errors';

export const isAWSError = (e: unknown): e is AWSError =>
Boolean(e && e instanceof Error && (e as AWSError).$metadata !== undefined);
Loading

0 comments on commit efc8b51

Please sign in to comment.