def branch(self): """Return the active branch name, or 'master' if not on a branch. """ from naucse.views import logger # Travis CI checks out specific commit, so there isn't an active branch if os.environ.get("TRAVIS") and os.environ.get("TRAVIS_BRANCH"): return os.environ.get("TRAVIS_BRANCH") repo = Repo(".") try: return repo.active_branch.name except TypeError: # thrown if not in a branch logger.warning("MetaInfo.branch: There is not active branch") return self._default_branch
def slug(self): """Return the slug of the repository based on the current branch. Returns the default if not on a branch, the branch doesn't have a remote, or the remote url can't be parsed. """ from naucse.views import logger # Travis CI checks out specific commit, so there isn't an active branch if os.environ.get("TRAVIS") and os.environ.get("TRAVIS_REPO_SLUG"): return os.environ.get("TRAVIS_REPO_SLUG") repo = Repo(".") try: active_branch = repo.active_branch except TypeError: # thrown if not in a branch logger.warning("MetaInfo.slug: There is not active branch") return self._default_slug try: remote_name = active_branch.remote_name except ValueError: tracking_branch = active_branch.tracking_branch() if tracking_branch is None: # a branch without a remote logger.warning( "MetaInfo.slug: The branch doesn't have a remote") return self._default_slug remote_name = tracking_branch.remote_name remote_url = repo.remotes[remote_name].url parsed = giturlparse.parse(remote_url) if hasattr(parsed, "owner") and hasattr(parsed, "repo"): return f"{parsed.owner}/{parsed.repo}" logger.warning("MetaInfo.slug: The url could not be parsed.") logger.debug("MetaInfo.slug: Parsed %s", parsed.__dict__) return self._default_slug