@@ -9,39 +9,68 @@ const { Octokit } = require('@octokit/rest');
99const yaml = require ( 'js-yaml' ) ;
1010const fs = require ( 'fs' ) ;
1111const path = require ( 'path' ) ;
12+ const { execSync } = require ( 'child_process' ) ;
1213
1314/**
14- * Get labels that were added by this action by checking issue events
15+ * Get labels that were added by this action using GitHub Actions cache
1516 */
16- async function getLabelsAddedByAction ( octokit , owner , repo , issueNumber ) {
17+ async function getLabelsAddedByAction ( owner , repo , issueNumber ) {
1718 try {
18- const events = await octokit . rest . issues . listEvents ( {
19- owner,
20- repo,
21- issue_number : issueNumber ,
22- per_page : 100
23- } ) ;
19+ const cacheKey = `pr-title-labeler-${ owner } -${ repo } -${ issueNumber } ` ;
20+ const cachePath = `/tmp/pr-title-labeler-cache-${ issueNumber } .json` ;
2421
25- const labelsAddedByAction = new Set ( ) ;
22+ // Try to restore from cache
23+ try {
24+ execSync ( `gh cache restore ${ cacheKey } --path ${ cachePath } ` , {
25+ stdio : 'pipe'
26+ } ) ;
2627
27- // Look for "labeled" events that were created by the GitHub Actions bot
28- for ( const event of events . data ) {
29- if (
30- event . event === 'labeled' &&
31- event . actor ?. login === 'github-actions[bot]' &&
32- event . label ?. name
33- ) {
34- labelsAddedByAction . add ( event . label . name ) ;
28+ if ( fs . existsSync ( cachePath ) ) {
29+ const cacheData = JSON . parse ( fs . readFileSync ( cachePath , 'utf8' ) ) ;
30+ const labels = new Set ( cacheData . labels || [ ] ) ;
31+ console . log (
32+ `💾 Retrieved from cache: [${ Array . from ( labels ) . join ( ', ' ) } ]`
33+ ) ;
34+ return labels ;
3535 }
36+ } catch ( error ) {
37+ // Cache miss or error - this is normal for first run
38+ console . log ( '💾 No cache found, starting fresh' ) ;
3639 }
3740
38- return labelsAddedByAction ;
41+ return new Set ( ) ;
3942 } catch ( error ) {
40- console . log ( '⚠️ Could not fetch issue events :' , error . message ) ;
43+ console . log ( '⚠️ Could not retrieve cache :' , error . message ) ;
4144 return new Set ( ) ;
4245 }
4346}
4447
48+ /**
49+ * Save labels that were added by this action to GitHub Actions cache
50+ */
51+ async function saveLabelsAddedByAction ( owner , repo , issueNumber , labels ) {
52+ try {
53+ const cacheKey = `pr-title-labeler-${ owner } -${ repo } -${ issueNumber } ` ;
54+ const cachePath = `/tmp/pr-title-labeler-cache-${ issueNumber } .json` ;
55+
56+ // Create cache data
57+ const cacheData = {
58+ labels : Array . from ( labels ) ,
59+ timestamp : new Date ( ) . toISOString ( )
60+ } ;
61+
62+ // Write to temporary file
63+ fs . writeFileSync ( cachePath , JSON . stringify ( cacheData , null , 2 ) ) ;
64+
65+ // Save to GitHub Actions cache
66+ execSync ( `gh cache save ${ cacheKey } ${ cachePath } ` , { stdio : 'pipe' } ) ;
67+
68+ console . log ( `💾 Saved to cache: [${ Array . from ( labels ) . join ( ', ' ) } ]` ) ;
69+ } catch ( error ) {
70+ console . log ( '⚠️ Could not save cache:' , error . message ) ;
71+ }
72+ }
73+
4574/**
4675 * Get current labels on the PR
4776 */
@@ -83,12 +112,23 @@ function findMatchingLabel(title, labelMappings) {
83112function getLabelsToRemove ( labelsAddedByAction , currentLabels , targetLabel ) {
84113 const labelsToRemove = new Set ( ) ;
85114
115+ console . log ( `🔍 Determining labels to remove...` ) ;
116+ console . log (
117+ `📋 Labels added by action: [${ Array . from ( labelsAddedByAction ) . join ( ', ' ) } ]`
118+ ) ;
119+ console . log ( `📋 Current labels: [${ Array . from ( currentLabels ) . join ( ', ' ) } ]` ) ;
120+ console . log ( `🎯 Target label: "${ targetLabel || 'none' } "` ) ;
121+
86122 for ( const addedLabel of labelsAddedByAction ) {
87123 if ( currentLabels . has ( addedLabel ) && addedLabel !== targetLabel ) {
88124 labelsToRemove . add ( addedLabel ) ;
125+ console . log ( `🗑️ Will remove: "${ addedLabel } "` ) ;
89126 }
90127 }
91128
129+ console . log (
130+ `📊 Labels to remove: [${ Array . from ( labelsToRemove ) . join ( ', ' ) } ]`
131+ ) ;
92132 return labelsToRemove ;
93133}
94134
@@ -220,7 +260,7 @@ async function run() {
220260 // Get current state
221261 const [ currentLabels , labelsAddedByAction ] = await Promise . all ( [
222262 getCurrentLabels ( octokit , owner , repo , pr . number ) ,
223- getLabelsAddedByAction ( octokit , owner , repo , pr . number )
263+ getLabelsAddedByAction ( owner , repo , pr . number )
224264 ] ) ;
225265
226266 // Check if target label already exists
@@ -249,6 +289,26 @@ async function run() {
249289 logNoMatchingPrefix ( pr . title , labelMappings ) ;
250290 }
251291
292+ // Update cache with current state
293+ const newLabelsAddedByAction = new Set ( labelsAddedByAction ) ;
294+
295+ // Remove labels that were removed from the PR
296+ for ( const removedLabel of labelsToRemove ) {
297+ newLabelsAddedByAction . delete ( removedLabel ) ;
298+ }
299+
300+ // Add new label if it was added by this action
301+ if ( targetLabel && ! currentLabels . has ( targetLabel ) ) {
302+ newLabelsAddedByAction . add ( targetLabel ) ;
303+ }
304+
305+ await saveLabelsAddedByAction (
306+ owner ,
307+ repo ,
308+ pr . number ,
309+ newLabelsAddedByAction
310+ ) ;
311+
252312 console . log ( '🎉 PR title labeler completed successfully!' ) ;
253313 } catch ( error ) {
254314 console . error ( `💥 Error labeling PR: ${ error . message } ` ) ;
0 commit comments