diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 2d01bc445..ca8922215 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -33,7 +33,7 @@ repos: rev: v2.2.4 hooks: - id: codespell - exclude_types: [json] + exclude_types: [json, pem] - repo: https://github.com/marco-c/taskcluster_yml_validator rev: v0.0.11 hooks: diff --git a/bot/code_review_bot/__init__.py b/bot/code_review_bot/__init__.py index 1bfe0c0f8..2c8d2b79c 100644 --- a/bot/code_review_bot/__init__.py +++ b/bot/code_review_bot/__init__.py @@ -191,41 +191,29 @@ def hash(self): We make the assumption that the message does not contain the line number If an error occurs reading the file content (locally or remotely), None is returned """ + from code_review_bot.revisions import GithubRevision, PhabricatorRevision + assert self.revision is not None, "Missing revision" + local_repository = None + if isinstance(self.revision, PhabricatorRevision): + if settings.mercurial_cache_checkout: + local_repository = settings.mercurial_cache_checkout + elif isinstance(self.revision, GithubRevision): + assert ( + settings.github_cache + ), "Github cache repository is mandatory to analyse a github revision" + local_repository = settings.github_cache + else: + raise Exception(self.revision.__class__) + raise NotImplementedError + # Build the hash only if the file is not autogenerated. # An autogenerated file resides in the build directory that it has the # format `obj-x86_64-pc-linux-gnu` file_content = None if "/obj-" not in self.path: - if settings.mercurial_cache_checkout: - logger.debug("Using the local repository to build issue's hash") - try: - with (settings.mercurial_cache_checkout / self.path).open() as f: - file_content = f.read() - except (FileNotFoundError, IsADirectoryError): - logger.warning( - "Failed to find issue's related file", path=self.path - ) - file_content = None - else: - try: - # Load all the lines affected by the issue - file_content = self.revision.load_file(self.path) - except ValueError: - # Build the hash with an empty content in case the path is erroneous - file_content = None - except requests.exceptions.HTTPError as e: - if e.response.status_code == 404: - logger.warning( - "Failed to download a file with an issue", path=self.path - ) - - # We still build the hash with empty content - file_content = None - else: - # When encountering another HTTP error, raise the issue - raise + file_content = self.revision.get_file_content(self.path, local_repository) if file_content is None: self._hash = None diff --git a/bot/code_review_bot/backend.py b/bot/code_review_bot/backend.py index 17019b5b3..7a81c6c9a 100644 --- a/bot/code_review_bot/backend.py +++ b/bot/code_review_bot/backend.py @@ -9,6 +9,7 @@ from code_review_bot import taskcluster from code_review_bot.config import GetAppUserAgent, settings +from code_review_bot.revisions import PhabricatorRevision from code_review_bot.tasks.lint import MozLintIssue logger = structlog.get_logger(__name__) @@ -46,38 +47,31 @@ def publish_revision(self, revision): logger.warn("Skipping revision publication on backend") return - # Check the repositories are urls - for url in (revision.base_repository, revision.head_repository): - assert isinstance(url, str), "Repository must be a string" - res = urllib.parse.urlparse(url) - assert res.scheme and res.netloc, f"Repository {url} is not an url" - - # Check the Mercurial changesets are strings - for changeset in ( - revision.base_changeset, - revision.head_changeset, - ): - assert isinstance(changeset, str), "Mercurial changeset must be a string" - - # Create revision on backend if it does not exists - data = { - "phabricator_id": revision.phabricator_id, - "phabricator_phid": revision.phabricator_phid, - "title": revision.title, - "bugzilla_id": revision.bugzilla_id, - "base_repository": revision.base_repository, - "head_repository": revision.head_repository, - "base_changeset": revision.base_changeset, - "head_changeset": revision.head_changeset, - } - - # Try to create the revision, or retrieve it in case it exists with that Phabricator ID. + elif isinstance(revision, PhabricatorRevision): + # Check the repositories are urls + for url in (revision.base_repository, revision.head_repository): + assert isinstance(url, str), "Repository must be a string" + res = urllib.parse.urlparse(url) + assert res.scheme and res.netloc, f"Repository {url} is not an url" + + # Check the Mercurial changesets are strings + for changeset in ( + revision.base_changeset, + revision.head_changeset, + ): + assert isinstance( + changeset, str + ), "Mercurial changeset must be a string" + + revision_data, diff_data = revision.serialize() + + # Try to create the revision, or retrieve it in case it exists with that provider and ID. # The backend always returns a revisions, either a new one, or a pre-existing one revision_url = "/v1/revision/" auth = (self.username, self.password) url_post = urllib.parse.urljoin(self.url, revision_url) response = requests.post( - url_post, headers=GetAppUserAgent(), json=data, auth=auth + url_post, headers=GetAppUserAgent(), json=revision_data, auth=auth ) if not response.ok: logger.warn(f"Backend rejected the payload: {response.content}") @@ -87,17 +81,14 @@ def publish_revision(self, revision): revision.issues_url = backend_revision["issues_bulk_url"] revision.id = backend_revision["id"] - # A revision may have no diff (e.g. Mozilla-central group tasks) - if not revision.diff_id: + # A revision may have no diff (e.g. Phabricator Mozilla-central group tasks) + if isinstance(revision, PhabricatorRevision) and not revision.diff_id: return backend_revision # Create diff attached to revision on backend data = { - "id": revision.diff_id, - "phid": revision.diff_phid, + **diff_data, "review_task_id": settings.taskcluster.task_id, - "mercurial_hash": revision.head_changeset, - "repository": revision.head_repository, } backend_diff = self.create(backend_revision["diffs_url"], data) diff --git a/bot/code_review_bot/cli.py b/bot/code_review_bot/cli.py index 6e2a9a875..8f590f6a0 100644 --- a/bot/code_review_bot/cli.py +++ b/bot/code_review_bot/cli.py @@ -26,7 +26,7 @@ ) from code_review_bot.config import settings from code_review_bot.report import get_reporters -from code_review_bot.revisions import PhabricatorRevision, Revision +from code_review_bot.revisions import GithubRevision, PhabricatorRevision, Revision from code_review_bot.tools.libmozdata import setup as setup_libmozdata from code_review_bot.tools.log import init_logger from code_review_bot.workflow import Workflow @@ -64,6 +64,13 @@ def parse_cli(): type=Path, default=None, ) + parser.add_argument( + "--github-repository", + help="Optional path to a up-to-date github repository matching the analyzed revision.\n" + "This argument is required for Github reviusions in order to compute issues' hashes based on file content.", + type=Path, + default=None, + ) parser.add_argument("--taskcluster-client-id", help="Taskcluster Client ID") parser.add_argument("--taskcluster-access-token", help="Taskcluster Access token") return parser.parse_args() @@ -116,6 +123,7 @@ def main(): taskcluster.secrets["repositories"], taskcluster.secrets["ssh_key"], args.mercurial_repository, + args.github_repository, ) # Setup statistics @@ -205,6 +213,11 @@ def main(): ) return 1 + if isinstance(revision, GithubRevision): + assert ( + args.github_repository is not None + ), "Girhub revision analysis requires the --github-repository argument to be set" + # Run workflow according to source w = Workflow( reporters, diff --git a/bot/code_review_bot/config.py b/bot/code_review_bot/config.py index c571aa3ef..dfa54b2cb 100644 --- a/bot/code_review_bot/config.py +++ b/bot/code_review_bot/config.py @@ -58,6 +58,7 @@ def __init__(self): # Cache to store whole repositories self.mercurial_cache = None + self.github_cache = None # SSH Key used to push on try self.ssh_key = None @@ -78,6 +79,7 @@ def setup( repositories, ssh_key=None, mercurial_cache=None, + github_cache=None, ): # Detect source from env if "TRY_TASK_ID" in os.environ and "TRY_TASK_GROUP_ID" in os.environ: @@ -148,6 +150,14 @@ def build_conf(nb, repo): # Save ssh key when mercurial cache is enabled self.ssh_key = ssh_key + # Store github cache path + if github_cache is not None: + self.github_cache = Path(github_cache) + assert ( + self.github_cache.exists() + ), f"Github cache does not exist {self.github_cache}" + logger.info("Using Github cache", path=self.mercurial_cache) + def load_user_blacklist(self, usernames, phabricator_api): """ Load all black listed users from Phabricator API diff --git a/bot/code_review_bot/github.py b/bot/code_review_bot/github.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/bot/code_review_bot/report/__init__.py b/bot/code_review_bot/report/__init__.py index f1d5294ee..ed79bb1ec 100644 --- a/bot/code_review_bot/report/__init__.py +++ b/bot/code_review_bot/report/__init__.py @@ -4,6 +4,7 @@ import structlog +from code_review_bot.report.github import GithubReporter from code_review_bot.report.lando import LandoReporter from code_review_bot.report.mail import MailReporter from code_review_bot.report.mail_builderrors import BuildErrorsReporter @@ -22,6 +23,7 @@ def get_reporters(configuration): "mail": MailReporter, "build_error": BuildErrorsReporter, "phabricator": PhabricatorReporter, + "github": GithubReporter, } out = {} diff --git a/bot/code_review_bot/report/github.py b/bot/code_review_bot/report/github.py new file mode 100644 index 000000000..25db93bf6 --- /dev/null +++ b/bot/code_review_bot/report/github.py @@ -0,0 +1,62 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +import structlog + +from code_review_bot.report.base import Reporter +from code_review_bot.sources.github import GithubClient, ReviewEvent + +logger = structlog.get_logger(__name__) + + +class GithubReporter(Reporter): + # Auth to Github using a configuration (from Taskcluster secret) + + def __init__(self, configuration={}, *args, **kwargs): + for key in ("client_id", "private_key_pem", "installation_id"): + if not configuration.get(key): + raise Exception(f"Missing github reporter configuration key {key}") + + # Setup github App secret from the configuration + self.github_client = GithubClient( + client_id=configuration["client_id"], + private_key=configuration["private_key_pem"], + installation_id=configuration["installation_id"], + ) + + self.analyzers_skipped = configuration.get("analyzers_skipped", []) + assert isinstance( + self.analyzers_skipped, list + ), "analyzers_skipped must be a list" + + def publish(self, issues, revision, task_failures, notices, reviewers): + """ + Publish issues on a Github pull request. + """ + if reviewers: + raise NotImplementedError + # Avoid publishing a patch from a de-activated analyzer + publishable_issues = [ + issue + for issue in issues + if issue.is_publishable() + and issue.analyzer.name not in self.analyzers_skipped + ] + + if publishable_issues: + # Publish a review summarizing detected, unresolved and closed issues + message = f"{len(issues)} issues have been found in this revision" + event = ReviewEvent.RequestChanges + else: + # Simply approve the pull request + logger.info("No publishable issue, approving the pull request") + message = None + event = ReviewEvent.Approved + + self.github_client.publish_review( + issues=publishable_issues, + revision=revision, + message=message, + event=event, + ) diff --git a/bot/code_review_bot/report/lando.py b/bot/code_review_bot/report/lando.py index 21ed73771..331a012b0 100644 --- a/bot/code_review_bot/report/lando.py +++ b/bot/code_review_bot/report/lando.py @@ -31,9 +31,11 @@ def publish(self, issues, revision, task_failures, links, reviewers): Send an email to administrators """ if not isinstance(revision, PhabricatorRevision): - raise NotImplementedError( - "Only Phabricator revisions are supported for now" + logger.warning( + "Lando publication only works with Phabricator revisions. Skipping.", + revision=revision, ) + return assert ( revision.phabricator_id and revision.phabricator_phid and revision.diff diff --git a/bot/code_review_bot/revisions/__init__.py b/bot/code_review_bot/revisions/__init__.py index 10d4224fa..f98bd00a7 100644 --- a/bot/code_review_bot/revisions/__init__.py +++ b/bot/code_review_bot/revisions/__init__.py @@ -3,6 +3,7 @@ # file, You can obtain one at http://mozilla.org/MPL/2.0/. from code_review_bot.revisions.base import ImprovementPatch, Revision +from code_review_bot.revisions.github import GithubRevision from code_review_bot.revisions.phabricator import PhabricatorRevision -__all__ = [ImprovementPatch, Revision, PhabricatorRevision] +__all__ = [ImprovementPatch, Revision, PhabricatorRevision, GithubRevision] diff --git a/bot/code_review_bot/revisions/base.py b/bot/code_review_bot/revisions/base.py index 1cd319401..492b9a35a 100644 --- a/bot/code_review_bot/revisions/base.py +++ b/bot/code_review_bot/revisions/base.py @@ -6,6 +6,7 @@ import random from abc import ABC from datetime import timedelta +from pathlib import Path import rs_parsepatch import structlog @@ -165,6 +166,25 @@ def contains(self, issue): lines = set(range(issue.line, issue.line + issue.nb_lines)) return not lines.isdisjoint(modified_lines) + def get_file_content( + self, file_path: str, local_cache_repository: Path | None = None + ): + if local_cache_repository: + logger.debug("Using the local repository to build issue's hash") + try: + with (local_cache_repository / file_path).open() as f: + file_content = f.read() + except (FileNotFoundError, IsADirectoryError): + logger.warning("Failed to find issue's related file", path=file_path) + file_content = None + else: + try: + file_content = self.load_file(file_path) + except ValueError: + # The path is erroneous, consider as empty content + file_content = None + return file_content + @property def has_clang_files(self): """ @@ -237,17 +257,26 @@ def as_dict(self): """ raise NotImplementedError + def serialize(self): + """ + Outputs a tuple of dicts for revision and diff sent to backend + """ + raise NotImplementedError + @staticmethod def from_try_task(try_task: dict, decision_task: dict, phabricator: PhabricatorAPI): """ - Load identifiers from Phabricator, using the remote task description + Load identifiers from Phabricator or Github, using the remote task description """ + from code_review_bot.revisions.github import GithubRevision from code_review_bot.revisions.phabricator import PhabricatorRevision # Load build target phid from the task env code_review = try_task["extra"]["code-review"] - # TODO: support github revision here too - return PhabricatorRevision.from_try_task( - code_review, decision_task, phabricator - ) + if "github" in code_review: + return GithubRevision(**code_review["github"]) + else: + return PhabricatorRevision.from_try_task( + code_review, decision_task, phabricator + ) diff --git a/bot/code_review_bot/revisions/github.py b/bot/code_review_bot/revisions/github.py new file mode 100644 index 000000000..fb69f8736 --- /dev/null +++ b/bot/code_review_bot/revisions/github.py @@ -0,0 +1,113 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +from functools import cached_property +from urllib.parse import urlparse + +import requests +import structlog + +from code_review_bot import taskcluster +from code_review_bot.revisions import Revision + +logger = structlog.get_logger(__name__) + + +class GithubRevision(Revision): + """ + A revision from a github pull-request + """ + + def __init__(self, repo_url, branch, pull_number, pull_head_sha): + super().__init__() + + self.repo_url = repo_url + self.branch = branch + self.pull_number = pull_number + self.pull_head_sha = pull_head_sha + + # Load the patch from Github + self.patch = self.load_patch() + + def __str__(self): + return f"Github pull request {self.repo_url} #{self.pull_number} ({self.pull_head_sha[:8]})" + + def __repr__(self): + return f"GithubRevision repo_url={self.repo_url} branch={self.branch} pull_number={self.pull_number} sha={self.pull_head_sha}" + + @property + def repo_name(self): + """ + Extract the name of the repository from its URL + """ + return urlparse(self.repo_url).path.strip("/") + + @property + def repository_slug(self): + """ + Generate a slug from the Github repository. + This method copies the automatic slug creation in backend's RepositoryGetOrCreateField serializer field. + """ + parsed = urlparse(self.repo_url) + return parsed.path.lstrip("/").replace("/", "-") + + def load_patch(self): + """ + Load the patch content for the current pull request HEAD + """ + # TODO: use specific sha + url = f"{self.repo_url}/pull/{self.pull_number}.diff" + logger.info("Loading github patch", url=url) + resp = requests.get(url, allow_redirects=True) + resp.raise_for_status() + return resp.content.decode() + + def as_dict(self): + return { + "repo_url": self.repo_url, + "branch": self.branch, + "pull_number": self.pull_number, + "pull_head_sha": self.pull_head_sha, + } + + @cached_property + def pull_request(self): + from code_review_bot.sources.github import GithubClient + + reporter_conf = next( + ( + reporter + for reporter in taskcluster.secrets["REPORTERS"] + if reporter["reporter"] == "github" + ), + None, + ) + # A github reporter configuration is required to perform a github Pull Request analysis + assert reporter_conf, "Github reporter secrets must be set to access information about the pull request" + client = GithubClient( + client_id=reporter_conf["client_id"], + private_key=reporter_conf["private_key_pem"], + installation_id=reporter_conf["installation_id"], + ) + return client.get_pull_request(self) + + def serialize(self): + """ + Outputs a tuple of dicts for revision and diff (empty for Github) sent to backend + """ + revision = { + "provider": "github", + "provider_id": self.pull_number, + "title": self.pull_request.title, + "bugzilla_id": None, + "base_repository": self.pull_request.base.repo.html_url, + "head_repository": self.repo_url, + } + diff = { + "provider": "github", + "provider_id": self.pull_head_sha, + "mercurial_hash": self.pull_head_sha, + "repository": self.repo_url, + } + return revision, diff diff --git a/bot/code_review_bot/revisions/phabricator.py b/bot/code_review_bot/revisions/phabricator.py index 8970e19ae..975268600 100644 --- a/bot/code_review_bot/revisions/phabricator.py +++ b/bot/code_review_bot/revisions/phabricator.py @@ -130,12 +130,13 @@ def __str__(self): return f"Phabricator #{self.diff_id} - {self.diff_phid}" @staticmethod - def from_try_task(try_task: dict, decision_task: dict, phabricator: PhabricatorAPI): + def from_try_task( + code_review: dict, decision_task: dict, phabricator: PhabricatorAPI + ): """ Load identifiers from Phabricator, using the remote task description """ # Load build target phid from the task env - code_review = try_task["extra"]["code-review"] build_target_phid = code_review.get("phabricator-diff") or code_review.get( "phabricator-build-target" ) @@ -361,8 +362,17 @@ def load_file(self, path): ) logger.info("Downloading HGMO file", url=url) - response = requests.get(url, headers=GetAppUserAgent()) - response.raise_for_status() + try: + response = requests.get(url, headers=GetAppUserAgent()) + response.raise_for_status() + except requests.exceptions.HTTPError as e: + if e.response.status_code == 404: + logger.warning("Failed to download file", path=self.path) + # Consider as empty content if the file is not found + return None + else: + # When encountering another HTTP error, raise the issue + raise e # Store in cache content = response.content.decode("utf-8") @@ -430,3 +440,26 @@ def as_dict(self): "head_changeset": self.head_changeset, "base_changeset": self.base_changeset, } + + def serialize(self): + """ + Outputs a tuple of dicts for revision and diff sent to backend + """ + revision = { + "provider": "phabricator", + "provider_id": self.phabricator_id, + "title": self.title, + "bugzilla_id": self.bugzilla_id, + "base_repository": self.base_repository, + "head_repository": self.head_repository, + "base_changeset": self.base_changeset, + "head_changeset": self.head_changeset, + } + diff = { + "id": self.diff_id, + "provider_id": self.diff_phid, + "mercurial_hash": self.head_changeset, + "repository": self.head_repository, + } + + return revision, diff diff --git a/bot/code_review_bot/sources/github.py b/bot/code_review_bot/sources/github.py new file mode 100644 index 000000000..b45f527df --- /dev/null +++ b/bot/code_review_bot/sources/github.py @@ -0,0 +1,97 @@ +#!/usr/bin/env python3 + +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + +import enum + +import structlog +from github import Auth, GithubIntegration +from github.PullRequest import ReviewComment + +from code_review_bot import Issue +from code_review_bot.revisions import GithubRevision + +logger = structlog.get_logger(__name__) + + +class ReviewEvent(enum.Enum): + """ + Review action you want to perform. + https://docs.github.com/en/rest/pulls/reviews?apiVersion=2022-11-28#create-a-review-for-a-pull-request--parameters + """ + + Pending = "PENDING" + Approved = "APPROVE" + RequestChanges = "REQUEST_CHANGES" + Comment = "COMMENT" + + +class GithubClient: + def __init__(self, client_id: str, private_key: str, installation_id: str): + self.client_id = client_id + + # Setup auth + self.auth = Auth.AppAuth(self.client_id, private_key) + self.github_integration = GithubIntegration(auth=self.auth) + + installations = self.github_integration.get_installations() + self.installation = next( + (i for i in installations if i.id == installation_id), None + ) + if not self.installation: + raise ValueError( + f"Installation ID is not available. Available installations are {list(installations)}" + ) + # setup API + self.api = self.installation.get_github_for_installation() + + self.review_comments = [] + + def get_pull_request(self, revision: GithubRevision): + repo = self.api.get_repo(revision.repo_name) + return repo.get_pull(revision.pull_number) + + def _build_review_comment(self, issue): + return ReviewComment( + path=issue.path, + line=issue.line, + body=issue.message, + ) + + def publish_review( + self, + issues: list[Issue], + revision: GithubRevision, + event: ReviewEvent, + message: str | None = None, + ): + """ + Publish a review from a list of publishable issues, requesting changes to the author. + """ + + if not isinstance(revision, GithubRevision): + logger.warning( + f"Revision must originate from Github in order to publish a review, skipping {revision}." + ) + return + + repo = self.api.get_repo(revision.repo_name) + pull_request = repo.get_pull(revision.pull_number) + + attrs = {} + if message is None: + assert ( + event == ReviewEvent.Approved + ), "Body can be left null only when approving a pull request" + else: + attrs["body"] = message + + pull_request.create_review( + commit=repo.get_commit(revision.pull_head_sha), + comments=[self._build_review_comment(issue) for issue in issues], + # https://docs.github.com/en/rest/pulls/reviews?apiVersion=2022-11-28#create-a-review-for-a-pull-request + event=event.value, + **attrs, + ) diff --git a/bot/code_review_bot/workflow.py b/bot/code_review_bot/workflow.py index 260a638e3..a02e87c69 100644 --- a/bot/code_review_bot/workflow.py +++ b/bot/code_review_bot/workflow.py @@ -20,7 +20,7 @@ from code_review_bot.config import settings from code_review_bot.mercurial import MercurialWorker, Repository, robust_checkout from code_review_bot.report.debug import DebugReporter -from code_review_bot.revisions import PhabricatorRevision, Revision +from code_review_bot.revisions import GithubRevision, PhabricatorRevision, Revision from code_review_bot.sources.phabricator import ( PhabricatorActions, PhabricatorBuildState, @@ -133,7 +133,7 @@ def run(self, revision): self.clone_repository(revision) # Mark know issues to avoid publishing them on this patch - self.find_previous_issues(issues, base_rev_changeset) + self.find_previous_issues(revision, issues, base_rev_changeset) new_issues_count = sum(issue.new_issue for issue in issues) logger.info( f"Found {new_issues_count} new issues (over {len(issues)} total detected issues)", @@ -265,9 +265,11 @@ def start_analysis(self, revision): logger.warning("Blacklisted author, stopping there.") return - # Cannot run without mercurial cache configured - if not settings.mercurial_cache: - raise Exception("Mercurial cache must be configured to start analysis") + # Cannot run without either mercurial or github cache configured + if not settings.mercurial_cache and not settings.github_cache: + raise Exception( + "One of Mercurial cache or github cache must be configured to start analysis" + ) # Cannot run without ssh key if not settings.ssh_key: @@ -361,6 +363,12 @@ def clone_repository(self, revision): Clone the repo locally when configured On production this should use a Taskcluster cache """ + if not isinstance(revision, PhabricatorRevision): + logger.info( + "Mercurial clone only supports Phabricator revisions, skipping." + ) + return + if not settings.mercurial_cache: logger.debug("Local clone not required") return @@ -490,7 +498,7 @@ def index(self, revision, **kwargs): }, ) - def find_previous_issues(self, issues, base_rev_changeset=None): + def find_previous_issues(self, revision, issues, base_rev_changeset=None): """ Look for known issues in the backend matching the given list of issues @@ -513,9 +521,17 @@ def find_previous_issues(self, issues, base_rev_changeset=None): base_revision_changeset=base_rev_changeset, ) + if isinstance(revision, PhabricatorRevision): + repository_slug = "mozilla-central" + elif isinstance(revision, GithubRevision): + # TODO: Rely on the central repository for known issues + repository_slug = revision.repository_slug + else: + raise NotImplementedError + for path, group_issues in issues_groups: known_issues = self.backend_api.list_repo_issues( - "mozilla-central", + repository_slug, date=current_date, revision_changeset=base_rev_changeset, path=path, @@ -673,11 +689,16 @@ def update_status(self, revision, state): """ Update build status on HarborMaster """ - if not isinstance(revision, PhabricatorRevision): - raise NotImplementedError( - "Only Phabricator revisions are supported for now" - ) + if isinstance(revision, GithubRevision): + logger.warning("No Lando publication for Github yet") + return + assert isinstance(state, BuildState) + + # Skip github status update, as we rely on the github reporter for publication + if isinstance(revision, GithubRevision): + return + if not revision.build_target_phid: logger.info( "No build target found, skipping HarborMaster update", state=state.value @@ -701,6 +722,7 @@ def publish_link(self, revision: Revision, slug: str, name: str, url: str): raise NotImplementedError( "Only Phabricator revisions are supported for now" ) + if not revision.build_target_phid: logger.info( "No build target found, skipping HarborMaster link creation", diff --git a/bot/requirements.txt b/bot/requirements.txt index 63a693933..9636e4bbe 100644 --- a/bot/requirements.txt +++ b/bot/requirements.txt @@ -1,6 +1,7 @@ aiohttp<4 influxdb==5.3.2 libmozdata==0.2.12 +PyGithub==2.8.1 python-hglib==2.6.2 pyyaml==6.0.3 rs_parsepatch==0.4.4 diff --git a/bot/tests/conftest.py b/bot/tests/conftest.py index 9d75614dc..4d65084d2 100644 --- a/bot/tests/conftest.py +++ b/bot/tests/conftest.py @@ -13,6 +13,8 @@ from collections import defaultdict, namedtuple from configparser import ConfigParser from contextlib import contextmanager +from datetime import UTC, datetime, timedelta +from textwrap import dedent from unittest.mock import MagicMock import hglib @@ -282,6 +284,64 @@ def diff_search(request): yield PhabricatorAPI(url="http://phabricator.test/api/", api_key="deadbeef") +@pytest.fixture +def mock_github(mock_config): + """ + Mock default github API calls made by the client + """ + diff = dedent( + """diff --git a/path/to/test.cpp b/path/to/test.cpp + index c57eff55..980a0d5f 100644 + --- a/path/to/test.cpp + +++ b/path/to/test.cpp + @@ -1 +1 @@ + -#include + +Hello World! + """ + ) + + responses.add( + responses.GET, + "https://github.tests.com/owner/repo-name/pull/1.diff", + json=diff, + ) + responses.add( + responses.GET, + "https://api.github.com:443/app/installations", + json=[ + { + "id": 123456789, + "access_tokens_url": "https://github.tests.com/app/installations/123456789/access_tokens", + } + ], + ) + responses.add( + responses.POST, + "https://api.github.com:443/app/installations/123456789/access_tokens", + json={ + "token": "auth_token", + "expires_at": (datetime.now(UTC) + timedelta(1)).strftime( + "%Y-%m-%dT%H:%M:%S.%fZ" + ), + }, + ) + responses.add( + responses.GET, + "https://api.github.com:443/repos/owner/repo-name", + json={"url": "https://api.github.com/repos/owner/repo-name"}, + ) + responses.add( + responses.GET, + "https://api.github.com:443/repos/owner/repo-name/pulls/1", + json={"url": "https://api.github.com/repos/owner/repo-name/pulls/1"}, + ) + responses.add( + responses.GET, + "https://api.github.com:443/repos/owner/repo-name/commits/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + json={"sha": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"}, + ) + + @pytest.fixture def mock_try_task(): """ @@ -290,6 +350,25 @@ def mock_try_task(): return {"extra": {"code-review": {"phabricator-diff": "PHID-HMBT-test"}}} +@pytest.fixture +def mock_github_try_task(): + """ + Mock a remote Try task definition from a github revision + """ + return { + "extra": { + "code-review": { + "github": { + "repo_url": "https://github.tests.com/owner/repo-name", + "branch": "test", + "pull_number": 1, + "pull_head_sha": "a" * 40, + } + } + } + } + + @pytest.fixture def mock_decision_task(): """ @@ -332,10 +411,12 @@ def mock_revision(mock_phabricator, mock_try_task, mock_decision_task, mock_conf """ Mock a mercurial revision """ - from code_review_bot.revisions import PhabricatorRevision + from code_review_bot.revisions import PhabricatorRevision, Revision with mock_phabricator as api: - return PhabricatorRevision.from_try_task(mock_try_task, mock_decision_task, api) + revision = Revision.from_try_task(mock_try_task, mock_decision_task, api) + assert isinstance(revision, PhabricatorRevision) + return revision @pytest.fixture diff --git a/bot/tests/fixtures/private_key.pem b/bot/tests/fixtures/private_key.pem new file mode 100644 index 000000000..8f33755ca --- /dev/null +++ b/bot/tests/fixtures/private_key.pem @@ -0,0 +1,28 @@ +# THIS IS A FAKE PRIVATE KEY USED FOR TESTS +-----BEGIN RSA PRIVATE KEY----- +MIIEowIBAAKCAQEAjIf0Q38ga5TF9CbNXewI/duyPJgz/TJAvdHvexwtp3qIIWfH +1CNK0NvcmaLWcvgyVn4nj8aLexQiJZQVQYII/YMwXAg2tK75dWkP56cWL0odb4Zs +o2GU14xRGdonixVb8COC8CxiLzFGBXpY2gMfru/9di6/0hRY1o5Qd2aYrXZVHDpe +0ATuIF0gR8MbMz8y8Azqvs+89epjAmKo+6+sU/7yITm9aJ685CEiug3127Kowk07 +TYeebXlzw2eqxcIw35loAs/oqyOIEX607KtVjCBl73FoEfuLHhsi1hFxBOg+HvXn +TzdFiLN/99ZGRRMlJhZCw/DWpZ9uip5MrohneQIDAQABAoIBAD9TVELGGnngBIPM +qGZWYobiZSLhAyxpZLskyuGTBQ+fK5DCD04MyT3slS+2LSSJq0VGe9VSBrBjli+Q +1zM5wYtbfoM6QEyTPF4oBb7BkEGnCDSlQnctFcE7vaAEqiUGbvN7TRmlJmlVrtPx +GfDDz5cpFfIXhuDHwnCMmL31QX+ITV8pmrYYwhpOp+js+25wA7PFzXhgqQtqgGbM +L0O1Tn/POqTbEUBi/S95KwMnNYVEAx6lwmcpLb0KD3x2g0bkHFyeIc+pMeJa+3Kn +OpY1DTlJP3nkpiLmpWIpKk4HMo6oZhcRo9DDLQtJzE+rllEwjZBrSKW3l4d3Eh6t +itsGj7ECgYEA+9OcFcUuMVwFlknTVSoykGv4DQ2gcN/W+H70bYHgAAvjolYxnsQX +hYucxJqTBaG8l5LXjlSynoqMnbm5FY1FLOu8lzt8YhPfc0f/N1jTVL5v52mxyUNr +HjPcbbv9+vihcrhEFkhgRN/lzimv7Eweo5x4wlm8aLOIb/Eg8Y8Wth8CgYEAjtwq +hsPdf0NP/tkJFSrB7Y2I1zBruNvTuJarcv1/w61XLEBR42Odq+Y11/crY8qkwK7z +A5SOxsI6o/si2RDlSZJ5w8t+7Kz/yr5PSFcQHGsokIadQgXCP/hetS2eiNNH94vM +YvwHvSl0ey47qK0h3ugqIZJ1pBSRc0NlfJz8v2cCgYEA0hXdd0QCn2cXuiNozPnh +KR8J10nw+XmkC7dODzV0PFWu2DV0O/F3dg/c/x+9W8tsXD9C2RjL0vvfB45zXAl5 +FlqsALa9s8zEc5Yy0memFmKxVKuWiENYT+AQGvPklMVrWxtiofxLY+ot+2pHu6hd +Pz1AeVMHnYl5X3oYc61d0x0CgYBBYT9RJ8hx2rN8lYVTm5rfBdwvZ2iVVH2jx8i1 +OpDDU8xGYzVW1JsvNY9ExEimRfJ6gFaVN+LT0cYWj/OV1eapchCp67KtzErQVaJh +H/8uklghNIo50frhXeCyGCuqwM752o/yaRd9mcBGM5V4D6wloKjPboDKU+NxFdIX +Yp1FVwKBgGgG4RA3UqAY51E7zA7k3WR3sj49c6oktXVi2n7FuO3PPVTg5LAZ/c81 +vVrip+dOQD53APtrwnFpDeM+AJ03RsIfjVVfB822xRpcy7jDA04bOmJ1Skouoptx +CyIV/PUVbtmNdxJ6T1dCzAvhmK6895FK+xCwBnpaN213Nx/eG49+ +-----END RSA PRIVATE KEY----- diff --git a/bot/tests/test_backend.py b/bot/tests/test_backend.py index e6cc07bba..8d3d922cc 100644 --- a/bot/tests/test_backend.py +++ b/bot/tests/test_backend.py @@ -37,8 +37,8 @@ def test_publication(mock_clang_tidy_issues, mock_revision, mock_backend, mock_h assert revisions[1] == { "bugzilla_id": 1234567, "id": 1, - "phabricator_id": 51, - "phabricator_phid": "PHID-DREV-zzzzz", + "provider": "phabricator", + "provider_id": 51, "title": "Static Analysis tests", "diffs_url": "http://code-review-backend.test/v1/revision/1/diffs/", "issues_bulk_url": "http://code-review-backend.test/v1/revision/1/issues/", @@ -55,7 +55,7 @@ def test_publication(mock_clang_tidy_issues, mock_revision, mock_backend, mock_h "id": 42, "issues_url": "http://code-review-backend.test/v1/diff/42/issues/", "mercurial_hash": "deadbeef1234", - "phid": "PHID-DIFF-test", + "provider_id": "PHID-DIFF-test", "review_task_id": "local instance", "repository": "http://hgmo/test-try", } @@ -132,8 +132,8 @@ def test_missing_bugzilla_id(mock_revision, mock_backend, mock_hgmo): assert revisions[1] == { "id": 1, "bugzilla_id": None, - "phabricator_id": 51, - "phabricator_phid": "PHID-DREV-zzzzz", + "provider": "phabricator", + "provider_id": 51, "title": "Static Analysis tests", "diffs_url": "http://code-review-backend.test/v1/revision/1/diffs/", "issues_bulk_url": "http://code-review-backend.test/v1/revision/1/issues/", diff --git a/bot/tests/test_index.py b/bot/tests/test_index.py index 768bf06d8..fa5e6c032 100644 --- a/bot/tests/test_index.py +++ b/bot/tests/test_index.py @@ -5,7 +5,7 @@ from unittest import mock from code_review_bot.config import TaskCluster -from code_review_bot.revisions import PhabricatorRevision +from code_review_bot.revisions import PhabricatorRevision, Revision class MockPhabricatorRevision(PhabricatorRevision): @@ -199,9 +199,8 @@ def test_index_from_try( """ with mock_phabricator as api: - revision = PhabricatorRevision.from_try_task( - mock_try_task, mock_decision_task, api - ) + revision = Revision.from_try_task(mock_try_task, mock_decision_task, api) + assert isinstance(revision, PhabricatorRevision) mock_workflow.index_service = mock.Mock() mock_config.taskcluster = TaskCluster("/tmp/dummy", "12345deadbeef", 0, False) diff --git a/bot/tests/test_reporter_github.py b/bot/tests/test_reporter_github.py new file mode 100644 index 000000000..430590443 --- /dev/null +++ b/bot/tests/test_reporter_github.py @@ -0,0 +1,163 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. + + +import json +from pathlib import Path + +import responses +from conftest import FIXTURES_DIR + +from code_review_bot.report.github import GithubReporter +from code_review_bot.revisions import GithubRevision, Revision +from code_review_bot.tasks.clang_tidy import ClangTidyIssue, ClangTidyTask +from code_review_bot.tasks.coverage import CoverageIssue, ZeroCoverageTask + + +def test_github_review( + monkeypatch, + mock_github, + mock_config, + phab, + mock_github_try_task, + mock_decision_task, + mock_task, + mock_backend_secret, +): + """ + Report 2 cland tidy issues by pushing a review to a Github pull request + """ + revision = Revision.from_try_task(mock_github_try_task, mock_decision_task, None) + assert isinstance(revision, GithubRevision) + revision.lines = { + # Add dummy lines diff + "test.txt": [0], + "path/to/test.cpp": [0], + "another_test.cpp": [41, 42, 43], + } + revision.files = ["test.txt", "test.cpp", "another_test.cpp"] + revision.id = 52 + monkeypatch.setattr(revision, "load_file", lambda x: "some_content") + + reporter = GithubReporter( + { + "client_id": "client_id", + "private_key_pem": (Path(FIXTURES_DIR) / "private_key.pem").read_text(), + "installation_id": 123456789, + } + ) + + issue_clang_tidy = ClangTidyIssue( + mock_task(ClangTidyTask, "source-test-clang-tidy"), + revision, + "another_test.cpp", + "42", + "51", + "modernize-use-nullptr", + "dummy message", + ) + assert issue_clang_tidy.is_publishable() + + issue_coverage = CoverageIssue( + mock_task(ZeroCoverageTask, "coverage"), + "path/to/test.cpp", + "1", + "This file is uncovered", + revision, + ) + assert issue_coverage.is_publishable() + + responses.add( + responses.POST, + "https://api.github.com:443/repos/owner/repo-name/pulls/1/reviews", + json={}, + ) + + reporter.publish([issue_clang_tidy, issue_coverage], revision, [], [], []) + assert [(call.request.method, call.request.url) for call in responses.calls] == [ + ("GET", "https://github.tests.com/owner/repo-name/pull/1.diff"), + ("GET", "https://api.github.com:443/app/installations"), + ( + "POST", + "https://api.github.com:443/app/installations/123456789/access_tokens", + ), + ("GET", "https://api.github.com:443/repos/owner/repo-name"), + ("GET", "https://api.github.com:443/repos/owner/repo-name/pulls/1"), + ( + "GET", + "https://api.github.com:443/repos/owner/repo-name/commits/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + ), + ("POST", "https://api.github.com:443/repos/owner/repo-name/pulls/1/reviews"), + ] + review_creation = responses.calls[-1] + assert json.loads(review_creation.request.body) == { + "body": "2 issues have been found in this revision", + "comments": [ + { + "body": "dummy message", + "path": "another_test.cpp", + "line": 42, + }, + { + "body": "This file is uncovered", + "path": "path/to/test.cpp", + "line": 1, + }, + ], + "commit_id": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "event": "REQUEST_CHANGES", + } + + +def test_github_review_approve( + monkeypatch, + mock_github, + mock_config, + phab, + mock_github_try_task, + mock_decision_task, + mock_task, + mock_backend_secret, +): + """In case no issue is found, the pull request is approved""" + revision = Revision.from_try_task(mock_github_try_task, mock_decision_task, None) + revision.lines = {} + revision.files = ["test.txt", "test.cpp", "another_test.cpp"] + revision.id = 52 + reporter = GithubReporter( + { + "client_id": "client_id", + "private_key_pem": (Path(FIXTURES_DIR) / "private_key.pem").read_text(), + "installation_id": 123456789, + } + ) + + responses.add( + responses.POST, + "https://api.github.com:443/repos/owner/repo-name/pulls/1/reviews", + json={}, + ) + + reporter.publish([], revision, [], [], []) + assert [(call.request.method, call.request.url) for call in responses.calls] == [ + ("GET", "https://github.tests.com/owner/repo-name/pull/1.diff"), + ("GET", "https://api.github.com:443/app/installations"), + ( + "POST", + "https://api.github.com:443/app/installations/123456789/access_tokens", + ), + ("GET", "https://api.github.com:443/repos/owner/repo-name"), + ("GET", "https://api.github.com:443/repos/owner/repo-name/pulls/1"), + ( + "GET", + "https://api.github.com:443/repos/owner/repo-name/commits/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + ), + ("POST", "https://api.github.com:443/repos/owner/repo-name/pulls/1/reviews"), + ] + review_creation = responses.calls[-1] + assert json.loads(review_creation.request.body) == { + "comments": [], + "commit_id": "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", + "event": "APPROVE", + } diff --git a/bot/tests/test_reporter_phabricator.py b/bot/tests/test_reporter_phabricator.py index 9fa67ccea..cc9e88af6 100644 --- a/bot/tests/test_reporter_phabricator.py +++ b/bot/tests/test_reporter_phabricator.py @@ -13,7 +13,7 @@ from code_review_bot import Level from code_review_bot.report.phabricator import PhabricatorReporter -from code_review_bot.revisions import ImprovementPatch, PhabricatorRevision +from code_review_bot.revisions import ImprovementPatch, PhabricatorRevision, Revision from code_review_bot.tasks.clang_format import ClangFormatIssue, ClangFormatTask from code_review_bot.tasks.clang_tidy import ClangTidyIssue, ClangTidyTask from code_review_bot.tasks.clang_tidy_external import ( @@ -270,9 +270,8 @@ def test_phabricator_clang_tidy( """ with mock_phabricator as api: - revision = PhabricatorRevision.from_try_task( - mock_try_task, mock_decision_task, api - ) + revision = Revision.from_try_task(mock_try_task, mock_decision_task, api) + assert isinstance(revision, PhabricatorRevision) revision.lines = { # Add dummy lines diff "another_test.cpp": [41, 42, 43] @@ -308,9 +307,8 @@ def test_phabricator_clang_format( """ with mock_phabricator as api: - revision = PhabricatorRevision.from_try_task( - mock_try_task, mock_decision_task, api - ) + revision = Revision.from_try_task(mock_try_task, mock_decision_task, api) + assert isinstance(revision, PhabricatorRevision) revision.lines = { # Add dummy lines diff "test.cpp": [41, 42, 43], @@ -352,9 +350,8 @@ def test_phabricator_mozlint( """ with mock_phabricator as api: - revision = PhabricatorRevision.from_try_task( - mock_try_task, mock_decision_task, api - ) + revision = Revision.from_try_task(mock_try_task, mock_decision_task, api) + assert isinstance(revision, PhabricatorRevision) revision.lines = { # Add dummy lines diff "python/test.py": [41, 42, 43], @@ -443,9 +440,8 @@ def test_phabricator_coverage( Test Phabricator reporter publication on a mock coverage issue """ with mock_phabricator as api: - revision = PhabricatorRevision.from_try_task( - mock_try_task, mock_decision_task, api - ) + revision = Revision.from_try_task(mock_try_task, mock_decision_task, api) + assert isinstance(revision, PhabricatorRevision) revision.lines = { # Add dummy lines diff "test.txt": [0], @@ -511,9 +507,8 @@ def raise_404(*args, **kwargs): raise HTTPError(response=resp_mock) with mock_phabricator as api: - revision = PhabricatorRevision.from_try_task( - mock_try_task, mock_decision_task, api - ) + revision = Revision.from_try_task(mock_try_task, mock_decision_task, api) + assert isinstance(revision, PhabricatorRevision) revision.lines = { # Add dummy lines diff "test.txt": [0], @@ -547,9 +542,8 @@ def test_phabricator_clang_tidy_and_coverage( """ with mock_phabricator as api: - revision = PhabricatorRevision.from_try_task( - mock_try_task, mock_decision_task, api - ) + revision = Revision.from_try_task(mock_try_task, mock_decision_task, api) + assert isinstance(revision, PhabricatorRevision) revision.lines = { # Add dummy lines diff "test.txt": [0], @@ -673,9 +667,8 @@ def test_phabricator_analyzers( api.comment = unittest.mock.Mock(return_value=True) # Always use the same setup, only varies the analyzers - revision = PhabricatorRevision.from_try_task( - mock_try_task, mock_decision_task, api - ) + revision = Revision.from_try_task(mock_try_task, mock_decision_task, api) + assert isinstance(revision, PhabricatorRevision) revision.lines = {"test.cpp": [0, 41, 42, 43], "dom/test.cpp": [42]} revision.id = 52 reporter = PhabricatorReporter( @@ -759,9 +752,8 @@ def test_phabricator_clang_tidy_build_error( from code_review_bot import Level with mock_phabricator as api: - revision = PhabricatorRevision.from_try_task( - mock_try_task, mock_decision_task, api - ) + revision = Revision.from_try_task(mock_try_task, mock_decision_task, api) + assert isinstance(revision, PhabricatorRevision) revision.lines = { # Add dummy lines diff "test.cpp": [41, 42, 43] @@ -819,9 +811,8 @@ def test_full_file( """ with mock_phabricator as api: - revision = PhabricatorRevision.from_try_task( - mock_try_task, mock_decision_task, api - ) + revision = Revision.from_try_task(mock_try_task, mock_decision_task, api) + assert isinstance(revision, PhabricatorRevision) revision.lines = { # Add dummy lines diff "xx.cpp": [123, 124, 125] @@ -883,9 +874,8 @@ def test_task_failures(mock_phabricator, phab, mock_try_task, mock_decision_task """ with mock_phabricator as api: - revision = PhabricatorRevision.from_try_task( - mock_try_task, mock_decision_task, api - ) + revision = Revision.from_try_task(mock_try_task, mock_decision_task, api) + assert isinstance(revision, PhabricatorRevision) revision.id = 52 reporter = PhabricatorReporter({"analyzers": ["clang-tidy"]}, api=api) @@ -910,9 +900,8 @@ def test_extra_errors( """ with mock_phabricator as api: - revision = PhabricatorRevision.from_try_task( - mock_try_task, mock_decision_task, api - ) + revision = Revision.from_try_task(mock_try_task, mock_decision_task, api) + assert isinstance(revision, PhabricatorRevision) revision.lines = {"path/to/file.py": [1, 2, 3]} revision.files = ["path/to/file.py"] revision.id = 52 @@ -1003,9 +992,8 @@ def test_phabricator_notices(mock_phabricator, phab, mock_try_task, mock_decisio """ with mock_phabricator as api: - revision = PhabricatorRevision.from_try_task( - mock_try_task, mock_decision_task, api - ) + revision = Revision.from_try_task(mock_try_task, mock_decision_task, api) + assert isinstance(revision, PhabricatorRevision) revision.lines = { # Add dummy lines diff "test.rst": [41, 42, 43], @@ -1053,9 +1041,8 @@ def test_phabricator_tgdiff(mock_phabricator, phab, mock_try_task, mock_decision """ with mock_phabricator as api: - revision = PhabricatorRevision.from_try_task( - mock_try_task, mock_decision_task, api - ) + revision = Revision.from_try_task(mock_try_task, mock_decision_task, api) + assert isinstance(revision, PhabricatorRevision) revision.lines = { # Add dummy lines diff "test.rst": [41, 42, 43], @@ -1091,9 +1078,8 @@ def test_phabricator_external_tidy( """ with mock_phabricator as api: - revision = PhabricatorRevision.from_try_task( - mock_try_task, mock_decision_task, api - ) + revision = Revision.from_try_task(mock_try_task, mock_decision_task, api) + assert isinstance(revision, PhabricatorRevision) revision.lines = { # Add dummy lines diff "another_test.cpp": [41, 42, 43] @@ -1144,9 +1130,8 @@ def test_phabricator_newer_diff( """ with mock_phabricator as api: - revision = PhabricatorRevision.from_try_task( - mock_try_task, mock_decision_task, api - ) + revision = Revision.from_try_task(mock_try_task, mock_decision_task, api) + assert isinstance(revision, PhabricatorRevision) revision.lines = { # Add dummy lines diff "test.txt": [0], @@ -1224,9 +1209,8 @@ def test_phabricator_former_diff_comparison( """ with mock_phabricator as api: - revision = PhabricatorRevision.from_try_task( - mock_try_task, mock_decision_task, api - ) + revision = Revision.from_try_task(mock_try_task, mock_decision_task, api) + assert isinstance(revision, PhabricatorRevision) revision.lines = { # Add dummy lines diff "test.txt": [0], @@ -1374,9 +1358,8 @@ def test_phabricator_before_after_comment( mock_taskcluster_config.secrets = {"BEFORE_AFTER_RATIO": 1} with mock_phabricator as api: - revision = PhabricatorRevision.from_try_task( - mock_try_task, mock_decision_task, api - ) + revision = Revision.from_try_task(mock_try_task, mock_decision_task, api) + assert isinstance(revision, PhabricatorRevision) revision.lines = { # Add dummy lines diff "test.txt": [0],