Skip to content

Commit 048c9e4

Browse files
committed
Fix PR context detection for issue creation
1 parent ffcec97 commit 048c9e4

File tree

2 files changed

+102
-26
lines changed

2 files changed

+102
-26
lines changed

dist/index.js

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31893,14 +31893,24 @@ async function run() {
3189331893
core.info(`🔍 Create issues enabled: ${createIssues}`);
3189431894
core.info(`🔍 Issue labels: ${issueLabels}`);
3189531895

31896-
if (createIssues && context.eventName === 'pull_request') {
31897-
core.info('\n🔗 Processing TODOs for issue creation...');
31898-
const result = await processTodosForIssues(todos, octokit, context, issueLabels);
31899-
issuesCreated = result.created;
31900-
issuesLinked = result.linked;
31901-
core.info(`📊 Issues created: ${issuesCreated}, Issues linked: ${issuesLinked}`);
31902-
} else if (createIssues) {
31903-
core.warning('⚠️ Issue creation is enabled but only works on pull_request events');
31896+
if (createIssues) {
31897+
// Check if we're in a PR context by looking for PR number in various places
31898+
const prNumber = context.payload.pull_request?.number ||
31899+
context.payload.number ||
31900+
context.payload.issue?.number;
31901+
31902+
core.info(`🔍 PR number detected: ${prNumber || 'none'}`);
31903+
31904+
if (prNumber || context.eventName === 'pull_request') {
31905+
core.info('\n🔗 Processing TODOs for issue creation...');
31906+
const result = await processTodosForIssues(todos, octokit, context, issueLabels);
31907+
issuesCreated = result.created;
31908+
issuesLinked = result.linked;
31909+
core.info(`📊 Issues created: ${issuesCreated}, Issues linked: ${issuesLinked}`);
31910+
} else {
31911+
core.warning('⚠️ Issue creation is enabled but no PR context detected. This usually means the action is running on a push event or manual trigger.');
31912+
core.info('💡 To create issues, run this action on a pull_request event or ensure it has access to PR context.');
31913+
}
3190431914
} else {
3190531915
core.info('ℹ️ Issue creation is disabled');
3190631916
}
@@ -32100,6 +32110,28 @@ async function findExistingIssue(todo, octokit, context) {
3210032110
async function createIssueForTodo(todo, octokit, context, labels) {
3210132111
const title = `TODO: ${todo.content.trim().substring(0, 50)}${todo.content.length > 50 ? '...' : ''}`;
3210232112

32113+
let contextInfo = '';
32114+
let assignees = [];
32115+
32116+
// Try to get PR number from various sources
32117+
const prNumber = context.payload.pull_request?.number ||
32118+
context.payload.number ||
32119+
context.payload.issue?.number;
32120+
32121+
if (prNumber) {
32122+
contextInfo = `This TODO was automatically detected by the TODO Creeper action in pull request #${prNumber}.`;
32123+
// Try to get the PR author
32124+
const prAuthor = context.payload.pull_request?.user?.login ||
32125+
context.payload.sender?.login;
32126+
if (prAuthor) {
32127+
assignees = [prAuthor];
32128+
}
32129+
} else if (context.eventName === 'push') {
32130+
contextInfo = `This TODO was automatically detected by the TODO Creeper action in commit ${context.sha.substring(0, 7)} on branch ${context.ref.replace('refs/heads/', '')}.`;
32131+
} else {
32132+
contextInfo = `This TODO was automatically detected by the TODO Creeper action.`;
32133+
}
32134+
3210332135
const body = `## TODO Item
3210432136

3210532137
**File:** \`${todo.file}\`
@@ -32112,7 +32144,7 @@ ${todo.content.trim()}
3211232144
\`\`\`
3211332145

3211432146
**Context:**
32115-
This TODO was automatically detected by the TODO Creeper action in pull request #${context.payload.pull_request.number}.
32147+
${contextInfo}
3211632148

3211732149
**Action Required:**
3211832150
Please review this TODO and either:
@@ -32123,14 +32155,20 @@ Please review this TODO and either:
3212332155
---
3212432156
*This issue was automatically created by [TODO Creeper](https://github.com/Gustrb/todo-creeper)*`;
3212532157

32126-
const { data: issue } = await octokit.rest.issues.create({
32158+
const issueData = {
3212732159
owner: context.repo.owner,
3212832160
repo: context.repo.repo,
3212932161
title: title,
3213032162
body: body,
32131-
labels: labels,
32132-
assignees: [context.payload.pull_request.user.login]
32133-
});
32163+
labels: labels
32164+
};
32165+
32166+
// Only add assignees if we have them
32167+
if (assignees.length > 0) {
32168+
issueData.assignees = assignees;
32169+
}
32170+
32171+
const { data: issue } = await octokit.rest.issues.create(issueData);
3213432172

3213532173
return issue;
3213632174
}

src/index.js

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,24 @@ async function run() {
5757
core.info(`🔍 Create issues enabled: ${createIssues}`);
5858
core.info(`🔍 Issue labels: ${issueLabels}`);
5959

60-
if (createIssues && context.eventName === 'pull_request') {
61-
core.info('\n🔗 Processing TODOs for issue creation...');
62-
const result = await processTodosForIssues(todos, octokit, context, issueLabels);
63-
issuesCreated = result.created;
64-
issuesLinked = result.linked;
65-
core.info(`📊 Issues created: ${issuesCreated}, Issues linked: ${issuesLinked}`);
66-
} else if (createIssues) {
67-
core.warning('⚠️ Issue creation is enabled but only works on pull_request events');
60+
if (createIssues) {
61+
// Check if we're in a PR context by looking for PR number in various places
62+
const prNumber = context.payload.pull_request?.number ||
63+
context.payload.number ||
64+
context.payload.issue?.number;
65+
66+
core.info(`🔍 PR number detected: ${prNumber || 'none'}`);
67+
68+
if (prNumber || context.eventName === 'pull_request') {
69+
core.info('\n🔗 Processing TODOs for issue creation...');
70+
const result = await processTodosForIssues(todos, octokit, context, issueLabels);
71+
issuesCreated = result.created;
72+
issuesLinked = result.linked;
73+
core.info(`📊 Issues created: ${issuesCreated}, Issues linked: ${issuesLinked}`);
74+
} else {
75+
core.warning('⚠️ Issue creation is enabled but no PR context detected. This usually means the action is running on a push event or manual trigger.');
76+
core.info('💡 To create issues, run this action on a pull_request event or ensure it has access to PR context.');
77+
}
6878
} else {
6979
core.info('ℹ️ Issue creation is disabled');
7080
}
@@ -264,6 +274,28 @@ async function findExistingIssue(todo, octokit, context) {
264274
async function createIssueForTodo(todo, octokit, context, labels) {
265275
const title = `TODO: ${todo.content.trim().substring(0, 50)}${todo.content.length > 50 ? '...' : ''}`;
266276

277+
let contextInfo = '';
278+
let assignees = [];
279+
280+
// Try to get PR number from various sources
281+
const prNumber = context.payload.pull_request?.number ||
282+
context.payload.number ||
283+
context.payload.issue?.number;
284+
285+
if (prNumber) {
286+
contextInfo = `This TODO was automatically detected by the TODO Creeper action in pull request #${prNumber}.`;
287+
// Try to get the PR author
288+
const prAuthor = context.payload.pull_request?.user?.login ||
289+
context.payload.sender?.login;
290+
if (prAuthor) {
291+
assignees = [prAuthor];
292+
}
293+
} else if (context.eventName === 'push') {
294+
contextInfo = `This TODO was automatically detected by the TODO Creeper action in commit ${context.sha.substring(0, 7)} on branch ${context.ref.replace('refs/heads/', '')}.`;
295+
} else {
296+
contextInfo = `This TODO was automatically detected by the TODO Creeper action.`;
297+
}
298+
267299
const body = `## TODO Item
268300
269301
**File:** \`${todo.file}\`
@@ -276,7 +308,7 @@ ${todo.content.trim()}
276308
\`\`\`
277309
278310
**Context:**
279-
This TODO was automatically detected by the TODO Creeper action in pull request #${context.payload.pull_request.number}.
311+
${contextInfo}
280312
281313
**Action Required:**
282314
Please review this TODO and either:
@@ -287,14 +319,20 @@ Please review this TODO and either:
287319
---
288320
*This issue was automatically created by [TODO Creeper](https://github.com/Gustrb/todo-creeper)*`;
289321

290-
const { data: issue } = await octokit.rest.issues.create({
322+
const issueData = {
291323
owner: context.repo.owner,
292324
repo: context.repo.repo,
293325
title: title,
294326
body: body,
295-
labels: labels,
296-
assignees: [context.payload.pull_request.user.login]
297-
});
327+
labels: labels
328+
};
329+
330+
// Only add assignees if we have them
331+
if (assignees.length > 0) {
332+
issueData.assignees = assignees;
333+
}
334+
335+
const { data: issue } = await octokit.rest.issues.create(issueData);
298336

299337
return issue;
300338
}

0 commit comments

Comments
 (0)