@@ -74,102 +74,107 @@ async function run() {
7474 ) . data . map ( ( label ) => label . name ) ;
7575 core . debug ( `Found ${ labels . length } labels: ${ labels . join ( ", " ) } ` ) ;
7676
77- // Get PR details to check for semantic commit messages
78- const prNumber = github . context . issue . number ;
79- core . debug ( `Processing PR #${ prNumber } ` ) ;
80-
81- core . debug ( "Fetching PR details..." ) ;
82- const { data : pullRequest } = await octokit . rest . pulls . get ( {
83- ...github . context . repo ,
84- pull_number : prNumber ,
85- } ) ;
86-
87- // Get the PR title and HEAD commit message
88- const prTitle = pullRequest . title ;
89- core . debug ( `PR title: "${ prTitle } "` ) ;
90-
91- // Get the HEAD commit message
92- core . debug ( "Fetching PR commits..." ) ;
93- const { data : commits } = await octokit . rest . pulls . listCommits ( {
94- ...github . context . repo ,
95- pull_number : prNumber ,
96- } ) ;
97-
98- core . debug ( `Found ${ commits . length } commits in PR` ) ;
99- const headCommitMessage = commits . length > 0 ? commits [ commits . length - 1 ] . commit . message : null ;
100- if ( headCommitMessage ) {
101- core . debug ( `HEAD commit message: "${ headCommitMessage } "` ) ;
77+ // If labels already exist, skip adding new labels
78+ if ( labels . length > 0 ) {
79+ core . info ( "Labels already exist on PR, skipping adding new labels" ) ;
10280 } else {
103- core . debug ( "No HEAD commit message found" ) ;
104- }
81+ // Get PR details to check for semantic commit messages
82+ const prNumber = github . context . issue . number ;
83+ core . debug ( `Processing PR #${ prNumber } ` ) ;
10584
106- // Try to extract semantic type from PR title or HEAD commit
107- core . debug ( "Extracting semantic type from PR title..." ) ;
108- const prTitleType = extractSemanticType ( prTitle ) ;
85+ core . debug ( "Fetching PR details..." ) ;
86+ const { data : pullRequest } = await octokit . rest . pulls . get ( {
87+ ...github . context . repo ,
88+ pull_number : prNumber ,
89+ } ) ;
10990
110- core . debug ( "Extracting semantic type from HEAD commit..." ) ;
111- const commitType = extractSemanticType ( headCommitMessage ) ;
91+ // Get the PR title and HEAD commit message
92+ const prTitle = pullRequest . title ;
93+ core . debug ( `PR title: "${ prTitle } "` ) ;
11294
113- // Use PR title type first, then fall back to commit type
114- 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- }
95+ // Get the HEAD commit message
96+ core . debug ( "Fetching PR commits..." ) ;
97+ const { data : commits } = await octokit . rest . pulls . listCommits ( {
98+ ...github . context . repo ,
99+ pull_number : prNumber ,
100+ } ) ;
120101
121- // If we found a semantic type that maps to one of our labels, add it if not present
122- if ( semanticType && SEMANTIC_TYPE_TO_LABEL [ semanticType ] ) {
123- const labelToAdd = SEMANTIC_TYPE_TO_LABEL [ semanticType ] ;
124- core . debug ( `Semantic type "${ semanticType } " maps to label "${ labelToAdd } "` ) ;
125-
126- // Only add the label if it's not already present
127- if ( ! labels . includes ( labelToAdd ) ) {
128- core . info ( `Adding label ${ labelToAdd } based on semantic commit type: ${ semanticType } ` ) ;
129-
130- core . debug ( "Calling GitHub API to add label..." ) ;
131- await octokit . rest . issues . addLabels ( {
132- ...github . context . repo ,
133- issue_number : prNumber ,
134- labels : [ labelToAdd ] ,
135- } ) ;
136- core . debug ( "Label added successfully via API" ) ;
137-
138- // Update our local labels array to include the new label
139- labels . push ( labelToAdd ) ;
140- addedSemanticLabel = true ;
141- core . debug ( `Updated local labels array: ${ labels . join ( ", " ) } ` ) ;
142-
143- // If we just added a label, give it time to apply
144- if ( addedSemanticLabel ) {
145- core . info ( "Added label based on semantic commit message. Waiting for label to apply..." ) ;
146- // Short delay to allow the label to be properly registered
147- core . debug ( "Waiting 2 seconds for label to propagate..." ) ;
148- await new Promise ( resolve => setTimeout ( resolve , 2000 ) ) ;
149- core . debug ( "Wait completed" ) ;
150-
151- // Refetch the labels to ensure we have the most up-to-date set
152- core . info ( "Refetching labels after adding semantic label..." ) ;
153- core . debug ( "Calling GitHub API to get updated labels..." ) ;
154- const updatedLabelsResponse = await octokit . rest . issues . listLabelsOnIssue ( {
155- ...github . context . repo ,
156- issue_number : github . context . issue . number ,
157- } ) ;
102+ core . debug ( `Found ${ commits . length } commits in PR` ) ;
103+ const headCommitMessage = commits . length > 0 ? commits [ commits . length - 1 ] . commit . message : null ;
104+ if ( headCommitMessage ) {
105+ core . debug ( `HEAD commit message: "${ headCommitMessage } "` ) ;
106+ } else {
107+ core . debug ( "No HEAD commit message found" ) ;
108+ }
158109
159- // Update our labels array with the freshly fetched labels
160- const updatedLabels = updatedLabelsResponse . data . map ( ( label ) => label . name ) ;
161- core . debug ( `Refetched ${ updatedLabels . length } labels: ${ updatedLabels . join ( ", " ) } ` ) ;
110+ // Try to extract semantic type from PR title or HEAD commit
111+ core . debug ( "Extracting semantic type from PR title..." ) ;
112+ const prTitleType = extractSemanticType ( prTitle ) ;
162113
163- // Replace our labels array with the updated one
164- labels . length = 0 ;
165- updatedLabels . forEach ( label => labels . push ( label ) ) ;
166- core . debug ( `Updated local labels array after refetch: ${ labels . join ( ", " ) } ` ) ;
167- }
114+ core . debug ( "Extracting semantic type from HEAD commit..." ) ;
115+ const commitType = extractSemanticType ( headCommitMessage ) ;
116+
117+ // Use PR title type first, then fall back to commit type
118+ const semanticType = prTitleType || commitType ;
119+ if ( semanticType ) {
120+ core . debug ( `Using semantic type: "${ semanticType } "` ) ;
168121 } else {
169- core . debug ( `Label "${ labelToAdd } " already exists on PR, no need to add it` ) ;
122+ core . debug ( "No semantic type found in PR title or HEAD commit" ) ;
123+ }
124+
125+ // If we found a semantic type that maps to one of our labels, add it if not present
126+ if ( semanticType && SEMANTIC_TYPE_TO_LABEL [ semanticType ] ) {
127+ const labelToAdd = SEMANTIC_TYPE_TO_LABEL [ semanticType ] ;
128+ core . debug ( `Semantic type "${ semanticType } " maps to label "${ labelToAdd } "` ) ;
129+
130+ // Only add the label if it's not already present
131+ if ( ! labels . includes ( labelToAdd ) ) {
132+ core . info ( `Adding label ${ labelToAdd } based on semantic commit type: ${ semanticType } ` ) ;
133+
134+ core . debug ( "Calling GitHub API to add label..." ) ;
135+ await octokit . rest . issues . addLabels ( {
136+ ...github . context . repo ,
137+ issue_number : prNumber ,
138+ labels : [ labelToAdd ] ,
139+ } ) ;
140+ core . debug ( "Label added successfully via API" ) ;
141+
142+ // Update our local labels array to include the new label
143+ labels . push ( labelToAdd ) ;
144+ addedSemanticLabel = true ;
145+ core . debug ( `Updated local labels array: ${ labels . join ( ", " ) } ` ) ;
146+
147+ // If we just added a label, give it time to apply
148+ if ( addedSemanticLabel ) {
149+ core . info ( "Added label based on semantic commit message. Waiting for label to apply..." ) ;
150+ // Short delay to allow the label to be properly registered
151+ core . debug ( "Waiting 2 seconds for label to propagate..." ) ;
152+ await new Promise ( resolve => setTimeout ( resolve , 2000 ) ) ;
153+ core . debug ( "Wait completed" ) ;
154+
155+ // Refetch the labels to ensure we have the most up-to-date set
156+ core . info ( "Refetching labels after adding semantic label..." ) ;
157+ core . debug ( "Calling GitHub API to get updated labels..." ) ;
158+ const updatedLabelsResponse = await octokit . rest . issues . listLabelsOnIssue ( {
159+ ...github . context . repo ,
160+ issue_number : github . context . issue . number ,
161+ } ) ;
162+
163+ // Update our labels array with the freshly fetched labels
164+ const updatedLabels = updatedLabelsResponse . data . map ( ( label ) => label . name ) ;
165+ core . debug ( `Refetched ${ updatedLabels . length } labels: ${ updatedLabels . join ( ", " ) } ` ) ;
166+
167+ // Replace our labels array with the updated one
168+ labels . length = 0 ;
169+ updatedLabels . forEach ( label => labels . push ( label ) ) ;
170+ core . debug ( `Updated local labels array after refetch: ${ labels . join ( ", " ) } ` ) ;
171+ }
172+ } else {
173+ core . debug ( `Label "${ labelToAdd } " already exists on PR, no need to add it` ) ;
174+ }
175+ } else if ( semanticType ) {
176+ core . debug ( `Semantic type "${ semanticType } " does not map to any of our labels` ) ;
170177 }
171- } else if ( semanticType ) {
172- core . debug ( `Semantic type "${ semanticType } " does not map to any of our labels` ) ;
173178 }
174179
175180 // ensure exactly one primary label is set
0 commit comments