Skip to content

Commit

Permalink
Fix executable name (#3)
Browse files Browse the repository at this point in the history
* Fix executable name

* Fix formatting

* Print informative error message
  • Loading branch information
jarrodmillman authored May 22, 2023
1 parent e2f4bb7 commit bac685f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
# devstats
# devstats
46 changes: 27 additions & 19 deletions devstats/__init__.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
import os
import requests
import sys
import json
import click

token = os.environ["GRAPH_API_KEY"]
try:
token = os.environ["GRAPH_API_KEY"]
except KeyError:
print("You need to set GRAPH_API_KEY")
print("But you shouldn't use this yet.")
sys.exit()

endpoint = r"https://api.github.com/graphql"
headers = {"Authorization": "bearer {}".format(token)}
headers = {"Authorization": f"bearer {token}"}


def load_query_from_file(fname, repo_owner="numpy", repo_name="numpy"):
"""
Load an 'issue' query from file and set the target repository, where
the target repository has the format:
https://github.com/<repo_owner>/<repo_name>
Parameters
----------
fname : str
Path to a text file containing a valid issue query according to the
Path to a text file containing a valid issue query according to the
GitHub GraphQL schema.
repo_owner : str
Owner of target repository on GitHub. Default is 'numpy'.
repo_name : str
Name of target repository on GitHub. Default is 'numpy'.
Returns
-------
query : str
Expand All @@ -36,7 +43,7 @@ def load_query_from_file(fname, repo_owner="numpy", repo_name="numpy"):
for general GitHub GraphQL queries. See ``examples/`` for some valid
templated issue queries.
"""
with open(fname, "r") as fh:
with open(fname) as fh:
query = fh.read()
# Set target repo from template
query = query.replace("_REPO_OWNER_", repo_owner)
Expand Down Expand Up @@ -85,10 +92,11 @@ def send_query(query, query_type, cursor=None):
cursor_ind = query.find(cursor_insertion_key) + len(cursor_insertion_key)
query = query[:cursor_ind] + f'after:"{cursor}", ' + query[cursor_ind:]
# Build request payload
payload = {'query' : ''.join(query.split('\n'))}
payload = {"query": "".join(query.split("\n"))}
response = requests.post(endpoint, json=payload, headers=headers)
return json.loads(response.content)


def get_all_responses(query, query_type):
"""
Helper function to bypass GitHub GraphQL API node limit.
Expand All @@ -106,20 +114,21 @@ def get_all_responses(query, query_type):
print("Done.")
return data


def parse_single_query(data, query_type):
"""
Parse the data returned by `send_query`
.. warning::
Like `send_query`, the logic here depends on the specific structure
of the query (e.g. it must be an issue or PR query, and must have a
total count).
"""
try:
total_count = data['data']['repository'][query_type]['totalCount']
data = data['data']['repository'][query_type]['edges']
last_cursor = data[-1]['cursor']
total_count = data["data"]["repository"][query_type]["totalCount"]
data = data["data"]["repository"][query_type]["edges"]
last_cursor = data[-1]["cursor"]
except KeyError as e:
print(data)
raise e
Expand Down Expand Up @@ -182,30 +191,29 @@ def dump(self, outfile):


@click.command()
@click.argument('repo_owner')
@click.argument('repo_name')
@click.argument("repo_owner")
@click.argument("repo_name")
def main(repo_owner, repo_name):
"""Download and save issue and pr data for `repo_owner`/`repo_name`."""
# Download issue data
issues = GithubGrabber(
'query_examples/issue_activity_since_date.gql',
'issues',
"query_examples/issue_activity_since_date.gql",
"issues",
repo_owner=repo_owner,
repo_name=repo_name,
)
issues.get()
issues.dump(f"{repo_name}_issues.json")
# Download PR data
prs = GithubGrabber(
'query_examples/pr_data_query.gql',
'pullRequests',
"query_examples/pr_data_query.gql",
"pullRequests",
repo_owner=repo_owner,
repo_name=repo_name,
)
prs.get()
prs.dump(f"{repo_name}_prs.json")



if __name__ == "__main__":
main()
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[build-system]
build-backend = "setuptools.build_meta"
requires = ["setuptools", "wheel"]

[project]
name = "devstats"
version = "0.1rc0.dev0"
Expand All @@ -21,7 +25,7 @@ dependencies = [
]

[project.scripts]
query = "query.__main__:main"
devstats = "devstats.__main__:main"

[project.optional-dependencies]
lint = ["pre-commit >= 3.r32"]
Expand Down

0 comments on commit bac685f

Please sign in to comment.