Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle redacted items on project #46

Merged
merged 1 commit into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions __tests__/completed-by.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,11 @@ describe('completedByAction', () => {
{
id: itemId,
content: {
__typename: 'DraftIssue',
id: 'content-id',
body: 'This is the item body',
title: 'Item Title'
}
},
type: 'DRAFT_ISSUE'
}
]);

Expand All @@ -168,11 +168,11 @@ describe('completedByAction', () => {
{
id: itemId,
content: {
__typename: 'DraftIssue',
id: 'content-id',
body: `Completed by ${itemUrl}`,
title: 'Item Title'
}
},
type: 'DRAFT_ISSUE'
}
]);
vi.mocked(getPullRequestState).mockResolvedValue('MERGED');
Expand Down Expand Up @@ -204,11 +204,11 @@ describe('completedByAction', () => {
{
id: itemId,
content: {
__typename: 'DraftIssue',
id: 'content-id',
body: `Completed by ${itemUrl}`,
title: 'Item Title'
}
},
type: 'DRAFT_ISSUE'
}
]);
vi.mocked(getPullRequestState).mockResolvedValue('OPEN');
Expand Down Expand Up @@ -238,14 +238,14 @@ describe('completedByAction', () => {
{
id: itemId,
content: {
__typename: 'DraftIssue',
id: 'content-id',
body: `
Completed by ${itemUrl1}
Completed by ${itemUrl2}
`,
title: 'Item Title'
}
},
type: 'DRAFT_ISSUE'
}
]);
vi.mocked(getPullRequestState)
Expand Down Expand Up @@ -278,11 +278,11 @@ describe('completedByAction', () => {
{
id: itemId,
content: {
__typename: 'DraftIssue',
id: 'content-id',
body: `Completed by ${itemUrl}`,
title: 'Item Title'
}
},
type: 'DRAFT_ISSUE'
}
]);
vi.mocked(getPullRequestState).mockRejectedValue(new Error(errorMessage));
Expand Down
16 changes: 8 additions & 8 deletions __tests__/copy-project.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,11 @@ describe('copyProjectAction', () => {
{
id: 'item-id',
content: {
__typename: 'DraftIssue',
id: 'content-id',
body: 'This is the item body',
title: 'Item Title'
}
},
type: 'DRAFT_ISSUE'
}
]);

Expand Down Expand Up @@ -268,11 +268,11 @@ describe('copyProjectAction', () => {
{
id: itemId,
content: {
__typename: 'DraftIssue',
id: contentId,
body: 'This is the item {{ foo }}',
title: 'Item {{ foo }}'
}
},
type: 'DRAFT_ISSUE'
}
]);

Expand Down Expand Up @@ -306,15 +306,15 @@ describe('copyProjectAction', () => {
{
id: itemId,
content: {
__typename: 'DraftIssue',
id: 'content-id',
body: `<!-- fields
{
"${field}": "${fieldValue}"
}
-->`,
title: 'Item Title'
}
},
type: 'DRAFT_ISSUE'
}
]);

Expand Down Expand Up @@ -348,11 +348,11 @@ describe('copyProjectAction', () => {
{
id: itemId,
content: {
__typename: 'DraftIssue',
id: contentId,
body: 'This is the item {{ foo }}',
title: 'Item {{ foo }}'
}
},
type: 'DRAFT_ISSUE'
}
]);
vi.mocked(editItem).mockImplementation(() => {
Expand Down
11 changes: 11 additions & 0 deletions __tests__/delete-project.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,15 @@ describe('deleteProjectAction', () => {
expect(core.setFailed).toHaveBeenCalledTimes(1);
expect(core.setFailed).toHaveBeenLastCalledWith('42');
});

it('passes inputs correctly', async () => {
mockGetInput({ owner, 'project-number': projectNumber });
vi.mocked(deleteProject).mockResolvedValue();

await index.deleteProjectAction();
expect(deleteProjectActionSpy).toHaveReturned();

expect(deleteProject).toHaveBeenCalledTimes(1);
expect(deleteProject).toHaveBeenLastCalledWith(owner, projectNumber);
});
});
30 changes: 25 additions & 5 deletions __tests__/edit-item.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ describe('editItemAction', () => {
mockGetInput({ owner, 'project-number': projectNumber, item });
vi.mocked(getItem).mockResolvedValue({
id: itemId,
content: { type: 'PullRequest' }
type: 'PULL_REQUEST'
} as ItemDetails);
vi.mocked(editItem).mockImplementation(() => {
throw new ProjectNotFoundError();
Expand Down Expand Up @@ -151,6 +151,26 @@ describe('editItemAction', () => {
expect(core.setFailed).toHaveBeenLastCalledWith('42');
});

it('cannot edit redacted items', async () => {
mockGetInput({
owner,
'project-number': projectNumber,
item,
title: 'New Title'
});
vi.mocked(getItem).mockResolvedValue({
type: 'REDACTED'
} as ItemDetails);

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

expect(core.setFailed).toHaveBeenCalledTimes(1);
expect(core.setFailed).toHaveBeenLastCalledWith(
'Cannot edit redacted items'
);
});

it('can only set title/body for draft issues', async () => {
mockGetInput({
owner,
Expand All @@ -159,7 +179,7 @@ describe('editItemAction', () => {
title: 'New Title'
});
vi.mocked(getItem).mockResolvedValue({
content: { type: 'PullRequest' }
type: 'PULL_REQUEST'
} as ItemDetails);

await index.editItemAction();
Expand All @@ -183,7 +203,7 @@ describe('editItemAction', () => {
});
vi.mocked(getItem).mockResolvedValue({
id: itemId,
content: { type: 'PullRequest' },
type: 'PULL_REQUEST',
projectId
} as ItemDetails);
vi.mocked(editItem).mockResolvedValue(itemId);
Expand All @@ -209,7 +229,7 @@ describe('editItemAction', () => {
});
vi.mocked(getItem).mockResolvedValue({
id: itemId,
content: { type: 'DraftIssue' },
type: 'DRAFT_ISSUE',
projectId
} as ItemDetails);
vi.mocked(editItem).mockResolvedValue(itemId);
Expand All @@ -224,7 +244,7 @@ describe('editItemAction', () => {
mockGetInput({ owner, 'project-number': projectNumber, item });
vi.mocked(getItem).mockResolvedValue({
id: itemId,
content: { type: 'PullRequest' },
type: 'PULL_REQUEST',
projectId
} as ItemDetails);
vi.mocked(editItem).mockResolvedValue(itemId);
Expand Down
35 changes: 31 additions & 4 deletions __tests__/get-item.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,10 @@ describe('getItemAction', () => {
});
vi.mocked(getItem).mockResolvedValue({
id: itemId,
content: { id: contentId, type: 'PullRequest', url, title, body },
content: { id: contentId, url, title, body },
field: { id: fieldId, value: fieldValue },
projectId
projectId,
type: 'PULL_REQUEST'
});

await index.getItemAction();
Expand Down Expand Up @@ -161,9 +162,10 @@ describe('getItemAction', () => {
});
vi.mocked(getItem).mockResolvedValue({
id: itemId,
content: { id: contentId, type: 'PullRequest', url, title, body },
content: { id: contentId, url, title, body },
field: { id: fieldId, value: null },
projectId
projectId,
type: 'PULL_REQUEST'
});

await index.getItemAction();
Expand All @@ -178,4 +180,29 @@ describe('getItemAction', () => {
expect(core.setOutput).toHaveBeenCalledWith('project-id', projectId);
expect(core.setOutput).toHaveBeenCalledWith('field-id', fieldId);
});

it('handles redacted items', async () => {
mockGetInput({
owner,
'project-number': projectNumber,
item,
field: 'Status'
});
vi.mocked(getItem).mockResolvedValue({
id: itemId,
content: null,
projectId,
type: 'REDACTED'
});

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

expect(core.setOutput).toHaveBeenCalledTimes(5);
expect(core.setOutput).toHaveBeenCalledWith('id', itemId);
expect(core.setOutput).toHaveBeenCalledWith('body', null);
expect(core.setOutput).toHaveBeenCalledWith('title', null);
expect(core.setOutput).toHaveBeenCalledWith('content-id', null);
expect(core.setOutput).toHaveBeenCalledWith('project-id', projectId);
});
});
25 changes: 25 additions & 0 deletions __tests__/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import type { Mock } from 'vitest';

import * as core from '@actions/core';
import * as exec from '@actions/exec';
import * as tc from '@actions/tool-cache';
import { Octokit } from '@octokit/core';
Expand Down Expand Up @@ -108,6 +109,30 @@ describe('helpers', () => {
);
});

it('sets GH_DEBUG if core.isDebug() is true', async () => {
const args = ['project', 'view'];
const stdout = 'output';
const token = 'gh-token';
mockGetInput({ token });
vi.mocked(core.isDebug).mockReturnValue(true);
vi.mocked(exec.getExecOutput).mockResolvedValue({
exitCode: 0,
stdout,
stderr: ''
});
await expect(helpers.execCliCommand(args)).resolves.toEqual(stdout);
expect(exec.getExecOutput).toHaveBeenCalledWith(
expect.anything(),
args,
expect.objectContaining({
env: {
GH_DEBUG: 'api',
GH_TOKEN: token
}
})
);
});

it('throws error on non-zero exit code', async () => {
const args = ['project', 'view'];
const stdout = 'output';
Expand Down
Loading
Loading