@@ -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+
1422function 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+
1953async 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