-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathissue-creator.ts
More file actions
170 lines (146 loc) · 4.31 KB
/
issue-creator.ts
File metadata and controls
170 lines (146 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import * as core from '@actions/core';
import * as github from '@actions/github';
import { RenderedIssue } from './template-renderer';
export interface IssueOptions {
labels: string[];
assignees: string[];
milestone?: number;
}
export interface CreatedIssue {
number: number;
url: string;
title: string;
}
export interface IssueCreationResult {
success: boolean;
issue?: CreatedIssue;
error?: string;
filePath: string;
}
/**
* Creates GitHub issues for the rendered issues
*/
export async function createIssues(
token: string,
issues: Array<{ rendered: RenderedIssue; filePath: string }>,
options: IssueOptions,
dryRun: boolean
): Promise<IssueCreationResult[]> {
const results: IssueCreationResult[] = [];
if (dryRun) {
core.info('🔍 Dry run mode - no issues will be created');
for (const { rendered, filePath } of issues) {
core.info(`\n📄 Would create issue for: ${filePath}`);
core.info(` Title: ${rendered.title}`);
core.info(` Labels: ${options.labels.join(', ')}`);
if (options.assignees.length > 0) {
core.info(` Assignees: ${options.assignees.join(', ')}`);
}
if (options.milestone) {
core.info(` Milestone: ${options.milestone}`);
}
// Avoid logging body preview to reduce risk of exposing sensitive content
results.push({
success: true,
filePath,
issue: {
number: 0,
url: '(dry run)',
title: rendered.title,
},
});
}
return results;
}
const octokit = github.getOctokit(token);
const { owner, repo } = github.context.repo;
for (const { rendered, filePath } of issues) {
try {
core.info(`Creating issue for: ${filePath}`);
const createParams: Parameters<typeof octokit.rest.issues.create>[0] = {
owner,
repo,
title: rendered.title,
body: rendered.body,
labels: options.labels,
};
// Add assignees if provided
if (options.assignees.length > 0) {
createParams.assignees = options.assignees;
}
// Add milestone if provided
if (options.milestone) {
createParams.milestone = options.milestone;
}
const response = await octokit.rest.issues.create(createParams);
const createdIssue: CreatedIssue = {
number: response.data.number,
url: response.data.html_url,
title: response.data.title,
};
core.info(`✅ Created issue #${createdIssue.number}: ${createdIssue.url}`);
results.push({
success: true,
issue: createdIssue,
filePath,
});
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
core.warning(`❌ Failed to create issue for ${filePath}: ${errorMessage}`);
results.push({
success: false,
error: errorMessage,
filePath,
});
}
}
return results;
}
/**
* Resolves a milestone name to its number
*/
export async function resolveMilestone(
token: string,
milestoneInput: string
): Promise<number | undefined> {
if (!milestoneInput) {
return undefined;
}
// If it's already a number, use it directly
const milestoneNumber = parseInt(milestoneInput, 10);
if (!isNaN(milestoneNumber)) {
return milestoneNumber;
}
// Otherwise, look up by name
try {
const octokit = github.getOctokit(token);
const { owner, repo } = github.context.repo;
const { data: milestones } = await octokit.rest.issues.listMilestones({
owner,
repo,
state: 'open',
});
const milestone = milestones.find(
m => m.title.toLowerCase() === milestoneInput.toLowerCase()
);
if (milestone) {
core.debug(`Resolved milestone "${milestoneInput}" to number ${milestone.number}`);
return milestone.number;
} else {
core.warning(`Milestone "${milestoneInput}" not found`);
return undefined;
}
} catch (error) {
core.warning(`Failed to resolve milestone: ${error}`);
return undefined;
}
}
/**
* Parses comma-separated list into array, trimming whitespace
*/
export function parseCommaSeparatedList(input: string | undefined): string[] {
if (!input || input.trim() === '') {
return [];
}
return input.split(',').map(item => item.trim()).filter(item => item.length > 0);
}