Skip to content

Commit

Permalink
test implemented for created assets in V1 to gh issue
Browse files Browse the repository at this point in the history
- test for created stories are implemented, but do not pass
- tests for created defects are not implemented and would not pass (no implementation)

make all tests pass
  • Loading branch information
andrew-codes committed Mar 2, 2019
1 parent 26d3d1d commit 103220f
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 11 deletions.
10 changes: 7 additions & 3 deletions packages/webhooked-plugin-issue-creation-gh-v1/src/index.js
Expand Up @@ -41,10 +41,10 @@ module.exports = async (req, options) => {
links: { name: 'Github Issue', url: req.body.issue.url },
name: req.body.issue.title,
scope,
taggedWith: [`github:${req.body.issue.number}`],
taggedWith: [`github-${req.body.issue.number}`],
team,
});
issues.editIssue(req.body.issue.number, { labels: [`'v1-${_oid}`] });
issues.editIssue(req.body.issue.number, { labels: [`v1-${_oid}`] });
return;
}
};
Expand Down Expand Up @@ -100,7 +100,11 @@ function validateOptions(options) {
),
)
.concat(
validatePropsAreStrings(options, ['scope'], p => `Invalid ${p} option`),
validatePropsAreStrings(
options,
['scope', 'webhookId'],
p => `Invalid ${p} option`,
),
);

return errors;
Expand Down
121 changes: 113 additions & 8 deletions packages/webhooked-plugin-issue-creation-gh-v1/tests/index.test.js
Expand Up @@ -6,18 +6,24 @@ const { when } = require('jest-when');
const plugin = require('../src/index');
const connector = require('@andrew-codes/v1sdk-fetch-connector');
const github = require('github-api');
const ghCreateIssue = jest.fn();
const ghEditIssue = jest.fn();
const ghToken = 'ghtoken';
when(github)
.calledWith({ token: ghToken })
.mockReturnValue({
getIssues: jest.fn().mockReturnValue({ editIssue: ghEditIssue }),
getIssues: jest
.fn()
.mockReturnValue({ createIssue: ghCreateIssue, editIssue: ghEditIssue }),
});
const v1Create = jest.fn();
const v1Update = jest.fn();
const connectedSdk = jest.fn();
const sdkConstructor = jest.fn();
connectedSdk.mockReturnValue({
withAccessToken: jest.fn().mockReturnValue({ create: v1Create }),
withAccessToken: jest
.fn()
.mockReturnValue({ create: v1Create, update: v1Update }),
});
sdkConstructor.mockReturnValue(connectedSdk);
connector.mockReturnValue(sdkConstructor);
Expand Down Expand Up @@ -88,61 +94,155 @@ test('throws error if there is an invalid scope option', async () => {
expect(error.invalidOptions).toContain('Invalid scope option');
}
});
test('throws error if there is an invalid webhookId option', async () => {
try {
await plugin(
createGhRequest('created'),
createOptionsWithValidConnection(),
);
} catch (error) {
expect(error.invalidOptions).toContain('Invalid webhookId option');
}
});

test('labeling a gh issue as a story will create a Story asset V1 in the configured scope, on the configured team, tagged with the gh issue identifier, and a link back to the github issue', async () => {
const scope = 'Scope:1';
const name = 'some title';
const url = 'some url';
const team = 'Team:102';
v1Create.mockReturnValue({ _oid: 'Issue:1234' });
const _oid = 'Story:1234';
const issueNumber = 123;
v1Create.mockReturnValue({ _oid });

await plugin(
createGhRequest('labeled', {
issue: { number: 123, title: name, url },
issue: { number: issueNumber, title: name, url },
label: { name: 'story' },
}),
createOptionsWithValidConnection({
assetToLabel: { Defect: 'defect', Story: 'story' },
scope,
team,
webhookId: 'v1 payload identifier',
}),
);

expect(v1Create).toBeCalledWith('Story', {
links: { name: 'Github Issue', url },
name,
scope,
taggedWith: [`github:${123}`],
taggedWith: [`github-${issueNumber}`],
team,
});
expect(ghEditIssue).toBeCalledWith(issueNumber, { labels: [`v1-${_oid}`] });
});

test('labeling a gh issue as a defect will create a Defect asset V1 in the configured scope, on the configured team, tagged with the gh issue identifier, and a link back to the github issue', async () => {
const scope = 'Scope:1';
const name = 'some title';
const url = 'some url';
const team = 'Team:102';
v1Create.mockReturnValue({ _oid: 'Issue:1234' });
const _oid = 'Defect:1234';
const issueNumber = 123;
v1Create.mockReturnValue({ _oid });

await plugin(
createGhRequest('labeled', {
issue: { number: 123, title: name, url },
issue: { number: issueNumber, title: name, url },
label: { name: 'defect' },
}),
createOptionsWithValidConnection({
assetToLabel: { Defect: 'defect', Story: 'story' },
scope,
team,
webhookId: 'v1 payload identifier',
}),
);

expect(v1Create).toBeCalledWith('Defect', {
links: { name: 'Github Issue', url },
name,
scope,
taggedWith: [`github:${123}`],
taggedWith: [`github-${issueNumber}`],
team,
});
expect(ghEditIssue).toBeCalledWith(issueNumber, { labels: [`v1-${_oid}`] });
});

test('creating a Story in V1 will create a new github issue, name it, describe it, label it with the asset oid, and add the created issue identifier to the asset along with a link to the created issue', async () => {
const _oid = 'Story:1234';
const scope = 'scope';
const webhookId = 'v1 payload identifier';
const name = 'defect name';
const description = 'defect description';
const issueNumber = '4';
const url = 'issue url';
ghCreateIssue.mockReturnValue(Promise.resolve({ number: issueNumber, url }));

await plugin(
createV1Request('AssetCreated', webhookId, {
_oid,
assetType: 'Story',
changes: [
{ name: 'Name', new: name },
{ name: 'Description', new: description },
],
}),
createOptionsWithValidConnection({
assetToLabel: { Defect: 'defect', Story: 'story' },
scope,
webhookId,
}),
);

expect(ghCreateIssue).toBeCalledWith({
title: name,
description,
labels: [`v1-${_oid}`],
});
expect(v1Update).toBeCalledWith(_oid, {
taggedWith: [`github-${issueNumber}`],
links: { name: 'Github Issue', url },
});
});
test('creating stories without a description are still create issues in gh sans the description', async () => {
const _oid = 'Story:1234';
const scope = 'scope';
const webhookId = 'a value indicating I came from V1';
const name = 'defect name';
const issueNumber = '4';
const url = 'issue url';
ghCreateIssue.mockReturnValue(Promise.resolve({ number: issueNumber, url }));

await plugin(
createV1Request('AssetCreated', webhookId, {
_oid,
assetType: 'Story',
changes: [{ name: 'Name', new: name }],
}),
createOptionsWithValidConnection({
assetToLabel: { Defect: 'defect', Story: 'story' },
scope,
webhookId,
}),
);

expect(ghCreateIssue).toBeCalledWith({
title: name,
labels: [`v1-${_oid}`],
});
expect(v1Update).toBeCalledWith(_oid, {
taggedWith: [`github-${issueNumber}`],
links: { name: 'Github Issue', url },
});
});

test('creating a Defect in V1 will create a new github issue, label it with the asset oid, and add the created issue identifier to the asset along with a link to the created issue', async () => {
throw new Error('Not Implemented');
});

test('creating defects without a description are still create issues in gh sans the description', async () => {
throw new Error('Not Implemented');
});

function createGhRequest(action, data = {}) {
Expand Down Expand Up @@ -173,3 +273,8 @@ function createOptionsWithValidConnection(data = {}) {
...data,
};
}
function createV1Request(eventType, webhookId, targetAsset) {
return {
events: [{ eventType, webhookId, targetAsset }],
};
}

0 comments on commit 103220f

Please sign in to comment.