|
| 1 | +name: Delete Spam Issues |
| 2 | + |
| 3 | +on: |
| 4 | + issues: |
| 5 | + types: [opened, edited] |
| 6 | + workflow_dispatch: |
| 7 | + # 允许手动触发 |
| 8 | + |
| 9 | +jobs: |
| 10 | + delete-spam-issues: |
| 11 | + runs-on: ubuntu-latest |
| 12 | + |
| 13 | + permissions: |
| 14 | + issues: write |
| 15 | + contents: read |
| 16 | + |
| 17 | + steps: |
| 18 | + - name: Checkout repository |
| 19 | + uses: actions/checkout@v4 |
| 20 | + |
| 21 | + - name: Delete spam issues with short descriptions |
| 22 | + env: |
| 23 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 24 | + run: | |
| 25 | + echo "Starting deletion of spam issues with short descriptions..." |
| 26 | +
|
| 27 | + # 获取仓库的node ID (用于GraphQL删除操作) |
| 28 | + repo_info=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ |
| 29 | + -H "Accept: application/vnd.github.v3+json" \ |
| 30 | + "https://api.github.com/repos/$GITHUB_REPOSITORY") |
| 31 | + repo_node_id=$(echo "$repo_info" | jq -r '.node_id') |
| 32 | +
|
| 33 | + # 获取所有开放的issues |
| 34 | + page=1 |
| 35 | + deleted_count=0 |
| 36 | + while true; do |
| 37 | + echo "Fetching issues page $page..." |
| 38 | +
|
| 39 | + issues=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \ |
| 40 | + -H "Accept: application/vnd.github.v3+json" \ |
| 41 | + "https://api.github.com/repos/$GITHUB_REPOSITORY/issues?state=open&page=$page&per_page=100") |
| 42 | +
|
| 43 | + # 检查是否还有issues |
| 44 | + issues_count=$(echo "$issues" | jq '. | length') |
| 45 | + if [ "$issues_count" = "0" ]; then |
| 46 | + break |
| 47 | + fi |
| 48 | +
|
| 49 | + echo "Processing $issues_count issues from page $page..." |
| 50 | +
|
| 51 | + # 遍历每个issue |
| 52 | + echo "$issues" | jq -r '.[] | select(.pull_request == null) | @base64' | while read -r issue_data; do |
| 53 | + issue=$(echo "$issue_data" | base64 --decode) |
| 54 | +
|
| 55 | + issue_number=$(echo "$issue" | jq -r '.number') |
| 56 | + issue_title=$(echo "$issue" | jq -r '.title') |
| 57 | + issue_body=$(echo "$issue" | jq -r '.body // ""') |
| 58 | + issue_user=$(echo "$issue" | jq -r '.user.login') |
| 59 | + issue_node_id=$(echo "$issue" | jq -r '.node_id') |
| 60 | +
|
| 61 | + # 计算标题和描述长度(去除空白字符) |
| 62 | + title_length=$(echo "$issue_title" | tr -d '[:space:]' | wc -c) |
| 63 | + body_length=$(echo "$issue_body" | tr -d '[:space:]' | wc -c) |
| 64 | +
|
| 65 | + echo "Issue #$issue_number: '$issue_title' by $issue_user (title: $title_length chars, body: $body_length chars)" |
| 66 | +
|
| 67 | + # 如果标题和描述都少于20个字符,则删除issue |
| 68 | + if [ "$title_length" -lt 20 ] && [ "$body_length" -lt 20 ]; then |
| 69 | + echo "Deleting spam issue #$issue_number due to short title ($title_length chars) and description ($body_length chars)" |
| 70 | +
|
| 71 | + # 使用GraphQL API删除issue |
| 72 | + delete_mutation='{ |
| 73 | + "query": "mutation { deleteIssue(input: {issueId: \"'$issue_node_id'\"}) { clientMutationId } }" |
| 74 | + }' |
| 75 | +
|
| 76 | + delete_result=$(curl -s -X POST \ |
| 77 | + -H "Authorization: token $GITHUB_TOKEN" \ |
| 78 | + -H "Accept: application/vnd.github.v4+json" \ |
| 79 | + -H "Content-Type: application/json" \ |
| 80 | + "https://api.github.com/graphql" \ |
| 81 | + -d "$delete_mutation") |
| 82 | +
|
| 83 | + # 检查删除结果 |
| 84 | + if echo "$delete_result" | jq -e '.data.deleteIssue' > /dev/null; then |
| 85 | + echo "Issue #$issue_number has been successfully deleted" |
| 86 | + deleted_count=$((deleted_count + 1)) |
| 87 | + else |
| 88 | + echo "Failed to delete issue #$issue_number" |
| 89 | + echo "Error: $(echo "$delete_result" | jq -r '.errors[0].message // "Unknown error"')" |
| 90 | + fi |
| 91 | + else |
| 92 | + echo "Issue #$issue_number has sufficient content (title: $title_length chars, body: $body_length chars), keeping it" |
| 93 | + fi |
| 94 | + done |
| 95 | +
|
| 96 | + page=$((page + 1)) |
| 97 | + done |
| 98 | +
|
| 99 | + echo "Spam cleanup completed. Deleted $deleted_count issues." |
| 100 | +
|
| 101 | + - name: Report deletion summary |
| 102 | + if: github.event_name == 'workflow_dispatch' |
| 103 | + env: |
| 104 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 105 | + run: | |
| 106 | + echo "Spam issue deletion workflow completed successfully" |
| 107 | + echo "This workflow automatically DELETES issues with BOTH title and description shorter than 20 characters" |
| 108 | + echo "?? DELETED issues cannot be recovered!" |
| 109 | + echo "It runs on:" |
| 110 | + echo "- New issues being opened (immediate deletion of spam)" |
| 111 | + echo "- Issues being edited (deletion if edited to become too short)" |
| 112 | + echo "- Manual trigger (on-demand cleanup)" |
0 commit comments