Skip to content

Commit

Permalink
Create branch data if missing a branch
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdon committed Oct 26, 2020
1 parent a68c812 commit aecbd8f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
7 changes: 5 additions & 2 deletions sleuthpr/services/branches.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@
logger = logging.getLogger(__name__)


def update_sha(installation: Installation, repository: Repository, name: str, sha: str):
def update_sha(installation: Installation, repository: Repository, name: str, sha: str) -> RepositoryBranch:
existing = repository.branches.filter(name=name).first()
if not existing:
RepositoryBranch.objects.create(repository=repository, name=name, head_sha=sha)
branch = RepositoryBranch.objects.create(repository=repository, name=name, head_sha=sha)
dirty = True
else:
dirty = dirty_set_all(existing, dict(head_sha=sha))
existing.save()
branch = existing

if dirty:
pull_requests.on_source_change(installation, repository, name, sha)

return branch
11 changes: 11 additions & 0 deletions sleuthpr/variables.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from sleuthpr.models import PullRequest
from sleuthpr.models import ReviewState
from sleuthpr.models import TriState
from sleuthpr.services import branches
from sleuthpr.triggers import BASE_BRANCH_UPDATED
from sleuthpr.triggers import PR_CLOSED
from sleuthpr.triggers import PR_CREATED
Expand Down Expand Up @@ -163,6 +164,16 @@ def _is_base_branch_synchronized(pull_request: PullRequest):
repo = pull_request.repository

branch_head = repo.branches.filter(name=pull_request.base_branch_name).first()

if not branch_head:
# this doesn't exist because it is a github action and doesn't have the data loaded from the push
# event, so load it here
branch_head = branches.update_sha(
pull_request.repository.installation,
pull_request.repository,
pull_request.base_branch_name,
pull_request.base_sha,
)
if branch_head:
branch_head.refresh_from_db()
logger.info(f"Branch {branch_head.name} and head {branch_head.head_sha} for pr {pull_request.remote_id}")
Expand Down

0 comments on commit aecbd8f

Please sign in to comment.