Skip to content

Commit 6c34d4b

Browse files
committed
Added stars for the repo
1 parent ddff78d commit 6c34d4b

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

devstats/__main__.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def query(repo_owner, repo_name, outdir):
4545
sys.exit()
4646

4747
headers = {"Authorization": f"bearer {token}"}
48-
query_files = glob(os.path.join(os.path.dirname(__file__), "queries/*.gql"))
48+
query_files = sorted(glob(os.path.join(os.path.dirname(__file__), "queries/*.gql")))
4949

5050
for n, query in enumerate(query_files):
5151
if n != 0:
@@ -55,7 +55,7 @@ def query(repo_owner, repo_name, outdir):
5555
# Parse query type from gql
5656
gql = open(query).read()
5757
qtype_match = re.match(
58-
r"query\s*{\s*repository\(.*?\)\s*{\s*(pullRequests|issues)",
58+
r"query\s*{\s*repository\(.*?\)\s*{\s*(pullRequests|issues|stargazerCount)",
5959
gql,
6060
flags=re.MULTILINE,
6161
)
@@ -65,7 +65,6 @@ def query(repo_owner, repo_name, outdir):
6565
else:
6666
qtype = qtype_match.group(1)
6767

68-
qname, qext = os.path.splitext(query)
6968
data = GithubGrabber(
7069
query,
7170
qtype,
@@ -74,7 +73,7 @@ def query(repo_owner, repo_name, outdir):
7473
repo_name=repo_name,
7574
)
7675
data.get()
77-
ftype = {"issues": "issues", "pullRequests": "PRs"}
76+
ftype = {"issues": "issues", "pullRequests": "PRs", "stargazerCount": "stars"}
7877
data.dump(f"{outdir}/{repo_name}_{ftype.get(qtype, qtype)}.json")
7978

8079

devstats/queries/repo_stars.gql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
query {
2+
repository(owner: "_REPO_OWNER_", name: "_REPO_NAME_") {
3+
stargazerCount
4+
}
5+
}

devstats/query.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import requests
55

6-
endpoint = r"https://api.github.com/graphql"
6+
ENDPOINT = r"https://api.github.com/graphql"
77

88

99
def load_query_from_file(fname, repo_owner="numpy", repo_name="numpy"):
@@ -72,10 +72,10 @@ def send_query(query, query_type, headers, cursor=None):
7272
This is intended mostly for internal use within `get_all_responses`.
7373
"""
7474
# TODO: Expand this, either by parsing the query type from the query
75-
# directly or manually adding more query_types to the set
76-
if query_type not in {"issues", "pullRequests"}:
75+
# Directly or manually adding more query_types to the set
76+
if query_type not in {"issues", "pullRequests", "stargazerCount"}:
7777
raise ValueError(
78-
"Only 'issues' and 'pullRequests' queries are currently supported"
78+
"Only 'issues', 'pullRequests' and 'stargazerCount' queries are currently supported"
7979
)
8080
# TODO: Generalize this
8181
# WARNING: The cursor injection depends on the specific structure of the
@@ -91,7 +91,7 @@ def send_query(query, query_type, headers, cursor=None):
9191
retries = max_retries
9292
while retries > 0:
9393
try:
94-
response = requests.post(endpoint, json=payload, headers=headers)
94+
response = requests.post(ENDPOINT, json=payload, headers=headers)
9595
except (
9696
requests.exceptions.ChunkedEncodingError,
9797
requests.exceptions.ConnectionError,
@@ -138,7 +138,15 @@ def get_all_responses(query, query_type, headers):
138138
print("Fetching...", end="", flush=True)
139139
rdata = send_query(query, query_type, headers, cursor=last_cursor)
140140
try:
141-
pdata, last_cursor, total_count = parse_single_query(rdata, query_type)
141+
# TODO: Generalize this
142+
if query_type == "stargazerCount":
143+
# Special case for stargazerCount
144+
pdata = [{"stargazerCount":rdata["data"]["repository"]["stargazerCount"]}]
145+
last_cursor = None
146+
total_count = 1
147+
else:
148+
# Normal case for issues/PRs
149+
pdata, last_cursor, total_count = parse_single_query(rdata, query_type)
142150
except (KeyError, TypeError):
143151
print("Malformed response; repeating request after 1 minute")
144152
time.sleep(1 * 60)

0 commit comments

Comments
 (0)