diff --git a/__tests__/get-item.test.ts b/__tests__/get-item.test.ts index f008fd0..5f32026 100644 --- a/__tests__/get-item.test.ts +++ b/__tests__/get-item.test.ts @@ -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'); @@ -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(); @@ -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); + }); }); diff --git a/__tests__/lib.test.ts b/__tests__/lib.test.ts index bf18e32..84fb7cf 100644 --- a/__tests__/lib.test.ts +++ b/__tests__/lib.test.ts @@ -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 () => { diff --git a/dist/archive-item.js b/dist/archive-item.js index bf6e670..b92292b 100644 --- a/dist/archive-item.js +++ b/dist/archive-item.js @@ -6783,11 +6783,6 @@ var FieldNotFoundError = class extends Error { super("Field not found", { cause }); } }; -var FieldHasNoValueError = class extends Error { - constructor(cause) { - super("Field has no value set", { cause }); - } -}; var ProjectNotFoundError = class extends Error { constructor(cause) { super("Project not found", { cause }); @@ -6914,15 +6909,19 @@ async function getItem(owner, projectNumber, item, field) { if (projectV2.field === null) { 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; } diff --git a/dist/delete-item.js b/dist/delete-item.js index f2c127e..7df8211 100644 --- a/dist/delete-item.js +++ b/dist/delete-item.js @@ -6783,11 +6783,6 @@ var FieldNotFoundError = class extends Error { super("Field not found", { cause }); } }; -var FieldHasNoValueError = class extends Error { - constructor(cause) { - super("Field has no value set", { cause }); - } -}; var ProjectNotFoundError = class extends Error { constructor(cause) { super("Project not found", { cause }); @@ -6914,15 +6909,19 @@ async function getItem(owner, projectNumber, item, field) { if (projectV2.field === null) { 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; } diff --git a/dist/edit-item.js b/dist/edit-item.js index ca6553e..79b8355 100644 --- a/dist/edit-item.js +++ b/dist/edit-item.js @@ -6783,11 +6783,6 @@ var FieldNotFoundError = class extends Error { super("Field not found", { cause }); } }; -var FieldHasNoValueError = class extends Error { - constructor(cause) { - super("Field has no value set", { cause }); - } -}; var ItemNotFoundError = class extends Error { constructor(cause) { super("Item not found", { cause }); @@ -6924,15 +6919,19 @@ async function getItem(owner, projectNumber, item, field) { if (projectV2.field === null) { 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; } diff --git a/dist/get-item.js b/dist/get-item.js index a9a2990..e3e343a 100644 --- a/dist/get-item.js +++ b/dist/get-item.js @@ -6783,11 +6783,6 @@ var FieldNotFoundError = class extends Error { super("Field not found", { cause }); } }; -var FieldHasNoValueError = class extends Error { - constructor(cause) { - super("Field has no value set", { cause }); - } -}; var ProjectNotFoundError = class extends Error { constructor(cause) { super("Project not found", { cause }); @@ -6914,15 +6909,19 @@ async function getItem(owner, projectNumber, item, field) { if (projectV2.field === null) { 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; } @@ -6983,7 +6982,9 @@ async function getItemAction() { } if (fullItem.field) { core2.setOutput("field-id", fullItem.field.id); - core2.setOutput("field-value", fullItem.field.value); + if (fullItem.field.value !== null) { + core2.setOutput("field-value", fullItem.field.value); + } } } catch (error) { if (error instanceof Error && error.stack) diff --git a/dist/github-script/index.js b/dist/github-script/index.js index e872dff..eff6e46 100644 --- a/dist/github-script/index.js +++ b/dist/github-script/index.js @@ -19332,7 +19332,7 @@ exports.getOctokit = getOctokit; "use strict"; Object.defineProperty(exports, "__esModule", ({ value: true })); -exports.getPullRequestState = exports.linkProjectToTeam = exports.linkProjectToRepository = exports.getProject = exports.editProject = exports.editItem = exports.deleteProject = exports.deleteItem = exports.copyProject = exports.closeProject = exports.archiveItem = exports.addItem = exports.getDraftIssues = exports.getItem = exports.handleCliError = exports.TeamNotFoundError = exports.SingleSelectOptionNotFoundError = exports.RepositoryNotFoundError = exports.ProjectNotFoundError = exports.ItemNotFoundError = exports.FieldHasNoValueError = exports.FieldNotFoundError = void 0; +exports.getPullRequestState = exports.linkProjectToTeam = exports.linkProjectToRepository = exports.getProject = exports.editProject = exports.editItem = exports.deleteProject = exports.deleteItem = exports.copyProject = exports.closeProject = exports.archiveItem = exports.addItem = exports.getDraftIssues = exports.getItem = exports.handleCliError = exports.TeamNotFoundError = exports.SingleSelectOptionNotFoundError = exports.RepositoryNotFoundError = exports.ProjectNotFoundError = exports.ItemNotFoundError = exports.FieldNotFoundError = void 0; const graphql_1 = __nccwpck_require__(8467); const helpers_1 = __nccwpck_require__(3015); class FieldNotFoundError extends Error { @@ -19341,12 +19341,6 @@ class FieldNotFoundError extends Error { } } exports.FieldNotFoundError = FieldNotFoundError; -class FieldHasNoValueError extends Error { - constructor(cause) { - super('Field has no value set', { cause }); - } -} -exports.FieldHasNoValueError = FieldHasNoValueError; class ItemNotFoundError extends Error { constructor(cause) { super('Item not found', { cause }); @@ -19500,15 +19494,20 @@ async function getItem(owner, projectNumber, item, field) { if (projectV2.field === null) { 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; } diff --git a/src/get-item.ts b/src/get-item.ts index 8ed3d12..2d0b4d7 100644 --- a/src/get-item.ts +++ b/src/get-item.ts @@ -32,7 +32,10 @@ export async function getItemAction(): Promise { 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 diff --git a/src/lib.ts b/src/lib.ts index e423e78..ec003ee 100644 --- a/src/lib.ts +++ b/src/lib.ts @@ -34,7 +34,7 @@ export interface ItemDetails { content: ItemContent; field?: { id: string; - value: string | number; + value: string | number | null; }; } @@ -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 }); @@ -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;