Simplify scripts: unify bot detection, remove redundant API calls and TOCTOU checks

This commit is contained in:
BukeLy 2026-03-02 17:23:33 +08:00
parent fd9330c434
commit 7df8510bde
4 changed files with 11 additions and 99 deletions

View file

@ -92,13 +92,16 @@ async function fetchDuplicateIssues() {
return issues.filter(i => new Date(i.created_at) < cutoff);
}
function isBot(user) {
return user.type === 'Bot' || user.login.endsWith('[bot]') || user.login === 'github-actions';
}
/**
* Finds the bot's duplicate comment on an issue (contains "possible duplicate").
*/
function findDuplicateComment(comments) {
return comments.find(c =>
(c.user.type === 'Bot' || c.user.login === 'github-actions[bot]') &&
c.body.includes('possible duplicate')
isBot(c.user) && c.body.includes('possible duplicate')
);
}
@ -107,9 +110,7 @@ function findDuplicateComment(comments) {
*/
function hasHumanCommentAfter(comments, afterDate) {
return comments.some(c => {
if (c.user.type === 'Bot' || c.user.login.endsWith('[bot]') || c.user.login === 'github-actions') {
return false;
}
if (isBot(c.user)) return false;
return new Date(c.created_at) > afterDate;
});
}
@ -145,12 +146,6 @@ async function closeAsDuplicate(issueNumber) {
`/repos/${REPO_OWNER}/${REPO_NAME}/issues/${issueNumber}`,
{ state: 'closed', state_reason: 'completed' }
);
await githubRequest(
'POST',
`/repos/${REPO_OWNER}/${REPO_NAME}/issues/${issueNumber}/labels`,
{ labels: ['duplicate'] }
);
}
async function processIssue(issue) {

View file

@ -66,32 +66,16 @@ if [ ${#DUPLICATES[@]} -gt 3 ]; then
DUPLICATES=("${DUPLICATES[@]:0:3}")
fi
# Validate that the base issue exists and is open
if ! gh issue view "$BASE_ISSUE" --repo "$REPO" --json state -q '.state' | grep -qi 'open'; then
echo "Error: Issue #$BASE_ISSUE is not open or does not exist" >&2
exit 1
fi
# Build the duplicate links list
LINKS=""
COUNT=0
LINKS=""
for dup in "${DUPLICATES[@]}"; do
# Validate duplicate issue exists
if gh issue view "$dup" --repo "$REPO" --json number -q '.number' > /dev/null 2>&1; then
COUNT=$((COUNT + 1))
LINKS="${LINKS}${COUNT}. https://github.com/${REPO}/issues/${dup}
COUNT=$((COUNT + 1))
LINKS="${LINKS}${COUNT}. https://github.com/${REPO}/issues/${dup}
"
else
echo "Warning: Issue #$dup does not exist, skipping" >&2
fi
done
if [ "$COUNT" -eq 0 ]; then
echo "Error: None of the specified duplicate issues exist" >&2
exit 1
fi
# Build and post the comment
# Build and post the comment — if the issue is closed or doesn't exist, gh will error out
COMMENT="Found ${COUNT} possible duplicate issue(s):
${LINKS}
@ -99,8 +83,6 @@ This issue will be automatically closed as a duplicate in 3 days.
- To prevent auto-closure, add a comment or react with :thumbsdown: on this comment."
gh issue comment "$BASE_ISSUE" --repo "$REPO" --body "$COMMENT"
# Add the duplicate label
gh issue edit "$BASE_ISSUE" --repo "$REPO" --add-label "duplicate"
echo "Posted duplicate comment on issue #$BASE_ISSUE with $COUNT potential duplicate(s)"