def process()

in src/olympia/reviewers/management/commands/auto_approve.py [0:0]


    def process(self, version_id):
        """Process a single version by id, figuring out if it should be
        auto-approved and calling the approval code if necessary."""
        version = Version.objects.get(pk=version_id)
        already_locked = AutoApprovalSummary.check_is_locked(version)
        if not already_locked:
            # Lock the addon for ourselves if possible. Even though
            # AutoApprovalSummary.create_summary_for_version() will do
            # call check_is_locked() again later when calculating the verdict,
            # we have to do it now to prevent overwriting an existing lock with
            # our own.
            set_reviewing_cache(version.addon.pk, settings.TASK_USER_ID)

        try:
            with transaction.atomic():
                log.info(
                    'Processing %s version %s...',
                    str(version.addon.name),
                    str(version.version),
                )

                if waffle.switch_is_active('run-action-in-auto-approve'):
                    # We want to execute `run_action()` only once.
                    summary_exists = AutoApprovalSummary.objects.filter(
                        version=version
                    ).exists()
                    if summary_exists:
                        log.info(
                            'Not running run_action() because it has '
                            'already been executed'
                        )
                    else:
                        ScannerResult.run_action(version)

                version.autoapprovalsummary, info = (
                    AutoApprovalSummary.create_summary_for_version(
                        version, dry_run=self.dry_run
                    )
                )
                self.stats.update({k: int(v) for k, v in info.items()})
                if version.autoapprovalsummary.verdict == self.successful_verdict:
                    if version.autoapprovalsummary.verdict == amo.AUTO_APPROVED:
                        self.approve(version)
                    self.stats['auto_approved'] += 1
                    verdict_string = version.autoapprovalsummary.get_verdict_display()
                else:
                    if version.autoapprovalsummary.verdict == amo.NOT_AUTO_APPROVED:
                        self.disapprove(version)
                    verdict_string = '{} ({})'.format(
                        version.autoapprovalsummary.get_verdict_display(),
                        ', '.join(
                            version.autoapprovalsummary.verdict_info_prettifier(info)
                        ),
                    )
                log.info(
                    'Auto Approval for %s version %s: %s',
                    str(version.addon.name),
                    str(version.version),
                    verdict_string,
                )

        # At this point, any exception should have rolled back the transaction,
        # so even if we did create/update an AutoApprovalSummary instance that
        # should have been rolled back. This ensures that, for instance, a
        # signing error doesn't leave the version and its autoapprovalsummary
        # in conflicting states.
        except AutoApprovalNoValidationResultError:
            log.info(
                'Version %s was skipped either because it had no '
                'files or because it had no validation attached.',
                version,
            )
            self.stats['error'] += 1
        except ApprovalNotAvailableError:
            statsd.incr('reviewers.auto_approve.approve.failure')
            log.info(
                'Version %s was skipped because approval action was not available',
                version,
            )
            self.stats['error'] += 1
        except SigningError:
            statsd.incr('reviewers.auto_approve.approve.failure')
            log.info('Version %s was skipped because of a signing error', version)
            self.stats['error'] += 1
        finally:
            # Always clear our own lock no matter what happens (but only ours).
            if not already_locked:
                clear_reviewing_cache(version.addon.pk)