Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions bot/code_review_bot/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from code_review_bot import taskcluster
from code_review_bot.config import GetAppUserAgent, settings
from code_review_bot.revisions.github import GithubRevision
from code_review_bot.tasks.lint import MozLintIssue

logger = structlog.get_logger(__name__)
Expand Down Expand Up @@ -46,6 +47,10 @@ def publish_revision(self, revision):
logger.warn("Skipping revision publication on backend")
return

if isinstance(revision, GithubRevision):
logger.warn("Skipping revision publication for github")
return {}

# Check the repositories are urls
for url in (revision.base_repository, revision.head_repository):
assert isinstance(url, str), "Repository must be a string"
Expand Down Expand Up @@ -115,6 +120,10 @@ def publish_issues(self, issues, revision):
logger.warn("Skipping issues publication on backend")
return

if isinstance(revision, GithubRevision):
logger.warn("Skipping issues publication for github")
return {}

published = 0
assert (
revision.issues_url is not None
Expand Down
9 changes: 4 additions & 5 deletions bot/code_review_bot/report/lando.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from code_review_bot import Level
from code_review_bot.report.base import Reporter
from code_review_bot.revisions import PhabricatorRevision
from code_review_bot.revisions import GithubRevision

logger = structlog.get_logger(__name__)

Expand All @@ -30,10 +30,9 @@ 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"
)
if isinstance(revision, GithubRevision):
logger.warning("No Lando publication for Github yet")
return

assert (
revision.phabricator_id and revision.phabricator_phid and revision.diff
Expand Down
3 changes: 2 additions & 1 deletion bot/code_review_bot/revisions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
13 changes: 8 additions & 5 deletions bot/code_review_bot/revisions/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,17 @@ def as_dict(self):
@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
)
52 changes: 52 additions & 0 deletions bot/code_review_bot/revisions/github.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# 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 requests
import structlog

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}"

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,
}
5 changes: 3 additions & 2 deletions bot/code_review_bot/revisions/phabricator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
14 changes: 9 additions & 5 deletions bot/code_review_bot/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -673,11 +673,14 @@ 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"
)
assert isinstance(state, BuildState)

if isinstance(revision, GithubRevision):
logger.warning(
"No github status update yet", revision=revision, status=state
)
return

if not revision.build_target_phid:
logger.info(
"No build target found, skipping HarborMaster update", state=state.value
Expand All @@ -701,6 +704,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",
Expand Down