Skip to content

Commit

Permalink
feat: add fail-if-item-not-found input to more actions (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsanders11 authored Jan 18, 2025
1 parent 64f724e commit 8bc0bd4
Show file tree
Hide file tree
Showing 16 changed files with 110 additions and 15 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,18 @@ jobs:
project-number: ${{ steps.copy-project.outputs.number }}
token: ${{ steps.get-auth-token.outputs.token }}

- name: Edit Item (Not Found)
uses: ./edit-item/
id: edit-item-not-found
with:
fail-if-item-not-found: false
field: Priority
field-value: ⛰️ High
item: foobar
owner: ${{ steps.copy-project.outputs.owner }}
project-number: ${{ steps.copy-project.outputs.number }}
token: ${{ steps.get-auth-token.outputs.token }}

- name: Get Item
uses: ./get-item/
id: get-edited-item
Expand All @@ -182,6 +194,17 @@ jobs:
project-number: ${{ steps.copy-project.outputs.number }}
token: ${{ steps.get-auth-token.outputs.token }}

- name: Get Item (Not Found)
uses: ./get-item/
id: get-item-not-found
with:
fail-if-item-not-found: false
field: Priority
item: foobar
owner: ${{ steps.copy-project.outputs.owner }}
project-number: ${{ steps.copy-project.outputs.number }}
token: ${{ steps.get-auth-token.outputs.token }}

- name: Check Edited Item Field Value
uses: ./github-script/
with:
Expand Down Expand Up @@ -236,6 +259,16 @@ jobs:
project-number: ${{ steps.copy-project.outputs.number }}
token: ${{ steps.get-auth-token.outputs.token }}

- name: Delete Item (Not Found)
uses: ./delete-item/
id: delete-item-not-found
with:
item: foobar
fail-if-item-not-found: false
owner: ${{ steps.copy-project.outputs.owner }}
project-number: ${{ steps.copy-project.outputs.number }}
token: ${{ steps.get-auth-token.outputs.token }}

- name: Link Project to Repository
uses: ./link-project/
id: link-project-to-repo
Expand Down
15 changes: 14 additions & 1 deletion __tests__/delete-item.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as core from '@actions/core';

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

vi.mock('@actions/core');
vi.mock('../src/lib');
Expand Down Expand Up @@ -51,6 +51,7 @@ describe('deleteItemAction', () => {

it('handles item not found', async () => {
mockGetInput({ owner, 'project-number': projectNumber, item });
mockGetBooleanInput({ 'fail-if-item-not-found': true });
vi.mocked(deleteItem).mockResolvedValue();

await index.deleteItemAction();
Expand All @@ -60,6 +61,18 @@ describe('deleteItemAction', () => {
expect(core.setFailed).toHaveBeenLastCalledWith(`Item not found: ${item}`);
});

it('can ignore item not found', async () => {
mockGetInput({ owner, 'project-number': projectNumber, item });
mockGetBooleanInput({ 'fail-if-item-not-found': false });
vi.mocked(getItem).mockResolvedValue(null);

await index.deleteItemAction();
expect(deleteItemActionSpy).toHaveReturned();

expect(core.setFailed).not.toHaveBeenCalled();
expect(core.setOutput).not.toHaveBeenCalled();
});

it('handles project not found', async () => {
mockGetInput({ owner, 'project-number': projectNumber, item });
vi.mocked(getItem).mockResolvedValue({ id: itemId } as ItemDetails);
Expand Down
15 changes: 14 additions & 1 deletion __tests__/edit-item.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as core from '@actions/core';

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

vi.mock('@actions/core');
vi.mock('../src/lib');
Expand Down Expand Up @@ -86,6 +86,7 @@ describe('editItemAction', () => {

it('handles item not found', async () => {
mockGetInput({ owner, 'project-number': projectNumber, item });
mockGetBooleanInput({ 'fail-if-item-not-found': true });
vi.mocked(editItem).mockResolvedValue(itemId);

await index.editItemAction();
Expand All @@ -95,6 +96,18 @@ describe('editItemAction', () => {
expect(core.setFailed).toHaveBeenLastCalledWith(`Item not found: ${item}`);
});

it('can ignore item not found', async () => {
mockGetInput({ owner, 'project-number': projectNumber, item });
mockGetBooleanInput({ 'fail-if-item-not-found': false });
vi.mocked(getItem).mockResolvedValue(null);

await index.editItemAction();
expect(editItemActionSpy).toHaveReturned();

expect(core.setFailed).not.toHaveBeenCalled();
expect(core.setOutput).not.toHaveBeenCalled();
});

it('handles project not found', async () => {
mockGetInput({ owner, 'project-number': projectNumber, item });
vi.mocked(getItem).mockResolvedValue({
Expand Down
15 changes: 14 additions & 1 deletion __tests__/get-item.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as core from '@actions/core';

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

vi.mock('@actions/core');
vi.mock('../src/lib');
Expand Down Expand Up @@ -52,6 +52,7 @@ describe('getItemAction', () => {

it('handles item not found', async () => {
mockGetInput({ owner, 'project-number': projectNumber, item });
mockGetBooleanInput({ 'fail-if-item-not-found': true });
vi.mocked(getItem).mockResolvedValue(null);

await index.getItemAction();
Expand All @@ -61,6 +62,18 @@ describe('getItemAction', () => {
expect(core.setFailed).toHaveBeenLastCalledWith(`Item not found: ${item}`);
});

it('can ignore item not found', async () => {
mockGetInput({ owner, 'project-number': projectNumber, item });
mockGetBooleanInput({ 'fail-if-item-not-found': false });
vi.mocked(getItem).mockResolvedValue(null);

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

expect(core.setFailed).not.toHaveBeenCalled();
expect(core.setOutput).not.toHaveBeenCalled();
});

it('handles project not found', async () => {
mockGetInput({ owner, 'project-number': projectNumber, item });
vi.mocked(getItem).mockImplementation(() => {
Expand Down
1 change: 1 addition & 0 deletions delete-item/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Can not delete archived items due to a bug in the GitHub GraphQL API, see <https
| `owner` | The owner of the project - either an organization or a user. If not provided, it defaults to the repository owner. | No | `${{ github.repository_owner }}` |
| `project-number` | The project number from the project's URL. | Yes | |
| `item` | The item to delete - may be the global ID for the item, the content ID, or the content URL. | No | `${{ github.event.pull_request.html_url \|\| github.event.issue.html_url }}` |
| `fail-if-item-not-found` | Should the action fail if the item is not found on the project | No | `true` |

## Outputs

Expand Down
4 changes: 4 additions & 0 deletions delete-item/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ inputs:
description: The item to delete - may be the global ID for the item, the content ID, or the content URL
required: false
default: ${{ github.event.pull_request.html_url || github.event.issue.html_url }}
fail-if-item-not-found:
description: Should the action fail if the item is not found on the project
required: false
default: true

runs:
using: node20
Expand Down
7 changes: 4 additions & 3 deletions dist/delete-item.js

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

7 changes: 4 additions & 3 deletions dist/edit-item.js

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

7 changes: 4 additions & 3 deletions dist/get-item.js

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

1 change: 1 addition & 0 deletions edit-item/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Can not edit archived items due to a bug in the GitHub GraphQL API, see <https:/
| `body` | Body for the item - can only be set for draft issues. | No | |
| `field` | Project field to set on the item. | No | |
| `field-value` | Value to set project field to. | No | |
| `fail-if-item-not-found` | Should the action fail if the item is not found on the project | No | `true` |

## Outputs

Expand Down
4 changes: 4 additions & 0 deletions edit-item/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ inputs:
field-value:
description: Value to set project field to
required: false
fail-if-item-not-found:
description: Should the action fail if the item is not found on the project
required: false
default: true

outputs:
id:
Expand Down
1 change: 1 addition & 0 deletions get-item/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Can not get archived items due to a bug in the GitHub GraphQL API, see <https://
| `project-number` | The project number from the project's URL. | Yes | |
| `item` | The item to get - may be the global ID for the item, the content ID, or the content URL. | No | `${{ github.event.pull_request.html_url \|\| github.event.issue.html_url }}` |
| `field` | Project field to get on the item. | No | |
| `fail-if-item-not-found` | Should the action fail if the item is not found on the project | No | `true` |

## Outputs

Expand Down
4 changes: 4 additions & 0 deletions get-item/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ inputs:
field:
description: Project field to get on the item
required: false
fail-if-item-not-found:
description: Should the action fail if the item is not found on the project
required: false
default: true

outputs:
id:
Expand Down
5 changes: 4 additions & 1 deletion src/delete-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ export async function deleteItemAction(): Promise<void> {
const projectNumber = core.getInput('project-number', { required: true });
const item = core.getInput('item', { required: true });

// Optional inputs
const failIfItemNotFound = core.getBooleanInput('fail-if-item-not-found');

// Item might be the item ID, the content ID, or the content URL
const fullItem = await getItem(owner, projectNumber, item);

if (!fullItem) {
core.setFailed(`Item not found: ${item}`);
if (failIfItemNotFound) core.setFailed(`Item not found: ${item}`);
return;
}

Expand Down
3 changes: 2 additions & 1 deletion src/edit-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export async function editItemAction(): Promise<void> {
const body = core.getInput('body');
const field = core.getInput('field');
const fieldValue = core.getInput('field-value', { required: !!field });
const failIfItemNotFound = core.getBooleanInput('fail-if-item-not-found');

if (!!fieldValue && !field) {
core.setFailed('Input required and not supplied: field');
Expand All @@ -24,7 +25,7 @@ export async function editItemAction(): Promise<void> {
const fullItem = await getItem(owner, projectNumber, item);

if (!fullItem) {
core.setFailed(`Item not found: ${item}`);
if (failIfItemNotFound) core.setFailed(`Item not found: ${item}`);
return;
}

Expand Down
3 changes: 2 additions & 1 deletion src/get-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ export async function getItemAction(): Promise<void> {

// Optional inputs
const field = core.getInput('field') || undefined;
const failIfItemNotFound = core.getBooleanInput('fail-if-item-not-found');

// Item might be the item ID, the content ID, or the content URL
const fullItem = await getItem(owner, projectNumber, item, field);

if (!fullItem) {
core.setFailed(`Item not found: ${item}`);
if (failIfItemNotFound) core.setFailed(`Item not found: ${item}`);
return;
}

Expand Down

0 comments on commit 8bc0bd4

Please sign in to comment.