diff --git a/tools/pull_request_hooks/autoLabel.js b/tools/pull_request_hooks/autoLabel.js index 809e81f49c0..40d99cde8c7 100644 --- a/tools/pull_request_hooks/autoLabel.js +++ b/tools/pull_request_hooks/autoLabel.js @@ -173,7 +173,7 @@ async function check_diff_files_for_labels(github, context) { * Main function to get the updated label set */ export async function get_updated_label_set({ github, context }) { - const { action, pull_request } = context.payload; + const { pull_request } = context.payload; const { body = "", diff_url, @@ -192,14 +192,38 @@ export async function get_updated_label_set({ github, context }) { labels_to_remove.forEach((label) => updated_labels.delete(label)); } - // Check body/title only when PR is opened, not on sync - if (action === "opened") { - if (title) - check_title_for_labels(title).forEach((label) => - updated_labels.add(label) - ); - if (body) - check_body_for_labels(body).forEach((label) => updated_labels.add(label)); + // Always check body/title (otherwise we can lose the changelog labels) + if (title) + check_title_for_labels(title).forEach((label) => + updated_labels.add(label) + ); + if (body) + check_body_for_labels(body).forEach((label) => updated_labels.add(label)); + + // Keep track of labels that were manually added by maintainers in the events. + // And make sure they -stay- added. + try { + await github.paginate( + github.rest.issues.listEventsForTimeline, + { + owner: context.repo.owner, + repo: context.repo.repo, + issue_number: context.payload.pull_request.number, + per_page: 100, + }, + (response) => { + for (const eventData of response.data) { + if ( + eventData.event === "labeled" && + eventData.actor?.login !== "github-actions" + ) { + updated_labels.add(eventData.label.name); + } + } + } + ); + } catch (error) { + console.error("Error fetching paginated events:", error); } // Always remove Test Merge Candidate