Skip to content

Commit

Permalink
fix: better handle fields with no value set
Browse files Browse the repository at this point in the history
  • Loading branch information
dsanders11 committed Sep 27, 2023
1 parent 027f6a4 commit 6853f5e
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 91 deletions.
36 changes: 34 additions & 2 deletions __tests__/get-item.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as core from '@actions/core';

import * as index from '../src/get-item';
import { ItemDetails, getItem } from '../src/lib';
import { getItem } from '../src/lib';
import { mockGetInput } from './utils';

jest.mock('@actions/core');
Expand Down Expand Up @@ -115,7 +115,7 @@ describe('getItemAction', () => {
content: { id: contentId, type: 'PullRequest', url, title, body },
field: { id: fieldId, value: fieldValue },
projectId
} as ItemDetails);
});

await index.getItemAction();
expect(getItemActionSpy).toHaveReturned();
Expand All @@ -130,4 +130,36 @@ describe('getItemAction', () => {
expect(core.setOutput).toHaveBeenCalledWith('field-id', fieldId);
expect(core.setOutput).toHaveBeenCalledWith('field-value', fieldValue);
});

it('handles field with no value set', async () => {
const url = 'https://github.com/dsanders11/project-actions/pull/2';
const contentId = 'content-id';
const title = 'Pull Request Title';
const body = 'Pull Request Description';
const fieldId = 'field-id';
mockGetInput({
owner,
'project-number': projectNumber,
item,
field: 'Status'
});
jest.mocked(getItem).mockResolvedValue({
id: itemId,
content: { id: contentId, type: 'PullRequest', url, title, body },
field: { id: fieldId, value: null },
projectId
});

await index.getItemAction();
expect(getItemActionSpy).toHaveReturned();

expect(core.setOutput).toHaveBeenCalledTimes(7);
expect(core.setOutput).toHaveBeenCalledWith('id', itemId);
expect(core.setOutput).toHaveBeenCalledWith('body', body);
expect(core.setOutput).toHaveBeenCalledWith('title', title);
expect(core.setOutput).toHaveBeenCalledWith('url', url);
expect(core.setOutput).toHaveBeenCalledWith('content-id', contentId);
expect(core.setOutput).toHaveBeenCalledWith('project-id', projectId);
expect(core.setOutput).toHaveBeenCalledWith('field-id', fieldId);
});
});
19 changes: 16 additions & 3 deletions __tests__/lib.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1119,9 +1119,22 @@ describe('lib', () => {
})
});

await expect(lib.getItem(owner, projectNumber, itemUrl)).rejects.toThrow(
lib.FieldHasNoValueError
);
const { __typename, ...content } = items[0].content;

await expect(
lib.getItem(owner, projectNumber, itemUrl, 'Name')
).resolves.toEqual({
id: itemId,
projectId,
content: {
type: __typename,
...content
},
field: {
id: fieldId,
value: null
}
});
});

it('throws other graphql errors', async () => {
Expand Down
25 changes: 12 additions & 13 deletions dist/archive-item.js

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

25 changes: 12 additions & 13 deletions dist/delete-item.js

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

25 changes: 12 additions & 13 deletions dist/edit-item.js

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

29 changes: 15 additions & 14 deletions dist/get-item.js

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

29 changes: 14 additions & 15 deletions dist/github-script/index.js

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

5 changes: 4 additions & 1 deletion src/get-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ export async function getItemAction(): Promise<void> {

if (fullItem.field) {
core.setOutput('field-id', fullItem.field.id);
core.setOutput('field-value', fullItem.field.value);

if (fullItem.field.value !== null) {
core.setOutput('field-value', fullItem.field.value);
}
}
} catch (error) {
// Fail the workflow run if an error occurs
Expand Down
32 changes: 15 additions & 17 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface ItemDetails {
content: ItemContent;
field?: {
id: string;
value: string | number;
value: string | number | null;
};
}

Expand Down Expand Up @@ -78,11 +78,6 @@ export class FieldNotFoundError extends Error {
super('Field not found', { cause });
}
}
export class FieldHasNoValueError extends Error {
constructor(cause?: Error) {
super('Field has no value set', { cause });
}
}
export class ItemNotFoundError extends Error {
constructor(cause?: Error) {
super('Item not found', { cause });
Expand Down Expand Up @@ -355,18 +350,21 @@ export async function getItem(
throw new FieldNotFoundError();
}

if (node.fieldValueByName === null) {
throw new FieldHasNoValueError();
if (node.fieldValueByName !== null) {
const { date, number, text, singleSelectValue } =
node.fieldValueByName;

details.field = {
id: projectV2.field.id,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
value: (date ?? number ?? text ?? singleSelectValue)!
};
} else {
details.field = {
id: projectV2.field.id,
value: null
};
}

const { date, number, text, singleSelectValue } =
node.fieldValueByName;

details.field = {
id: projectV2.field.id,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
value: (date ?? number ?? text ?? singleSelectValue)!
};
}

return details;
Expand Down

0 comments on commit 6853f5e

Please sign in to comment.