Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create alter_main.py #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions alter_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import requests
import csv
import os
import datetime

GITHUB_API_URL = "https://api.github.com"
GITHUB_SEARCH_ISSUES_API = "/search/issues"
CSV_DIRECTORY = "data"
GITHUB_TOKEN = os.environ.get("GITHUB_TOKEN")

session = requests.Session()
if GITHUB_TOKEN:
session.headers.update({"Authorization": f"token {GITHUB_TOKEN}"})

def get_issues_count(query):
"""Get the number of issues with the provided label"""
url = f"{GITHUB_API_URL}{GITHUB_SEARCH_ISSUES_API}"
params = {"q": query}
response = session.get(url, params=params)
data = response.json()
if "errors" in data:
print(f"Error while searching for '{query}': {data['errors'][0]['message']}")
exit(1)
return data["total_count"]

def update_csv_file(repo, path):
"""Add today's records to the provided csv file"""
today = str(datetime.date.today())

with open(path, "r") as f:
reader = csv.reader(f)
content = list(reader)

if len(content) == 1 or content[1][0] != today:
content.insert(1, [today])

for i in range(1, len(content[0])):
query = content[0][i]
param = content[1][0]
count = get_issues_count(f"is:pr repo:{repo} {query}={param}")
content[1][i] = count

with open(path, "w") as f:
writer = csv.writer(f, lineterminator="\n")
writer.writerows(content)

if __name__ == "__main__":
if len(sys.argv) < 2:
print("usage: %s <repo> [files ...]" % sys.argv[0])
exit(1)
repo = sys.argv[1]

if not GITHUB_TOKEN:
print("Warning: the $GITHUB_TOKEN environment variable is not set!")
print("The script will still work, but it might be rate limited.")

# If a list of files to update isn't provided through args, update all the
# .csv files in the `data/` directory
files = sys.argv[2:]
if not files:
path = os.path.join(os.path.dirname(__file__), CSV_DIRECTORY, repo)
for file in os.listdir(path):
if file.endswith(".csv"):
files.append(os.path.join(path, file))

for file in files:
update_csv_file(repo, file)