Skip to content

Commit 07c8415

Browse files
author
Jeroen Baten
committed
saving attachments works
1 parent ced2273 commit 07c8415

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

bugzilla2github.conf.sample

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ github_url = "https://api.github.com"
44
github_owner = "kwoot"
55
github_repo = "bz2gh"
66

7+
78
# Database settings
89
engine = django.db.backends.postgresql_psycopg2
910
dbhost = localhost

bugzilla2github.py

+21-9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
reload(sys)
2828
sys.setdefaultencoding('utf-8')
2929

30+
path_to_dir="./attachments/"
31+
3032
# read config file
3133
configFile = "bugzilla2github.conf"
3234
config = ConfigParser.RawConfigParser()
@@ -63,6 +65,8 @@ def read_bugs(conn):
6365
results = cur.fetchall()
6466
except (Exception, psycopg2.DatabaseError) as error:
6567
print(error)
68+
results = None
69+
colnames = None
6670
return results,colnames
6771

6872
def read_comments(conn,bug_id):
@@ -71,11 +75,13 @@ def read_comments(conn,bug_id):
7175
# create a new cursor object
7276
cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
7377
# execute the SELECT statement
74-
cur.execute(""" SELECT * from longdescs where bug_id=%s limit 1""" % bug_id,)
78+
cur.execute(""" SELECT * from longdescs where bug_id=%s """ % bug_id,)
7579
colnames = [desc[0] for desc in cur.description]
7680
results = cur.fetchall()
7781
except (Exception, psycopg2.DatabaseError) as error:
7882
print(error)
83+
results = None
84+
colnames = None
7985
return results,colnames
8086

8187
def read_attachment(conn,attach_id):
@@ -89,21 +95,23 @@ def read_attachment(conn,attach_id):
8995
#print query
9096
cur.execute(""" SELECT * from attachments where attach_id=%s """ % attach_id, )
9197
colnames = [desc[0] for desc in cur.description]
92-
results = cur.fetchall()
98+
result = cur.fetchone()
9399
except (Exception, psycopg2.DatabaseError) as error:
94100
print(error)
95-
return results,colnames
101+
result=None
102+
colnames=None
103+
return result,colnames
96104

97-
def save_attachment(conn,attach_id,filename):
105+
def save_attachment(conn,attach_id,bug_id,comment_id,filename):
98106
""" read BLOB data from a table """
99107
#conn = None
100108
try:
101109
# create a new cursor object
102110
cur = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
103111
# execute the SELECT statement
104-
cur.execute(""" SELECT thedata FROM atatch_data WHERE id=%s """,(attach_id,))
112+
cur.execute(""" SELECT thedata FROM attach_data WHERE id=%s """,(attach_id,))
105113
blob = cur.fetchone()
106-
open(path_to_dir + blob[0] + '.' + blob[1], 'wb').write(blob[2])
114+
open(path_to_dir + str(bug_id) + '_' + str(comment_id) + "_"+ filename, 'wb').write(blob[0])
107115
# close the communication with the PostgresQL database
108116
cur.close()
109117
except (Exception, psycopg2.DatabaseError) as error:
@@ -132,13 +140,17 @@ def save_attachment(conn,attach_id,filename):
132140
for comment in comments:
133141
comment_id=comment["comment_id"]
134142
attach_id=comment["extra_data"]
135-
if attach_id>0:
143+
if attach_id is not None:
136144
print " attach_id",attach_id
137145
# check attachment record for this comment
138146
attachment,colnames=read_attachment(conn,attach_id)
139147
#for activity in activities:
140-
pprint(attachment)
141-
result=save_attachment(attach_id,bug_id,comment_id)
148+
#pprint(colnames)
149+
#pprint(attachment)
150+
#attachment=attachment.pop
151+
filename=attachment["filename"]
152+
print " We have an attachment:",filename
153+
result=save_attachment(conn,attach_id,bug_id,comment_id,filename)
142154

143155
#pprint(comment)
144156
print " comment_id",comment["comment_id"]

0 commit comments

Comments
 (0)