From 76adb97e22bebfffe609e74e962c858ce0da695e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E9=9B=85=20=C2=B7=20Misaki=20Masa?= Date: Tue, 11 Mar 2025 12:37:31 +0800 Subject: [PATCH] ci: add label only if not removed manually (#2470) --- scripts/validate-form/main.js | 49 +++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/scripts/validate-form/main.js b/scripts/validate-form/main.js index 2b71368a..ecbfff02 100644 --- a/scripts/validate-form/main.js +++ b/scripts/validate-form/main.js @@ -61,10 +61,49 @@ module.exports = async ({ github, context, core }) => { } } + async function lastLabeledAt(id) { + try { + const { data: events } = await github.rest.issues.listEvents({ + ...context.repo, + issue_number: id, + per_page : 100, + }) + + const all = events.filter(v => v.event === "labeled" && v.label?.name === LABEL_NAME) + return all.at(-1)?.created_at + } catch (e) { + core.error(`Error getting label timestamp: ${e.message}`) + return null + } + } + + async function removedLabelManually(id) { + try { + const { data: events } = await github.rest.issues.listEvents({ + ...context.repo, + issue_number: id, + per_page : 100, + }) + + const all = events.filter(v => v.event === "unlabeled" && v.label?.name === LABEL_NAME) + return all.length === 0 ? false : !all.at(-1).actor.login.endsWith("[bot]") + } catch (e) { + core.error(`Error checking label removal history: ${e.message}`) + return false + } + } + async function updateLabels(id, mark, body) { try { const marked = await hasLabel(id, LABEL_NAME) - if (mark && !marked) { + + if (!mark && marked) { + await github.rest.issues.removeLabel({ + ...context.repo, + issue_number: id, + name : LABEL_NAME, + }) + } else if (mark && !marked && !await removedLabelManually(id)) { await github.rest.issues.addLabels({ ...context.repo, issue_number: id, @@ -75,12 +114,6 @@ module.exports = async ({ github, context, core }) => { issue_number: id, body, }) - } else if (!mark && marked) { - await github.rest.issues.removeLabel({ - ...context.repo, - issue_number: id, - name : LABEL_NAME, - }) } } catch (e) { core.error(`Error updating labels: ${e.message}`) @@ -99,7 +132,7 @@ module.exports = async ({ github, context, core }) => { const twoDaysAgo = new Date(now - 2 * 24 * 60 * 60 * 1000) for (const issue of issues) { - const markedAt = new Date(issue.labels_at || issue.created_at) + const markedAt = new Date(await lastLabeledAt(issue.number) || issue.created_at) if (markedAt < twoDaysAgo) { await github.rest.issues.update({ ...context.repo,