Skip to content

Commit aa49263

Browse files
Merge pull request #95 from DiogoRibeiro7/test/todo-action-issue-creation
Test/todo action issue creation
2 parents 777e071 + 942d59b commit aa49263

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/ActionMain.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,45 @@ const LABELS_BY_TAG: Record<string, string[]> = {
1111
HACK: ['technical-debt']
1212
};
1313

14+
const DEFAULT_LABEL_COLOR = 'cccccc';
15+
const LABEL_COLORS: Record<string, string> = {
16+
bug: 'd73a4a',
17+
enhancement: 'a2eeef',
18+
todo: 'ededed',
19+
'technical-debt': 'f9d0c4'
20+
};
21+
1422
function labelsFromMetadata(metadata?: Record<string, string>): string[] {
1523
if (!metadata) return [];
1624
return Object.entries(metadata).map(([key, value]) => `${key}:${value}`);
1725
}
1826

27+
async function ensureLabelExists(
28+
octokit: ReturnType<typeof github.getOctokit>,
29+
owner: string,
30+
repo: string,
31+
label: string
32+
) {
33+
try {
34+
await octokit.rest.issues.getLabel({ owner, repo, name: label });
35+
} catch (err: any) {
36+
if (err.status === 404) {
37+
const base = label.toLowerCase().split(':')[0];
38+
const color = LABEL_COLORS[base] || DEFAULT_LABEL_COLOR;
39+
await octokit.rest.issues.createLabel({
40+
owner,
41+
repo,
42+
name: label,
43+
color,
44+
description: 'Auto-created by smart-todo-action'
45+
});
46+
core.info(`🏷️ Created label "${label}"`);
47+
} else {
48+
core.warning(`⚠️ Failed to check/create label "${label}": ${err.message}`);
49+
}
50+
}
51+
}
52+
1953
async function run(): Promise<void> {
2054
try {
2155
const token = core.getInput('repo-token', { required: true });
@@ -40,6 +74,10 @@ async function run(): Promise<void> {
4074
const metaLabels = labelsFromMetadata(todo.metadata);
4175
const labels = [...baseLabels, ...metaLabels];
4276

77+
for (const label of labels) {
78+
await ensureLabelExists(octokit, owner, repo, label);
79+
}
80+
4381
try {
4482
await octokit.rest.issues.create({
4583
owner,

0 commit comments

Comments
 (0)