-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindCommitForIssue.py
223 lines (190 loc) · 8.68 KB
/
FindCommitForIssue.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/env python
# -*- coding: utf-8 -*
#
# Program to List Issues from a Project
# - retrieve first comment
# - retrieve original Bugzilla ID
# - check if Bugzilla ID is mentioned in a git commit log.
#
# How to use the script:
# 0. Create a virtual environment containing modules mentioned in freeze.txt
# 1. Generate a GitHub access token:
# - on GitHub select "Settings"
# - select "Personal access tokens"
# - click "Generate new token"
# - type a token description, i.e. "bugzilla2github"
# - select "public_repo" to access just public repositories
# - save the generated token into the migration script
# 3. Copy bugzilla2github.conf.sample to FindCommitsForIssue.conf
# - Well, no, I just used the AddIssuesToProject.conf again.
# - Change all settings to fit your setup
# 4. Run the script. Good luck....
import json, requests, sys, os
import ConfigParser
from pprint import pprint, pformat
from dateutil.tz import tzlocal
import subprocess
reload(sys)
sys.setdefaultencoding('utf-8')
destination_project_name = "Cleanup"
destination_column_name = "To do"
#destination_column_name = "In progress"
page_size=100
page_size=30 # 30 seems to be the GitHub maximum
my_page=1
# read config file
configFile = "AddIssuesToProject.conf"
config = ConfigParser.RawConfigParser()
try:
config.read(configFile)
# Read GitHub vars
GITHUB_TOKEN = config.get('settings', 'github_token')
GITHUB_URL = config.get('settings', 'github_url')
GITHUB_OWNER = config.get('settings', 'github_owner')
GITHUB_REPO = config.get('settings', 'github_repo')
except:
print "Error reading configfile '" + configFile + "'."
print "Program aborted."
sys.exit(1)
def get_projects_from_github(repo, token):
# GET /repos/:owner/:repo/projects
urlparts = (str(GITHUB_URL), "repos", str(GITHUB_OWNER), str(GITHUB_REPO), "projects")
url = "/".join(urlparts)
# pprint(url)
# d=issue
headers = {"Authorization": "token " + GITHUB_TOKEN,
"Accept": "application/vnd.github.inertia-preview+json"}
# "Accept": "application/vnd.github.golden-comet-preview+json" }
# result=requests.get(url, headers=headers, data = json.dumps(d))
result = requests.get(url, headers=headers)
return result
def get_columns_from_github(repo, token, project_id):
# GET /projects/:project_id/columns
urlparts = (str(GITHUB_URL), "projects", str(project_id), "columns")
url = "/".join(urlparts)
#pprint(url)
# d=issue
headers = {"Authorization": "token " + GITHUB_TOKEN,
"Accept": "application/vnd.github.inertia-preview+json"}
# "Accept": "application/vnd.github.golden-comet-preview+json" }
# result=requests.get(url, headers=headers, data = json.dumps(d))
result = requests.get(url, headers=headers)
return result
def get_open_issues_from_github(repo, token, project_id, page=1):
# GET /repos/:owner/:repo/issues
urlparts = (str(GITHUB_URL), "repos", str(GITHUB_OWNER), str(GITHUB_REPO), "issues")
url = "/".join(urlparts)
url += "?per_page="+str(page_size)+"&page=" + str(page)
#pprint(url)
headers = {"Authorization": "token " + GITHUB_TOKEN,
"Accept": "application/vnd.github.inertia-preview+json"}
# "Accept": "application/vnd.github.symmetra-preview+json"}
# result=requests.get(url, headers=headers, data = json.dumps(d))
result = requests.get(url, headers=headers)
return result
def get_issue_from_content_url(issue_url):
headers = {"Authorization": "token " + GITHUB_TOKEN,
"Accept": "application/vnd.github.inertia-preview+json"}
# "Accept": "application/vnd.github.symmetra-preview+json"}
# result=requests.get(url, headers=headers, data = json.dumps(d))
result = requests.get(issue_url, headers=headers)
return result
def get_project_cards_from_github(repo, token, column_id,page=1):
# GET /projects/columns/:column_id/cards
urlparts = (str(GITHUB_URL), "projects", "columns", str(column_id), "cards")
url = "/".join(urlparts)
url += "?per_page="+str(page_size)+"&page=" + str(page)
#pprint(url)
headers = {"Authorization": "token " + GITHUB_TOKEN,
"Accept": "application/vnd.github.inertia-preview+json"}
# "Accept": "application/vnd.github.symmetra-preview+json"}
# result=requests.get(url, headers=headers, data = json.dumps(d))
result = requests.get(url, headers=headers)
return result
print "Start"
print "====="
# Find all defined projects
projects = get_projects_from_github(GITHUB_REPO, GITHUB_TOKEN)
# pprint(projects)
if projects.status_code == 200:
# pprint(projects.json())
for project in projects.json():
print "Project " + project["name"] + " has id: " + str(project["id"])
# Find all defined columns
# my_project_id=1327353
# print my_project_id
my_project_id = [project["id"] for project in projects.json() if project["name"] == destination_project_name][0]
print "My project id is " + str(my_project_id)
# print my_project_id
columns = get_columns_from_github(GITHUB_REPO, GITHUB_TOKEN, my_project_id)
# pprint(columns)
# pprint(columns.json())
my_column_id = [column["id"] for column in columns.json() if column["name"] == destination_column_name][0]
print "My column id is " + str(my_column_id)
cards=get_project_cards_from_github(GITHUB_REPO,GITHUB_TOKEN,my_column_id,my_page)
#pprint(cards.json())
for card in cards.json():
if card.get("content_url") is not None:
github_issue_url=card.get("content_url")
#print "card references issue "+issue_url
github_issue_id=github_issue_url.split("/")[-1]
#print "GitHub (not Bugzilla!) issue we are looking for is "+str(issue)
# Now get first message from url and get original Bugzilla id
github_issue=get_issue_from_content_url(github_issue_url)
#pprint(issue.json())
body=github_issue.json().get("body")
#print body
bugzillastring=body.split('\n', 1)[0]
# Let's skip issues originally not form bugzilla
if "Original Bugzilla Bug ID" in bugzillastring:
start = bugzillastring.index("[") + 1
end = bugzillastring.index("]", start)
bugzilla_id=bugzillastring[start:end]
#print "Looking for bugzilla ID: " + str(bugzilla_id)
print ".",
#print "Is there a commit message referencing issue "+str(bugzilla_id)+"?"
#print "issue url= https://github.com/LibrePlan/libreplan/issues/"+str(issue)
#print "issue url= " + issue_url
mydir="/home/jeroen/libreplan"
cmd="git log --all --grep='#"+str(bugzilla_id)+"' "
#cmd = "git log --all --grep='#" + str(bugzilla_id) + "' | grep '^commit' "
#cmd = "git log --all --grep='#" + str(issue) + "'"
#print cmd
os.chdir("/home/jeroen/libreplan")
p=subprocess.Popen(cmd,
cwd=mydir,
stdout=subprocess.PIPE,
shell=True)
out, err = p.communicate()
#output=subprocess.check_output([cmd,])
# if "commit" in output:
#pprint(out)
if "commit" in out:
print "\n"
print "=" * 80
outs=out.split("\n")
for commitstr in outs:
print commitstr
print "-" * 80
print "issue url= https://github.com/LibrePlan/libreplan/issues/"+str(github_issue_id)
#print "issue url= " + github_issue_url
cmd = "git log --all --grep='#" + str(bugzilla_id) + "' | grep '^commit' "
#print cmd
os.chdir("/home/jeroen/libreplan")
p=subprocess.Popen(cmd,
cwd=mydir,
stdout=subprocess.PIPE,
shell=True)
out, err = p.communicate()
#output=subprocess.check_output([cmd,])
# if "commit" in output:
#pprint(out)
outs=out.split("\n")
for commitstr in outs:
print commitstr
#os.system(cmd)
#sys.exit(1)
else:
print "Not an original Bugzilla issue."
print "\n"
print "Done checking " + str(len(cards.json())) + " cards of page " + str(my_page)+ "."