Skip to content

Commit b4b4a5c

Browse files
authored
chore: refactor spam list updating (hiero-ledger#1749)
Signed-off-by: MonaaEid <monaa_eid@hotmail.com>
1 parent 5600d7b commit b4b4a5c

3 files changed

Lines changed: 40 additions & 60 deletions

File tree

.github/scripts/update-spam-list.js

Lines changed: 34 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
*/
99

1010
const fs = require('fs').promises;
11-
const path = require('path');
1211

1312
const SPAM_LIST_PATH = '.github/spam-list.txt';
1413
const dryRun = (process.env.DRY_RUN || 'false').toString().toLowerCase() === 'true';
@@ -72,31 +71,12 @@ async function computeSpamListUpdates(spamUsers, rehabilitatedUsers) {
7271
};
7372
}
7473

75-
// Write the updated spam list to file
76-
async function updateSpamListFile(usernames) {
77-
if (dryRun) {
78-
console.log('[DRY RUN] Would write to spam list file:');
79-
console.log(usernames.join('\n'));
80-
return;
81-
}
82-
83-
const content = usernames.join('\n') + (usernames.length > 0 ? '\n' : '');
84-
85-
// Ensure directory exists
86-
const dir = path.dirname(SPAM_LIST_PATH);
87-
await fs.mkdir(dir, { recursive: true });
88-
89-
await fs.writeFile(SPAM_LIST_PATH, content, 'utf8');
90-
91-
}
92-
93-
// Generate PR title and body with summary of changes
9474

9575
function generateSummary(additions, removals) {
96-
const title = `chore: Update spam list (${additions.length} additions, ${removals.length} removals)`;
76+
const title = `Update spam list (${additions.length} additions, ${removals.length} removals)`;
9777

9878
let body = '## Automated Spam List Update\n\n';
99-
body += 'This PR automatically updates the spam list based on recent PR activity.\n\n';
79+
body += 'This issue details the updates to the spam list based on recent PR activity.\n\n';
10080

10181
if (additions.length > 0) {
10282
body += `### ➕ Additions (${additions.length})\n\n`;
@@ -206,37 +186,49 @@ module.exports = async ({github, context, core}) => {
206186
}
207187
}
208188

209-
// ... rest remains the same
210-
const { additions, removals, finalSpamList } = await computeSpamListUpdates(
189+
// After processing all PRs, compute the final spam list updates
190+
const { additions, removals } = await computeSpamListUpdates(
211191
spamUsers,
212192
rehabilitatedUsers
213193
);
214194

215195
console.log(`Additions: ${additions.length}`);
216196
console.log(`Removals: ${removals.length}`);
217197

218-
if (additions.length > 0 || removals.length > 0) {
219-
await updateSpamListFile(finalSpamList);
220-
}
221-
222198
const { title, body } = generateSummary(additions, removals);
223199
const hasChanges = additions.length > 0 || removals.length > 0;
224200

225-
const branchName = hasChanges
226-
? `spam-list-update-${new Date().toISOString().split('T')[0]}`
227-
: '';
228-
229-
core.setOutput('has-changes', hasChanges.toString());
230-
core.setOutput('pr-title', title);
231-
core.setOutput('pr-body', body);
232-
core.setOutput('branch-name', branchName);
201+
if (hasChanges) {
202+
if (dryRun) {
203+
console.log('[DRY RUN] Would create issue with:');
204+
console.log(`Title: ${title}`);
205+
console.log(`Body: ${body}`);
206+
} else {
207+
// Check for existing open issue to avoid duplicates
208+
const { data: existing } = await github.rest.issues.listForRepo({
209+
owner,
210+
repo,
211+
labels: 'spam-list-update',
212+
state: 'open',
213+
per_page: 1
214+
});
215+
if (existing.length > 0) {
216+
console.log(`Skipping issue creation: open issue #${existing[0].number} already exists`);
217+
return;
218+
}
219+
await github.rest.issues.create({
220+
owner,
221+
repo,
222+
title,
223+
body,
224+
labels: ['spam-list-update', 'automated']
225+
});
226+
console.log('Issue created successfully');
227+
}
228+
} else {
229+
console.log('No changes needed, skipping issue creation');
230+
}
233231

234-
return {
235-
hasChanges,
236-
title,
237-
body,
238-
branchName
239-
};
240232
} catch (error) {
241233
core.setFailed(`Failed to update spam list: ${error.message}`);
242234
throw error;

.github/workflows/cron-update-spam-list.yml

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ on:
1111
default: 'true'
1212

1313
permissions:
14-
contents: write
15-
pull-requests: write
14+
contents: read
15+
pull-requests: read
16+
issues: write
1617

1718
env:
1819
DRY_RUN: ${{ github.event.inputs.dry_run || 'false' }}
@@ -22,12 +23,12 @@ jobs:
2223
runs-on: ubuntu-latest
2324
steps:
2425
- name: Harden runner (audit outbound calls)
25-
uses: step-security/harden-runner@e3f713f2d8f53843e71c69a996d56f51aa9adfb9 # v2.14.1
26+
uses: step-security/harden-runner@5ef0c079ce82195b2a36a210272d6b661572d83e # v2.14.2
2627
with:
2728
egress-policy: audit
2829

2930
- name: Checkout repository
30-
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
31+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
3132

3233
- name: Update spam list
3334
id: update-spam-list
@@ -37,17 +38,3 @@ jobs:
3738
script: |
3839
const updateSpamList = require('./.github/scripts/update-spam-list.js');
3940
await updateSpamList({ github, context, core });
40-
41-
42-
- name: Create pull request
43-
if: ${{ steps.update-spam-list.outputs.has-changes == 'true' && env.DRY_RUN != 'true' }}
44-
uses: peter-evans/create-pull-request@c0f553fe549906ede9cf27b5156039d195d2ece0 # v8.1.0
45-
with:
46-
token: ${{ secrets.GITHUB_TOKEN }}
47-
commit-message: ${{ steps.update-spam-list.outputs.pr-title }}
48-
branch: ${{ steps.update-spam-list.outputs.branch-name }}
49-
title: ${{ steps.update-spam-list.outputs.pr-title }}
50-
body: ${{ steps.update-spam-list.outputs.pr-body }}
51-
labels: automated, spam-management
52-
add-paths: |
53-
.github/spam-list.txt

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.
291291
- Fixed incorrect run instructions and broaden error handling in `token_dissociate_transaction.py` example to improve usability for new users (#1468)
292292
- Update `.github/scripts/bot-advanced-check.sh` to unassign unqualified users.
293293
- Fixed broken project structure link in `CONTRIBUTING.md` (#1664)
294+
- Refactor spam list update logic and remove unused pull request creation step `.github/scripts/update-spam-list.js` `.github/workflows/cron-update-spam-list.yml`.
294295

295296
### Removed
296297

0 commit comments

Comments
 (0)