@@ -35,141 +35,192 @@ const SEMANTIC_TYPE_TO_LABEL = {
3535function extractSemanticType ( message ) {
3636 if ( ! message ) return null ;
3737
38+ core . debug ( `Attempting to extract semantic type from: "${ message } "` ) ;
39+
3840 // Match standard semantic commit format: type(scope): message
3941 const semanticRegex = / ^ ( \w + ) (?: \( [ \w - ] + \) ) ? : \s / ;
4042 const match = message . match ( semanticRegex ) ;
4143
4244 if ( match && match [ 1 ] ) {
43- return match [ 1 ] . toLowerCase ( ) ;
45+ const type = match [ 1 ] . toLowerCase ( ) ;
46+ core . debug ( `Extracted semantic type: "${ type } "` ) ;
47+ return type ;
4448 }
4549
50+ core . debug ( "No semantic type found in message" ) ;
4651 return null ;
4752}
4853
4954async function run ( ) {
5055 try {
5156 // get inputs
5257 const token = core . getInput ( "token" , { required : true } ) ;
58+ core . debug ( "Token retrieved successfully" ) ;
5359
5460 // set up github client
5561 const octokit = github . getOctokit ( token ) ;
62+ core . debug ( "GitHub client initialized" ) ;
5663
5764 // Track if we added a label based on semantic commit
5865 let addedSemanticLabel = false ;
5966
6067 // fetch the list of labels
68+ core . debug ( "Fetching current PR labels..." ) ;
6169 const labels = (
6270 await octokit . rest . issues . listLabelsOnIssue ( {
6371 ...github . context . repo ,
6472 issue_number : github . context . issue . number ,
6573 } )
6674 ) . data . map ( ( label ) => label . name ) ;
67- core . debug ( `Found labels: ${ labels . join ( ", " ) } ` ) ;
75+ core . debug ( `Found ${ labels . length } labels: ${ labels . join ( ", " ) } ` ) ;
6876
6977 // Get PR details to check for semantic commit messages
7078 const prNumber = github . context . issue . number ;
79+ core . debug ( `Processing PR #${ prNumber } ` ) ;
80+
81+ core . debug ( "Fetching PR details..." ) ;
7182 const { data : pullRequest } = await octokit . rest . pulls . get ( {
7283 ...github . context . repo ,
7384 pull_number : prNumber ,
7485 } ) ;
7586
7687 // Get the PR title and HEAD commit message
7788 const prTitle = pullRequest . title ;
89+ core . debug ( `PR title: "${ prTitle } "` ) ;
7890
7991 // Get the HEAD commit message
92+ core . debug ( "Fetching PR commits..." ) ;
8093 const { data : commits } = await octokit . rest . pulls . listCommits ( {
8194 ...github . context . repo ,
8295 pull_number : prNumber ,
8396 } ) ;
8497
98+ core . debug ( `Found ${ commits . length } commits in PR` ) ;
8599 const headCommitMessage = commits . length > 0 ? commits [ commits . length - 1 ] . commit . message : null ;
100+ if ( headCommitMessage ) {
101+ core . debug ( `HEAD commit message: "${ headCommitMessage } "` ) ;
102+ } else {
103+ core . debug ( "No HEAD commit message found" ) ;
104+ }
86105
87106 // Try to extract semantic type from PR title or HEAD commit
107+ core . debug ( "Extracting semantic type from PR title..." ) ;
88108 const prTitleType = extractSemanticType ( prTitle ) ;
109+
110+ core . debug ( "Extracting semantic type from HEAD commit..." ) ;
89111 const commitType = extractSemanticType ( headCommitMessage ) ;
90112
91113 // Use PR title type first, then fall back to commit type
92114 const semanticType = prTitleType || commitType ;
115+ if ( semanticType ) {
116+ core . debug ( `Using semantic type: "${ semanticType } "` ) ;
117+ } else {
118+ core . debug ( "No semantic type found in PR title or HEAD commit" ) ;
119+ }
93120
94121 // If we found a semantic type that maps to one of our labels, add it if not present
95122 if ( semanticType && SEMANTIC_TYPE_TO_LABEL [ semanticType ] ) {
96123 const labelToAdd = SEMANTIC_TYPE_TO_LABEL [ semanticType ] ;
124+ core . debug ( `Semantic type "${ semanticType } " maps to label "${ labelToAdd } "` ) ;
97125
98126 // Only add the label if it's not already present
99127 if ( ! labels . includes ( labelToAdd ) ) {
100128 core . info ( `Adding label ${ labelToAdd } based on semantic commit type: ${ semanticType } ` ) ;
129+
130+ core . debug ( "Calling GitHub API to add label..." ) ;
101131 await octokit . rest . issues . addLabels ( {
102132 ...github . context . repo ,
103133 issue_number : prNumber ,
104134 labels : [ labelToAdd ] ,
105135 } ) ;
136+ core . debug ( "Label added successfully via API" ) ;
106137
107138 // Update our local labels array to include the new label
108139 labels . push ( labelToAdd ) ;
109140 addedSemanticLabel = true ;
141+ core . debug ( `Updated local labels array: ${ labels . join ( ", " ) } ` ) ;
110142
111143 // If we just added a label, give it time to apply
112144 if ( addedSemanticLabel ) {
113145 core . info ( "Added label based on semantic commit message. Waiting for label to apply..." ) ;
114146 // Short delay to allow the label to be properly registered
147+ core . debug ( "Waiting 2 seconds for label to propagate..." ) ;
115148 await new Promise ( resolve => setTimeout ( resolve , 2000 ) ) ;
149+ core . debug ( "Wait completed" ) ;
116150
117151 // Refetch the labels to ensure we have the most up-to-date set
118152 core . info ( "Refetching labels after adding semantic label..." ) ;
153+ core . debug ( "Calling GitHub API to get updated labels..." ) ;
119154 const updatedLabelsResponse = await octokit . rest . issues . listLabelsOnIssue ( {
120155 ...github . context . repo ,
121156 issue_number : github . context . issue . number ,
122157 } ) ;
123158
124159 // Update our labels array with the freshly fetched labels
125160 const updatedLabels = updatedLabelsResponse . data . map ( ( label ) => label . name ) ;
126- core . debug ( `Updated labels after adding semantic label : ${ updatedLabels . join ( ", " ) } ` ) ;
161+ core . debug ( `Refetched ${ updatedLabels . length } labels : ${ updatedLabels . join ( ", " ) } ` ) ;
127162
128163 // Replace our labels array with the updated one
129164 labels . length = 0 ;
130165 updatedLabels . forEach ( label => labels . push ( label ) ) ;
166+ core . debug ( `Updated local labels array after refetch: ${ labels . join ( ", " ) } ` ) ;
131167 }
168+ } else {
169+ core . debug ( `Label "${ labelToAdd } " already exists on PR, no need to add it` ) ;
132170 }
171+ } else if ( semanticType ) {
172+ core . debug ( `Semantic type "${ semanticType } " does not map to any of our labels` ) ;
133173 }
134174
135175 // ensure exactly one primary label is set
176+ core . debug ( "Checking for primary labels..." ) ;
136177 const primaryLabels = PRIMARY_LABELS . filter ( ( label ) =>
137178 labels . includes ( label )
138179 ) ;
139- core . debug ( `Found primary labels: ${ primaryLabels . join ( ", " ) } ` ) ;
180+ core . debug ( `Found ${ primaryLabels . length } primary labels: ${ primaryLabels . join ( ", " ) } ` ) ;
140181
141182 if ( primaryLabels . length !== 1 ) {
183+ core . debug ( `Primary label check failed: found ${ primaryLabels . length } primary labels` ) ;
142184 throw new Error (
143185 `Exactly one primary label must be set from [${ PRIMARY_LABELS . join ( ", " ) } ]. Found: ${ primaryLabels . join ( ", " ) } `
144186 ) ;
145187 }
188+ core . debug ( "Primary label check passed" ) ;
146189
147190 // if the primary label is a bug, ensure a bug label is set
148191 if ( primaryLabels [ 0 ] === "type::bug" ) {
192+ core . debug ( "Primary label is type::bug, checking for bug labels..." ) ;
149193 const bugLabels = BUG_LABELS . filter ( ( label ) => labels . includes ( label ) ) ;
150- core . debug ( `type::bug is set, found bug labels: ${ bugLabels . join ( ", " ) } ` ) ;
194+ core . debug ( `Found ${ bugLabels . length } bug labels: ${ bugLabels . join ( ", " ) } ` ) ;
151195 if ( bugLabels . length !== 1 ) {
196+ core . debug ( `Bug label check failed: found ${ bugLabels . length } bug labels` ) ;
152197 throw new Error (
153198 `Exactly one bug label must be set for primary type::bug. Found: ${ bugLabels . join (
154199 ", "
155200 ) } `
156201 ) ;
157202 }
203+ core . debug ( "Bug label check passed" ) ;
158204 }
159205
160206 // ensure no more than one severity label is set
207+ core . debug ( "Checking for severity labels..." ) ;
161208 const severityLabels = SEVERITY_LABELS . filter ( ( label ) =>
162209 labels . includes ( label )
163210 ) ;
164- core . debug ( `Found severity labels: ${ severityLabels . join ( ", " ) } ` ) ;
211+ core . debug ( `Found ${ severityLabels . length } severity labels: ${ severityLabels . join ( ", " ) } ` ) ;
165212 if ( severityLabels . length > 1 ) {
213+ core . debug ( `Severity label check failed: found ${ severityLabels . length } severity labels` ) ;
166214 throw new Error (
167215 `No more than one severity label may be set. Found: ${ severityLabels . join (
168216 ", "
169217 ) } `
170218 ) ;
171219 }
220+ core . debug ( "Severity label check passed" ) ;
221+
172222 } catch ( error ) {
223+ core . debug ( `Error caught: ${ error . message } ` ) ;
173224 if ( error instanceof Error ) core . setFailed ( error . message ) ;
174225 }
175226}
0 commit comments