from django.core.exceptions import ObjectDoesNotExist

from db.models.repos import CodeReference


def get_project_code_reference(project, commit=None):
    if not project.has_code:
        return None

    repo = project.repo

    if commit:
        try:
            return CodeReference.objects.get(repo=repo, commit=commit)
        except ObjectDoesNotExist:
            return None

    # If no commit is provided we get the last commit, and save new ref if not found
    last_commit = repo.last_commit
    if not last_commit:
        return None

    code_reference, _ = CodeReference.objects.get_or_create(repo=repo, commit=last_commit[0])
    return code_reference


def get_code_reference(instance, commit):
    return get_project_code_reference(instance.project, commit=commit)


def assign_code_reference(instance, commit=None):
    if instance.code_reference is not None:
        return
    if not commit and instance.specification and instance.specification.build:
        commit = instance.specification.build.commit
    code_reference = get_code_reference(instance=instance, commit=commit)
    if code_reference:
        instance.code_reference = code_reference

    return instance

from django.core.exceptions import ObjectDoesNotExist

from db.models.repos import CodeReference


def get_code_reference(instance, commit=None, external_repo=None):
    project = instance.project

    repo = project.repo if project.has_code else external_repo

    if not repo:
        return None

    if commit:
        try:
            return CodeReference.objects.get(repo=repo, commit=commit)
        except ObjectDoesNotExist:
            return None

    # If no commit is provided we get the last commit, and save new ref if not found
    last_commit = repo.last_commit
    if not last_commit:
        return None

    code_reference, _ = CodeReference.objects.get_or_create(repo=repo, commit=last_commit[0])
    return code_reference


def assign_code_reference(instance, commit=None):
    if instance.code_reference is not None:
        return
    build = instance.specification.build if instance.specification else None
    if not commit and build:
        commit = build.commit
    external_repo = build.git if build and build.git else None
    code_reference = get_code_reference(instance=instance,
                                        commit=commit,
                                        external_repo=external_repo)
    if code_reference:
        instance.code_reference = code_reference

    return instance

Extend code references with external repos
Extend code references with external repos

  from django.core.exceptions import ObjectDoesNotExist
  
  from db.models.repos import CodeReference
  
  
- def get_project_code_reference(project, commit=None):
-     if not project.has_code:
+ def get_code_reference(instance, commit=None, external_repo=None):
+     project = instance.project
+ 
+     repo = project.repo if project.has_code else external_repo
+ 
+     if not repo:
          return None
- 
-     repo = project.repo
  
      if commit:
          try:
              return CodeReference.objects.get(repo=repo, commit=commit)
          except ObjectDoesNotExist:
              return None
  
      # If no commit is provided we get the last commit, and save new ref if not found
      last_commit = repo.last_commit
      if not last_commit:
          return None
  
      code_reference, _ = CodeReference.objects.get_or_create(repo=repo, commit=last_commit[0])
      return code_reference
  
  
- def get_code_reference(instance, commit):
-     return get_project_code_reference(instance.project, commit=commit)
- 
- 
  def assign_code_reference(instance, commit=None):
      if instance.code_reference is not None:
          return
-     if not commit and instance.specification and instance.specification.build:
-         commit = instance.specification.build.commit
+     build = instance.specification.build if instance.specification else None
+     if not commit and build:
+         commit = build.commit
+     external_repo = build.git if build and build.git else None
-     code_reference = get_code_reference(instance=instance, commit=commit)
+     code_reference = get_code_reference(instance=instance,
+                                         commit=commit,
+                                         external_repo=external_repo)
      if code_reference:
          instance.code_reference = code_reference
  
      return instance
  
Extend code references with external repos
## Code Before:
from django.core.exceptions import ObjectDoesNotExist

from db.models.repos import CodeReference


def get_project_code_reference(project, commit=None):
    if not project.has_code:
        return None

    repo = project.repo

    if commit:
        try:
            return CodeReference.objects.get(repo=repo, commit=commit)
        except ObjectDoesNotExist:
            return None

    # If no commit is provided we get the last commit, and save new ref if not found
    last_commit = repo.last_commit
    if not last_commit:
        return None

    code_reference, _ = CodeReference.objects.get_or_create(repo=repo, commit=last_commit[0])
    return code_reference


def get_code_reference(instance, commit):
    return get_project_code_reference(instance.project, commit=commit)


def assign_code_reference(instance, commit=None):
    if instance.code_reference is not None:
        return
    if not commit and instance.specification and instance.specification.build:
        commit = instance.specification.build.commit
    code_reference = get_code_reference(instance=instance, commit=commit)
    if code_reference:
        instance.code_reference = code_reference

    return instance

## Instruction:
Extend code references with external repos
## Code After:
from django.core.exceptions import ObjectDoesNotExist

from db.models.repos import CodeReference


def get_code_reference(instance, commit=None, external_repo=None):
    project = instance.project

    repo = project.repo if project.has_code else external_repo

    if not repo:
        return None

    if commit:
        try:
            return CodeReference.objects.get(repo=repo, commit=commit)
        except ObjectDoesNotExist:
            return None

    # If no commit is provided we get the last commit, and save new ref if not found
    last_commit = repo.last_commit
    if not last_commit:
        return None

    code_reference, _ = CodeReference.objects.get_or_create(repo=repo, commit=last_commit[0])
    return code_reference


def assign_code_reference(instance, commit=None):
    if instance.code_reference is not None:
        return
    build = instance.specification.build if instance.specification else None
    if not commit and build:
        commit = build.commit
    external_repo = build.git if build and build.git else None
    code_reference = get_code_reference(instance=instance,
                                        commit=commit,
                                        external_repo=external_repo)
    if code_reference:
        instance.code_reference = code_reference

    return instance
