-
Notifications
You must be signed in to change notification settings - Fork 32
/
count_votes.py
132 lines (110 loc) · 4.23 KB
/
count_votes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import sys
import requests
def fetch_github_comment(comment_url):
# Extract the necessary parts from the comment URL
parts = comment_url.split("/")
owner, repo = parts[3], parts[4]
comment_id = comment_url.split("#issuecomment-")[-1]
# Construct the API URL
api_url = (
f"https://api.github.com/repos/{owner}/{repo}/issues/comments/{comment_id}"
)
# Make the request to the GitHub API
response = requests.get(api_url)
if response.status_code == 200:
return response.json()["body"]
else:
raise Exception(f"Failed to fetch comment: {response.status_code}")
def parse_votes(text):
lines = text.split("\n")
yes_votes = []
no_votes = []
abstain_votes = []
not_voted = []
invalid_votes = []
current_voter = None
vote_count = 0
for line in lines:
line = line.strip()
if line.startswith("@"):
# This line contains the voter's information
if current_voter and vote_count == 0:
not_voted.append(current_voter)
current_voter = line
vote_count = 0
elif line.startswith("- [x]") or line.startswith("- [ ]"):
# This line contains a vote
if current_voter:
if "[x]" in line:
vote_count += 1
if "yes" in line.lower():
yes_votes.append(current_voter)
elif "no" in line.lower():
no_votes.append(current_voter)
elif "abstain" in line.lower():
abstain_votes.append(current_voter)
else:
# If we encounter any other type of line, check if the previous voter's vote was valid
if current_voter:
if vote_count > 1:
invalid_votes.append(current_voter)
elif vote_count == 0:
not_voted.append(current_voter)
current_voter = None
vote_count = 0
# Check the last voter
if current_voter:
if vote_count > 1:
invalid_votes.append(current_voter)
elif vote_count == 0:
not_voted.append(current_voter)
return yes_votes, no_votes, abstain_votes, not_voted, invalid_votes
def print_results(yes_votes, no_votes, abstain_votes, not_voted, invalid_votes):
total_voters = (
len(yes_votes)
+ len(no_votes)
+ len(abstain_votes)
+ len(not_voted)
+ len(invalid_votes)
)
valid_voters = len(yes_votes) + len(no_votes) + len(abstain_votes)
print(
f"Total voters: {total_voters} (valid: {valid_voters} = {valid_voters / total_voters * 100:.2f}%)"
)
print(
f"\nYes votes ({len(yes_votes)} / {len(yes_votes) / valid_voters * 100:.2f}%):"
)
for voter in yes_votes:
print(f"- {voter}")
print(f"\nNo votes ({len(no_votes)} / {len(no_votes) / valid_voters * 100:.2f}%)):")
for voter in no_votes:
print(f"- {voter}")
print(
f"\nAbstain votes ({len(abstain_votes)} / {len(abstain_votes) / valid_voters * 100:.2f}%):"
)
for voter in abstain_votes:
print(f"- {voter}")
print(f"\nNot voted ({len(not_voted)}):")
for voter in not_voted:
print(f"- {voter}")
print(f"\nInvalid votes ({len(invalid_votes)}):")
for voter in invalid_votes:
print(f"- {voter}")
# print("\nGitHub handles summary:")
# print("Yes:", ", ".join([vote.split()[0] for vote in yes_votes]))
# print("No:", ", ".join([vote.split()[0] for vote in no_votes]))
# print("Abstain:", ", ".join([vote.split()[0] for vote in abstain_votes]))
# print("Not voted:", ", ".join([vote.split()[0] for vote in not_voted]))
# print("Invalid votes:", ", ".join([vote.split()[0] for vote in invalid_votes]))
# Example usage
comment_url = "https://github.com/conda/ceps/pull/75#issuecomment-2203197834"
try:
if len(sys.argv) > 1:
comment_url = sys.argv[1]
comment_text = fetch_github_comment(comment_url)
yes_votes, no_votes, abstain_votes, not_voted, invalid_votes = parse_votes(
comment_text
)
print_results(yes_votes, no_votes, abstain_votes, not_voted, invalid_votes)
except Exception as e:
print(f"An error occurred: {e}")