From 3303ede916eca344f7cfcde69d099e553767ea90 Mon Sep 17 00:00:00 2001 From: iBug Date: Wed, 6 Sep 2023 16:08:42 +0800 Subject: [PATCH] count_reactions: Count negative reactions as well --- .github/workflows/main.yml | 10 ++++++---- scripts/count_reactions.py | 22 ++++++++++++++-------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index cc2b763..8954742 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,6 +1,9 @@ name: Count Reactions on: + push: + branches: + - master schedule: - cron: '0 0 * * *' issues: @@ -17,10 +20,10 @@ jobs: issues: write steps: - name: Checkout repository - uses: actions/checkout@v2 + uses: actions/checkout@v3 - name: Set up Python - uses: actions/setup-python@v2 + uses: actions/setup-python@v4 - name: Install dependencies run: | @@ -30,5 +33,4 @@ jobs: env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: - python scripts/count_reactions.py - + python3 scripts/count_reactions.py diff --git a/scripts/count_reactions.py b/scripts/count_reactions.py index 3493e0f..accc92b 100644 --- a/scripts/count_reactions.py +++ b/scripts/count_reactions.py @@ -1,27 +1,33 @@ +#!/usr/bin/python3 + import os from github import Github + def count_reactions(token, repo_name): g = Github(token) repo = g.get_repo(repo_name) - issues = repo.get_issues(state="open", labels=["needvote"]) reactions_count = {} for issue in issues: reactions = issue.get_reactions() - reaction_dates = set() + up_dates, down_dates = set(), set() for reaction in reactions: + date = reaction.created_at.date() if reaction.content == "+1": - reaction_dates.add(reaction.created_at.date()) - reactions_count[issue.number] = len(reaction_dates) + up_dates.add(date) + elif reaction.content == "-1": + down_dates.add(date) + reactions_count[issue.number] = (len(up_dates), len(down_dates)) - sorted_reactions_count = dict(sorted(reactions_count.items(), key=lambda item: item[1], reverse=True)) + sorted_reactions_count = sorted(reactions_count.items(), key=lambda item: (item[1][0] - item[1][1], item[0]), reverse=True) - table = "* Votes:\n | Title| Priority |\n | --- | --- |\n" - for issue_number, count in sorted_reactions_count.items(): - table += f" | #{issue_number} | {count} |\n" + table = "## Votes\n\n| Title| Priority | Score |\n| --- | --- | --- |\n" + for issue_number, (up_count, down_count) in sorted_reactions_count: + score = up_count - down_count + table += f"| #{issue_number} | {score} | +{up_count}, -{down_count} |\n" repo.get_issue(366).edit(body=table)