def bugs(app): ''' Add an analysis and some bugs ''' from shipit_uplift.models import ( BugAnalysis, BugResult, Contributor, BugContributor ) # Add 2 analysis analysis = [] for i in ('Dev', 'Release'): a = BugAnalysis(name=i) a.version = 1 # dummy version app.db.session.add(a) analysis.append(a) # Add bugs with real payload paths = glob.glob(os.path.join(FIXTURES_DIR, 'bug_*.json')) sorted(paths) bugs = [] for path in paths: payload = json.load(open(path)) bug = BugResult(bugzilla_id=payload['bug']['id']) bug.payload = pickle.dumps(payload, 2) analysis[0].bugs.append(bug) app.db.session.add(bug) bugs.append(bug) # Add creator & link to bug user = payload['analysis']['users']['creator'] contrib = Contributor(bugzilla_id=user['id']) contrib.name = user.get('real_name', user['name']) contrib.email = user['email'] contrib.avatar_url = 'gravatar_url_here' contrib.karma = 1 contrib.comment_private = 'hidden comment' contrib.comment_public = 'Top Contributor' link = BugContributor(bug=bug, contributor=contrib, roles='creator') app.db.session.add(contrib) app.db.session.add(link) app.db.session.commit() return bugs
def create_bug(): ''' Create a new bug, or update its payload ''' # Load bug bugzilla_id = request.json.get('bugzilla_id') if not bugzilla_id: raise Exception('Missing bugzilla id') try: bug = BugResult.query.filter_by(bugzilla_id=bugzilla_id).one() analysis_existing = [a[0] for a in bug.analysis.values('analysis_id')] except Exception: bug = BugResult(bugzilla_id=bugzilla_id) analysis_existing = [] # Update bug payload payload = request.json.get('payload') payload_hash = request.json.get('payload_hash') if not payload or not payload_hash: raise Exception('Missing payload updates.') if 'analysis' not in payload: raise Exception('Missing analysis in payload') if payload['analysis'].get('uplift_comment') is None: raise Exception('Missing uplift comment in analysis') bug.payload = pickle.dumps(payload, 2) bug.payload_hash = payload_hash # Sync analysis in both ways: # * adding new bugs # * removing deprecated bugs analysis_needed = request.json.get('analysis', []) add = set(analysis_needed).difference(analysis_existing) analysis = BugAnalysis.query \ .filter(BugAnalysis.id.in_(add)) \ .all() for a in analysis: logger.debug('Adding new bug', analysis=a.id, bug=bug.bugzilla_id) a.bugs.append(bug) rm = set(analysis_existing).difference(analysis_needed) analysis = BugAnalysis.query \ .filter(BugAnalysis.id.in_(rm)) \ .all() for a in analysis: logger.debug('Removing old bug', analysis=a.id, bug=bug.bugzilla_id) a.bugs.remove(bug) # Save all changes db.session.add(bug) # Load users for user in payload.get('users', []): # Get or create user in db try: contrib = Contributor.query.filter_by(bugzilla_id=user['id']).one() except: contrib = Contributor(bugzilla_id=user['id']) contrib.name = user.get('real_name', user['name']) contrib.email = user['email'] contrib.avatar_url = gravatar(user['email']) db.session.add(contrib) # Link contributor to bug try: link = BugContributor.query.filter_by( bug_id=bug.id, contributor_id=contrib.id).one() except Exception: link = BugContributor(bug=bug, contributor=contrib) link.roles = ','.join(user['roles']) db.session.add(link) # Commit all those changes db.session.commit() # Send back the bug return serialize_bug(bug)