Esempio n. 1
0
def get_repo(repo_id):
    """Return information about the repo.

    Meta: (immutable through this endpoint)
        * file tree
        * date repo was created
        * list of users by access level
        * access level of current auth context

    Settings: (mutable)
        * list of users by access level
        * remote repos with which to sync
        * privacy setting: (public or private)
    """
    auth_context = get_auth_context()

    try:
        if not auth_context.can_read_repo(repo_id):
            return abort(http.UNAUTHORIZED)
    except KeyError:
        pass

    repo = Repository(os.path.join(current_app.config.get('git_root'),
                                   repo_id))

    if not repo.is_valid_repo:
        abort(404)

    return jsonify({})
Esempio n. 2
0
def get_repo(repo_id):
    """Return information about the repo.

    Meta: (immutable through this endpoint)
        * file tree
        * date repo was created
        * list of users by access level
        * access level of current auth context

    Settings: (mutable)
        * list of users by access level
        * remote repos with which to sync
        * privacy setting: (public or private)
    """
    auth_context = get_auth_context()

    try:
        if not auth_context.can_read_repo(repo_id):
            return abort(http.UNAUTHORIZED)
    except KeyError:
        pass

    repo = Repository(
        os.path.join(
            current_app.config.get('git_root'),
            repo_id
        )
    )

    if not repo.is_valid_repo:
        abort(404)

    return jsonify({

    })
Esempio n. 3
0
def add_repo():
    """Create a new repository object

    Return {'url': '/<repo_id>/'}
    """

    auth_context = get_auth_context()

    if not auth_context.can_create_repos:
        return abort(http.UNAUTHORIZED)

    # create a new auth context as the initial write key
    writer = KeyAuthContext()
    writer.save()

    meta = RepoMeta()
    meta.is_public = bool(request.form.get('is_public'))
    meta.access_admin.append(auth_context)
    meta.access_write.append(writer)

    meta.save()

    name = meta._id

    repo = Repository(os.path.join(current_app.config.get('git_root'), name))
    repo.init()

    return jsonify({
        'url': url_for('.get_repo', repo_id=name),
        'write_key': writer._id,
    })
Esempio n. 4
0
def get_file(repo_id, path):
    """ Simple web request. Return the static file.
    """
    auth_context = get_auth_context()

    if not auth_context.can_read_repo(repo_id):
        return abort(http.UNAUTHORIZED)

    repo = get_repo(repo_id)

    if request.args.get('info') is not None:
        return jsonify(
            {'versions': [v.sha for v in repo.get_file(path).versions]})

    if request.args.get('sha'):
        sha = request.args.get('sha')
        f = repo.get_file(path)
        if sha not in [x.sha for x in f.versions]:
            abort(http.NOT_FOUND)
        else:
            return send_file(StringIO(f.get_version_by_sha(sha).content))

    path = path.split('/')
    if len(path) == 1:
        path = path[0]
        dirname = ''
    else:
        path = path[-1]
        dirname = '/'.join(path[:-1])

    return send_from_directory(
        directory=os.path.join(repo.path, dirname),
        filename=path,
    )
Esempio n. 5
0
def add_repo():
    """Create a new repository object

    Return {'url': '/<repo_id>/'}
    """

    auth_context = get_auth_context()

    if not auth_context.can_create_repos:
        return abort(http.UNAUTHORIZED)

    meta = RepoMeta()
    meta.is_public = bool(request.form.get('is_public'))
    meta.access_admin.append(auth_context)

    meta.save()

    name = meta._id

    repo = Repository(
        os.path.join(
            current_app.config.get('git_root'),
            name
        )
    )
    repo.init()

    return jsonify({
        'url': url_for('.get_repo', repo_id=name)
    })
Esempio n. 6
0
def get_file(repo_id, path):
    """ Simple web request. Return the static file.
    """
    auth_context = get_auth_context()

    if not auth_context.can_read_repo(repo_id):
        return abort(http.UNAUTHORIZED)

    repo = get_repo(repo_id)

    if request.args.get('info') is not None:
        return jsonify({
            'versions': [v.sha for v in repo.get_file(path).versions]
        })

    if request.args.get('sha'):
        sha = request.args.get('sha')
        f = repo.get_file(path)
        if sha not in [x.sha for x in f.versions]:
            abort(http.NOT_FOUND)
        else:
            return send_file(StringIO(f.get_version_by_sha(sha).content))

    path = path.split('/')
    if len(path) == 1:
        path = path[0]
        dirname = ''
    else:
        path = path[-1]
        dirname = '/'.join(path[:-1])

    return send_from_directory(
        directory=os.path.join(repo.path, dirname),
        filename=path,
    )
Esempio n. 7
0
def get_repos():
    """ Return a list of repositories
    """
    auth_context = get_auth_context()

    repos = [
        x for x in os.listdir(current_app.config.get('git_root'))
        if (os.path.isdir(os.path.join(current_app.config.get('git_root'), x))
            and auth_context.can_read_repo(x))
    ]

    return jsonify({'repos': repos})
Esempio n. 8
0
def get_repos():
    """ Return a list of repositories
    """
    auth_context = get_auth_context()

    repos = [
        x for x in os.listdir(current_app.config.get('git_root'))
        if (
            os.path.isdir(os.path.join(current_app.config.get('git_root'), x))
            and auth_context.can_read_repo(x)
        )
    ]

    return jsonify({'repos': repos})
Esempio n. 9
0
def add_file(repo_id, path=None):
    """ Adds a file to the repo at this path and commit."""
    # TODO: Consider implications of allowing a PUT to the repo level, with
    #       a dict containing the path.
    auth_context = get_auth_context()

    if not auth_context.can_write_repo(repo_id):
        return abort(http.UNAUTHORIZED)

    f = request.files.get('file')

    if not f or len(request.files) != 1:
        abort(400)

    repo = get_repo(repo_id)
    path = urlparse(path).path

    add_and_commit_file(repo, path, f)

    return jsonify({'url': url_for('.get_file', repo_id=repo_id, path=path)})
Esempio n. 10
0
def add_file(repo_id, path=None):
    """ Adds a file to the repo at this path and commit."""
    # TODO: Consider implications of allowing a PUT to the repo level, with
    #       a dict containing the path.
    auth_context = get_auth_context()

    if not auth_context.can_write_repo(repo_id):
        return abort(http.UNAUTHORIZED)

    f = request.files.get('file')

    if not f or len(request.files) != 1:
        abort(400)

    repo = get_repo(repo_id)
    path = urlparse(path).path

    add_and_commit_file(repo, path, f)

    return jsonify({'url': url_for('.get_file', repo_id=repo_id, path=path)})
Esempio n. 11
0
def delete_file(repo_id, path):
    auth_context = get_auth_context()

    if not auth_context.can_write_repo(repo_id):
        return abort(http.UNAUTHORIZED)

    repo = get_repo(repo_id)

    path_parts = path.split('/')
    fs_path = os.path.join(repo.path, *path_parts)

    if not os.path.isfile(fs_path):
        abort(404)

    repo.delete_file(
        file_path=os.path.join(*path_parts),
        commit_author='{} <{}>'.format(auth_context.full_name,
                                       auth_context.email),
        commit_message='Deleted {}'.format(os.path.join(*path_parts)),
    )

    resp = make_response()
    resp.status_code = http.NO_CONTENT
    return resp
Esempio n. 12
0
def add_file(repo_id, path=None):
    """ Adds a file to the repo at this path and commit."""
    auth_context = get_auth_context()

    if not auth_context.can_write_repo(repo_id):
        return abort(http.UNAUTHORIZED)

    f = request.files.get('file')

    if not f or len(request.files) != 1:
        abort(400)

    repo = get_repo(repo_id)
    path = urlparse(path).path

    add_and_commit_file(
        repo,
        path,
        f,
        author_name=auth_context.full_name,
        author_email=auth_context.email,
    )

    return jsonify({'url': url_for('.get_file', repo_id=repo_id, path=path)})