How can I get the total number of commits across all our team's projects, in the last 12 months?

Choc Chip Digital December 10, 2013

For an end-of-year report I want to show how many commits our team has made in total this year, across all our BitBucket projects (approx 140 projects).

Is there a way to get a total number of commits for all the projects together?

PS I know this isn't a measure of team effectiveness, but it would be a fun stat to know. Another one would be the total number of lines that have been added or changed, across all projects, or other things like that.

5 answers

1 accepted

3 votes
Answer accepted
Jeff Thomas
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
December 11, 2013

I wanted to do this for my stuff, so I whipped up a quick (and dirty) Python script to get these stats. All of my repositories are under by username, so it was easy to find all the commits. If you let me know how yours are setup, I can probably modify the script. It runs slow, but I'll work on improving it. Let me know if you have any questions, I'll be working on improving it soon.

You'll need to install the requests and python-dateutil modules to run the script.

Sample output

Stats for 2013

Total commits in username/repo1: 21
	Lines added: 1139
	Lines removed: 59

Total commits in username/repo2: 2
	Lines added: 9593
	Lines removed: 0

Total commits in username/repo3: 4
	Lines added: 772
	Lines removed: 75

Total commits: 27
Total lines added: 11504
Total lines removed: 134

Script

import requests, dateutil.parser

baseUrlv2 = "https://bitbucket.org/api/2.0"
baseUrlv1 = "https://bitbucket.org/api/1.0"

username = ""
password = ""
year = 2013

totalCommits = 0
totalAdd = 0
totalRemove = 0
overallAdd = 0
overallRemove = 0
commitCount = 0
commits = []

print ""
print "Stats for {year}".format(year=year)
print ""

r = requests.get("{base}/user/repositories/".format(base=baseUrlv1),
	auth=(username, password))

repos = r.json()

for repo in repos:
	repoSlug = repo['slug']
	r = requests.get("{base}/repositories/{username}/{repo}/commits".format(base=baseUrlv2, username=username, repo=repoSlug),
	auth=(username, password))

	c = r.json()
	commits.extend(c['values'])

	while 'next' in c:
		r = requests.get("{next}".format(next=c['next']), 
			auth=(username, password))
		c = r.json()
		commits.extend(c['values'])

	for commit in commits:
		commitDate = dateutil.parser.parse(commit['date'])
		if commitDate.year == year:
			commitCount += 1

			r = requests.get("{base}/repositories/{username}/{repo}/changesets/{hash}/diffstat/".format(base=baseUrlv1, username=username, repo=repoSlug, hash=commit['hash']),
				auth=(username, password))
			
			try:
				stats = r.json()
			except ValueError:
			    # decoding failed
			    continue

			for stat in stats:
				try:
					totalAdd += stat['diffstat']['added']
					totalRemove += stat['diffstat']['removed']
				except TypeError:
					continue

	print "Total commits in {user}/{repo}: {count}".format(user=username, repo=repoSlug, count=commitCount)		
	print "\tLines added: {add}".format(add=totalAdd)
	print "\tLines removed: {remove}\n".format(remove=totalRemove)
	totalCommits += commitCount	
	overallAdd += totalAdd
	overallRemove += totalRemove
	#reset counters
	commitCount = 0
	totalAdd = 0
	totalRemove = 0
	commits = []

print ""
print "Total commits: {count}".format(count=totalCommits)
print "Total lines added: {count}".format(count=overallAdd)
print "Total lines removed: {count}".format(count=overallRemove)


Choc Chip Digital December 12, 2013

Wow thanks Jeff, this looks perfect! I'll try it out and report back. But I'm marking this as accepted because it looks like it will do what I need.

David Lockie November 28, 2014

Hi Jeff, Thanks for this. I had to add a check after line 32 because repos with no commits were causing an error. if len(c) > 1: Hope that helps someone!

joe_midi July 5, 2017

How would this work for a team account?

Javier Escalante Alfaro October 24, 2017

@joe_midi Hi I believe i ran into that problem today, because all of the repos had different owners it would return a 404 on the second request for every repo, to solve it i changed the following in lines 28 and 29:

repoSlug = repo['slug']
r = requests.get("{base}/repositories/{username}/{repo}/commits".format(base=baseUrlv2, username=username, repo=repoSlug),
auth=(username, password))

For this one:

repoOwner = repo['owner']
repoSlug = repo['slug']
url = "{base}/repositories/{owner}/{repo}/commits".format(base=baseUrlv2, username=repoOwner, repo=repoSlug)
r = requests.get(url, auth=(username, password))

The difference is in the parameter username witch is no longer equal to the account owner you specify on top but to the owner of the repo it is currently looping, i hope it helps.

mayankdiatm January 17, 2018

Just out of curiosity, is this API works for bitbucket server or bitbucket cloud

("{base}/user/repositories/".format(base=baseUrlv1)

I am using bitbucket server and that too API version 1.0. I cannot find this API listed .Bitbucket version : Atlassian Bitbucket v5.2.0

Like Ansar Rezaei likes this
Ansar Rezaei
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 28, 2018

Same here. Did you use this with bitbucket server?

2 votes
Cao Van Thanh February 4, 2021

I just extended @Jeff Thomas  script for API v2.0 that works on 2021.

You can use it from: https://github.com/bc67da8d/bitbucket-commit-counter

Kamogelo_Mashishi September 20, 2021

Does this actually work for you?

I get a 401 status code when I try run this. Seems it has to do with the Basic Authentication :\ 

mikevanvossen December 7, 2022

I just extended your script, so that it worked for me (per workspace):

https://github.com/mikev0546/bitbucket-commit-counter

0 votes
GopJay November 29, 2020

Hi Jeff,

I am looking to generate a report in JIRA for the commits made in the last 12 months which contains fileName, commit Id, JIRA ID, repository Name, Lines changed. Please let me know if you have updated script

 

Thanks,

Jay

0 votes
Deleted user July 7, 2020

This is more of a Quesiton for Jeff. Both base URL V1 and V2 seem unsupported now. Is there an update to your script?

0 votes
myphpdelights December 11, 2013

If you install https://github.com/visionmedia/git-extrasgit extras on your enviroment machine, you can check the history of all your commits and logs.

git summary

Choc Chip Digital December 12, 2013

Hi Rey

I notice that 'git summary' still only gives me stats for a single repo when used on the local machine. Is there a way to get it to show a total for all commits across every project in a folder, for example?

Choc Chip Digital December 12, 2013

Hi Ray, when you say 'environment machine', what do you mean, exactly? The source is hosted BitBucket and we have multiple devs who each work from their individual computers.

It does look like a useful tool though, I'll check it out.

Narendra Jha June 30, 2014

Hi,

I was also looking thsi kind of script for get commit report monthly wise.

I need no of commit report by monthly wise . could You pls suggets,..where and what are need to modify in script

Thank You

Gaurav Agrawal November 26, 2019

The python script does not work for Bitbucket server/ datacenter edition

Anastasia Voronova November 27, 2019

Hi Gaurav,

 

For Bitbucket Server/Data Center you may use Awesome Graphs for Bitbucket app.

I.e., the Activity graph shows total number of commits per project or repository during the selected period of time up to 12 months.

Gaurav Agrawal November 27, 2019

Thanks for this Anastasia. Do we have any opensource version of it?

Like Anastasia Voronova likes this
Anastasia Voronova November 27, 2019

Gaurav, we don't have an opensource version, but you can try it for free and even after the end of the trial period some of the basic features remain available for you. 

Gaurav Agrawal December 3, 2019

Hi Anastasia, Thanks. That's wonderful to know. Can I know what all basic features will remain available in Bitbucket post trial ends?

Anastasia Voronova December 3, 2019

Hi Gaurav!

Sure! The Contributors, Commits and Punchcard graphs will remain. 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events