-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindRepos.py
79 lines (64 loc) · 2.17 KB
/
findRepos.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
import csv
import os
import json
import requests
from datetime import datetime
from percentTracker import PercentTracker
import sys
org = "EBSCOIS"
# SECRETS ARE PULLED FROM A LOCAL secrets.json FILE.
secretsPath = "secrets.json"
with open(secretsPath) as f:
secrets = json.load(f)
# Client for pinging GitHub API
def gitHubApi(url):
payload = {}
headers = {
'Accept': 'application/vnd.github.v3+json',
'Authorization': 'Bearer {}'.format(secrets["githubToken"])
}
response = requests.request("GET", url, headers=headers, data=payload)
respJson = json.loads(response.text)
return respJson
# Gets last commit of provided repo
def getLastCommit(repo):
url = "https://api.github.com/repos/{}/commits".format(repo)
commits = gitHubApi(url)
return commits[0]["commit"]["author"]["name"]
# Finds repos based on query string and writes report
def findTestRepos(q):
recordCount = 0
page = 1
newRecordCount = 1
items = []
print("retrieving records...")
while newRecordCount != 0:
url = "https://api.github.com/search/repositories?q={} +in:name +org:{} &per_page=100&page={}".format(
q, org, page)
newRecords = gitHubApi(url)
newRecordCount = len(newRecords["items"])
recordCount += newRecordCount
print(page, len(items))
items = [*items, *newRecords["items"]]
page += 1
rows = []
print("finding commits...")
percent_tracker = PercentTracker()
percent_tracker.amount = len(items)
for i in items:
percent_tracker.print_message(i["full_name"])
row = {"repoName": i["full_name"],
"lastCommitter": getLastCommit(i["full_name"])}
rows.append(row)
date = datetime.now().strftime("%Y%m%d")
csvpath = "reports/{}-{}-repos.csv".format(date, sys.argv[-1])
print()
print("writing report!")
with open(csvpath, mode='w', encoding='utf-8') as csvfile:
csv_writer = csv.DictWriter(
csvfile, fieldnames=["repoName", "lastCommitter"])
csv_writer.writeheader()
csv_writer.writerows(rows)
print(csvpath)
if not sys.argv[-1].endswith(".py"):
findTestRepos(sys.argv[-1])