Commits

illume committed 6f9eed5

Added a conversion class for converting the fields.
Added a create_attachments() function which creates the attachments in folders.

  • Participants
  • Parent commits 9a4e6c6

Comments (0)

Files changed (1)

File create_issues.py

 
 Add --user username:password afterwards to authenticate, otherwise it does not work.
 
+
+You probably need to set up the components, versions and other parts used in your bugzilla.
+
+
+
 """
+import os,sys,urllib, pprint, glob, base64
+import xml.etree.cElementTree as ElementTree
 
 
-import os,sys,urllib
-import pprint
-import glob
-import xml.etree.cElementTree as ElementTree
+ATTACHMENT_BASE_URL = "http://www.pygame.org/old_bug_attachments/"
+
 
 issue_keys = "title content component milestone version responsible status kind".split()
 valid_status = "new open resolved hold invalid duplicate wontfix".split()
 valid_kind = "bug enhancement proposal".split()
 
+
+
+
+
+
 def validate_issue(issue):
 	for k in issue_keys:
 		if k not in issue:
 
         # create a list of long_desc since the xml2dict func does not work correctly here.
         xmldict['bug']['long_desc'] = []
+        xmldict['bug']['attachment'] = []
         for x in tree.getroot().getiterator():
+            #print x.tag
             if x.tag == 'long_desc':
                 xmldict['bug']['long_desc'].append(XmlDictConfig(x))
+            if x.tag == 'attachment':
+                xmldict['bug']['attachment'].append(XmlDictConfig(x))
+            if x.tag == 'data':
+                #print XmlDictConfig(x)
+                xmldict['bug']['attachment'][-1]['data'] = XmlDictConfig(x)
+                xmldict['bug']['attachment'][-1]['data']['text'] = x.text
 
 
-        bugs[xmldict['bug']['bug_id']] = xmldict['bug']
+
+        bugs[int(xmldict['bug']['bug_id'])] = xmldict['bug']
 
     return bugs
 
                 rep_platform= set([]), 
                 priority= set([]), 
                 bug_severity= set([]), 
+                assigned_to = set([]), 
                 target_milestone= set([])
                 )
     for bug_id, b in bugs.items():
         for k in info.keys():
             if k in b:
-                info[k].add(b[k])
+                if k == "assigned_to":
+                    if 'name' in b[k]:
+                        info[k].add(b[k]['name'])
+                    else:
+                        info[k].add(b[k])
+                else:
+                    info[k].add(b[k])
 
     return info
 
-def convertBug(bug):
-    """ converts a bugzilla bug into a bitbucket issue.
+
+
+
+
+
+
+class ConvertBug(object):
+    """ For each issue field, there is a method with the same name.
+
+        The method takes a bug as input, and outputs the field value.
     """
-    i = dict(title="just a test issue", 
-             content="some content to test", 
-             component="pygameissues", 
-             milestone="bitbucketmigration", 
-             version="1.9.2", 
-             responsible="illume", 
-             status="new", 
-             kind="bug")
+    def convert(self, bug):
+        """ converts a bugzilla bug into a bitbucket issue.
+        """
+        i = dict(title="just a test issue", 
+                 content="some content to test", 
+                 component="pygameissues", 
+                 milestone="bitbucketmigration", 
+                 version="1.9.2", 
+                 responsible="illume", 
+                 status="new", 
+                 kind="bug")
 
+        for k in i.keys():
+            i[k] = getattr(self, k)(bug)
+        return i
 
+    def title(self, bug):
+        return bug['short_desc']
+
+    def content(self, bug):
+        # concat each long_desc together.
+        content = u""
+        for long_desc in bug['long_desc']:
+            #pprint.pprint( long_desc )
+            c = u"%s\n%s\n\n%s\n\n\n" % (long_desc['bug_when'], 
+                                       long_desc['who']['name'], 
+                                       long_desc['thetext'])
+            # we create links to attachments, since the bitbucket API can not add attachments.
+            if bug['attachment']:
+                c += "Attachments:\n"
+                for attachment in bug['attachment']:
+                    c += "%s\n" % ("/".join([ATTACHMENT_BASE_URL.rstrip("/"), 
+                                    attachment['attachid'], 
+                                    urllib.quote(attachment['filename'])]))
+            content += c
+        return content
+
+    def component(self, bug):
+
+        if bug['component'] == '(Miscellaneous)':
+            return 'other'
+        if bug['component'].startswith('pygame.'):
+            return bug['component'].replace('pygame.', '')
+
+        return ''
+
+    def milestone(self, bug):
+        'NOTE: empty, since we did not use milestones'
+        return ''
+
+    def version(self, bug):
+        return bug['version']
+
+    def responsible(self, bug):
+        responsible = {'illume': 'illume',
+               'Lenard Lindstrom': 'llindstrom'
+               }
+        if 'name' in bug['assigned_to']:
+            assigned_to = bug['assigned_to']['name']
+        else:
+            assigned_to = bug['assigned_to']
+         
+        if assigned_to in responsible:
+            return responsible[assigned_to]
+        else:
+            return 'illume'
+
+
+    def status(self, bug):
+        bugzilla_status = {'ASSIGNED': 'open', 
+                           'CLOSED': 'resolved', 
+                           'NEW' : 'new', 
+                           'RESOLVED': 'resolved'}
+        # "new open resolved hold invalid duplicate wontfix"
+        return bugzilla_status[ bug['bug_status'] ]
+
+    def kind(self, bug):
+        return 'bug'
+
+    def priority(self, bug):
+        'NOTE: this is not supported by API yet'
+        priorities = "Blocker Critical Major Minor Trivial".split()
+        bugzilla_ps = ['P1', 'P2', 'P3', 'P4', 'P5']
+        return priorities[ bugzilla_ps.index(bug['priority']) ]
+
+
+
+def create_attachments(bugs):
+    """ create_attachments(bugs) decodes the attachments and creates a 
+          directory of files outputting the attachments in there
+    """
+    join = os.path.join
+    def mkdir(s):
+        #print 'making dir:%s:' % s
+        return os.mkdir(s)
+
+    if not os.path.exists('attachment'):
+        mkdir('attachment')
+
+    for bug_id, bug in bugs.items():
+        #if "attachment" in bug and bug['attachment']:
+        #    pprint.pprint(bug)
+        #    raise
+
+        for attachment in bug['attachment']:
+            attach_path = join('attachment', str(attachment['attachid']))
+            if not os.path.exists(attach_path):
+                mkdir(attach_path)
+
+            fname = join(attach_path, attachment['filename'])
+            if not attachment['data']['encoding'] == 'base64':
+                raise ValueError('unknown encoding')
+            encoded_string = attachment['data']['text']
+            decoded = base64.decodestring(encoded_string)
+
+            f = open(fname, "wb")
+            f.write(decoded)
+            f.close()
 
 
 
 bugs = create_bugzilla_dicts()
-#pprint.pprint( bugs['3'] )
 
-info = extractInfo(bugs)
-pprint.pprint(info)
+# sort the bugs via id.
+
+
+# this stuff is useful for when you first start converting.
+if 0:
+    pprint.pprint( bugs[3] )
+    info = extractInfo(bugs)
+    pprint.pprint(info)
+
+create_attachments(bugs)
+
+issues = [ ConvertBug().convert(bug) for bug_id, bug in sorted(bugs.items())]
+pprint.pprint(issues[69-1])
+print(issues[69-1]['content'])
+
+