Skip to content

Commit c30c059

Browse files
authored
fix: better handle fields with no value set (#3)
1 parent 027f6a4 commit c30c059

File tree

9 files changed

+134
-91
lines changed

9 files changed

+134
-91
lines changed

__tests__/get-item.test.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as core from '@actions/core';
22

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

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

120120
await index.getItemAction();
121121
expect(getItemActionSpy).toHaveReturned();
@@ -130,4 +130,36 @@ describe('getItemAction', () => {
130130
expect(core.setOutput).toHaveBeenCalledWith('field-id', fieldId);
131131
expect(core.setOutput).toHaveBeenCalledWith('field-value', fieldValue);
132132
});
133+
134+
it('handles field with no value set', async () => {
135+
const url = 'https://github.com/dsanders11/project-actions/pull/2';
136+
const contentId = 'content-id';
137+
const title = 'Pull Request Title';
138+
const body = 'Pull Request Description';
139+
const fieldId = 'field-id';
140+
mockGetInput({
141+
owner,
142+
'project-number': projectNumber,
143+
item,
144+
field: 'Status'
145+
});
146+
jest.mocked(getItem).mockResolvedValue({
147+
id: itemId,
148+
content: { id: contentId, type: 'PullRequest', url, title, body },
149+
field: { id: fieldId, value: null },
150+
projectId
151+
});
152+
153+
await index.getItemAction();
154+
expect(getItemActionSpy).toHaveReturned();
155+
156+
expect(core.setOutput).toHaveBeenCalledTimes(7);
157+
expect(core.setOutput).toHaveBeenCalledWith('id', itemId);
158+
expect(core.setOutput).toHaveBeenCalledWith('body', body);
159+
expect(core.setOutput).toHaveBeenCalledWith('title', title);
160+
expect(core.setOutput).toHaveBeenCalledWith('url', url);
161+
expect(core.setOutput).toHaveBeenCalledWith('content-id', contentId);
162+
expect(core.setOutput).toHaveBeenCalledWith('project-id', projectId);
163+
expect(core.setOutput).toHaveBeenCalledWith('field-id', fieldId);
164+
});
133165
});

__tests__/lib.test.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,9 +1119,22 @@ describe('lib', () => {
11191119
})
11201120
});
11211121

1122-
await expect(lib.getItem(owner, projectNumber, itemUrl)).rejects.toThrow(
1123-
lib.FieldHasNoValueError
1124-
);
1122+
const { __typename, ...content } = items[0].content;
1123+
1124+
await expect(
1125+
lib.getItem(owner, projectNumber, itemUrl, 'Name')
1126+
).resolves.toEqual({
1127+
id: itemId,
1128+
projectId,
1129+
content: {
1130+
type: __typename,
1131+
...content
1132+
},
1133+
field: {
1134+
id: fieldId,
1135+
value: null
1136+
}
1137+
});
11251138
});
11261139

11271140
it('throws other graphql errors', async () => {

dist/archive-item.js

Lines changed: 12 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/delete-item.js

Lines changed: 12 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/edit-item.js

Lines changed: 12 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/get-item.js

Lines changed: 15 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/github-script/index.js

Lines changed: 14 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/get-item.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ export async function getItemAction(): Promise<void> {
3232

3333
if (fullItem.field) {
3434
core.setOutput('field-id', fullItem.field.id);
35-
core.setOutput('field-value', fullItem.field.value);
35+
36+
if (fullItem.field.value !== null) {
37+
core.setOutput('field-value', fullItem.field.value);
38+
}
3639
}
3740
} catch (error) {
3841
// Fail the workflow run if an error occurs

src/lib.ts

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export interface ItemDetails {
3434
content: ItemContent;
3535
field?: {
3636
id: string;
37-
value: string | number;
37+
value: string | number | null;
3838
};
3939
}
4040

@@ -78,11 +78,6 @@ export class FieldNotFoundError extends Error {
7878
super('Field not found', { cause });
7979
}
8080
}
81-
export class FieldHasNoValueError extends Error {
82-
constructor(cause?: Error) {
83-
super('Field has no value set', { cause });
84-
}
85-
}
8681
export class ItemNotFoundError extends Error {
8782
constructor(cause?: Error) {
8883
super('Item not found', { cause });
@@ -355,18 +350,21 @@ export async function getItem(
355350
throw new FieldNotFoundError();
356351
}
357352

358-
if (node.fieldValueByName === null) {
359-
throw new FieldHasNoValueError();
353+
if (node.fieldValueByName !== null) {
354+
const { date, number, text, singleSelectValue } =
355+
node.fieldValueByName;
356+
357+
details.field = {
358+
id: projectV2.field.id,
359+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
360+
value: (date ?? number ?? text ?? singleSelectValue)!
361+
};
362+
} else {
363+
details.field = {
364+
id: projectV2.field.id,
365+
value: null
366+
};
360367
}
361-
362-
const { date, number, text, singleSelectValue } =
363-
node.fieldValueByName;
364-
365-
details.field = {
366-
id: projectV2.field.id,
367-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
368-
value: (date ?? number ?? text ?? singleSelectValue)!
369-
};
370368
}
371369

372370
return details;

0 commit comments

Comments
 (0)