Skip to content
content.py 61.3 KiB
Newer Older
class CourseCommitSHADoesNotExist(Exception):
def get_course_commit_sha(course, participation, repo=None,
                          raise_on_nonexistent_preview_commit=False):
    # type: (Course, Optional[Participation], Optional[Repo_ish], Optional[bool]) -> bytes  # noqa
    sha = course.active_git_commit_sha

    def is_commit_sha_valid(repo, commit_sha):
        # type: (Repo_ish, Text) -> bool
        if isinstance(repo, SubdirRepoWrapper):
            repo = repo.repo
        try:
            repo[commit_sha.encode()]
        except KeyError:
            if raise_on_nonexistent_preview_commit:
                raise CourseCommitSHADoesNotExist(
                    _("Preview revision '%s' does not exist--"
                      "showing active course content instead."
    if participation is not None:
        if participation.preview_git_commit_sha:
            preview_sha = participation.preview_git_commit_sha
            if repo is not None:
                commit_sha_valid = is_commit_sha_valid(repo, preview_sha)
            else:
                with get_course_repo(course) as repo:
                    commit_sha_valid = is_commit_sha_valid(repo, preview_sha)

            if not commit_sha_valid:
                preview_sha = None
            if preview_sha is not None:
                sha = preview_sha
def list_flow_ids(repo, commit_sha):
    # type: (Repo_ish, bytes) -> List[Text]
    flow_ids = []
    try:
        flows_tree = get_repo_blob(repo, "flows", commit_sha)
    except ObjectDoesNotExist:
        # That's OK--no flows yet.
        pass
    else:
        for entry in flows_tree.items():
            if entry.path.endswith(b".yml"):
                flow_ids.append(entry.path[:-4].decode("utf-8"))

    return sorted(flow_ids)

# vim: foldmethod=marker