Skip to content

Commit

Permalink
fix: wait for draft issues to populate in copy project (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsanders11 authored Mar 5, 2025
1 parent b48ede6 commit 7fc3c54
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 3 deletions.
19 changes: 18 additions & 1 deletion __tests__/copy-project.test.mts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import {
editItem,
editProject,
getDraftIssues,
getProject,
linkProjectToRepository,
linkProjectToTeam
linkProjectToTeam,
ProjectDetails
} from '../src/lib.js';
import { mockGetBooleanInput, mockGetInput } from './utils.js';

Expand Down Expand Up @@ -231,6 +233,11 @@ describe('copyProjectAction', () => {
'template-view': templateView
});
mockGetBooleanInput({ drafts: true });
vi.mocked(getProject).mockResolvedValue({
items: {
totalCount: 1
}
} as ProjectDetails);
vi.mocked(getDraftIssues).mockResolvedValue([
{
id: 'item-id',
Expand Down Expand Up @@ -264,6 +271,11 @@ describe('copyProjectAction', () => {
'template-view': templateView
});
mockGetBooleanInput({ drafts: true });
vi.mocked(getProject).mockResolvedValue({
items: {
totalCount: 1
}
} as ProjectDetails);
vi.mocked(getDraftIssues).mockResolvedValue([
{
id: itemId,
Expand Down Expand Up @@ -302,6 +314,11 @@ describe('copyProjectAction', () => {
'template-view': templateView
});
mockGetBooleanInput({ drafts: true });
vi.mocked(getProject).mockResolvedValue({
items: {
totalCount: 1
}
} as ProjectDetails);
vi.mocked(getDraftIssues).mockResolvedValue([
{
id: itemId,
Expand Down
33 changes: 32 additions & 1 deletion dist/copy-project.js

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

27 changes: 26 additions & 1 deletion src/copy-project.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { setTimeout } from 'node:timers/promises';

import * as core from '@actions/core';

import {
copyProject,
DraftIssueItem,
editItem,
editProject,
getDraftIssues,
getProject,
linkProjectToRepository,
linkProjectToTeam
} from './lib';
Expand Down Expand Up @@ -69,7 +73,28 @@ export async function copyProjectAction(): Promise<void> {

// Do template interpolation on draft issues
if (templateView) {
const draftIssues = await getDraftIssues(project.id);
// HACK - It seems that GitHub now populates the draft issues
// on the new project in an async manner so they may not all
// be available yet. Wait until the number of draft issues
// matches the number of draft issues in the source project.
const {
items: { totalCount: draftIssueCount }
} = await getProject(owner, projectNumber);

let draftIssues: DraftIssueItem[] = [];

for (let i = 0; i < 10; i++) {
await setTimeout(1000);
draftIssues = await getDraftIssues(project.id);
if (draftIssues.length === draftIssueCount) break;
}

// Log an error but continue as partial success is better than failure
if (draftIssues.length !== draftIssueCount) {
core.error(
`Not all draft issues available for interpolation, expected ${draftIssueCount} but got ${draftIssues.length}`
);
}

for (const draftIssue of draftIssues) {
try {
Expand Down

0 comments on commit 7fc3c54

Please sign in to comment.