~edwargix/git.sr.ht

25406aa635961546808cff887e8f15b4ab803182 — Drew DeVault 5 years ago 368a8bc
API: add route for uploading artifacts
1 files changed, 31 insertions(+), 0 deletions(-)

M gitsrht/blueprints/api.py
M gitsrht/blueprints/api.py => gitsrht/blueprints/api.py +31 -0
@@ 7,12 7,14 @@ from gitsrht.blueprints.repo import lookup_ref, collect_refs
from gitsrht.types import Artifact
from gitsrht.git import Repository as GitRepository, commit_time, annotate_tree
from gitsrht.git import get_log
from gitsrht.repos import upload_artifact
from gitsrht.webhooks import RepoWebhook
from io import BytesIO
from itertools import groupby
from scmsrht.access import UserAccess
from scmsrht.blueprints.api import get_user, get_repo
from srht.api import paginated_response
from srht.database import db
from srht.oauth import current_token, oauth
from srht.redis import redis
from srht.validation import Validation


@@ 92,6 94,35 @@ def repo_refs_GET(username, reponame):
            "results_per_page": len(refs),
        }

@data.route("/api/repos/<reponame>/artifacts/<path:refname>", defaults={"username": None}, methods=["POST"])
@data.route("/api/<username>/repos/<reponame>/artifacts/<path:refname>", methods=["POST"])
@oauth("data:write")
def repo_refs_by_name_POST(username, reponame, refname):
    user = get_user(username)
    repo = get_repo(user, reponame, needs=UserAccess.manage)

    with GitRepository(repo.path) as git_repo:
        try:
            tag = git_repo.revparse_single(refname)
        except KeyError:
            abort(404)
        except ValueError:
            abort(404)
        if isinstance(tag, pygit2.Commit):
            target = tag.oid.hex
        else:
            target = tag.target.hex
        valid = Validation(request)
        f = request.files.get("file")
        valid.expect(f, "File is required", field="file")
        if not valid.ok:
            return valid.response
        artifact = upload_artifact(valid, repo, target, f, f.filename)
        if not valid.ok:
            return valid.response
        db.session.commit()
        return artifact.to_dict()

# dear god, this routing
@data.route("/api/repos/<reponame>/log",
        defaults={"username": None, "ref": None, "path": ""})