예제 #1
0
    def update_bug(self, bug, use_db=True):
        """
        Update a bug
        """

        # Skip when it's already processed in instance
        bug_id = bug['id']
        if bug_id in self.bugs:
            logger.warn('Bug {} already processed.'.format(bug_id))
            return self.bugs[bug_id]

        # Compute the hash of the new bug
        bug_hash = compute_dict_hash(bug)

        if use_db:
            # Fetch or create existing bug result
            try:
                br = BugResult.query.filter_by(bugzilla_id=bug_id).one()
                logger.info('Update existing {}'.format(br))

                # Check the bug has changed since last update
                if br.payload_hash == bug_hash:
                    logger.info('Same bug hash, skip bug analysis {}'.format(br))  # noqa
                    return br

            except NoResultFound:
                br = BugResult(bug_id)
                logger.info('Create new {}'.format(br))
        else:
            # Create a new instance
            br = BugResult(bug_id)
            logger.info('Create new {}'.format(br))

        # Do patch analysis
        try:
            analysis = bug_analysis(bug_id)
        except Exception as e:
            logger.error('Patch analysis failed on {} : {}'.format(bug_id, e))
            return

        # Build html version of uplift comment
        if analysis['uplift_comment']:
            analysis['uplift_comment']['html'] = parse_uplift_comment(
                analysis['uplift_comment']['text'], bug_id)

        payload = {
            'url': '{}/{}'.format(self.bugzilla_url, bug['id']),
            'bug': bug,
            'analysis': analysis,
            'users': self.load_users(analysis),
        }
        br.payload = use_db and pickle.dumps(payload, 2) or payload
        br.payload_hash = bug_hash
        logger.info('Updated payload of {}'.format(br))

        # Save in local cache
        self.bugs[bug_id] = br

        return br
예제 #2
0
def bugs(app):
    """
    Add an analysis and some bugs
    """
    from shipit_dashboard.models import (
        BugAnalysis, BugResult, Contributor, BugContributor
    )
    from releng_common.db import db

    # Add 2 analysis
    analysis = []
    for i in ('A', 'B'):
        a = BugAnalysis(name='Analysis Test {}'.format(i))
        a.parameters = 'bugzilla=test'  # dummy params.
        db.session.add(a)
        analysis.append(a)

    # Add bugs with real payload
    paths = glob.glob(os.path.join(FIXTURES_DIR, 'bug_*.json'))
    sorted(paths)
    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)
        db.session.add(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')
        db.session.add(contrib)
        db.session.add(link)

    db.session.commit()
예제 #3
0
    def update_bug(self, bug, use_db=True):
        """
        Update a bug
        """

        # Skip when it's already processed in instance
        bug_id = bug['id']
        if bug_id in self.bugs:
            logger.warn('Bug {} already processed.'.format(bug_id))
            return self.bugs[bug_id]

        # Compute the hash of the new bug
        bug_hash = compute_dict_hash(bug)

        if use_db:
            # Fetch or create existing bug result
            try:
                br = BugResult.query.filter_by(bugzilla_id=bug_id).one()
                logger.info('Update existing {}'.format(br))

                # Check the bug has changed since last update
                if br.payload_hash == bug_hash:
                    logger.info('Same bug hash, skip bug analysis {}'.format(
                        br))  # noqa
                    return br

            except NoResultFound:
                br = BugResult(bug_id)
                logger.info('Create new {}'.format(br))
        else:
            # Create a new instance
            br = BugResult(bug_id)
            logger.info('Create new {}'.format(br))

        # Do patch analysis
        try:
            analysis = bug_analysis(bug_id)
        except Exception as e:
            logger.error('Patch analysis failed on {} : {}'.format(bug_id, e))
            return

        # Build html version of uplift comment
        if analysis['uplift_comment']:
            analysis['uplift_comment']['html'] = parse_uplift_comment(
                analysis['uplift_comment']['text'], bug_id)

        payload = {
            'url': '{}/{}'.format(self.bugzilla_url, bug['id']),
            'bug': bug,
            'analysis': analysis,
            'users': self.load_users(analysis),
        }
        br.payload = use_db and pickle.dumps(payload, 2) or payload
        br.payload_hash = bug_hash
        logger.info('Updated payload of {}'.format(br))

        # Save in local cache
        self.bugs[bug_id] = br

        return br
예제 #4
0
def bugs(app):
    """
    Add an analysis and some bugs
    """
    from shipit_dashboard.models import BugAnalysis, BugResult
    from releng_common.db import db

    # Add an analysis
    analysis = BugAnalysis(name='Analysis Test A')
    analysis.parameters = 'bugzilla=test'  # dummy params.
    db.session.add(analysis)

    # Add bugs with real payload
    paths = glob.glob(os.path.join(FIXTURES_DIR, 'bug_*.json'))
    sorted(paths)
    for path in paths:
        payload = json.load(open(path))
        bug = BugResult(bugzilla_id=payload['bug']['id'])
        bug.payload = pickle.dumps(payload, 2)
        analysis.bugs.append(bug)
        db.session.add(bug)

    db.session.commit()
예제 #5
0
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()
    except:
        bug = BugResult(bugzilla_id=bugzilla_id)

    # Load all analysis
    analysis_ids = request.json.get('analysis', [])
    analysis = BugAnalysis.query.filter(BugAnalysis.id.in_(analysis_ids)).all()
    if not analysis:
        raise Exception('No analysis found for {}'.format(analysis_ids))

    # 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.')
    bug.payload = pickle.dumps(payload, 2)
    bug.payload_hash = payload_hash

    # Attach bug to its analysis
    for a in analysis:
        a.bugs.append(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)
예제 #6
0
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)
예제 #7
0
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()
    except:
        bug = BugResult(bugzilla_id=bugzilla_id)

    # Load all analysis
    analysis_ids = request.json.get('analysis', [])
    analysis = BugAnalysis.query.filter(BugAnalysis.id.in_(analysis_ids)).all()
    if not analysis:
        raise Exception('No analysis found for {}'.format(analysis_ids))

    # 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.')
    bug.payload = pickle.dumps(payload, 2)
    bug.payload_hash = payload_hash

    # Attach bug to its analysis
    for a in analysis:
        a.bugs.append(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)