def test_revision(mock_autoland_task, mock_phabricator, mock_hgmo): """ Validate the creation of an autoland Revision """ with mock_phabricator as api: revision = Revision.from_autoland(mock_autoland_task, api) assert revision.as_dict() == { "bugzilla_id": 1234567, "diff_id": 1, "diff_phid": "PHID-DIFF-autoland", "has_clang_files": False, "id": 123, "mercurial_revision": "deadbeef123", "phid": "PHID-DREV-azcDeadbeef", "repository": "https://hg.mozilla.org/integration/autoland", "target_repository": "https://hg.mozilla.org/mozilla-central", "title": "Static Analysis tests", "url": "https://phabricator.services.mozilla.com/D123", }
def main(): args = parse_cli() taskcluster.auth(args.taskcluster_client_id, args.taskcluster_access_token) taskcluster.load_secrets( args.taskcluster_secret, prefixes=["common", "code-review-bot", "bot"], required=( "APP_CHANNEL", "REPORTERS", "PHABRICATOR", "ALLOWED_PATHS", "repositories", ), existing={ "APP_CHANNEL": "development", "REPORTERS": [], "ZERO_COVERAGE_ENABLED": True, "ALLOWED_PATHS": ["*"], "task_failures_ignored": [], }, local_secrets=yaml.safe_load(args.configuration) if args.configuration else None, ) init_logger( "bot", channel=taskcluster.secrets.get("APP_CHANNEL", "dev"), PAPERTRAIL_HOST=taskcluster.secrets.get("PAPERTRAIL_HOST"), PAPERTRAIL_PORT=taskcluster.secrets.get("PAPERTRAIL_PORT"), SENTRY_DSN=taskcluster.secrets.get("SENTRY_DSN"), ) # Setup settings before stats settings.setup( taskcluster.secrets["APP_CHANNEL"], taskcluster.secrets["ALLOWED_PATHS"], taskcluster.secrets["repositories"], ) # Setup statistics influx_conf = taskcluster.secrets.get("influxdb") if influx_conf: stats.auth(influx_conf) # Load reporters reporters = get_reporters(taskcluster.secrets["REPORTERS"]) # Load index service index_service = taskcluster.get_service("index") # Load queue service queue_service = taskcluster.get_service("queue") # Load Phabricator API phabricator = taskcluster.secrets["PHABRICATOR"] phabricator_reporting_enabled = "phabricator" in reporters phabricator_api = PhabricatorAPI(phabricator["api_key"], phabricator["url"]) if phabricator_reporting_enabled: reporters["phabricator"].setup_api(phabricator_api) # Load unique revision try: if settings.autoland_group_id: revision = Revision.from_autoland( queue_service.task(settings.autoland_group_id), phabricator_api ) else: revision = Revision.from_try( queue_service.task(settings.try_task_id), phabricator_api ) except Exception as e: # Report revision loading failure on production only # On testing or dev instances, we can use different Phabricator # configuration that do not match all the pulse messages sent if settings.on_production: raise else: logger.info( "Failed to load revision", task=settings.try_task_id, error=str(e), phabricator=phabricator["url"], ) return 1 # Run workflow according to source w = Workflow( reporters, index_service, queue_service, phabricator_api, taskcluster.secrets["ZERO_COVERAGE_ENABLED"], # Update build status only when phabricator reporting is enabled update_build=phabricator_reporting_enabled, task_failures_ignored=taskcluster.secrets["task_failures_ignored"], ) try: if revision.repository == REPO_AUTOLAND: w.ingest_autoland(revision) else: w.run(revision) except Exception as e: # Log errors to papertrail logger.error("Static analysis failure", revision=revision, error=e) # Index analysis state extras = {} if isinstance(e, AnalysisException): extras["error_code"] = e.code extras["error_message"] = str(e) w.index(revision, state="error", **extras) # Update Harbormaster status w.update_status(revision, state=BuildState.Fail) # Then raise to mark task as erroneous raise return 0