Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: throw readable error if connecting to JIRA fails #73

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion babel.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
module.exports = {
presets: [
'@babel/preset-env',
[
'@babel/preset-env',
{
targets: {
node: 'current'
}
}
],
'@babel/preset-typescript',
],
};
10 changes: 9 additions & 1 deletion lib/verifyConditions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,13 @@ export async function verifyConditions(config: PluginConfig, context: PluginCont
throw new SemanticReleaseError(`JIRA_AUTH must be a string`);
}
const jira = makeClient(config, context);
await jira.project.getProject({ projectIdOrKey: config.projectId });
try {
await jira.project.getProject({ projectIdOrKey: config.projectId });
} catch (e) {
if (typeof e === 'string' && e.includes('"statusCode"')) {
throw new SemanticReleaseError(`connecting to jira failed with status ${JSON.parse(e).statusCode}`);
} else {
throw new SemanticReleaseError(`connecting to jira failed`, '', e);
}
}
}
9 changes: 6 additions & 3 deletions test/fakedata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ export const upcomingRelease: UpcomingRelease = {
type: ''
}

export const pluginConfig: Partial<PluginConfig> = {
export const pluginConfig: PluginConfig = {
...baseConfig,
projectId: 'TEST',
jiraHost: 'testjira.com'
jiraHost: 'testjira.com',
ticketPrefixes: ['TEST'],
}

export const logger = {
Expand All @@ -62,7 +63,9 @@ export const logger = {

export const pluginContext: PluginContext = {
cwd: '',
env: {},
env: {
JIRA_AUTH: 'myauth'
},
logger: logger as any,
options: baseConfig,
stderr: null,
Expand Down
34 changes: 34 additions & 0 deletions test/verifyConditions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { pluginConfig, pluginContext } from './fakedata';

function throwOnJiraGetProject(mockError: string | Error): void {
jest.mock('jira-connector', () => ({
__esModule: true,
default: jest.fn().mockImplementation(() => ({
project: {
getProject: () => {
throw mockError;
},
},
})),
}));
}

describe('verifyConditions', () => {
afterEach(() => {
jest.resetModules();
});

describe('JIRA connection check', () => {
it('catches status errors', () => {
throwOnJiraGetProject('{"statusCode": 401, "response": ""}');
const verifyConditions = require('../lib/verifyConditions').verifyConditions;
expect(() => verifyConditions(pluginConfig, pluginContext)).rejects.toThrow('connecting to jira failed with status 401');
});

it('catches other errors', () => {
throwOnJiraGetProject(new Error('foobar'));
const verifyConditions = require('../lib/verifyConditions').verifyConditions;
expect(() => verifyConditions(pluginConfig, pluginContext)).rejects.toThrow('connecting to jira failed');
});
});
});