Skip to content

Commit a85572c

Browse files
committed
Another attempt
1 parent 92d56b6 commit a85572c

File tree

1 file changed

+56
-76
lines changed
  • .github/actions/pr-title-labeler

1 file changed

+56
-76
lines changed

.github/actions/pr-title-labeler/index.js

Lines changed: 56 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -11,70 +11,70 @@ const fs = require('fs');
1111
const path = require('path');
1212

1313
/**
14-
* Get labels that were added by this action using a simple file-based cache
14+
* Get labels that were likely added by this action using a conservative approach
15+
* Since we can't rely on persistent storage, we'll use a smart heuristic:
16+
* 1. Only consider labels that are in our configuration
17+
* 2. Only consider labels added by GitHub Actions recently
18+
* 3. Be conservative - only remove when we're confident
1519
*/
16-
async function getLabelsAddedByAction(owner, repo, issueNumber) {
20+
async function getLabelsAddedByAction(
21+
octokit,
22+
owner,
23+
repo,
24+
issueNumber,
25+
labelMappings
26+
) {
1727
try {
18-
const cacheDir = '/tmp/pr-title-labeler-cache';
19-
const cacheFile = path.join(
20-
cacheDir,
21-
`${owner}-${repo}-${issueNumber}.json`
22-
);
23-
24-
// Ensure cache directory exists
25-
if (!fs.existsSync(cacheDir)) {
26-
fs.mkdirSync(cacheDir, { recursive: true });
27-
}
28-
29-
// Try to read from cache file
30-
if (fs.existsSync(cacheFile)) {
31-
const cacheData = JSON.parse(fs.readFileSync(cacheFile, 'utf8'));
32-
const labels = new Set(cacheData.labels || []);
33-
console.log(
34-
`💾 Retrieved from cache: [${Array.from(labels).join(', ')}]`
35-
);
36-
return labels;
37-
}
28+
const events = await octokit.rest.issues.listEvents({
29+
owner,
30+
repo,
31+
issue_number: issueNumber,
32+
per_page: 100
33+
});
3834

39-
console.log('💾 No cache found, starting fresh');
40-
return new Set();
41-
} catch (error) {
42-
console.log('⚠️ Could not retrieve cache:', error.message);
43-
return new Set();
44-
}
45-
}
35+
const labelsAddedByAction = new Set();
36+
const configuredLabels = new Set(
37+
labelMappings.map(mapping => mapping.label)
38+
);
4639

47-
/**
48-
* Save labels that were added by this action to file-based cache
49-
*/
50-
async function saveLabelsAddedByAction(owner, repo, issueNumber, labels) {
51-
try {
52-
const cacheDir = '/tmp/pr-title-labeler-cache';
53-
const cacheFile = path.join(
54-
cacheDir,
55-
`${owner}-${repo}-${issueNumber}.json`
40+
console.log(
41+
`🔍 Checking ${events.data.length} events for labels added by this action...`
42+
);
43+
console.log(
44+
`📋 Configured labels: [${Array.from(configuredLabels).join(', ')}]`
5645
);
5746

58-
// Ensure cache directory exists
59-
if (!fs.existsSync(cacheDir)) {
60-
fs.mkdirSync(cacheDir, { recursive: true });
47+
// Look for "labeled" events that were created by GitHub Actions
48+
for (const event of events.data) {
49+
if (event.event === 'labeled' && event.label?.name) {
50+
const actorLogin = event.actor?.login;
51+
52+
console.log(
53+
`📋 Found labeled event: "${event.label.name}" by "${actorLogin || 'unknown'}"`
54+
);
55+
56+
// Conservative approach: only consider labels that:
57+
// 1. Are in our configuration (could have been added by this action)
58+
// 2. Were added by GitHub Actions bot
59+
if (
60+
actorLogin === 'github-actions[bot]' &&
61+
configuredLabels.has(event.label.name)
62+
) {
63+
labelsAddedByAction.add(event.label.name);
64+
console.log(
65+
`✅ Added "${event.label.name}" to action-added labels (configured + GitHub Actions)`
66+
);
67+
}
68+
}
6169
}
6270

63-
// Create cache data
64-
const cacheData = {
65-
labels: Array.from(labels),
66-
timestamp: new Date().toISOString(),
67-
owner,
68-
repo,
69-
issueNumber
70-
};
71-
72-
// Write to cache file
73-
fs.writeFileSync(cacheFile, JSON.stringify(cacheData, null, 2));
74-
75-
console.log(`💾 Saved to cache: [${Array.from(labels).join(', ')}]`);
71+
console.log(
72+
`📊 Labels added by this action: [${Array.from(labelsAddedByAction).join(', ')}]`
73+
);
74+
return labelsAddedByAction;
7675
} catch (error) {
77-
console.log('⚠️ Could not save cache:', error.message);
76+
console.log('⚠️ Could not fetch issue events:', error.message);
77+
return new Set();
7878
}
7979
}
8080

@@ -267,7 +267,7 @@ async function run() {
267267
// Get current state
268268
const [currentLabels, labelsAddedByAction] = await Promise.all([
269269
getCurrentLabels(octokit, owner, repo, pr.number),
270-
getLabelsAddedByAction(owner, repo, pr.number)
270+
getLabelsAddedByAction(octokit, owner, repo, pr.number, labelMappings)
271271
]);
272272

273273
// Check if target label already exists
@@ -296,26 +296,6 @@ async function run() {
296296
logNoMatchingPrefix(pr.title, labelMappings);
297297
}
298298

299-
// Update cache with current state
300-
const newLabelsAddedByAction = new Set(labelsAddedByAction);
301-
302-
// Remove labels that were removed from the PR
303-
for (const removedLabel of labelsToRemove) {
304-
newLabelsAddedByAction.delete(removedLabel);
305-
}
306-
307-
// Add new label if it was added by this action
308-
if (targetLabel && !currentLabels.has(targetLabel)) {
309-
newLabelsAddedByAction.add(targetLabel);
310-
}
311-
312-
await saveLabelsAddedByAction(
313-
owner,
314-
repo,
315-
pr.number,
316-
newLabelsAddedByAction
317-
);
318-
319299
console.log('🎉 PR title labeler completed successfully!');
320300
} catch (error) {
321301
console.error(`💥 Error labeling PR: ${error.message}`);

0 commit comments

Comments
 (0)