Skip to content

Commit 7fc3c54

Browse files
authored
fix: wait for draft issues to populate in copy project (#53)
1 parent b48ede6 commit 7fc3c54

File tree

3 files changed

+76
-3
lines changed

3 files changed

+76
-3
lines changed

__tests__/copy-project.test.mts

+18-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ import {
88
editItem,
99
editProject,
1010
getDraftIssues,
11+
getProject,
1112
linkProjectToRepository,
12-
linkProjectToTeam
13+
linkProjectToTeam,
14+
ProjectDetails
1315
} from '../src/lib.js';
1416
import { mockGetBooleanInput, mockGetInput } from './utils.js';
1517

@@ -231,6 +233,11 @@ describe('copyProjectAction', () => {
231233
'template-view': templateView
232234
});
233235
mockGetBooleanInput({ drafts: true });
236+
vi.mocked(getProject).mockResolvedValue({
237+
items: {
238+
totalCount: 1
239+
}
240+
} as ProjectDetails);
234241
vi.mocked(getDraftIssues).mockResolvedValue([
235242
{
236243
id: 'item-id',
@@ -264,6 +271,11 @@ describe('copyProjectAction', () => {
264271
'template-view': templateView
265272
});
266273
mockGetBooleanInput({ drafts: true });
274+
vi.mocked(getProject).mockResolvedValue({
275+
items: {
276+
totalCount: 1
277+
}
278+
} as ProjectDetails);
267279
vi.mocked(getDraftIssues).mockResolvedValue([
268280
{
269281
id: itemId,
@@ -302,6 +314,11 @@ describe('copyProjectAction', () => {
302314
'template-view': templateView
303315
});
304316
mockGetBooleanInput({ drafts: true });
317+
vi.mocked(getProject).mockResolvedValue({
318+
items: {
319+
totalCount: 1
320+
}
321+
} as ProjectDetails);
305322
vi.mocked(getDraftIssues).mockResolvedValue([
306323
{
307324
id: itemId,

dist/copy-project.js

+32-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/copy-project.ts

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
import { setTimeout } from 'node:timers/promises';
2+
13
import * as core from '@actions/core';
24

35
import {
46
copyProject,
7+
DraftIssueItem,
58
editItem,
69
editProject,
710
getDraftIssues,
11+
getProject,
812
linkProjectToRepository,
913
linkProjectToTeam
1014
} from './lib';
@@ -69,7 +73,28 @@ export async function copyProjectAction(): Promise<void> {
6973

7074
// Do template interpolation on draft issues
7175
if (templateView) {
72-
const draftIssues = await getDraftIssues(project.id);
76+
// HACK - It seems that GitHub now populates the draft issues
77+
// on the new project in an async manner so they may not all
78+
// be available yet. Wait until the number of draft issues
79+
// matches the number of draft issues in the source project.
80+
const {
81+
items: { totalCount: draftIssueCount }
82+
} = await getProject(owner, projectNumber);
83+
84+
let draftIssues: DraftIssueItem[] = [];
85+
86+
for (let i = 0; i < 10; i++) {
87+
await setTimeout(1000);
88+
draftIssues = await getDraftIssues(project.id);
89+
if (draftIssues.length === draftIssueCount) break;
90+
}
91+
92+
// Log an error but continue as partial success is better than failure
93+
if (draftIssues.length !== draftIssueCount) {
94+
core.error(
95+
`Not all draft issues available for interpolation, expected ${draftIssueCount} but got ${draftIssues.length}`
96+
);
97+
}
7398

7499
for (const draftIssue of draftIssues) {
75100
try {

0 commit comments

Comments
 (0)