Skip to content

Commit

Permalink
refactor: use gh project link/unlink (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsanders11 authored Mar 19, 2024
1 parent b62ccce commit fbaf72a
Show file tree
Hide file tree
Showing 21 changed files with 175 additions and 354 deletions.
7 changes: 5 additions & 2 deletions __tests__/copy-project.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ describe('copyProjectAction', () => {
expect(copyProjectActionSpy).toHaveReturned();
expect(linkProjectToRepository).toHaveBeenCalledTimes(1);
expect(linkProjectToRepository).toHaveBeenCalledWith(
newProjectId,
newProjectNumber.toString(),
repository
);
});
Expand All @@ -198,7 +198,10 @@ describe('copyProjectAction', () => {
await index.copyProjectAction();
expect(copyProjectActionSpy).toHaveReturned();
expect(linkProjectToTeam).toHaveBeenCalledTimes(1);
expect(linkProjectToTeam).toHaveBeenCalledWith(newProjectId, team);
expect(linkProjectToTeam).toHaveBeenCalledWith(
newProjectNumber.toString(),
team
);
});

it('does not get draft issues if no template view', async () => {
Expand Down
164 changes: 42 additions & 122 deletions __tests__/lib.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1211,6 +1211,10 @@ describe('lib', () => {
const repository = 'dsanders11/project-actions';
const repositoryId = 'repository-id';

beforeEach(() => {
jest.resetAllMocks();
});

it('handles repository not found', async () => {
const mockOctokit = mockGetOctokit();
jest.mocked(mockOctokit.graphql).mockRejectedValue(
Expand All @@ -1224,7 +1228,7 @@ describe('lib', () => {
);

await expect(
lib.linkProjectToRepository(projectId, repository)
lib.linkProjectToRepository(projectNumber, repository)
).rejects.toThrow(lib.RepositoryNotFoundError);
});

Expand All @@ -1240,69 +1244,26 @@ describe('lib', () => {
jest.mocked(mockOctokit.graphql).mockRejectedValue(error);

await expect(
lib.linkProjectToRepository(projectId, repository)
).rejects.toBe(error);

jest
.mocked(mockOctokit.graphql)
.mockResolvedValueOnce({
repository: {
id: repositoryId
}
})
.mockRejectedValue(error);

await expect(
lib.linkProjectToRepository(projectId, repository)
lib.linkProjectToRepository(projectNumber, repository)
).rejects.toBe(error);
});

it('handles project not found', async () => {
const mockOctokit = mockGetOctokit();
const error = createMockGraphqlResponseError([
{
type: 'NOT_FOUND',
message: `Could not resolve to a node with the global id of '${projectId}'`,
path: ['']
}
]);
jest
.mocked(mockOctokit.graphql<lib.RepositoryIdResponse>)
.mockResolvedValueOnce({
repository: {
id: repositoryId
}
})
.mockRejectedValue(error);
});
mockProjectNotFoundError();

await expect(
lib.linkProjectToRepository(projectId, repository)
lib.linkProjectToRepository(projectNumber, repository)
).rejects.toThrow(lib.ProjectNotFoundError);
});

it('handles repository not found by ID', async () => {
const mockOctokit = mockGetOctokit();
const error = createMockGraphqlResponseError([
{
type: 'NOT_FOUND',
message: `Could not resolve to a node with the global id of '${repositoryId}'`,
path: ['']
}
]);
jest
.mocked(mockOctokit.graphql)
.mockResolvedValueOnce({
repository: {
id: repositoryId
}
})
.mockRejectedValue(error);

await expect(
lib.linkProjectToRepository(projectId, repository)
).rejects.toThrow(lib.RepositoryNotFoundError);
});

it('links repository to project', async () => {
const mockOctokit = mockGetOctokit();
jest
Expand All @@ -1313,13 +1274,13 @@ describe('lib', () => {
}
})
.mockResolvedValueOnce({});
jest.mocked(execCliCommand);

await expect(
lib.linkProjectToRepository(projectId, repository)
lib.linkProjectToRepository(projectNumber, repository)
).resolves.toEqual(repositoryId);
expect(mockOctokit.graphql).toHaveBeenLastCalledWith(
expect.stringContaining('linkProjectV2ToRepository'),
{ projectId, repositoryId }
expect(execCliCommand).toHaveBeenCalledWith(
expect.arrayContaining(['link', '--repo'])
);
});

Expand All @@ -1333,13 +1294,13 @@ describe('lib', () => {
}
})
.mockResolvedValueOnce({});
jest.mocked(execCliCommand);

await expect(
lib.linkProjectToRepository(projectId, repository, false)
lib.linkProjectToRepository(projectNumber, repository, false)
).resolves.toEqual(repositoryId);
expect(mockOctokit.graphql).toHaveBeenLastCalledWith(
expect.stringContaining('unlinkProjectV2FromRepository'),
{ projectId, repositoryId }
expect(execCliCommand).toHaveBeenCalledWith(
expect.arrayContaining(['unlink', '--repo'])
);
});
});
Expand All @@ -1348,6 +1309,10 @@ describe('lib', () => {
const team = 'foo/bar';
const teamId = 'team-id';

beforeEach(() => {
jest.resetAllMocks();
});

it('handles team not found', async () => {
const mockOctokit = mockGetOctokit();
jest.mocked(mockOctokit.graphql).mockRejectedValue(
Expand All @@ -1360,7 +1325,7 @@ describe('lib', () => {
])
);

await expect(lib.linkProjectToTeam(projectId, team)).rejects.toThrow(
await expect(lib.linkProjectToTeam(projectNumber, team)).rejects.toThrow(
lib.TeamNotFoundError
);

Expand All @@ -1370,7 +1335,7 @@ describe('lib', () => {
}
});

await expect(lib.linkProjectToTeam(projectId, team)).rejects.toThrow(
await expect(lib.linkProjectToTeam(projectNumber, team)).rejects.toThrow(
lib.TeamNotFoundError
);
});
Expand All @@ -1386,69 +1351,24 @@ describe('lib', () => {
]);
jest.mocked(mockOctokit.graphql).mockRejectedValue(error);

await expect(lib.linkProjectToTeam(projectId, team)).rejects.toBe(error);

jest
.mocked(mockOctokit.graphql)
.mockResolvedValueOnce({
organization: {
team: {
id: teamId
}
}
})
.mockRejectedValue(error);

await expect(lib.linkProjectToTeam(projectId, team)).rejects.toBe(error);
await expect(lib.linkProjectToTeam(projectNumber, team)).rejects.toBe(
error
);
});

it('handles project not found', async () => {
const mockOctokit = mockGetOctokit();
const error = createMockGraphqlResponseError([
{
type: 'NOT_FOUND',
message: `Could not resolve to a node with the global id of '${projectId}'`,
path: ['']
}
]);
jest
.mocked(mockOctokit.graphql)
.mockResolvedValueOnce({
organization: {
team: {
id: teamId
}
jest.mocked(mockOctokit.graphql).mockResolvedValueOnce({
organization: {
team: {
id: teamId
}
})
.mockRejectedValue(error);

await expect(lib.linkProjectToTeam(projectId, team)).rejects.toThrow(
lib.ProjectNotFoundError
);
});

it('handles team not found by ID', async () => {
const mockOctokit = mockGetOctokit();
const error = createMockGraphqlResponseError([
{
type: 'NOT_FOUND',
message: `Could not resolve to a node with the global id of '${teamId}'`,
path: ['']
}
]);
jest
.mocked(mockOctokit.graphql)
.mockResolvedValueOnce({
organization: {
team: {
id: teamId
}
}
})
.mockRejectedValue(error);
});
mockProjectNotFoundError();

await expect(lib.linkProjectToTeam(projectId, team)).rejects.toThrow(
lib.TeamNotFoundError
await expect(lib.linkProjectToTeam(projectNumber, team)).rejects.toThrow(
lib.ProjectNotFoundError
);
});

Expand All @@ -1464,13 +1384,13 @@ describe('lib', () => {
}
})
.mockResolvedValueOnce({});
jest.mocked(execCliCommand);

await expect(lib.linkProjectToTeam(projectId, team)).resolves.toEqual(
await expect(lib.linkProjectToTeam(projectNumber, team)).resolves.toEqual(
teamId
);
expect(mockOctokit.graphql).toHaveBeenLastCalledWith(
expect.stringContaining('linkProjectV2ToTeam'),
{ projectId, teamId }
expect(execCliCommand).toHaveBeenCalledWith(
expect.arrayContaining(['link', '--team'])
);
});

Expand All @@ -1486,13 +1406,13 @@ describe('lib', () => {
}
})
.mockResolvedValueOnce({});
jest.mocked(execCliCommand);

await expect(
lib.linkProjectToTeam(projectId, team, false)
lib.linkProjectToTeam(projectNumber, team, false)
).resolves.toEqual(teamId);
expect(mockOctokit.graphql).toHaveBeenLastCalledWith(
expect.stringContaining('unlinkProjectV2FromTeam'),
{ projectId, teamId }
expect(execCliCommand).toHaveBeenCalledWith(
expect.arrayContaining(['unlink', '--team'])
);
});
});
Expand Down
14 changes: 11 additions & 3 deletions __tests__/link-project.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ describe('linkProjectAction', () => {

expect(linkProjectToRepository).toHaveBeenCalledTimes(1);
expect(linkProjectToRepository).toHaveBeenLastCalledWith(
projectId,
projectNumber,
repository,
true
);
Expand All @@ -131,7 +131,11 @@ describe('linkProjectAction', () => {
expect(linkProjectActionSpy).toHaveReturned();

expect(linkProjectToTeam).toHaveBeenCalledTimes(1);
expect(linkProjectToTeam).toHaveBeenLastCalledWith(projectId, team, true);
expect(linkProjectToTeam).toHaveBeenLastCalledWith(
projectNumber,
team,
true
);
expect(core.setOutput).toHaveBeenCalledTimes(2);
expect(core.setOutput).toHaveBeenCalledWith('team-id', teamId);
expect(core.setOutput).toHaveBeenCalledWith('project-id', projectId);
Expand All @@ -149,7 +153,11 @@ describe('linkProjectAction', () => {
expect(linkProjectActionSpy).toHaveReturned();

expect(linkProjectToTeam).toHaveBeenCalledTimes(1);
expect(linkProjectToTeam).toHaveBeenLastCalledWith(projectId, team, false);
expect(linkProjectToTeam).toHaveBeenLastCalledWith(
projectNumber,
team,
false
);
expect(core.setOutput).toHaveBeenCalledTimes(2);
expect(core.setOutput).toHaveBeenCalledWith('team-id', teamId);
expect(core.setOutput).toHaveBeenCalledWith('project-id', projectId);
Expand Down
2 changes: 1 addition & 1 deletion dist/add-item.js

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

2 changes: 1 addition & 1 deletion dist/archive-item.js

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

2 changes: 1 addition & 1 deletion dist/close-project.js

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

2 changes: 1 addition & 1 deletion dist/completed-by.js

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

Loading

0 comments on commit fbaf72a

Please sign in to comment.