-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcounter.py
More file actions
83 lines (68 loc) · 2.65 KB
/
counter.py
File metadata and controls
83 lines (68 loc) · 2.65 KB
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
import requests, dateutil.parser
import base64
import math
# Configuration
year = 2022
login_string = "YOUR_BITBUCKET_USERNAME:YOUR_BITBUCKET_APP_KEY"
workspace = "YOUR_WORKSPACE"
# By a lot of repo's 100 is recommended because of the rate limit of the API
pagelen = 100
#vars
totalCommits = 0
commitCount = 0
commits = []
print ("")
print ("Stats for {year}".format(year=year))
print ("")
login_string_bytes = login_string.encode("ascii")
base64_login_bytes = base64.b64encode(login_string_bytes)
base64_string = base64_login_bytes.decode("ascii")
r = requests.get('https://api.bitbucket.org/2.0/repositories/{workspace}/?q=updated_on>="{year}-01-01"&pagelen={pagelen}'.format(year=year, workspace=workspace, pagelen=pagelen), headers={'Authorization': 'Basic {base64_string}'.format(base64_string=base64_string)})
counterPages = 0
commitsAuthors = {}
repoCommits = {}
repos = r.json()
amountOfPages = math.ceil(repos['size'] / pagelen);
totalSize = repos['size']
while counterPages < amountOfPages:
counterPages += 1
for repo in repos["values"]:
commitLink = repo["links"]["commits"]["href"] + '?pagelen={pagelen}'.format(pagelen=pagelen)
repoSlug = repo["slug"]
# print(repoSlug)
# continue
r = requests.get(commitLink,
headers={'Authorization': 'Basic {base64_string}'.format(base64_string=base64_string)})
c = r.json()
commits.extend(c['values'])
while 'next' in c:
# print("next page")
r = requests.get("{next}".format(next=c['next']),
headers={'Authorization': 'Basic {base64_string}'.format(base64_string=base64_string)})
c = r.json()
commits.extend(c['values'])
for commit in commits:
#print("count commit")
commitDate = dateutil.parser.parse(commit['date'])
if commitDate.year == year:
commitCount += 1
if commit['author']['raw'] in commitsAuthors:
commitsAuthors[commit['author']['raw']] += 1
else:
commitsAuthors[commit['author']['raw']] = 1
if(commitCount >= 1):
repoCommits['{repo}'.format(repo=repoSlug)] = commitCount
print ("Total authors: {authors}".format(authors=commitsAuthors))
print ("Total commits in {repo}: {count}".format(repo=repoSlug, count=commitCount))
totalCommits += commitCount
#reset counters
commitCount = 0
commits = []
if "next" in repos and repos['next']is not None:
r = requests.get("{next}".format(next=repos['next']), headers={'Authorization': 'Basic {base64_string}'.format(base64_string=base64_string)})
repos = r.json()
print("")
print ("Total overall commits: {count}".format(count=totalCommits))
print ("Total authors: {authors}".format(authors=commitsAuthors))
print ("Total repoCommits: {authors}".format(authors=repoCommits))
print("")