-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDocAndCode.py
193 lines (145 loc) · 5.69 KB
/
DocAndCode.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import requests
import json
import os
import re
import datetime
import shutil
#######################################################
################ CONFIGURATION ################
#### Set below variables before running this ####
#######################################################
# @OAUTHTOKEN: Your GitHub Personal Access Token
# @repoOwner: Repository Owner
# e.g. torvalds
# @repoName: Repository Name
# e.g. linux
#######################################################
OAUTHTOKEN = ''
repoOwner = ''
repoName = ''
#######################################################
repoInfo = '{}/{}'.format(repoOwner, repoName)
dirPath = ''
reportDir = '{}Report'.format(dirPath)
headers = {
'User-Agent': 'request'
}
issuesJSON = {}
commitsJSON = {}
def cleanFiles():
print('Preparando arquivos.', end=' ')
if os.path.exists(reportDir):
shutil.rmtree(reportDir)
print('OK')
def checkIfDirExists():
if not os.path.exists(reportDir):
os.makedirs(reportDir)
def getIssues():
print("Coletando informações de Issues.", end=' ')
url = 'https://api.github.com/repos/{}/issues?state=all'.format(repoInfo)
r = requests.get(url = url, headers = headers)
issues = r.json()
checkIfDirExists()
for issue in issues:
assignees = []
for assignee in issue['assignees']:
assignees.append({
'login': assignee['login'],
'url': assignee['html_url']
})
issuesJSON[issue['number']] = {
'number': issue['number'],
'title': issue['title'],
'body': issue['body'],
'url': issue['url'],
'assignees': assignees,
'labels': issue['labels']
}
printIssuesToFile(issuesJSON[issue['number']])
updateReport(issuesJSON[issue['number']])
print("OK")
def printIssuesToFile(myjson):
issueFile = open(reportDir + '/' + 'issue_' + str(myjson['number']) + '.md', 'w+')
issueFile.write('## [#{} - {}]({})\n\n'.format(myjson['number'], myjson['title'], myjson['url']))
issueFile.write('{}\n\n'.format(myjson['body']))
issueFile.write('* **Assignees**:\n')
if len(myjson['assignees']) != 0:
for assignee in myjson['assignees']:
issueFile.write(' * [{}]({})\n'.format(assignee['login'], assignee['url']))
issueFile.write('\n')
else:
issueFile.write('\n')
issueFile.write('* **Labels**:\n')
if len(myjson['labels']) != 0:
for label in myjson['labels']:
issueFile.write(' * <span style=\"background-color:#{};\">{}</span>\n'.format(label['color'], label['name']))
issueFile.write('\n')
else:
issueFile.write('\n')
issueFile.write('## Commits\n')
issueFile.close()
def getCommits():
print("Coletando informações de Commits.", end=' ')
url = 'https://api.github.com/repos/{}/commits?access_token={}'.format(repoInfo, OAUTHTOKEN)
r = requests.get(url = url, headers = headers)
commits = r.json()
checkIfDirExists()
for commit in commits:
url = 'https://api.github.com/repos/{}/commits/{}?access_token={}'.format(repoInfo, commit['sha'], OAUTHTOKEN)
r = requests.get(url = url, headers = headers)
commitData = r.json()
additions = 0;
deletions = 0;
files = []
for commitFiles in commitData['files']:
files.append({
'filename': commitFiles['filename'],
'url': commitFiles['blob_url']
})
additions += commitFiles['additions']
deletions += commitFiles['deletions']
date = commitData['commit']['author']['date'].split('T')[0]
date = datetime.datetime.strptime(date, '%Y-%m-%d').strftime('%d/%m/%y')
commitsJSON[commitData['sha']] = {
'date': date,
'message': commitData['commit']['message'],
'author': {
'login': commitData['author']['login'],
'url': commitData['author']['html_url'],
},
'link': commitData['html_url'],
'files': files,
'additions': additions,
'deletions': deletions
}
printCommitsToFile(commitsJSON[commitData['sha']])
print('OK')
def printCommitsToFile(myjson):
tags = re.findall(r"#[0-9]+", myjson['message'])
for tag in tags:
filePath = '{}/issue_{}.md'.format(reportDir, tag.replace('#', ''))
if os.path.exists(filePath):
issueFile = open(filePath, 'a')
issueFile.write('\n**[{}]({})**\n'.format(myjson['message'], myjson['link']))
issueFile.write('* **Data**: {}\n'.format(myjson['date']))
issueFile.write('* **Autor**: [{}]({})\n'.format(myjson['author']['login'], myjson['author']['url']))
issueFile.write('* **Adições**: {}\n'.format(myjson['additions']))
issueFile.write('* **Remocões**: {}\n'.format(myjson['deletions']))
issueFile.write('* **Arquivos alterados**:\n')
for fileData in myjson['files']:
issueFile.write(' * [{}]({})\n'.format(fileData['filename'], fileData['url']))
issueFile.close()
def createReportFile():
report = open('{}DocAndCode.md'.format(dirPath), 'w+')
report.write('## Relatório Doc&Code\n\n')
report.write('### Issues:\n\n')
report.close()
def updateReport(myjson):
report = open('{}DocAndCode.md'.format(dirPath), 'a')
report.write('[#{} - {}](issue_{})\n\n'.format(myjson['number'], myjson['title'], myjson['number']))
report.close()
cleanFiles()
createReportFile()
getIssues()
getCommits()
print('Processo Finalizado\n\nRelatório Doc&Code gerado em {}\n'.format(dirPath))