Last active
August 7, 2018 07:02
-
-
Save mayankdiatm/717079d6fb9de0347549dceaf070c1d6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cookielib | |
import urllib2 | |
import json | |
import base64 | |
import simplejson | |
import ast | |
import datetime | |
import xlsxwriter | |
import subprocess | |
import os | |
import commands | |
###### | |
import sys | |
#reload(sys) | |
#sys.setdefaultencoding('utf8') | |
###### | |
class API(): | |
api_url = 'https://bitbucket.zzz.zzz/rest/api/1.0/' | |
def __init__(self, username, password, proxy=None): | |
encodedstring = base64.encodestring("%s:%s" % (username, password))[:-1] | |
self._auth = "Basic %s" % encodedstring | |
self._opener = self._create_opener(proxy) | |
def _create_opener(self, proxy=None): | |
cj = cookielib.LWPCookieJar() | |
cookie_handler = urllib2.HTTPCookieProcessor(cj) | |
if proxy: | |
proxy_handler = urllib2.ProxyHandler(proxy) | |
opener = urllib2.build_opener(cookie_handler, proxy_handler) | |
else: | |
opener = urllib2.build_opener(cookie_handler) | |
return opener | |
def get_projects(self): | |
project_list = [] | |
query_url = self.api_url + 'projects' | |
try: | |
req = urllib2.Request(query_url, None, {"Authorization": self._auth}) | |
handler = self._opener.open(req) | |
except urllib2.HTTPError, e: | |
print e.headers | |
raise e | |
t = simplejson.load(handler) | |
print len(t['values']) | |
t1 = t['values'] | |
i = 0 | |
while i < len(t['values']): | |
print t1[i]['key'] + " " + t1[i]['name'] | |
project_list.append(t1[i]['key']) | |
i = i + 1 | |
with open('projects.txt', 'w') as output_file: | |
output_file.write("\n".join(map(lambda x: str(x), project_list))) | |
def get_all_repositories_of_a_project(self): | |
proj_repo_map = {} | |
with open('projects.txt', 'r') as input_file: | |
for line in iter(input_file): | |
print("Project Key {}".format(line.strip())) | |
query_url = self.api_url + 'projects' + '/' + line.strip() + '/' + 'repos' | |
print query_url | |
try: | |
req = urllib2.Request(query_url, None, {"Authorization": self._auth}) | |
handler = self._opener.open(req) | |
except urllib2.HTTPError, e: | |
print e.headers | |
raise e | |
t = simplejson.load(handler) | |
print "Total Number of Repositories are :", len(t['values']) | |
t1 = t['values'] | |
i = 0 | |
repository_list = [] | |
while i < len(t['values']): | |
print t1[i]['slug'] | |
repository_list.append(t1[i]['slug']) | |
i = i + 1 | |
proj_repo_map[line.strip()] = repository_list | |
print proj_repo_map | |
with open('project_repositories.txt', 'w') as output_file: | |
output_file.write(str(proj_repo_map)) | |
def get_all_branches_of_a_repository(self): | |
with open('project_repositories.txt', 'r') as input_file: | |
c = input_file.read() | |
whip = ast.literal_eval(c) | |
#print whip | |
#print type(whip) | |
#for key in whip: | |
#print "key: %s , value: %s" % (key, whip[key]) | |
keys = whip.keys() | |
workbook = xlsxwriter.Workbook('xyz.xlsx') | |
bold = workbook.add_format({'bold': 1}) | |
if '{proj_key}' in keys: | |
print "Branches of {proj_key} are :", whip['{proj_key}'] | |
for item in iter(whip['{proj_key}']): | |
print "Getting latest commit for {proj_key} Projects for the branches under " + item + " repository" + \ | |
'\n' | |
query_url = self.api_url + 'projects/{proj_key}/repos/' + item + '/branches' | |
print query_url | |
try: | |
req = urllib2.Request(query_url, None, {"Authorization": self._auth}) | |
handler = self._opener.open(req) | |
except urllib2.HTTPError, e: | |
print e.headers | |
raise e | |
t = simplejson.load(handler) | |
print "Total number of Branches under " + item + " are: ", len(t['values']) | |
t1 = t['values'] | |
i = 0 | |
worksheet = workbook.add_worksheet(str(item)) | |
worksheet.write('A1', 'Branches', bold) | |
worksheet.write('B1', 'Commited By', bold) | |
worksheet.write('C1', 'Latest Commit ID', bold) | |
worksheet.write('D1', 'Latest Commit Message', bold) | |
worksheet.write('E1', 'Date & Time of Latest Commit', bold) | |
##### New Changes ######## | |
worksheet.write('F1', 'Total Commits in Branch', bold) | |
worksheet.write('G1', 'Count in September 2017 ', bold) | |
worksheet.write('H1', 'Count in October 2017 ', bold) | |
worksheet.write('I1', 'Count in November 2017 ', bold) | |
worksheet.write('J1', 'Count in December 2017 ', bold) | |
row = 1 | |
col = 0 | |
clone_counter = 0 | |
while i < len(t['values']): | |
print t1[i]['displayId'] + ' :- ' + t1[i]['latestChangeset'] | |
query_url = self.api_url + 'projects/{proj_key}/repos/' + item + '/commits/' + t1[i][ | |
'latestChangeset'] | |
print query_url | |
try: | |
req = urllib2.Request(query_url, None, {"Authorization": self._auth}) | |
handler = self._opener.open(req) | |
except urllib2.HTTPError, e: | |
print e.headers | |
raise e | |
commit = simplejson.load(handler) | |
#print commit | |
#print len(commit) | |
h = commit['committer']['emailAddress'] | |
print "Following Commit was commited by" , h | |
date_of_commit = commit['authorTimestamp']/1000 | |
q = ( | |
datetime.datetime.fromtimestamp( | |
int(date_of_commit) | |
).strftime('%Y-%m-%d %H:%M:%S') | |
) | |
print "Date and Time of Commit :", q | |
print "####################################################################" + '\n' | |
#col = 0 | |
###### New Changes ####### | |
os.chdir('C:\clone') | |
print os.getcwd() | |
command = 'git clone --bare ssh://[email protected]/{proj_key}/'+item+'.git' | |
print command | |
if clone_counter == 0: | |
os.system(command) | |
clone_counter = clone_counter+1 | |
os.chdir(str(item)+'.git') | |
if t1[i]['displayId'] == 'master': | |
git_string = 'master' | |
else: | |
git_string = '^'+t1[i]['displayId'] | |
print git_string | |
git_command = 'git rev-list --count HEAD' + " " +git_string | |
commit_count = subprocess.check_output(git_command) | |
branch_creation_command = 'git for-each-ref --sort=-committerdate --format=%(committerdate:iso8601) refs/heads/' | |
out = subprocess.check_output(branch_creation_command) | |
branch_string = t1[i]['displayId'] | |
sept_count = 'git log --oneline --after="2017-09-01" --before="2017-09-30"' + " " + branch_string + " " + '| wc -l' | |
sept_commit_count = commands.getstatusoutput(sept_count)[0] | |
print sept_commit_count | |
oct_count = 'git log --oneline --after="2017-10-01" --before="2017-10-31"' + " " + branch_string + " " + '| wc -l' | |
oct_commit_count = commands.getstatusoutput(oct_count)[0] | |
nov_count = 'git log --oneline --after="2017-11-01" --before="2017-11-30"' + " " + branch_string + " " + '| wc -l' | |
nov_commit_count = commands.getstatusoutput(nov_count)[0] | |
dec_count = 'git log --oneline --after="2017-12-01" --before="2017-12-31"' + " " + branch_string + " " + '| wc -l' | |
dec_commit_count = commands.getstatusoutput(dec_count)[0] | |
########################## | |
worksheet.write_string(row, col, t1[i]['displayId']) | |
worksheet.write_string(row, col+1, h) | |
worksheet.write_string(row, col + 2, t1[i]['latestChangeset']) | |
worksheet.write_string(row, col+3, commit['message']) | |
worksheet.write_string(row, col+4, q) | |
worksheet.write_string(row, col+5, commit_count) | |
worksheet.write_string(row, col+6, str(sept_commit_count)) | |
worksheet.write_string(row, col+7, str(oct_commit_count)) | |
worksheet.write_string(row, col+8, str(nov_commit_count)) | |
worksheet.write_string(row, col+9, str(dec_commit_count)) | |
row += 1 | |
i = i + 1 | |
#os.chdir('C:\clone') | |
#print os.getcwd() | |
#branch_creation_command = 'git for-each-ref --sort=-committerdate --format=\'%1B[32m%(committerdate:iso8601) %1B[34m%(committerdate:relative) %1B[0;m%(refname:short)\' refs/heads/' | |
#print command | |
#os.system(command) | |
#os.chdir(str(item)+'.git') | |
#proc = subprocess.Popen(['git', 'log', '--pretty=format:"%h - %an, %ar : %s"'], | |
# stdout=subprocess.PIPE, shell=True) | |
#(out, err) = proc.communicate() | |
#print "program output:", out | |
#out = subprocess.check_output(branch_creation_command) | |
#print type(out) | |
#worksheet.write_string('H1','Branch Creation Date ',bold) | |
#row=row+3 | |
#for line in out.splitlines(): | |
#row=row+1 | |
#line = unicode(line,errors='ignore') | |
#worksheet.write_string(row, col ,line) | |
#worksheet.write('A10', 'History', bold) | |
#worksheet.write_string(row+3, 0, 'History') | |
#worksheet.write_string(row+4, 0 , out) | |
workbook.close() | |
def get_history_of_commits(self): | |
year = 2017 | |
print "" | |
print "Stats for {year}".format(year=year) | |
print "" | |
#query_url = self.api_url + 'projects/{proj_key}/repos/{repo_key}/commits' | |
query_url = 'https://bitbucket.zzz.zzz/rest/api/1.0/projects/{proj_key}/repos/{repo_key}/commits/?until=master' | |
print query_url | |
try: | |
req = urllib2.Request(query_url, None, {"Authorization": self._auth}) | |
handler = self._opener.open(req) | |
except urllib2.HTTPError, e: | |
print e.headers | |
raise e | |
print simplejson.load(handler) | |
def get_issues(self, username, repository): | |
query_url = self.api_url + 'repositories/%s/%s/issues/' % (username, repository) | |
try: | |
req = urllib2.Request(query_url, None, {"Authorization": self._auth}) | |
handler = self._opener.open(req) | |
except urllib2.HTTPError, e: | |
print e.headers | |
raise e | |
print json.load(handler) | |
api = API(username='abc', password='abc') | |
#api.get_projects() | |
#api.get_all_repositories_of_a_project() | |
api.get_all_branches_of_a_repository() | |
#api.get_history_of_commits() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
First two methods for JIRA also.